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