u-blox / Mbed OS example-ublox-cellular-radio-access-technology

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