Point Labs / Mbed OS example-geneva-cellular-interface

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

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