example-ublox-cellular-interface_test
Dependencies: ublox-at-cellular-interface ublox-cellular-base ublox-cellular-base-n2xx ublox-at-cellular-interface-n2xx
main.cpp@43:0865a8f69787, 2019-11-08 (annotated)
- Committer:
- wajahat.abbas@u-blox.com
- Date:
- Fri Nov 08 15:41:30 2019 +0500
- Revision:
- 43:0865a8f69787
- Parent:
- 38:e09e02c31ba1
Updated example program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rob.meades@u-blox.com | 1:581335dbdd60 | 1 | /* mbed Microcontroller Library |
rob.meades@u-blox.com | 1:581335dbdd60 | 2 | * Copyright (c) 2017 u-blox |
rob.meades@u-blox.com | 1:581335dbdd60 | 3 | * |
rob.meades@u-blox.com | 1:581335dbdd60 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rob.meades@u-blox.com | 1:581335dbdd60 | 5 | * you may not use this file except in compliance with the License. |
rob.meades@u-blox.com | 1:581335dbdd60 | 6 | * You may obtain a copy of the License at |
rob.meades@u-blox.com | 1:581335dbdd60 | 7 | * |
rob.meades@u-blox.com | 1:581335dbdd60 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rob.meades@u-blox.com | 1:581335dbdd60 | 9 | * |
rob.meades@u-blox.com | 1:581335dbdd60 | 10 | * Unless required by applicable law or agreed to in writing, software |
rob.meades@u-blox.com | 1:581335dbdd60 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rob.meades@u-blox.com | 1:581335dbdd60 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rob.meades@u-blox.com | 1:581335dbdd60 | 13 | * See the License for the specific language governing permissions and |
rob.meades@u-blox.com | 1:581335dbdd60 | 14 | * limitations under the License. |
rob.meades@u-blox.com | 1:581335dbdd60 | 15 | */ |
rob.meades@u-blox.com | 1:581335dbdd60 | 16 | #include "mbed.h" |
rob.meades@u-blox.com | 1:581335dbdd60 | 17 | #include "UbloxATCellularInterface.h" |
rob.meades@u-blox.com | 28:e33af9f1ce3e | 18 | #include "UbloxATCellularInterfaceN2xx.h" |
RobMeades | 8:3e170c40a284 | 19 | |
rob.meades@u-blox.com | 31:4a36566b5385 | 20 | // You must select the correct interface library for your board, by |
rob.meades@u-blox.com | 31:4a36566b5385 | 21 | // uncommenting the correct line below. Supported combinations are |
rob.meades@u-blox.com | 31:4a36566b5385 | 22 | // indicated with a "Y" in the table below. |
rob.meades@u-blox.com | 28:e33af9f1ce3e | 23 | // |
wajahat.abbas@u-blox.com | 38:e09e02c31ba1 | 24 | // C030_U201 C030_N211 C027 C030_R41XM |
wajahat.abbas@u-blox.com | 38:e09e02c31ba1 | 25 | // UbloxATCellularInterface Y - Y Y |
wajahat.abbas@u-blox.com | 38:e09e02c31ba1 | 26 | // UbloxATCellularInterfaceN2xx - Y - - |
rob.meades@u-blox.com | 28:e33af9f1ce3e | 27 | // Note: the N211 module supports only UDP, not TCP |
rob.meades@u-blox.com | 28:e33af9f1ce3e | 28 | |
wajahat.abbas@u-blox.com | 38:e09e02c31ba1 | 29 | // UbloxATCellularInterface and UbloxATCellularInterfaceN2xx |
wajahat.abbas@u-blox.com | 38:e09e02c31ba1 | 30 | // uses an IP stack on the cellular module and hence uses less RAM (significant on C027). |
wajahat.abbas@u-blox.com | 38:e09e02c31ba1 | 31 | // This also allows other AT command operations (e.g. sending an SMS) to happen |
rob.meades@u-blox.com | 28:e33af9f1ce3e | 32 | // during a data transfer (for which you should replace the |
rob.meades@u-blox.com | 28:e33af9f1ce3e | 33 | // UbloxATCellularInterface library with the UbloxATCellularInterfaceExt |
rob.meades@u-blox.com | 28:e33af9f1ce3e | 34 | // library). However, it is slower than using the LWIP/PPP on the mbed |
rob.meades@u-blox.com | 28:e33af9f1ce3e | 35 | // MCU interface since more string parsing is required. |
RobMeades | 8:3e170c40a284 | 36 | #define INTERFACE_CLASS UbloxATCellularInterface |
rob.meades@u-blox.com | 28:e33af9f1ce3e | 37 | //#define INTERFACE_CLASS UbloxATCellularInterfaceN2xx |
rob.meades@u-blox.com | 1:581335dbdd60 | 38 | |
rob.meades@u-blox.com | 1:581335dbdd60 | 39 | // The credentials of the SIM in the board. If PIN checking is enabled |
rob.meades@u-blox.com | 1:581335dbdd60 | 40 | // for your SIM card you must set this to the required PIN. |
rob.meades@u-blox.com | 1:581335dbdd60 | 41 | #define PIN "0000" |
rob.meades@u-blox.com | 1:581335dbdd60 | 42 | |
rob.meades@u-blox.com | 1:581335dbdd60 | 43 | // Network credentials. You should set this according to your |
rob.meades@u-blox.com | 30:4e55e975dd0b | 44 | // network/SIM card. For C030 non-N2xx boards, leave the parameters as NULL |
rob.meades@u-blox.com | 1:581335dbdd60 | 45 | // otherwise, if you do not know the APN for your network, you may |
rob.meades@u-blox.com | 1:581335dbdd60 | 46 | // either try the fairly common "internet" for the APN (and leave the |
rob.meades@u-blox.com | 1:581335dbdd60 | 47 | // username and password NULL), or you may leave all three as NULL and then |
rob.meades@u-blox.com | 1:581335dbdd60 | 48 | // a lookup will be attempted for a small number of known networks |
rob.meades@u-blox.com | 1:581335dbdd60 | 49 | // (see APN_db.h in mbed-os/features/netsocket/cellular/utils). |
rob.meades@u-blox.com | 1:581335dbdd60 | 50 | #define APN NULL |
rob.meades@u-blox.com | 1:581335dbdd60 | 51 | #define USERNAME NULL |
rob.meades@u-blox.com | 1:581335dbdd60 | 52 | #define PASSWORD NULL |
fahim.alavi@u-blox.com | 35:8e65f2bee044 | 53 | #define TCP_SERVER "os.mbed.com" |
rob.meades@u-blox.com | 1:581335dbdd60 | 54 | |
rob.meades@u-blox.com | 1:581335dbdd60 | 55 | // LEDs |
rob.meades@u-blox.com | 1:581335dbdd60 | 56 | DigitalOut ledRed(LED1, 1); |
rob.meades@u-blox.com | 1:581335dbdd60 | 57 | DigitalOut ledGreen(LED2, 1); |
rob.meades@u-blox.com | 1:581335dbdd60 | 58 | DigitalOut ledBlue(LED3, 1); |
rob.meades@u-blox.com | 1:581335dbdd60 | 59 | |
rob.meades@u-blox.com | 1:581335dbdd60 | 60 | // The user button |
rob.meades@u-blox.com | 1:581335dbdd60 | 61 | volatile bool buttonPressed = false; |
rob.meades@u-blox.com | 1:581335dbdd60 | 62 | |
rob.meades@u-blox.com | 1:581335dbdd60 | 63 | static void good() { |
rob.meades@u-blox.com | 1:581335dbdd60 | 64 | ledGreen = 0; |
rob.meades@u-blox.com | 1:581335dbdd60 | 65 | ledBlue = 1; |
rob.meades@u-blox.com | 1:581335dbdd60 | 66 | ledRed = 1; |
rob.meades@u-blox.com | 1:581335dbdd60 | 67 | } |
rob.meades@u-blox.com | 1:581335dbdd60 | 68 | |
rob.meades@u-blox.com | 1:581335dbdd60 | 69 | static void bad() { |
rob.meades@u-blox.com | 1:581335dbdd60 | 70 | ledRed = 0; |
rob.meades@u-blox.com | 1:581335dbdd60 | 71 | ledGreen = 1; |
rob.meades@u-blox.com | 1:581335dbdd60 | 72 | ledBlue = 1; |
rob.meades@u-blox.com | 1:581335dbdd60 | 73 | } |
rob.meades@u-blox.com | 1:581335dbdd60 | 74 | |
rob.meades@u-blox.com | 1:581335dbdd60 | 75 | static void event() { |
rob.meades@u-blox.com | 1:581335dbdd60 | 76 | ledBlue = 0; |
rob.meades@u-blox.com | 1:581335dbdd60 | 77 | ledRed = 1; |
rob.meades@u-blox.com | 1:581335dbdd60 | 78 | ledGreen = 1; |
rob.meades@u-blox.com | 1:581335dbdd60 | 79 | } |
rob.meades@u-blox.com | 1:581335dbdd60 | 80 | |
rob.meades@u-blox.com | 1:581335dbdd60 | 81 | static void pulseEvent() { |
rob.meades@u-blox.com | 1:581335dbdd60 | 82 | event(); |
rob.meades@u-blox.com | 1:581335dbdd60 | 83 | wait_ms(500); |
rob.meades@u-blox.com | 1:581335dbdd60 | 84 | good(); |
rob.meades@u-blox.com | 1:581335dbdd60 | 85 | } |
rob.meades@u-blox.com | 1:581335dbdd60 | 86 | |
rob.meades@u-blox.com | 1:581335dbdd60 | 87 | static void ledOff() { |
rob.meades@u-blox.com | 1:581335dbdd60 | 88 | ledBlue = 1; |
rob.meades@u-blox.com | 1:581335dbdd60 | 89 | ledRed = 1; |
rob.meades@u-blox.com | 1:581335dbdd60 | 90 | ledGreen = 1; |
rob.meades@u-blox.com | 1:581335dbdd60 | 91 | } |
rob.meades@u-blox.com | 1:581335dbdd60 | 92 | |
rob.meades@u-blox.com | 1:581335dbdd60 | 93 | static void printNtpTime(char * buf, int len) |
rob.meades@u-blox.com | 1:581335dbdd60 | 94 | { |
rob.meades@u-blox.com | 1:581335dbdd60 | 95 | time_t timestamp = 0; |
rob.meades@u-blox.com | 1:581335dbdd60 | 96 | struct tm *localTime; |
rob.meades@u-blox.com | 1:581335dbdd60 | 97 | char timeString[25]; |
rob.meades@u-blox.com | 1:581335dbdd60 | 98 | time_t TIME1970 = 2208988800U; |
rob.meades@u-blox.com | 1:581335dbdd60 | 99 | |
rob.meades@u-blox.com | 1:581335dbdd60 | 100 | if (len >= 43) { |
rob.meades@u-blox.com | 1:581335dbdd60 | 101 | timestamp |= ((int) *(buf + 40)) << 24; |
rob.meades@u-blox.com | 1:581335dbdd60 | 102 | timestamp |= ((int) *(buf + 41)) << 16; |
rob.meades@u-blox.com | 1:581335dbdd60 | 103 | timestamp |= ((int) *(buf + 42)) << 8; |
rob.meades@u-blox.com | 1:581335dbdd60 | 104 | timestamp |= ((int) *(buf + 43)); |
rob.meades@u-blox.com | 1:581335dbdd60 | 105 | timestamp -= TIME1970; |
rob.meades@u-blox.com | 1:581335dbdd60 | 106 | localTime = localtime(×tamp); |
rob.meades@u-blox.com | 1:581335dbdd60 | 107 | if (localTime) { |
rob.meades@u-blox.com | 1:581335dbdd60 | 108 | if (strftime(timeString, sizeof(timeString), "%a %b %d %H:%M:%S %Y", localTime) > 0) { |
rob.meades@u-blox.com | 1:581335dbdd60 | 109 | printf("NTP timestamp is %s.\n", timeString); |
rob.meades@u-blox.com | 1:581335dbdd60 | 110 | } |
rob.meades@u-blox.com | 1:581335dbdd60 | 111 | } |
rob.meades@u-blox.com | 1:581335dbdd60 | 112 | } |
rob.meades@u-blox.com | 1:581335dbdd60 | 113 | } |
rob.meades@u-blox.com | 1:581335dbdd60 | 114 | |
rob.meades@u-blox.com | 1:581335dbdd60 | 115 | static void cbButton() |
rob.meades@u-blox.com | 1:581335dbdd60 | 116 | { |
rob.meades@u-blox.com | 1:581335dbdd60 | 117 | buttonPressed = true; |
rob.meades@u-blox.com | 1:581335dbdd60 | 118 | } |
rob.meades@u-blox.com | 1:581335dbdd60 | 119 | |
rob.meades@u-blox.com | 1:581335dbdd60 | 120 | /* This example program for the u-blox C030 and C027 boards instantiates |
wajahat.abbas@u-blox.com | 38:e09e02c31ba1 | 121 | * the UbloxATCellularInterface and uses it to make a simple sockets |
wajahat.abbas@u-blox.com | 38:e09e02c31ba1 | 122 | * connection to a server, using 2.pool.ntp.org for UDP and developer.mbed.org for TCP. |
wajahat.abbas@u-blox.com | 38:e09e02c31ba1 | 123 | * For a more comprehensive example, where higher layer protocols |
wajahat.abbas@u-blox.com | 38:e09e02c31ba1 | 124 | * make use of the same sockets interface, see example-ublox-mbed-client. |
rob.meades@u-blox.com | 1:581335dbdd60 | 125 | * Progress may be monitored with a serial terminal running at 9600 baud. |
rob.meades@u-blox.com | 1:581335dbdd60 | 126 | * The LED on the C030 board will turn green when this program is |
rob.meades@u-blox.com | 1:581335dbdd60 | 127 | * operating correctly, pulse blue when a sockets operation is completed |
rob.meades@u-blox.com | 1:581335dbdd60 | 128 | * and turn red if there is a failure. |
rob.meades@u-blox.com | 1:581335dbdd60 | 129 | */ |
rob.meades@u-blox.com | 1:581335dbdd60 | 130 | |
rob.meades@u-blox.com | 1:581335dbdd60 | 131 | int main() |
rob.meades@u-blox.com | 1:581335dbdd60 | 132 | { |
RobMeades | 10:1f35371d572d | 133 | INTERFACE_CLASS *interface = new INTERFACE_CLASS(); |
RobMeades | 5:bf352de1d3e5 | 134 | // If you need to debug the cellular interface, comment out the |
RobMeades | 5:bf352de1d3e5 | 135 | // instantiation above and uncomment the one below. |
rob.meades@u-blox.com | 31:4a36566b5385 | 136 | // For the N2xx interface, change xxx to MBED_CONF_UBLOX_CELL_BAUD_RATE, |
rob.meades@u-blox.com | 31:4a36566b5385 | 137 | // while for the non-N2xx interface change it to MBED_CONF_UBLOX_CELL_N2XX_BAUD_RATE. |
RobMeades | 8:3e170c40a284 | 138 | // INTERFACE_CLASS *interface = new INTERFACE_CLASS(MDMTXD, MDMRXD, |
rob.meades@u-blox.com | 31:4a36566b5385 | 139 | // xxx, |
RobMeades | 8:3e170c40a284 | 140 | // true); |
rob.meades@u-blox.com | 28:e33af9f1ce3e | 141 | #ifndef TARGET_UBLOX_C030_N211 |
rob.meades@u-blox.com | 1:581335dbdd60 | 142 | TCPSocket sockTcp; |
rob.meades@u-blox.com | 28:e33af9f1ce3e | 143 | #endif |
rob.meades@u-blox.com | 1:581335dbdd60 | 144 | UDPSocket sockUdp; |
rob.meades@u-blox.com | 1:581335dbdd60 | 145 | SocketAddress udpServer; |
rob.meades@u-blox.com | 1:581335dbdd60 | 146 | SocketAddress udpSenderAddress; |
rob.meades@u-blox.com | 1:581335dbdd60 | 147 | SocketAddress tcpServer; |
rob.meades@u-blox.com | 1:581335dbdd60 | 148 | char buf[1024]; |
rob.meades@u-blox.com | 1:581335dbdd60 | 149 | int x; |
RobMeades | 4:3e2b789c3adc | 150 | #ifdef TARGET_UBLOX_C027 |
RobMeades | 4:3e2b789c3adc | 151 | // No user button on C027 |
RobMeades | 4:3e2b789c3adc | 152 | InterruptIn userButton(NC); |
RobMeades | 4:3e2b789c3adc | 153 | #else |
rob.meades@u-blox.com | 1:581335dbdd60 | 154 | InterruptIn userButton(SW0); |
RobMeades | 4:3e2b789c3adc | 155 | #endif |
wajahat.abbas@u-blox.com | 43:0865a8f69787 | 156 | |
rob.meades@u-blox.com | 1:581335dbdd60 | 157 | // Attach a function to the user button |
rob.meades@u-blox.com | 1:581335dbdd60 | 158 | userButton.rise(&cbButton); |
wajahat.abbas@u-blox.com | 43:0865a8f69787 | 159 | |
rob.meades@u-blox.com | 1:581335dbdd60 | 160 | good(); |
rob.meades@u-blox.com | 1:581335dbdd60 | 161 | printf("Starting up, please wait up to 180 seconds for network registration to complete...\n"); |
RobMeades | 33:48101a4c3f14 | 162 | interface->set_credentials(APN, USERNAME, PASSWORD); |
wajahat.abbas@u-blox.com | 43:0865a8f69787 | 163 | #ifdef TARGET_UBLOX_C030_R41XM |
wajahat.abbas@u-blox.com | 43:0865a8f69787 | 164 | interface->init(PIN); |
wajahat.abbas@u-blox.com | 43:0865a8f69787 | 165 | interface->disable_power_saving_mode(); //Please use PSM example to test PSM functionality(https://os.mbed.com/teams/ublox/code/example-ublox-cellular-psm/) |
wajahat.abbas@u-blox.com | 43:0865a8f69787 | 166 | #endif |
RobMeades | 33:48101a4c3f14 | 167 | for (x = 0; interface->connect(PIN) != 0; x++) { |
RobMeades | 33:48101a4c3f14 | 168 | if (x > 0) { |
RobMeades | 33:48101a4c3f14 | 169 | bad(); |
RobMeades | 33:48101a4c3f14 | 170 | printf("Retrying (have you checked that an antenna is plugged in and your APN is correct?)...\n"); |
RobMeades | 33:48101a4c3f14 | 171 | } |
RobMeades | 33:48101a4c3f14 | 172 | } |
RobMeades | 33:48101a4c3f14 | 173 | pulseEvent(); |
wajahat.abbas@u-blox.com | 43:0865a8f69787 | 174 | |
RobMeades | 33:48101a4c3f14 | 175 | printf("Getting the IP address of \"developer.mbed.org\" and \"2.pool.ntp.org\"...\n"); |
RobMeades | 33:48101a4c3f14 | 176 | if ((interface->gethostbyname("2.pool.ntp.org", &udpServer) == 0) && |
fahim.alavi@u-blox.com | 35:8e65f2bee044 | 177 | (interface->gethostbyname(TCP_SERVER, &tcpServer) == 0)) { |
rob.meades@u-blox.com | 1:581335dbdd60 | 178 | pulseEvent(); |
RobMeades | 33:48101a4c3f14 | 179 | |
RobMeades | 33:48101a4c3f14 | 180 | udpServer.set_port(123); |
fahim.alavi@u-blox.com | 35:8e65f2bee044 | 181 | tcpServer.set_port(80); |
RobMeades | 33:48101a4c3f14 | 182 | printf("\"2.pool.ntp.org\" address: %s on port %d.\n", udpServer.get_ip_address(), udpServer.get_port()); |
fahim.alavi@u-blox.com | 35:8e65f2bee044 | 183 | printf("\"os.mbed.com\" address: %s on port %d.\n", tcpServer.get_ip_address(), tcpServer.get_port()); |
rob.meades@u-blox.com | 28:e33af9f1ce3e | 184 | |
RobMeades | 33:48101a4c3f14 | 185 | printf("Performing socket operations in a loop (until the user button is pressed on C030 or forever on C027)...\n"); |
RobMeades | 33:48101a4c3f14 | 186 | while (!buttonPressed) { |
RobMeades | 33:48101a4c3f14 | 187 | // UDP Sockets |
RobMeades | 33:48101a4c3f14 | 188 | printf("=== UDP ===\n"); |
RobMeades | 33:48101a4c3f14 | 189 | printf("Opening a UDP socket...\n"); |
RobMeades | 33:48101a4c3f14 | 190 | if (sockUdp.open(interface) == 0) { |
RobMeades | 33:48101a4c3f14 | 191 | pulseEvent(); |
RobMeades | 33:48101a4c3f14 | 192 | printf("UDP socket open.\n"); |
RobMeades | 33:48101a4c3f14 | 193 | sockUdp.set_timeout(10000); |
RobMeades | 33:48101a4c3f14 | 194 | printf("Sending time request to \"2.pool.ntp.org\" over UDP socket...\n"); |
RobMeades | 33:48101a4c3f14 | 195 | memset (buf, 0, sizeof(buf)); |
RobMeades | 33:48101a4c3f14 | 196 | *buf = '\x1b'; |
RobMeades | 33:48101a4c3f14 | 197 | if (sockUdp.sendto(udpServer, (void *) buf, 48) == 48) { |
rob.meades@u-blox.com | 1:581335dbdd60 | 198 | pulseEvent(); |
RobMeades | 33:48101a4c3f14 | 199 | printf("Socket send completed, waiting for UDP response...\n"); |
RobMeades | 33:48101a4c3f14 | 200 | x = sockUdp.recvfrom(&udpSenderAddress, buf, sizeof (buf)); |
RobMeades | 33:48101a4c3f14 | 201 | if (x > 0) { |
rob.meades@u-blox.com | 1:581335dbdd60 | 202 | pulseEvent(); |
RobMeades | 33:48101a4c3f14 | 203 | printf("Received %d byte response from server %s on UDP socket:\n" |
RobMeades | 33:48101a4c3f14 | 204 | "-------------------------------------------------------\n", |
RobMeades | 33:48101a4c3f14 | 205 | x, udpSenderAddress.get_ip_address()); |
RobMeades | 33:48101a4c3f14 | 206 | printNtpTime(buf, x); |
RobMeades | 33:48101a4c3f14 | 207 | printf("-------------------------------------------------------\n"); |
RobMeades | 33:48101a4c3f14 | 208 | } |
wajahat.abbas@u-blox.com | 43:0865a8f69787 | 209 | } |
RobMeades | 33:48101a4c3f14 | 210 | printf("Closing socket...\n"); |
RobMeades | 33:48101a4c3f14 | 211 | sockUdp.close(); |
RobMeades | 33:48101a4c3f14 | 212 | pulseEvent(); |
RobMeades | 33:48101a4c3f14 | 213 | printf("Socket closed.\n"); |
RobMeades | 33:48101a4c3f14 | 214 | } |
wajahat.abbas@u-blox.com | 43:0865a8f69787 | 215 | |
RobMeades | 33:48101a4c3f14 | 216 | #ifndef TARGET_UBLOX_C030_N211 |
RobMeades | 33:48101a4c3f14 | 217 | // TCP Sockets |
RobMeades | 33:48101a4c3f14 | 218 | printf("=== TCP ===\n"); |
RobMeades | 33:48101a4c3f14 | 219 | printf("Opening a TCP socket...\n"); |
RobMeades | 33:48101a4c3f14 | 220 | if (sockTcp.open(interface) == 0) { |
RobMeades | 33:48101a4c3f14 | 221 | pulseEvent(); |
RobMeades | 33:48101a4c3f14 | 222 | printf("TCP socket open.\n"); |
RobMeades | 33:48101a4c3f14 | 223 | sockTcp.set_timeout(10000); |
RobMeades | 33:48101a4c3f14 | 224 | printf("Connecting socket to %s on port %d...\n", tcpServer.get_ip_address(), tcpServer.get_port()); |
RobMeades | 33:48101a4c3f14 | 225 | if (sockTcp.connect(tcpServer) == 0) { |
RobMeades | 33:48101a4c3f14 | 226 | pulseEvent(); |
fahim.alavi@u-blox.com | 35:8e65f2bee044 | 227 | printf("Connected, sending HTTP GET request to %s over socket...\n", TCP_SERVER); |
RobMeades | 33:48101a4c3f14 | 228 | strcpy (buf, "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n"); |
RobMeades | 33:48101a4c3f14 | 229 | // Note: since this is a short string we can send it in one go as it will |
RobMeades | 33:48101a4c3f14 | 230 | // fit within the default buffer sizes. Normally you should call sock.send() |
RobMeades | 33:48101a4c3f14 | 231 | // in a loop until your entire buffer has been sent. |
RobMeades | 33:48101a4c3f14 | 232 | if (sockTcp.send((void *) buf, strlen(buf)) == (int) strlen(buf)) { |
RobMeades | 33:48101a4c3f14 | 233 | pulseEvent(); |
RobMeades | 33:48101a4c3f14 | 234 | printf("Socket send completed, waiting for response...\n"); |
RobMeades | 33:48101a4c3f14 | 235 | x = sockTcp.recv(buf, sizeof (buf)); |
rob.meades@u-blox.com | 1:581335dbdd60 | 236 | if (x > 0) { |
rob.meades@u-blox.com | 1:581335dbdd60 | 237 | pulseEvent(); |
RobMeades | 33:48101a4c3f14 | 238 | printf("Received %d byte response from server on TCP socket:\n" |
RobMeades | 33:48101a4c3f14 | 239 | "----------------------------------------------------\n%.*s" |
RobMeades | 33:48101a4c3f14 | 240 | "----------------------------------------------------\n", |
RobMeades | 33:48101a4c3f14 | 241 | x, x, buf); |
rob.meades@u-blox.com | 1:581335dbdd60 | 242 | } |
rob.meades@u-blox.com | 1:581335dbdd60 | 243 | } |
rob.meades@u-blox.com | 1:581335dbdd60 | 244 | } |
RobMeades | 33:48101a4c3f14 | 245 | printf("Closing socket...\n"); |
RobMeades | 33:48101a4c3f14 | 246 | sockTcp.close(); |
RobMeades | 33:48101a4c3f14 | 247 | pulseEvent(); |
RobMeades | 33:48101a4c3f14 | 248 | printf("Socket closed.\n"); |
rob.meades@u-blox.com | 1:581335dbdd60 | 249 | } |
RobMeades | 33:48101a4c3f14 | 250 | #endif |
RobMeades | 33:48101a4c3f14 | 251 | wait_ms(5000); |
RobMeades | 33:48101a4c3f14 | 252 | #ifndef TARGET_UBLOX_C027 |
RobMeades | 33:48101a4c3f14 | 253 | printf("[Checking if user button has been pressed]\n"); |
RobMeades | 33:48101a4c3f14 | 254 | #endif |
rob.meades@u-blox.com | 1:581335dbdd60 | 255 | } |
wajahat.abbas@u-blox.com | 43:0865a8f69787 | 256 | |
RobMeades | 33:48101a4c3f14 | 257 | pulseEvent(); |
RobMeades | 33:48101a4c3f14 | 258 | printf("User button was pressed, stopping...\n"); |
RobMeades | 33:48101a4c3f14 | 259 | interface->disconnect(); |
RobMeades | 33:48101a4c3f14 | 260 | ledOff(); |
RobMeades | 33:48101a4c3f14 | 261 | printf("Stopped.\n"); |
rob.meades@u-blox.com | 1:581335dbdd60 | 262 | } else { |
rob.meades@u-blox.com | 1:581335dbdd60 | 263 | bad(); |
RobMeades | 33:48101a4c3f14 | 264 | printf("Unable to get IP address of \"developer.mbed.org\" or \"2.pool.ntp.org\".\n"); |
rob.meades@u-blox.com | 1:581335dbdd60 | 265 | } |
rob.meades@u-blox.com | 1:581335dbdd60 | 266 | } |
RobMeades | 32:bdc45c7052cc | 267 | // End Of File |