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