Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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 #ifdef TARGET_UBLOX_C030_R41XM 00164 interface->init(PIN); 00165 interface->disable_power_saving_mode(); //Please use PSM example to test PSM functionality(https://os.mbed.com/teams/ublox/code/example-ublox-cellular-psm/) 00166 #endif 00167 for (x = 0; interface->connect(PIN) != 0; x++) { 00168 if (x > 0) { 00169 bad(); 00170 printf("Retrying (have you checked that an antenna is plugged in and your APN is correct?)...\n"); 00171 } 00172 } 00173 pulseEvent(); 00174 00175 printf("Getting the IP address of \"developer.mbed.org\" and \"2.pool.ntp.org\"...\n"); 00176 if ((interface->gethostbyname("2.pool.ntp.org", &udpServer) == 0) && 00177 (interface->gethostbyname(TCP_SERVER, &tcpServer) == 0)) { 00178 pulseEvent(); 00179 00180 udpServer.set_port(123); 00181 tcpServer.set_port(80); 00182 printf("\"2.pool.ntp.org\" address: %s on port %d.\n", udpServer.get_ip_address(), udpServer.get_port()); 00183 printf("\"os.mbed.com\" address: %s on port %d.\n", tcpServer.get_ip_address(), tcpServer.get_port()); 00184 00185 printf("Performing socket operations in a loop (until the user button is pressed on C030 or forever on C027)...\n"); 00186 while (!buttonPressed) { 00187 // UDP Sockets 00188 printf("=== UDP ===\n"); 00189 printf("Opening a UDP socket...\n"); 00190 if (sockUdp.open(interface) == 0) { 00191 pulseEvent(); 00192 printf("UDP socket open.\n"); 00193 sockUdp.set_timeout(10000); 00194 printf("Sending time request to \"2.pool.ntp.org\" over UDP socket...\n"); 00195 memset (buf, 0, sizeof(buf)); 00196 *buf = '\x1b'; 00197 if (sockUdp.sendto(udpServer, (void *) buf, 48) == 48) { 00198 pulseEvent(); 00199 printf("Socket send completed, waiting for UDP response...\n"); 00200 x = sockUdp.recvfrom(&udpSenderAddress, buf, sizeof (buf)); 00201 if (x > 0) { 00202 pulseEvent(); 00203 printf("Received %d byte response from server %s on UDP socket:\n" 00204 "-------------------------------------------------------\n", 00205 x, udpSenderAddress.get_ip_address()); 00206 printNtpTime(buf, x); 00207 printf("-------------------------------------------------------\n"); 00208 } 00209 } 00210 printf("Closing socket...\n"); 00211 sockUdp.close(); 00212 pulseEvent(); 00213 printf("Socket closed.\n"); 00214 } 00215 00216 #ifndef TARGET_UBLOX_C030_N211 00217 // TCP Sockets 00218 printf("=== TCP ===\n"); 00219 printf("Opening a TCP socket...\n"); 00220 if (sockTcp.open(interface) == 0) { 00221 pulseEvent(); 00222 printf("TCP socket open.\n"); 00223 sockTcp.set_timeout(10000); 00224 printf("Connecting socket to %s on port %d...\n", tcpServer.get_ip_address(), tcpServer.get_port()); 00225 if (sockTcp.connect(tcpServer) == 0) { 00226 pulseEvent(); 00227 printf("Connected, sending HTTP GET request to %s over socket...\n", TCP_SERVER); 00228 strcpy (buf, "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n"); 00229 // Note: since this is a short string we can send it in one go as it will 00230 // fit within the default buffer sizes. Normally you should call sock.send() 00231 // in a loop until your entire buffer has been sent. 00232 if (sockTcp.send((void *) buf, strlen(buf)) == (int) strlen(buf)) { 00233 pulseEvent(); 00234 printf("Socket send completed, waiting for response...\n"); 00235 x = sockTcp.recv(buf, sizeof (buf)); 00236 if (x > 0) { 00237 pulseEvent(); 00238 printf("Received %d byte response from server on TCP socket:\n" 00239 "----------------------------------------------------\n%.*s" 00240 "----------------------------------------------------\n", 00241 x, x, buf); 00242 } 00243 } 00244 } 00245 printf("Closing socket...\n"); 00246 sockTcp.close(); 00247 pulseEvent(); 00248 printf("Socket closed.\n"); 00249 } 00250 #endif 00251 wait_ms(5000); 00252 #ifndef TARGET_UBLOX_C027 00253 printf("[Checking if user button has been pressed]\n"); 00254 #endif 00255 } 00256 00257 pulseEvent(); 00258 printf("User button was pressed, stopping...\n"); 00259 interface->disconnect(); 00260 ledOff(); 00261 printf("Stopped.\n"); 00262 } else { 00263 bad(); 00264 printf("Unable to get IP address of \"developer.mbed.org\" or \"2.pool.ntp.org\".\n"); 00265 } 00266 } 00267 // End Of File
Generated on Tue Jul 12 2022 20:36:26 by
1.7.2