Example program to demonstrate the use of the modem providing CAT-M1, NB1 and 2G support. It may be used on the C030-R412M boards.
Dependencies: ublox-at-cellular-interface ublox-cellular-base ublox-cellular-base-n2xx ublox-at-cellular-interface-n2xx
main.cpp@0:2a2cf217d00f, 2019-06-12 (annotated)
- Committer:
- wajahat.abbas@u-blox.com
- Date:
- Wed Jun 12 14:15:16 2019 +0500
- Revision:
- 0:2a2cf217d00f
- Child:
- 1:b9de22f79488
Added example program to show usage to set/get RAT and MNO profiles
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 1 | /* mbed Microcontroller Library |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 2 | * Copyright (c) 2017 u-blox |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 3 | * |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 5 | * you may not use this file except in compliance with the License. |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 6 | * You may obtain a copy of the License at |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 7 | * |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 9 | * |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 10 | * Unless required by applicable law or agreed to in writing, software |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 13 | * See the License for the specific language governing permissions and |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 14 | * limitations under the License. |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 15 | */ |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 16 | #include "mbed.h" |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 17 | #include "UbloxATCellularInterface.h" |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 18 | #include "CellularContext.h" |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 19 | #include "UbloxATCellularInterfaceN2xx.h" |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 20 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 21 | #define UBLOX_AT_CELLULAR_INTERFACE 1 |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 22 | #define ONBOARD_CELLULAR_INTERFACE 2 |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 23 | #define UBLOX_AT_CELLULAR_INTERFACE_N2XX 3 |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 24 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 25 | // You must select the correct interface library for your board, by |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 26 | // uncommenting the correct line below. Supported combinations are |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 27 | // indicated with a "Y" in the table below. |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 28 | // |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 29 | // C030_U201 C030_N211 C027 |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 30 | // UBLOX_AT_CELLULAR_INTERFACE Y - Y |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 31 | // ONBOARD_CELLULAR_INTERFACE Y - Y |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 32 | // UBLOX_AT_CELLULAR_INTERFACE_N2XX - Y - |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 33 | // Note: the N211 module supports only UDP, not TCP |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 34 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 35 | // ONBOARD_CELLULAR_INTERFACE uses LWIP and the PPP cellular interface |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 36 | // on the mbed MCU, while using UBLOX_AT_CELLULAR_INTERFACE and |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 37 | // UBLOX_AT_CELLULAR_INTERFACE_N2XX uses an IP stack on the cellular |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 38 | // module and hence uses less RAM (significant on C027). This also |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 39 | // allows other AT command operations (e.g. sending an SMS) to happen |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 40 | // during a data transfer (for which you should replace the |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 41 | // UBLOX_AT_CELLULAR_INTERFACE library with the UbloxATCellularInterfaceExt |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 42 | // library). However, it is slower than using the LWIP/PPP on the mbed |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 43 | // MCU interface since more string parsing is required. |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 44 | #define INTERFACE_USED UBLOX_AT_CELLULAR_INTERFACE |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 45 | //#define INTERFACE_USED ONBOARD_CELLULAR_INTERFACE |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 46 | //#define INTERFACE_USED UBLOX_AT_CELLULAR_INTERFACE_N2XX |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 47 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 48 | #if (INTERFACE_USED == UBLOX_AT_CELLULAR_INTERFACE) |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 49 | #define INTERFACE_CLASS UbloxATCellularInterface |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 50 | #elif (INTERFACE_USED == ONBOARD_CELLULAR_INTERFACE) |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 51 | #define INTERFACE_CLASS OnboardCellularInterface |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 52 | #elif (INTERFACE_USED == UBLOX_AT_CELLULAR_INTERFACE_N2XX) |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 53 | #define INTERFACE_CLASS UbloxATCellularInterfaceN2xx |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 54 | #else |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 55 | #error please define an interface class to use |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 56 | #endif |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 57 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 58 | // The credentials of the SIM in the board. If PIN checking is enabled |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 59 | // for your SIM card you must set this to the required PIN. |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 60 | #define PIN "0000" |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 61 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 62 | // Network credentials. You should set this according to your |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 63 | // network/SIM card. For C030 non-N2xx boards, leave the parameters as NULL |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 64 | // otherwise, if you do not know the APN for your network, you may |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 65 | // either try the fairly common "internet" for the APN (and leave the |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 66 | // username and password NULL), or you may leave all three as NULL and then |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 67 | // a lookup will be attempted for a small number of known networks |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 68 | // (see APN_db.h in mbed-os/features/netsocket/cellular/utils). |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 69 | #define APN NULL |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 70 | #define USERNAME NULL |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 71 | #define PASSWORD NULL |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 72 | #define TCP_SERVER "os.mbed.com" |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 73 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 74 | // LEDs |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 75 | DigitalOut ledRed(LED1, 1); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 76 | DigitalOut ledGreen(LED2, 1); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 77 | DigitalOut ledBlue(LED3, 1); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 78 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 79 | // The user button |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 80 | volatile bool buttonPressed = false; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 81 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 82 | static void good() { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 83 | ledGreen = 0; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 84 | ledBlue = 1; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 85 | ledRed = 1; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 86 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 87 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 88 | static void bad() { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 89 | ledRed = 0; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 90 | ledGreen = 1; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 91 | ledBlue = 1; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 92 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 93 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 94 | static void event() { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 95 | ledBlue = 0; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 96 | ledRed = 1; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 97 | ledGreen = 1; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 98 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 99 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 100 | static void pulseEvent() { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 101 | event(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 102 | wait_ms(500); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 103 | good(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 104 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 105 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 106 | static void ledOff() { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 107 | ledBlue = 1; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 108 | ledRed = 1; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 109 | ledGreen = 1; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 110 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 111 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 112 | static void printNtpTime(char * buf, int len) |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 113 | { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 114 | time_t timestamp = 0; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 115 | struct tm *localTime; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 116 | char timeString[25]; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 117 | time_t TIME1970 = 2208988800U; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 118 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 119 | if (len >= 43) { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 120 | timestamp |= ((int) *(buf + 40)) << 24; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 121 | timestamp |= ((int) *(buf + 41)) << 16; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 122 | timestamp |= ((int) *(buf + 42)) << 8; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 123 | timestamp |= ((int) *(buf + 43)); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 124 | timestamp -= TIME1970; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 125 | localTime = localtime(×tamp); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 126 | if (localTime) { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 127 | if (strftime(timeString, sizeof(timeString), "%a %b %d %H:%M:%S %Y", localTime) > 0) { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 128 | printf("NTP timestamp is %s.\n", timeString); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 129 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 130 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 131 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 132 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 133 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 134 | static void cbButton() |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 135 | { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 136 | buttonPressed = true; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 137 | pulseEvent(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 138 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 139 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 140 | /* This changes the modem RAT and MNO profile to default values. |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 141 | * MBED_CONF_APP_CHANGE_RAT_MNO_TO_DEFAULT macro is set to 0 in mbed_app.json |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 142 | * to avoid accidently changing RAT and MNO. Not all modems support +URAT and +UMNOPROF commands. |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 143 | */ |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 144 | #if (INTERFACE_USED == UBLOX_AT_CELLULAR_INTERFACE) && !defined(TARGET_UBLOX_C027) && !defined(TARGET_UBLOX_C030_N211) |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 145 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 146 | #ifndef MBED_CONF_APP_CHANGE_RAT_MNO_TO_DEFAULT |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 147 | #define MBED_CONF_APP_CHANGE_RAT_MNO_TO_DEFAULT 0 |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 148 | #endif |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 149 | bool change_rat_mno_flag = MBED_CONF_APP_CHANGE_RAT_MNO_TO_DEFAULT; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 150 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 151 | void change_rat_mno(UbloxATCellularInterface* interface) |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 152 | { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 153 | #ifdef TARGET_UBLOX_C030_R41XM |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 154 | int current_profile; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 155 | #endif |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 156 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 157 | #if defined(TARGET_UBLOX_C030_U201) || defined(TARGET_UBLOX_C030_R412M) |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 158 | int selected, preferred, second_preferred; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 159 | #endif |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 160 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 161 | if (change_rat_mno_flag == true) { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 162 | printf("\nSetting URAT and MNO profile to default values...\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 163 | change_rat_mno_flag = false; //do the following just once as to demonstrate the usage |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 164 | //perform deregistration and set URAT and MNO profile to default values |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 165 | if ( (interface->is_registered_csd() || interface->is_registered_psd() || interface->is_registered_eps()) ) { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 166 | printf("De-registering...\n\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 167 | interface->nwk_deregistration(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 168 | pulseEvent(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 169 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 170 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 171 | #ifdef TARGET_UBLOX_C030_R41XM |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 172 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 173 | #ifdef TARGET_UBLOX_C030_R412M //+URAT is not supported by R410M |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 174 | printf("Setting modem RAT to CATM1 and NB1...\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 175 | if (interface->set_modem_rat(UbloxATCellularInterface::LTE_CATM1, UbloxATCellularInterface::LTE_CATNB1)) { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 176 | printf("RAT configured\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 177 | pulseEvent(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 178 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 179 | if (interface->get_modem_rat(&selected, &preferred, &second_preferred)) { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 180 | printf("selected RAT: %d\npreferred RAT: %d\nsecond_preferred RAT: %d\n", selected, preferred, second_preferred); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 181 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 182 | #endif |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 183 | printf("Setting MNO profile to 0 (default)...\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 184 | if (interface->set_mno_profile()) { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 185 | printf("MNO profile configured\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 186 | pulseEvent(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 187 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 188 | if (interface->get_mno_profile(¤t_profile)) { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 189 | printf("current_profile is: %d\n", (int)current_profile); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 190 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 191 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 192 | #elif defined TARGET_UBLOX_C030_U201 |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 193 | printf("Setting modem RAT to GSM/UMTS and GSM_GPRS_EGPRS...\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 194 | if (interface->set_modem_rat(UbloxATCellularInterface::GSM_UMTS, UbloxATCellularInterface::GSM_GPRS_EGPRS)) { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 195 | printf("RAT configured\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 196 | pulseEvent(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 197 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 198 | if (interface->get_modem_rat(&selected, &preferred, &second_preferred)) { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 199 | printf("selected RAT: %d\npreferred RAT: %d\nsecond_preferred RAT: %d\n", selected, preferred, second_preferred); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 200 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 201 | #endif //TARGET_UBLOX_C030_R41XM |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 202 | printf("\nRebooting modem for settings to take effect...\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 203 | if (interface->reboot_modem()) { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 204 | printf("Reboot successful\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 205 | pulseEvent(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 206 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 207 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 208 | printf("Performing registration, please wait...\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 209 | for (int x = 0; interface->connect(PIN) != 0; x++) { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 210 | if (x > 0) { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 211 | bad(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 212 | printf("Retrying (have you checked that an antenna is plugged in and your APN is correct?)...\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 213 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 214 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 215 | pulseEvent(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 216 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 217 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 218 | #endif |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 219 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 220 | /* This example program for the u-blox C030 and C027 boards instantiates |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 221 | * the UbloxATCellularInterface or OnboardCellularInterface and uses it |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 222 | * to make a simple sockets connection to a server, using 2.pool.ntp.org |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 223 | * for UDP and developer.mbed.org for TCP. For a more comprehensive example, |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 224 | * where higher layer protocols make use of the same sockets interface, |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 225 | * see example-ublox-mbed-client. |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 226 | * Progress may be monitored with a serial terminal running at 9600 baud. |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 227 | * The LED on the C030 board will turn green when this program is |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 228 | * operating correctly, pulse blue when a sockets operation is completed |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 229 | * and turn red if there is a failure. |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 230 | */ |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 231 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 232 | int main() |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 233 | { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 234 | INTERFACE_CLASS *interface = new INTERFACE_CLASS(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 235 | // If you need to debug the cellular interface, comment out the |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 236 | // instantiation above and uncomment the one below. |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 237 | // For the N2xx interface, change xxx to MBED_CONF_UBLOX_CELL_BAUD_RATE, |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 238 | // while for the non-N2xx interface change it to MBED_CONF_UBLOX_CELL_N2XX_BAUD_RATE. |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 239 | // INTERFACE_CLASS *interface = new INTERFACE_CLASS(MDMTXD, MDMRXD, |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 240 | // xxx, |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 241 | // true); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 242 | #ifndef TARGET_UBLOX_C030_N211 |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 243 | TCPSocket sockTcp; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 244 | #endif |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 245 | UDPSocket sockUdp; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 246 | SocketAddress udpServer; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 247 | SocketAddress udpSenderAddress; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 248 | SocketAddress tcpServer; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 249 | char buf[1024]; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 250 | int x; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 251 | #ifdef TARGET_UBLOX_C027 |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 252 | // No user button on C027 |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 253 | InterruptIn userButton(NC); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 254 | #else |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 255 | InterruptIn userButton(SW0); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 256 | #endif |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 257 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 258 | // Attach a function to the user button |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 259 | userButton.rise(&cbButton); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 260 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 261 | good(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 262 | printf("Starting up, please wait up to 180 seconds for network registration to complete...\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 263 | interface->set_credentials(APN, USERNAME, PASSWORD); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 264 | for (x = 0; interface->connect(PIN) != 0; x++) { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 265 | if (x > 0) { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 266 | bad(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 267 | printf("Retrying (have you checked that an antenna is plugged in and your APN is correct?)...\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 268 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 269 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 270 | pulseEvent(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 271 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 272 | printf("\nGetting the IP address of \"developer.mbed.org\" and \"2.pool.ntp.org\"...\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 273 | if ((interface->gethostbyname("2.pool.ntp.org", &udpServer) == 0) && |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 274 | (interface->gethostbyname(TCP_SERVER, &tcpServer) == 0)) { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 275 | pulseEvent(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 276 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 277 | udpServer.set_port(123); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 278 | tcpServer.set_port(80); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 279 | printf("\"2.pool.ntp.org\" address: %s on port %d.\n", udpServer.get_ip_address(), udpServer.get_port()); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 280 | printf("\"os.mbed.com\" address: %s on port %d.\n", tcpServer.get_ip_address(), tcpServer.get_port()); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 281 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 282 | printf("\nPerforming socket operations in a loop (until the user button is pressed on C030 or forever on C027)...\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 283 | while (!buttonPressed) { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 284 | // UDP Sockets |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 285 | printf("=== UDP ===\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 286 | printf("Opening a UDP socket...\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 287 | if (sockUdp.open(interface) == 0) { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 288 | pulseEvent(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 289 | printf("UDP socket open.\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 290 | sockUdp.set_timeout(10000); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 291 | printf("Sending time request to \"2.pool.ntp.org\" over UDP socket...\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 292 | memset (buf, 0, sizeof(buf)); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 293 | *buf = '\x1b'; |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 294 | if (sockUdp.sendto(udpServer, (void *) buf, 48) == 48) { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 295 | pulseEvent(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 296 | printf("Socket send completed, waiting for UDP response...\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 297 | x = sockUdp.recvfrom(&udpSenderAddress, buf, sizeof (buf)); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 298 | if (x > 0) { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 299 | pulseEvent(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 300 | printf("Received %d byte response from server %s on UDP socket:\n" |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 301 | "-------------------------------------------------------\n", |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 302 | x, udpSenderAddress.get_ip_address()); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 303 | printNtpTime(buf, x); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 304 | printf("-------------------------------------------------------\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 305 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 306 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 307 | printf("Closing socket...\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 308 | sockUdp.close(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 309 | pulseEvent(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 310 | printf("Socket closed.\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 311 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 312 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 313 | #ifndef TARGET_UBLOX_C030_N211 |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 314 | // TCP Sockets |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 315 | printf("=== TCP ===\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 316 | printf("Opening a TCP socket...\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 317 | if (sockTcp.open(interface) == 0) { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 318 | pulseEvent(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 319 | printf("TCP socket open.\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 320 | sockTcp.set_timeout(10000); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 321 | printf("Connecting socket to %s on port %d...\n", tcpServer.get_ip_address(), tcpServer.get_port()); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 322 | if (sockTcp.connect(tcpServer) == 0) { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 323 | pulseEvent(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 324 | printf("Connected, sending HTTP GET request to %s over socket...\n", TCP_SERVER); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 325 | strcpy (buf, "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 326 | // Note: since this is a short string we can send it in one go as it will |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 327 | // fit within the default buffer sizes. Normally you should call sock.send() |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 328 | // in a loop until your entire buffer has been sent. |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 329 | if (sockTcp.send((void *) buf, strlen(buf)) == (int) strlen(buf)) { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 330 | pulseEvent(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 331 | printf("Socket send completed, waiting for response...\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 332 | x = sockTcp.recv(buf, sizeof (buf)); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 333 | if (x > 0) { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 334 | pulseEvent(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 335 | printf("Received %d byte response from server on TCP socket:\n" |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 336 | "----------------------------------------------------\n%.*s" |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 337 | "----------------------------------------------------\n", |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 338 | x, x, buf); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 339 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 340 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 341 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 342 | printf("Closing socket...\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 343 | sockTcp.close(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 344 | pulseEvent(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 345 | printf("Socket closed.\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 346 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 347 | #if (INTERFACE_USED == UBLOX_AT_CELLULAR_INTERFACE) && !defined(TARGET_UBLOX_C027) |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 348 | change_rat_mno(interface); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 349 | #endif //(INTERFACE_USED == UBLOX_AT_CELLULAR_INTERFACE) |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 350 | #endif //TARGET_UBLOX_C030_N211 |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 351 | wait_ms(5000); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 352 | #ifndef TARGET_UBLOX_C027 |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 353 | printf("[Checking if user button has been pressed]\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 354 | #endif |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 355 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 356 | |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 357 | pulseEvent(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 358 | printf("User button was pressed, stopping...\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 359 | interface->disconnect(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 360 | ledOff(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 361 | printf("Stopped.\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 362 | } else { |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 363 | bad(); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 364 | printf("Unable to get IP address of \"developer.mbed.org\" or \"2.pool.ntp.org\".\n"); |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 365 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 366 | } |
wajahat.abbas@u-blox.com | 0:2a2cf217d00f | 367 | // End Of File |