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