updates

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 "CellularContext.h"
00019 #include "UbloxATCellularInterfaceN2xx.h"
00020 
00021 // You must select the correct interface library for your board, by
00022 // uncommenting the correct line below. Supported combinations are
00023 // indicated with a "Y" in the table below.
00024 //
00025 //                            C030_U201   C030_N211      C027
00026 // UbloxATCellularInterface       Y            -           Y
00027 // OnboardCellularInterface       Y            -           Y
00028 // UbloxATCellularInterfaceN2xx   -            Y           -
00029 // Note: the N211 module supports only UDP, not TCP
00030 
00031 // OnboardCellularInterface uses LWIP and the PPP cellular interface
00032 // on the mbed MCU, while using UbloxATCellularInterface and
00033 // UbloxATCellularInterfaceN2xx uses an IP stack on the cellular
00034 // module and hence uses less RAM (significant on C027).  This also
00035 // allows other AT command operations (e.g. sending an SMS) to happen
00036 // during a data transfer (for which you should replace the
00037 // UbloxATCellularInterface library with the UbloxATCellularInterfaceExt
00038 // library).  However, it is slower than using the LWIP/PPP on the mbed
00039 // MCU interface since more string parsing is required.
00040 #define INTERFACE_CLASS  UbloxATCellularInterface
00041 //#define INTERFACE_CLASS  OnboardCellularInterface
00042 //#define INTERFACE_CLASS  UbloxATCellularInterfaceN2xx
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         NULL
00056 #define USERNAME    NULL
00057 #define PASSWORD    NULL
00058 #define TCP_SERVER "os.mbed.com"
00059 
00060 // LEDs
00061 DigitalOut ledRed(LED1, 1);
00062 DigitalOut ledGreen(LED2, 1);
00063 DigitalOut ledBlue(LED3, 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     pulseEvent();
00124 }
00125 
00126 /* This example program for the u-blox C030 and C027 boards instantiates
00127  * the UbloxATCellularInterface or OnboardCellularInterface and uses it
00128  * to make a simple sockets connection to a server, using 2.pool.ntp.org
00129  * for UDP and developer.mbed.org for TCP.  For a more comprehensive example,
00130  * where higher layer protocols make use of the same sockets interface,
00131  * see example-ublox-mbed-client.
00132  * Progress may be monitored with a serial terminal running at 9600 baud.
00133  * The LED on the C030 board will turn green when this program is
00134  * operating correctly, pulse blue when a sockets operation is completed
00135  * and turn red if there is a failure.
00136  */
00137 
00138 int main()
00139 {
00140     INTERFACE_CLASS *interface = new INTERFACE_CLASS();
00141     // If you need to debug the cellular interface, comment out the
00142     // instantiation above and uncomment the one below.
00143     // For the N2xx interface, change xxx to MBED_CONF_UBLOX_CELL_BAUD_RATE,
00144     // while for the non-N2xx interface change it to MBED_CONF_UBLOX_CELL_N2XX_BAUD_RATE.
00145 //    INTERFACE_CLASS *interface = new INTERFACE_CLASS(MDMTXD, MDMRXD,
00146 //                                                     xxx,
00147 //                                                     true);
00148 #ifndef TARGET_UBLOX_C030_N211
00149     TCPSocket sockTcp;
00150 #endif
00151     UDPSocket sockUdp;
00152     SocketAddress udpServer;
00153     SocketAddress udpSenderAddress;
00154     SocketAddress tcpServer;
00155     char buf[1024];
00156     int x;
00157 #ifdef TARGET_UBLOX_C027
00158     // No user button on C027
00159     InterruptIn userButton(NC);
00160 #else
00161     InterruptIn userButton(SW0);
00162 #endif
00163     
00164     // Attach a function to the user button
00165     userButton.rise(&cbButton);
00166     
00167     good();
00168     printf("Starting up, please wait up to 180 seconds for network registration to complete...\n");
00169     interface->set_credentials(APN, USERNAME, PASSWORD);
00170     for (x = 0; interface->connect(PIN) != 0; x++) {
00171         if (x > 0) {
00172             bad();
00173             printf("Retrying (have you checked that an antenna is plugged in and your APN is correct?)...\n");
00174         }
00175     }
00176     pulseEvent();
00177     
00178     printf("Getting the IP address of \"developer.mbed.org\" and \"2.pool.ntp.org\"...\n");
00179     if ((interface->gethostbyname("2.pool.ntp.org", &udpServer) == 0) &&
00180         (interface->gethostbyname(TCP_SERVER, &tcpServer) == 0)) {
00181         pulseEvent();
00182 
00183         udpServer.set_port(123);
00184         tcpServer.set_port(80);
00185         printf("\"2.pool.ntp.org\" address: %s on port %d.\n", udpServer.get_ip_address(), udpServer.get_port());
00186         printf("\"os.mbed.com\" address: %s on port %d.\n", tcpServer.get_ip_address(), tcpServer.get_port());
00187 
00188         printf("Performing socket operations in a loop (until the user button is pressed on C030 or forever on C027)...\n");
00189         while (!buttonPressed) {
00190             // UDP Sockets
00191             printf("=== UDP ===\n");
00192             printf("Opening a UDP socket...\n");
00193             if (sockUdp.open(interface) == 0) {
00194                 pulseEvent();
00195                 printf("UDP socket open.\n");
00196                 sockUdp.set_timeout(10000);
00197                 printf("Sending time request to \"2.pool.ntp.org\" over UDP socket...\n");
00198                 memset (buf, 0, sizeof(buf));
00199                 *buf = '\x1b';
00200                 if (sockUdp.sendto(udpServer, (void *) buf, 48) == 48) {
00201                     pulseEvent();
00202                     printf("Socket send completed, waiting for UDP response...\n");
00203                     x = sockUdp.recvfrom(&udpSenderAddress, buf, sizeof (buf));
00204                     if (x > 0) {
00205                         pulseEvent();
00206                         printf("Received %d byte response from server %s on UDP socket:\n"
00207                                "-------------------------------------------------------\n",
00208                                x, udpSenderAddress.get_ip_address());
00209                         printNtpTime(buf, x);
00210                         printf("-------------------------------------------------------\n");
00211                     }
00212                 }                
00213                 printf("Closing socket...\n");
00214                 sockUdp.close();
00215                 pulseEvent();
00216                 printf("Socket closed.\n");
00217             }
00218             
00219 #ifndef TARGET_UBLOX_C030_N211
00220             // TCP Sockets
00221             printf("=== TCP ===\n");
00222             printf("Opening a TCP socket...\n");
00223             if (sockTcp.open(interface) == 0) {
00224                 pulseEvent();
00225                 printf("TCP socket open.\n");
00226                 sockTcp.set_timeout(10000);
00227                 printf("Connecting socket to %s on port %d...\n", tcpServer.get_ip_address(), tcpServer.get_port());
00228                 if (sockTcp.connect(tcpServer) == 0) {
00229                     pulseEvent();
00230                     printf("Connected, sending HTTP GET request to %s over socket...\n", TCP_SERVER);
00231                     strcpy (buf, "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n");
00232                     // Note: since this is a short string we can send it in one go as it will
00233                     // fit within the default buffer sizes.  Normally you should call sock.send()
00234                     // in a loop until your entire buffer has been sent.
00235                     if (sockTcp.send((void *) buf, strlen(buf)) == (int) strlen(buf)) {
00236                         pulseEvent();
00237                         printf("Socket send completed, waiting for response...\n");
00238                         x = sockTcp.recv(buf, sizeof (buf));
00239                         if (x > 0) {
00240                             pulseEvent();
00241                             printf("Received %d byte response from server on TCP socket:\n"
00242                                    "----------------------------------------------------\n%.*s"
00243                                    "----------------------------------------------------\n",
00244                                     x, x, buf);
00245                         }
00246                     }
00247                 }
00248                 printf("Closing socket...\n");
00249                 sockTcp.close();
00250                 pulseEvent();
00251                 printf("Socket closed.\n");
00252             }
00253 #endif
00254             wait_ms(5000);
00255 #ifndef TARGET_UBLOX_C027
00256             printf("[Checking if user button has been pressed]\n");
00257 #endif
00258         }
00259         
00260         pulseEvent();
00261         printf("User button was pressed, stopping...\n");
00262         interface->disconnect();
00263         ledOff();
00264         printf("Stopped.\n");
00265     } else {
00266         bad();
00267         printf("Unable to get IP address of \"developer.mbed.org\" or \"2.pool.ntp.org\".\n");
00268     }
00269 }
00270 // End Of File