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