Andreas Stefan / Mbed OS ublox NB-IoT

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