Simple example for SARA modem.
Dependencies: ublox-at-cellular-interface-n2xx ublox-at-cellular-interface ublox-cellular-base-n2xx ublox-cellular-base
Fork of example-ublox-cellular-interface by
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 NULL 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 #define APN "nb.inetd.gdsp" 00057 #define USERNAME NULL 00058 #define PASSWORD NULL 00059 00060 // LEDs 00061 DigitalOut ledRed(LED1, 1); 00062 DigitalOut ledGreen(LED2, 1); 00063 DigitalOut ledBlue(LED3, 1); 00064 00065 DigitalIn testInput(PD_5); 00066 00067 // The user button 00068 volatile bool buttonPressed = false; 00069 00070 static void good() { 00071 ledGreen = 0; 00072 ledBlue = 1; 00073 ledRed = 1; 00074 } 00075 00076 static void bad() { 00077 ledRed = 0; 00078 ledGreen = 1; 00079 ledBlue = 1; 00080 } 00081 00082 static void event() { 00083 ledBlue = 0; 00084 ledRed = 1; 00085 ledGreen = 1; 00086 } 00087 00088 static void pulseEvent() { 00089 event(); 00090 wait_ms(500); 00091 good(); 00092 } 00093 00094 static void ledOff() { 00095 ledBlue = 1; 00096 ledRed = 1; 00097 ledGreen = 1; 00098 } 00099 00100 static void printNtpTime(char * buf, int len) 00101 { 00102 time_t timestamp = 0; 00103 struct tm *localTime; 00104 char timeString[25]; 00105 time_t TIME1970 = 2208988800U; 00106 00107 if (len >= 43) { 00108 timestamp |= ((int) *(buf + 40)) << 24; 00109 timestamp |= ((int) *(buf + 41)) << 16; 00110 timestamp |= ((int) *(buf + 42)) << 8; 00111 timestamp |= ((int) *(buf + 43)); 00112 timestamp -= TIME1970; 00113 localTime = localtime(×tamp); 00114 if (localTime) { 00115 if (strftime(timeString, sizeof(timeString), "%a %b %d %H:%M:%S %Y", localTime) > 0) { 00116 printf("NTP timestamp is %s.\n", timeString); 00117 } 00118 } 00119 } 00120 } 00121 00122 static void cbButton() 00123 { 00124 buttonPressed = true; 00125 pulseEvent(); 00126 } 00127 00128 /* This example program for the u-blox C030 and C027 boards instantiates 00129 * the UbloxATCellularInterface or OnboardCellularInterface and uses it 00130 * to make a simple sockets connection to a server, using 2.pool.ntp.org 00131 * for UDP and developer.mbed.org for TCP. For a more comprehensive example, 00132 * where higher layer protocols make use of the same sockets interface, 00133 * see example-ublox-mbed-client. 00134 * Progress may be monitored with a serial terminal running at 9600 baud. 00135 * The LED on the C030 board will turn green when this program is 00136 * operating correctly, pulse blue when a sockets operation is completed 00137 * and turn red if there is a failure. 00138 */ 00139 00140 int main() 00141 { 00142 //INTERFACE_CLASS *interface = new INTERFACE_CLASS(); 00143 // If you need to debug the cellular interface, comment out the 00144 // instantiation above and uncomment the one below. 00145 // For the N2xx interface, change xxx to MBED_CONF_UBLOX_CELL_BAUD_RATE, 00146 // while for the non-N2xx interface change it to MBED_CONF_UBLOX_CELL_N2XX_BAUD_RATE. 00147 INTERFACE_CLASS *interface = new INTERFACE_CLASS(MDMTXD, MDMRXD, 00148 MBED_CONF_UBLOX_CELL_N2XX_BAUD_RATE, 00149 true); 00150 #ifndef TARGET_UBLOX_C030_N211 00151 TCPSocket sockTcp; 00152 #endif 00153 UDPSocket sockUdp; 00154 SocketAddress udpServer; 00155 SocketAddress udpSenderAddress; 00156 SocketAddress tcpServer; 00157 char buf[1024]; 00158 int x; 00159 #ifdef TARGET_UBLOX_C027 00160 // No user button on C027 00161 InterruptIn userButton(NC); 00162 #else 00163 InterruptIn userButton(SW0); 00164 #endif 00165 00166 // Attach a function to the user button 00167 userButton.rise(&cbButton); 00168 00169 good(); 00170 printf("Starting up, please wait up to 180 seconds for network registration to complete...\n"); 00171 interface->set_credentials(APN, USERNAME, PASSWORD); 00172 for (x = 0; interface->connect(PIN) != 0; x++) { 00173 if (x > 0) { 00174 bad(); 00175 printf("Retrying (have you checked that an antenna is plugged in and your APN is correct?)...\n"); 00176 } 00177 } 00178 pulseEvent(); 00179 00180 printf("Getting the IP address of \"developer.mbed.org\" and \"2.pool.ntp.org\"...\n"); 00181 if ((interface->gethostbyname("2.pool.ntp.org", &udpServer) == 0) && 00182 (interface->gethostbyname("developer.mbed.org", &tcpServer) == 0)) { 00183 pulseEvent(); 00184 00185 udpServer.set_port(123); 00186 printf("\"2.pool.ntp.org\" address: %s on port %d.\n", udpServer.get_ip_address(), udpServer.get_port()); 00187 printf("\"developer.mbed.org\" address: %s on port %d.\n", tcpServer.get_ip_address(), tcpServer.get_port()); 00188 tcpServer.set_port(80); 00189 00190 printf("Performing socket operations in a loop (until the user button is pressed on C030 or forever on C027)...\n"); 00191 while (!buttonPressed) { 00192 // UDP Sockets 00193 printf("=== UDP ===\n"); 00194 printf("Opening a UDP socket...\n"); 00195 if (sockUdp.open(interface) == 0) { 00196 pulseEvent(); 00197 printf("UDP socket open.\n"); 00198 sockUdp.set_timeout(10000); 00199 printf("Sending time request to \"2.pool.ntp.org\" over UDP socket...\n"); 00200 memset (buf, 0, sizeof(buf)); 00201 *buf = '\x1b'; 00202 if (sockUdp.sendto(udpServer, (void *) buf, 48) == 48) { 00203 pulseEvent(); 00204 printf("Socket send completed, waiting for UDP response...\n"); 00205 x = sockUdp.recvfrom(&udpSenderAddress, buf, sizeof (buf)); 00206 if (x > 0) { 00207 pulseEvent(); 00208 printf("Received %d byte response from server %s on UDP socket:\n" 00209 "-------------------------------------------------------\n", 00210 x, udpSenderAddress.get_ip_address()); 00211 printNtpTime(buf, x); 00212 printf("-------------------------------------------------------\n"); 00213 } 00214 } 00215 printf("Closing socket...\n"); 00216 sockUdp.close(); 00217 pulseEvent(); 00218 printf("Socket closed.\n"); 00219 } 00220 00221 #ifndef TARGET_UBLOX_C030_N211 00222 // TCP Sockets 00223 printf("=== TCP ===\n"); 00224 printf("Opening a TCP socket...\n"); 00225 if (sockTcp.open(interface) == 0) { 00226 pulseEvent(); 00227 printf("TCP socket open.\n"); 00228 sockTcp.set_timeout(10000); 00229 printf("Connecting socket to %s on port %d...\n", tcpServer.get_ip_address(), tcpServer.get_port()); 00230 if (sockTcp.connect(tcpServer) == 0) { 00231 pulseEvent(); 00232 printf("Connected, sending HTTP GET request to \"developer.mbed.org\" over socket...\n"); 00233 strcpy (buf, "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n"); 00234 // Note: since this is a short string we can send it in one go as it will 00235 // fit within the default buffer sizes. Normally you should call sock.send() 00236 // in a loop until your entire buffer has been sent. 00237 if (sockTcp.send((void *) buf, strlen(buf)) == (int) strlen(buf)) { 00238 pulseEvent(); 00239 printf("Socket send completed, waiting for response...\n"); 00240 x = sockTcp.recv(buf, sizeof (buf)); 00241 if (x > 0) { 00242 pulseEvent(); 00243 printf("Received %d byte response from server on TCP socket:\n" 00244 "----------------------------------------------------\n%.*s" 00245 "----------------------------------------------------\n", 00246 x, x, buf); 00247 } 00248 } 00249 } 00250 printf("Closing socket...\n"); 00251 sockTcp.close(); 00252 pulseEvent(); 00253 printf("Socket closed.\n"); 00254 } 00255 #endif 00256 wait_ms(5000); 00257 #ifndef TARGET_UBLOX_C027 00258 printf("[Checking if user button has been pressed]\n"); 00259 #endif 00260 } 00261 00262 pulseEvent(); 00263 printf("User button was pressed, stopping...\n"); 00264 interface->disconnect(); 00265 ledOff(); 00266 printf("Stopped.\n"); 00267 } else { 00268 bad(); 00269 printf("Unable to get IP address of \"developer.mbed.org\" or \"2.pool.ntp.org\".\n"); 00270 } 00271 } 00272 00273 // End Of File
Generated on Sat Jul 16 2022 04:03:08 by
1.7.2
