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