Wajahat Abbas / Mbed OS example-ublox-cellular-interface_rat_mno

Dependencies:   ublox-at-cellular-interface ublox-cellular-base ublox-cellular-base-n2xx ublox-at-cellular-interface-n2xx

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2017 u-blox
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "mbed.h"
00017 #include "UbloxATCellularInterface.h"
00018 #include "OnboardCellularInterface.h"
00019 #include "UbloxATCellularInterfaceN2xx.h"
00020 
00021 #define UBLOX_AT_CELLULAR_INTERFACE      1
00022 #define ONBOARD_CELLULAR_INTERFACE       2
00023 #define UBLOX_AT_CELLULAR_INTERFACE_N2XX 3
00024 
00025 // You must select the correct interface library for your board, by
00026 // uncommenting the correct line below. Supported combinations are
00027 // indicated with a "Y" in the table below.
00028 //
00029 //                                   C030_U201   C030_N211      C027
00030 // UBLOX_AT_CELLULAR_INTERFACE           Y            -           Y
00031 // ONBOARD_CELLULAR_INTERFACE            Y            -           Y
00032 // UBLOX_AT_CELLULAR_INTERFACE_N2XX      -            Y           -
00033 // Note: the N211 module supports only UDP, not TCP
00034 
00035 // ONBOARD_CELLULAR_INTERFACE uses LWIP and the PPP cellular interface
00036 // on the mbed MCU, while using UBLOX_AT_CELLULAR_INTERFACE and
00037 // UBLOX_AT_CELLULAR_INTERFACE_N2XX uses an IP stack on the cellular
00038 // module and hence uses less RAM (significant on C027).  This also
00039 // allows other AT command operations (e.g. sending an SMS) to happen
00040 // during a data transfer (for which you should replace the
00041 // UBLOX_AT_CELLULAR_INTERFACE library with the UbloxATCellularInterfaceExt
00042 // library).  However, it is slower than using the LWIP/PPP on the mbed
00043 // MCU interface since more string parsing is required.
00044 #define INTERFACE_USED UBLOX_AT_CELLULAR_INTERFACE
00045 //#define INTERFACE_USED ONBOARD_CELLULAR_INTERFACE
00046 //#define INTERFACE_USED UBLOX_AT_CELLULAR_INTERFACE_N2XX
00047 
00048 #if (INTERFACE_USED == UBLOX_AT_CELLULAR_INTERFACE)
00049 #define INTERFACE_CLASS  UbloxATCellularInterface
00050 #elif (INTERFACE_USED == ONBOARD_CELLULAR_INTERFACE)
00051 #define INTERFACE_CLASS  OnboardCellularInterface
00052 #elif (INTERFACE_USED == UBLOX_AT_CELLULAR_INTERFACE_N2XX)
00053 #define INTERFACE_CLASS  UbloxATCellularInterfaceN2xx
00054 #else
00055 #error please define an interface class to use
00056 #endif
00057 
00058 // The credentials of the SIM in the board.  If PIN checking is enabled
00059 // for your SIM card you must set this to the required PIN.
00060 #define PIN "0000"
00061 
00062 // Network credentials.  You should set this according to your
00063 // network/SIM card.  For C030 non-N2xx boards, leave the parameters as NULL
00064 // otherwise, if you do not know the APN for your network, you may
00065 // either try the fairly common "internet" for the APN (and leave the
00066 // username and password NULL), or you may leave all three as NULL and then
00067 // a lookup will be attempted for a small number of known networks
00068 // (see APN_db.h in mbed-os/features/netsocket/cellular/utils).
00069 #define APN         NULL
00070 #define USERNAME    NULL
00071 #define PASSWORD    NULL
00072 #define TCP_SERVER "os.mbed.com"
00073 
00074 // LEDs
00075 DigitalOut ledRed(LED1, 1);
00076 DigitalOut ledGreen(LED2, 1);
00077 DigitalOut ledBlue(LED3, 1);
00078 
00079 // The user button
00080 volatile bool buttonPressed = false;
00081 
00082 static void good() {
00083     ledGreen = 0;
00084     ledBlue = 1;
00085     ledRed = 1;
00086 }
00087 
00088 static void bad() {
00089     ledRed = 0;
00090     ledGreen = 1;
00091     ledBlue = 1;
00092 }
00093 
00094 static void event() {
00095     ledBlue = 0;
00096     ledRed = 1;
00097     ledGreen = 1;
00098 }
00099 
00100 static void pulseEvent() {
00101     event();
00102     wait_ms(500);
00103     good();
00104 }
00105 
00106 static void ledOff() {
00107     ledBlue = 1;
00108     ledRed = 1;
00109     ledGreen = 1;
00110 }
00111 
00112 static void printNtpTime(char * buf, int len)
00113 {
00114     time_t timestamp = 0;
00115     struct tm *localTime;
00116     char timeString[25];
00117     time_t TIME1970 = 2208988800U;
00118 
00119     if (len >= 43) {
00120         timestamp |= ((int) *(buf + 40)) << 24;
00121         timestamp |= ((int) *(buf + 41)) << 16;
00122         timestamp |= ((int) *(buf + 42)) << 8;
00123         timestamp |= ((int) *(buf + 43));
00124         timestamp -= TIME1970;
00125         localTime = localtime(&timestamp);
00126         if (localTime) {
00127             if (strftime(timeString, sizeof(timeString), "%a %b %d %H:%M:%S %Y", localTime) > 0) {
00128                 printf("NTP timestamp is %s.\n", timeString);
00129             }
00130         }
00131     }
00132 }
00133 
00134 static void cbButton()
00135 {
00136     buttonPressed = true;
00137     pulseEvent();
00138 }
00139 
00140 /* This changes the modem RAT and MNO profile to default values.
00141  * MBED_CONF_APP_CHANGE_RAT_MNO_TO_DEFAULT macro is set to 0 in mbed_app.json
00142  * to avoid accidently changing RAT and MNO. Not all modems support +URAT and +UMNOPROF commands.
00143  */
00144 #if (INTERFACE_USED == UBLOX_AT_CELLULAR_INTERFACE) && !defined(TARGET_UBLOX_C027) && !defined(TARGET_UBLOX_C030_N211)
00145 
00146 #ifndef MBED_CONF_APP_CHANGE_RAT_MNO_TO_DEFAULT
00147 #define MBED_CONF_APP_CHANGE_RAT_MNO_TO_DEFAULT 0
00148 #endif
00149 bool change_rat_mno_flag = MBED_CONF_APP_CHANGE_RAT_MNO_TO_DEFAULT;
00150 
00151 void change_rat_mno(UbloxATCellularInterface* interface)
00152 {
00153 #ifdef TARGET_UBLOX_C030_R41XM
00154     int current_profile;
00155 #endif
00156 
00157 #if defined(TARGET_UBLOX_C030_U201) || defined(TARGET_UBLOX_C030_R412M)
00158     int selected, preferred, second_preferred;
00159 #endif
00160 
00161     if (change_rat_mno_flag == true) {
00162         printf("\nSetting URAT and MNO profile to default values...\n");
00163         change_rat_mno_flag = false; //do the following just once as to demonstrate the usage
00164         //perform deregistration and set URAT and MNO profile to default values
00165         if ( (interface->is_registered_csd() || interface->is_registered_psd() || interface->is_registered_eps()) ) {
00166             printf("De-registering...\n\n");
00167             interface->nwk_deregistration();
00168             pulseEvent();
00169         }
00170 
00171 #ifdef TARGET_UBLOX_C030_R41XM
00172 
00173 #ifdef TARGET_UBLOX_C030_R412M //+URAT is not supported by R410M
00174         printf("Setting modem RAT to CATM1 and NB1...\n");
00175         if (interface->set_modem_rat(UbloxATCellularInterface::LTE_CATM1, UbloxATCellularInterface::LTE_CATNB1)) {
00176             printf("RAT configured\n");
00177             pulseEvent();
00178         }
00179         if (interface->get_modem_rat(&selected, &preferred, &second_preferred)) {
00180             printf("selected RAT: %d\npreferred RAT: %d\nsecond_preferred RAT: %d\n", selected, preferred, second_preferred);
00181         }
00182 #endif
00183         printf("Setting MNO profile to 0 (default)...\n");
00184         if (interface->set_mno_profile()) {
00185             printf("MNO profile configured\n");
00186             pulseEvent();
00187         }
00188         if (interface->get_mno_profile(&current_profile)) {
00189             printf("current_profile is: %d\n", (int)current_profile);
00190         }
00191 
00192 #elif defined TARGET_UBLOX_C030_U201
00193         printf("Setting modem RAT to GSM/UMTS and GSM_GPRS_EGPRS...\n");
00194         if (interface->set_modem_rat(UbloxATCellularInterface::GSM_UMTS, UbloxATCellularInterface::GSM_GPRS_EGPRS)) {
00195             printf("RAT configured\n");
00196             pulseEvent();
00197         }
00198         if (interface->get_modem_rat(&selected, &preferred, &second_preferred)) {
00199             printf("selected RAT: %d\npreferred RAT: %d\nsecond_preferred RAT: %d\n", selected, preferred, second_preferred);
00200         }
00201 #endif //TARGET_UBLOX_C030_R41XM
00202         printf("\nRebooting modem for settings to take effect...\n");
00203         if (interface->reboot_modem()) {
00204             printf("Reboot successful\n");
00205             pulseEvent();
00206         }
00207 
00208         printf("Performing registration, please wait...\n");
00209         for (int x = 0; interface->connect(PIN) != 0; x++) {
00210             if (x > 0) {
00211                 bad();
00212                 printf("Retrying (have you checked that an antenna is plugged in and your APN is correct?)...\n");
00213             }
00214         }
00215         pulseEvent();
00216     }
00217 }
00218 #endif
00219 
00220 /* This example program for the u-blox C030 and C027 boards instantiates
00221  * the UbloxATCellularInterface or OnboardCellularInterface and uses it
00222  * to make a simple sockets connection to a server, using 2.pool.ntp.org
00223  * for UDP and developer.mbed.org for TCP.  For a more comprehensive example,
00224  * where higher layer protocols make use of the same sockets interface,
00225  * see example-ublox-mbed-client.
00226  * Progress may be monitored with a serial terminal running at 9600 baud.
00227  * The LED on the C030 board will turn green when this program is
00228  * operating correctly, pulse blue when a sockets operation is completed
00229  * and turn red if there is a failure.
00230  */
00231 
00232 int main()
00233 {
00234     INTERFACE_CLASS *interface = new INTERFACE_CLASS();
00235     // If you need to debug the cellular interface, comment out the
00236     // instantiation above and uncomment the one below.
00237     // For the N2xx interface, change xxx to MBED_CONF_UBLOX_CELL_BAUD_RATE,
00238     // while for the non-N2xx interface change it to MBED_CONF_UBLOX_CELL_N2XX_BAUD_RATE.
00239 //    INTERFACE_CLASS *interface = new INTERFACE_CLASS(MDMTXD, MDMRXD,
00240 //                                                     xxx,
00241 //                                                     true);
00242 #ifndef TARGET_UBLOX_C030_N211
00243     TCPSocket sockTcp;
00244 #endif
00245     UDPSocket sockUdp;
00246     SocketAddress udpServer;
00247     SocketAddress udpSenderAddress;
00248     SocketAddress tcpServer;
00249     char buf[1024];
00250     int x;
00251 #ifdef TARGET_UBLOX_C027
00252     // No user button on C027
00253     InterruptIn userButton(NC);
00254 #else
00255     InterruptIn userButton(SW0);
00256 #endif
00257 
00258     // Attach a function to the user button
00259     userButton.rise(&cbButton);
00260 
00261     good();
00262     printf("Starting up, please wait up to 180 seconds for network registration to complete...\n");
00263     interface->set_credentials(APN, USERNAME, PASSWORD);
00264 
00265     for (x = 0; interface->connect(PIN) != 0; x++) {
00266         if (x > 0) {
00267             bad();
00268             printf("Retrying (have you checked that an antenna is plugged in and your APN is correct?)...\n");
00269         }
00270     }
00271     pulseEvent();
00272     
00273     printf("\nGetting the IP address of \"developer.mbed.org\" and \"2.pool.ntp.org\"...\n");
00274     if ((interface->gethostbyname("2.pool.ntp.org", &udpServer) == 0) &&
00275         (interface->gethostbyname(TCP_SERVER, &tcpServer) == 0)) {
00276         pulseEvent();
00277 
00278         udpServer.set_port(123);
00279         tcpServer.set_port(80);
00280         printf("\"2.pool.ntp.org\" address: %s on port %d.\n", udpServer.get_ip_address(), udpServer.get_port());
00281         printf("\"os.mbed.com\" address: %s on port %d.\n", tcpServer.get_ip_address(), tcpServer.get_port());
00282 
00283         printf("\nPerforming socket operations in a loop (until the user button is pressed on C030 or forever on C027)...\n");
00284         while (!buttonPressed) {
00285             // UDP Sockets
00286             printf("=== UDP ===\n");
00287             printf("Opening a UDP socket...\n");
00288             if (sockUdp.open(interface) == 0) {
00289                 pulseEvent();
00290                 printf("UDP socket open.\n");
00291                 sockUdp.set_timeout(10000);
00292                 printf("Sending time request to \"2.pool.ntp.org\" over UDP socket...\n");
00293                 memset (buf, 0, sizeof(buf));
00294                 *buf = '\x1b';
00295                 if (sockUdp.sendto(udpServer, (void *) buf, 48) == 48) {
00296                     pulseEvent();
00297                     printf("Socket send completed, waiting for UDP response...\n");
00298                     x = sockUdp.recvfrom(&udpSenderAddress, buf, sizeof (buf));
00299                     if (x > 0) {
00300                         pulseEvent();
00301                         printf("Received %d byte response from server %s on UDP socket:\n"
00302                                "-------------------------------------------------------\n",
00303                                x, udpSenderAddress.get_ip_address());
00304                         printNtpTime(buf, x);
00305                         printf("-------------------------------------------------------\n");
00306                     }
00307                 }                
00308                 printf("Closing socket...\n");
00309                 sockUdp.close();
00310                 pulseEvent();
00311                 printf("Socket closed.\n");
00312             }
00313             
00314 #ifndef TARGET_UBLOX_C030_N211
00315             // TCP Sockets
00316             printf("=== TCP ===\n");
00317             printf("Opening a TCP socket...\n");
00318             if (sockTcp.open(interface) == 0) {
00319                 pulseEvent();
00320                 printf("TCP socket open.\n");
00321                 sockTcp.set_timeout(10000);
00322                 printf("Connecting socket to %s on port %d...\n", tcpServer.get_ip_address(), tcpServer.get_port());
00323                 if (sockTcp.connect(tcpServer) == 0) {
00324                     pulseEvent();
00325                     printf("Connected, sending HTTP GET request to %s over socket...\n", TCP_SERVER);
00326                     strcpy (buf, "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n");
00327                     // Note: since this is a short string we can send it in one go as it will
00328                     // fit within the default buffer sizes.  Normally you should call sock.send()
00329                     // in a loop until your entire buffer has been sent.
00330                     if (sockTcp.send((void *) buf, strlen(buf)) == (int) strlen(buf)) {
00331                         pulseEvent();
00332                         printf("Socket send completed, waiting for response...\n");
00333                         x = sockTcp.recv(buf, sizeof (buf));
00334                         if (x > 0) {
00335                             pulseEvent();
00336                             printf("Received %d byte response from server on TCP socket:\n"
00337                                    "----------------------------------------------------\n%.*s"
00338                                    "----------------------------------------------------\n",
00339                                     x, x, buf);
00340                         }
00341                     }
00342                 }
00343                 printf("Closing socket...\n");
00344                 sockTcp.close();
00345                 pulseEvent();
00346                 printf("Socket closed.\n");
00347             }
00348 #if (INTERFACE_USED == UBLOX_AT_CELLULAR_INTERFACE) && !defined(TARGET_UBLOX_C027)
00349             change_rat_mno(interface);
00350 #endif //(INTERFACE_USED == UBLOX_AT_CELLULAR_INTERFACE)
00351 #endif //TARGET_UBLOX_C030_N211
00352             wait_ms(5000);
00353 #ifndef TARGET_UBLOX_C027
00354             printf("[Checking if user button has been pressed]\n");
00355 #endif
00356         }
00357         
00358         pulseEvent();
00359         printf("User button was pressed, stopping...\n");
00360         interface->disconnect();
00361         ledOff();
00362         printf("Stopped.\n");
00363     } else {
00364         bad();
00365         printf("Unable to get IP address of \"developer.mbed.org\" or \"2.pool.ntp.org\".\n");
00366     }
00367 }
00368 // End Of File