
Testing C030_R410M
Dependencies: ublox-at-cellular-interface-n2xx ublox-at-cellular-interface ublox-cellular-base-n2xx ublox-cellular-base ublox-ppp-cellular-interface
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 "UbloxPPPCellularInterface.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 // UbloxPPPCellularInterface Y - Y 00028 // UbloxATCellularInterface Y - Y 00029 // UbloxATCellularInterfaceN2xx - Y - 00030 // Note: the N211 module supports only UDP, not TCP 00031 00032 // UbloxPPPCellularInterface 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 UbloxPPPCellularInterface 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 "0000" 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 NULL 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 // 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(×tamp); 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 UbloxPPPCellularInterface 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 if (interface->init(PIN)) { 00170 pulseEvent(); 00171 interface->set_credentials(APN, USERNAME, PASSWORD); 00172 printf("Registered, connecting to the packet network...\n"); 00173 for (x = 0; interface->connect() != 0; x++) { 00174 if (x > 0) { 00175 bad(); 00176 printf("Retrying (have you checked that an antenna is plugged in and your APN is correct?)...\n"); 00177 } 00178 } 00179 pulseEvent(); 00180 00181 printf("Getting the IP address of \"developer.mbed.org\" and \"2.pool.ntp.org\"...\n"); 00182 if ((interface->gethostbyname("2.pool.ntp.org", &udpServer) == 0) && 00183 (interface->gethostbyname("developer.mbed.org", &tcpServer) == 0)) { 00184 pulseEvent(); 00185 00186 udpServer.set_port(123); 00187 printf("\"2.pool.ntp.org\" address: %s on port %d.\n", udpServer.get_ip_address(), udpServer.get_port()); 00188 printf("\"developer.mbed.org\" address: %s on port %d.\n", tcpServer.get_ip_address(), tcpServer.get_port()); 00189 tcpServer.set_port(80); 00190 00191 printf("Performing socket operations in a loop (until the user button is pressed on C030 or forever on C027)...\n"); 00192 while (!buttonPressed) { 00193 // UDP Sockets 00194 printf("=== UDP ===\n"); 00195 printf("Opening a UDP socket...\n"); 00196 if (sockUdp.open(interface) == 0) { 00197 pulseEvent(); 00198 printf("UDP socket open.\n"); 00199 sockUdp.set_timeout(10000); 00200 printf("Sending time request to \"2.pool.ntp.org\" over UDP socket...\n"); 00201 memset (buf, 0, sizeof(buf)); 00202 *buf = '\x1b'; 00203 if (sockUdp.sendto(udpServer, (void *) buf, 48) == 48) { 00204 pulseEvent(); 00205 printf("Socket send completed, waiting for UDP response...\n"); 00206 x = sockUdp.recvfrom(&udpSenderAddress, buf, sizeof (buf)); 00207 if (x > 0) { 00208 pulseEvent(); 00209 printf("Received %d byte response from server %s on UDP socket:\n" 00210 "-------------------------------------------------------\n", 00211 x, udpSenderAddress.get_ip_address()); 00212 printNtpTime(buf, x); 00213 printf("-------------------------------------------------------\n"); 00214 } 00215 } 00216 printf("Closing socket...\n"); 00217 sockUdp.close(); 00218 pulseEvent(); 00219 printf("Socket closed.\n"); 00220 } 00221 00222 #ifndef TARGET_UBLOX_C030_N211 00223 // TCP Sockets 00224 printf("=== TCP ===\n"); 00225 printf("Opening a TCP socket...\n"); 00226 if (sockTcp.open(interface) == 0) { 00227 pulseEvent(); 00228 printf("TCP socket open.\n"); 00229 sockTcp.set_timeout(10000); 00230 printf("Connecting socket to %s on port %d...\n", tcpServer.get_ip_address(), tcpServer.get_port()); 00231 if (sockTcp.connect(tcpServer) == 0) { 00232 pulseEvent(); 00233 printf("Connected, sending HTTP GET request to \"developer.mbed.org\" over socket...\n"); 00234 strcpy (buf, "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n"); 00235 // Note: since this is a short string we can send it in one go as it will 00236 // fit within the default buffer sizes. Normally you should call sock.send() 00237 // in a loop until your entire buffer has been sent. 00238 if (sockTcp.send((void *) buf, strlen(buf)) == (int) strlen(buf)) { 00239 pulseEvent(); 00240 printf("Socket send completed, waiting for response...\n"); 00241 x = sockTcp.recv(buf, sizeof (buf)); 00242 if (x > 0) { 00243 pulseEvent(); 00244 printf("Received %d byte response from server on TCP socket:\n" 00245 "----------------------------------------------------\n%.*s" 00246 "----------------------------------------------------\n", 00247 x, x, buf); 00248 } 00249 } 00250 } 00251 printf("Closing socket...\n"); 00252 sockTcp.close(); 00253 pulseEvent(); 00254 printf("Socket closed.\n"); 00255 } 00256 #endif 00257 wait_ms(5000); 00258 #ifndef TARGET_UBLOX_C027 00259 printf("[Checking if user button has been pressed]\n"); 00260 #endif 00261 } 00262 00263 pulseEvent(); 00264 printf("User button was pressed, stopping...\n"); 00265 interface->disconnect(); 00266 interface->deinit(); 00267 ledOff(); 00268 printf("Stopped.\n"); 00269 } else { 00270 bad(); 00271 printf("Unable to get IP address of \"developer.mbed.org\" or \"2.pool.ntp.org\".\n"); 00272 } 00273 } else { 00274 bad(); 00275 printf("Unable to initialise the interface.\n"); 00276 } 00277 } 00278 00279 // End Of File
Generated on Sun Jan 13 2019 18:41:01 by
