WiFi DipCortex USB CDC
Dependencies: HTTPClient NTPClient USBDevice WebSocketClient cc3000_hostdriver_mbedsocket mbed
Fork of WiFiDip-UsbKitchenSink by
http://www.soldersplash.co.uk/products/wifi-dipcortex/
Demo shows you how to implement the CC3000 library with the WiFi DipCortex.
The demo shows :
- USB CDC ( Serial ) Menu system allow control of the module and starting each example
- Smart Connect
- Manual connect
- Connection status
- Ping
- TCP Client
- TCP Server
- Web Socket read/write to sockets.mbed.org
- HTTP Client test Post, Put, Delete
- Posting ADC data to Xively every 1 second
- UDP Client
- UDP Server
- NTP Example, contacts time server to get the current time
You will need a Driver for the USB CDC port which can be found here : http://www.soldersplash.co.uk/docs/DipCortex-USB-CDC.zip
Please refer to : http://mbed.org/users/SolderSplashLabs/notebook/dipcortex---getting-started-with-mbed/ as well as the SolderSplash Forum for support http://forum.soldersplash.co.uk/viewforum.php?f=15
tcpTests.cpp@4:ce953c80c5b3, 2014-02-07 (annotated)
- Committer:
- SolderSplashLabs
- Date:
- Fri Feb 07 00:26:14 2014 +0000
- Revision:
- 4:ce953c80c5b3
- Parent:
- 3:15828ac052f1
Added mDNS after smart config
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SolderSplashLabs | 0:0bce3a738bcb | 1 | #include "mbed.h" |
SolderSplashLabs | 0:0bce3a738bcb | 2 | #include "cc3000.h" |
SolderSplashLabs | 0:0bce3a738bcb | 3 | #include "USBSerial.h" |
SolderSplashLabs | 0:0bce3a738bcb | 4 | |
SolderSplashLabs | 0:0bce3a738bcb | 5 | #include "TCPSocketConnection.h" |
SolderSplashLabs | 0:0bce3a738bcb | 6 | #include "TCPSocketServer.h" |
SolderSplashLabs | 0:0bce3a738bcb | 7 | #include "HTTPClient.h" |
SolderSplashLabs | 0:0bce3a738bcb | 8 | #include "Websocket.h" |
SolderSplashLabs | 0:0bce3a738bcb | 9 | |
SolderSplashLabs | 0:0bce3a738bcb | 10 | using namespace mbed_cc3000; |
SolderSplashLabs | 0:0bce3a738bcb | 11 | |
SolderSplashLabs | 0:0bce3a738bcb | 12 | extern cc3000 wifi; |
SolderSplashLabs | 0:0bce3a738bcb | 13 | extern USBSerial pc; |
SolderSplashLabs | 0:0bce3a738bcb | 14 | |
SolderSplashLabs | 0:0bce3a738bcb | 15 | HTTPClient http; |
SolderSplashLabs | 0:0bce3a738bcb | 16 | |
SolderSplashLabs | 3:15828ac052f1 | 17 | // NOTE : Change SolderSplashLabs to anything else for your own channel |
SolderSplashLabs | 0:0bce3a738bcb | 18 | // Goto the viewier page here : http://sockets.mbed.org/SolderSplashLabs/viewer to see the websocket writes |
SolderSplashLabs | 3:15828ac052f1 | 19 | const char WEB_SOCKET_URL[] = {"ws://sockets.mbed.org/ws/SolderSplashLabs/rw"}; |
SolderSplashLabs | 0:0bce3a738bcb | 20 | const char* ECHO_SERVER_ADDRESS = "192.168.0.10"; |
SolderSplashLabs | 0:0bce3a738bcb | 21 | const int ECHO_SERVER_PORT_TCP = 80; |
SolderSplashLabs | 0:0bce3a738bcb | 22 | char hello[] = "Hello World\r\n"; |
SolderSplashLabs | 0:0bce3a738bcb | 23 | |
SolderSplashLabs | 3:15828ac052f1 | 24 | // Put the tmpbuffer into RAM1 |
SolderSplashLabs | 3:15828ac052f1 | 25 | char tmpBuffer[512] __attribute__((section("AHBSRAM0"))); |
SolderSplashLabs | 0:0bce3a738bcb | 26 | |
SolderSplashLabs | 3:15828ac052f1 | 27 | // Add your own feed Id and API key here.. |
SolderSplashLabs | 0:0bce3a738bcb | 28 | char XivelyHostname[] = "api.xively.com"; |
SolderSplashLabs | 1:6f4eaec531c4 | 29 | char XivelyPath[] = "/v2/feeds/YOUR-FEED-ID-HERE"; |
SolderSplashLabs | 1:6f4eaec531c4 | 30 | char XivelyApiKey[] = "YOUR-API-KEY-HERE"; |
SolderSplashLabs | 0:0bce3a738bcb | 31 | |
SolderSplashLabs | 0:0bce3a738bcb | 32 | AnalogIn adc0(P0_11); //P2 |
SolderSplashLabs | 0:0bce3a738bcb | 33 | AnalogIn adc1(P0_12); //P3 |
SolderSplashLabs | 0:0bce3a738bcb | 34 | AnalogIn adc2(P0_13); //P4 |
SolderSplashLabs | 0:0bce3a738bcb | 35 | AnalogIn adc3(P0_14); //P5 |
SolderSplashLabs | 0:0bce3a738bcb | 36 | AnalogIn adc5(P0_16); //P8 |
SolderSplashLabs | 0:0bce3a738bcb | 37 | AnalogIn adc6(P0_22); //P9 |
SolderSplashLabs | 0:0bce3a738bcb | 38 | AnalogIn adc7(P0_23); //P10 |
SolderSplashLabs | 0:0bce3a738bcb | 39 | |
SolderSplashLabs | 0:0bce3a738bcb | 40 | // ------------------------------------------------------------------------------------------------------------ |
SolderSplashLabs | 0:0bce3a738bcb | 41 | /*! |
SolderSplashLabs | 1:6f4eaec531c4 | 42 | @brief Post all analog inputs to xively every second |
SolderSplashLabs | 0:0bce3a738bcb | 43 | */ |
SolderSplashLabs | 0:0bce3a738bcb | 44 | // ------------------------------------------------------------------------------------------------------------ |
SolderSplashLabs | 0:0bce3a738bcb | 45 | void XivelySimpleTest ( void ) |
SolderSplashLabs | 0:0bce3a738bcb | 46 | { |
SolderSplashLabs | 0:0bce3a738bcb | 47 | TCPSocketConnection socket; |
SolderSplashLabs | 0:0bce3a738bcb | 48 | int res = 0; |
SolderSplashLabs | 0:0bce3a738bcb | 49 | uint16_t counter = 0; |
SolderSplashLabs | 0:0bce3a738bcb | 50 | int httpCmdLen = 0; |
SolderSplashLabs | 0:0bce3a738bcb | 51 | |
SolderSplashLabs | 0:0bce3a738bcb | 52 | //if ( wifi.is_connected() ) |
SolderSplashLabs | 0:0bce3a738bcb | 53 | |
SolderSplashLabs | 0:0bce3a738bcb | 54 | while (1) |
SolderSplashLabs | 0:0bce3a738bcb | 55 | { |
SolderSplashLabs | 0:0bce3a738bcb | 56 | if (socket.connect(XivelyHostname, 80) < 0) |
SolderSplashLabs | 0:0bce3a738bcb | 57 | { |
SolderSplashLabs | 0:0bce3a738bcb | 58 | pc.printf("\r\nUnable to connect to (%s) on port (%d)\r\n", XivelyHostname, 80); |
SolderSplashLabs | 0:0bce3a738bcb | 59 | socket.close(); |
SolderSplashLabs | 0:0bce3a738bcb | 60 | } |
SolderSplashLabs | 0:0bce3a738bcb | 61 | else |
SolderSplashLabs | 0:0bce3a738bcb | 62 | { |
SolderSplashLabs | 0:0bce3a738bcb | 63 | // Block for 1 second |
SolderSplashLabs | 0:0bce3a738bcb | 64 | socket.set_blocking( true, 1000 ); |
SolderSplashLabs | 0:0bce3a738bcb | 65 | counter ++; |
SolderSplashLabs | 0:0bce3a738bcb | 66 | |
SolderSplashLabs | 0:0bce3a738bcb | 67 | // Build the header |
SolderSplashLabs | 0:0bce3a738bcb | 68 | httpCmdLen = sprintf(&tmpBuffer[0], "PUT %s.csv HTTP/1.1\r\n", XivelyPath ); |
SolderSplashLabs | 0:0bce3a738bcb | 69 | httpCmdLen += sprintf(&tmpBuffer[httpCmdLen], "Host: %s\r\nUser-Agent: WiFi-DipCortex\r\n", XivelyHostname); |
SolderSplashLabs | 0:0bce3a738bcb | 70 | httpCmdLen += sprintf(&tmpBuffer[httpCmdLen], "X-ApiKey: %s\r\n", XivelyApiKey); |
SolderSplashLabs | 1:6f4eaec531c4 | 71 | // TODO : hardcoded data length of 91, using printf with padding to fix the string length |
SolderSplashLabs | 0:0bce3a738bcb | 72 | httpCmdLen += sprintf(&tmpBuffer[httpCmdLen], "Content-Type: text/csv\r\nContent-Length: 91\r\n"); |
SolderSplashLabs | 0:0bce3a738bcb | 73 | httpCmdLen += sprintf(&tmpBuffer[httpCmdLen], "Connection: close\r\n\r\n"); |
SolderSplashLabs | 0:0bce3a738bcb | 74 | |
SolderSplashLabs | 0:0bce3a738bcb | 75 | // add the data |
SolderSplashLabs | 0:0bce3a738bcb | 76 | httpCmdLen += sprintf(&tmpBuffer[httpCmdLen], "ADC0, %05d\r\nADC1, %05d\r\nADC2, %05d\r\nADC3, %05d\r\nADC5, %05d\r\nADC6, %05d\r\nADC7, %05d\r\n\0", |
SolderSplashLabs | 0:0bce3a738bcb | 77 | adc0.read_u16(), adc1.read_u16(), adc2.read_u16(), adc3.read_u16(), adc5.read_u16(), adc6.read_u16(), adc7.read_u16()); |
SolderSplashLabs | 0:0bce3a738bcb | 78 | |
SolderSplashLabs | 0:0bce3a738bcb | 79 | pc.printf("Data to be sent : \r\n %s", tmpBuffer); |
SolderSplashLabs | 0:0bce3a738bcb | 80 | |
SolderSplashLabs | 0:0bce3a738bcb | 81 | pc.printf("Posting ADC's to Xively \r\n"); |
SolderSplashLabs | 0:0bce3a738bcb | 82 | res = socket.send_all(tmpBuffer, httpCmdLen); |
SolderSplashLabs | 0:0bce3a738bcb | 83 | |
SolderSplashLabs | 0:0bce3a738bcb | 84 | if ( res > 0 ) |
SolderSplashLabs | 0:0bce3a738bcb | 85 | { |
SolderSplashLabs | 0:0bce3a738bcb | 86 | pc.printf("%05d : Data Sent \r\n", counter); |
SolderSplashLabs | 0:0bce3a738bcb | 87 | } |
SolderSplashLabs | 0:0bce3a738bcb | 88 | else |
SolderSplashLabs | 0:0bce3a738bcb | 89 | { |
SolderSplashLabs | 0:0bce3a738bcb | 90 | pc.printf("Failed to send\r\n"); |
SolderSplashLabs | 0:0bce3a738bcb | 91 | break; |
SolderSplashLabs | 0:0bce3a738bcb | 92 | } |
SolderSplashLabs | 0:0bce3a738bcb | 93 | |
SolderSplashLabs | 0:0bce3a738bcb | 94 | res = socket.receive(tmpBuffer, 512); |
SolderSplashLabs | 0:0bce3a738bcb | 95 | |
SolderSplashLabs | 0:0bce3a738bcb | 96 | if ( res > 0 ) |
SolderSplashLabs | 0:0bce3a738bcb | 97 | { |
SolderSplashLabs | 0:0bce3a738bcb | 98 | pc.printf("TCP Socket Recv'd : \r\n %s", tmpBuffer); |
SolderSplashLabs | 0:0bce3a738bcb | 99 | tmpBuffer[res] = '\0'; |
SolderSplashLabs | 0:0bce3a738bcb | 100 | } |
SolderSplashLabs | 0:0bce3a738bcb | 101 | else |
SolderSplashLabs | 0:0bce3a738bcb | 102 | { |
SolderSplashLabs | 0:0bce3a738bcb | 103 | tmpBuffer[0] = '\0'; |
SolderSplashLabs | 0:0bce3a738bcb | 104 | pc.printf("TCP : Failed to Recv\r\n"); |
SolderSplashLabs | 0:0bce3a738bcb | 105 | break; |
SolderSplashLabs | 0:0bce3a738bcb | 106 | } |
SolderSplashLabs | 0:0bce3a738bcb | 107 | |
SolderSplashLabs | 0:0bce3a738bcb | 108 | socket.close(); |
SolderSplashLabs | 0:0bce3a738bcb | 109 | } |
SolderSplashLabs | 0:0bce3a738bcb | 110 | |
SolderSplashLabs | 0:0bce3a738bcb | 111 | wait_ms(1000); |
SolderSplashLabs | 0:0bce3a738bcb | 112 | |
SolderSplashLabs | 0:0bce3a738bcb | 113 | if ( pc.readable() ) |
SolderSplashLabs | 0:0bce3a738bcb | 114 | { |
SolderSplashLabs | 0:0bce3a738bcb | 115 | pc.printf("Ending Xively Post \r\n"); |
SolderSplashLabs | 0:0bce3a738bcb | 116 | pc.getc(); |
SolderSplashLabs | 0:0bce3a738bcb | 117 | break; |
SolderSplashLabs | 0:0bce3a738bcb | 118 | } |
SolderSplashLabs | 0:0bce3a738bcb | 119 | } |
SolderSplashLabs | 0:0bce3a738bcb | 120 | } |
SolderSplashLabs | 0:0bce3a738bcb | 121 | |
SolderSplashLabs | 0:0bce3a738bcb | 122 | // ------------------------------------------------------------------------------------------------------------ |
SolderSplashLabs | 0:0bce3a738bcb | 123 | /*! |
SolderSplashLabs | 0:0bce3a738bcb | 124 | @brief Exercise the HTTP Client library |
SolderSplashLabs | 0:0bce3a738bcb | 125 | */ |
SolderSplashLabs | 0:0bce3a738bcb | 126 | // ------------------------------------------------------------------------------------------------------------ |
SolderSplashLabs | 0:0bce3a738bcb | 127 | void HttpClientTest ( void ) |
SolderSplashLabs | 0:0bce3a738bcb | 128 | { |
SolderSplashLabs | 0:0bce3a738bcb | 129 | |
SolderSplashLabs | 0:0bce3a738bcb | 130 | //GET data |
SolderSplashLabs | 0:0bce3a738bcb | 131 | pc.printf("\r\nTrying to fetch page... \r\n"); |
SolderSplashLabs | 0:0bce3a738bcb | 132 | int ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", tmpBuffer, 128); |
SolderSplashLabs | 0:0bce3a738bcb | 133 | if (!ret) |
SolderSplashLabs | 0:0bce3a738bcb | 134 | { |
SolderSplashLabs | 0:0bce3a738bcb | 135 | pc.printf("Page fetched successfully - read %d characters \r\n", strlen(tmpBuffer)); |
SolderSplashLabs | 0:0bce3a738bcb | 136 | pc.printf("Result: %s \r\n", tmpBuffer); |
SolderSplashLabs | 0:0bce3a738bcb | 137 | } |
SolderSplashLabs | 0:0bce3a738bcb | 138 | else |
SolderSplashLabs | 0:0bce3a738bcb | 139 | { |
SolderSplashLabs | 0:0bce3a738bcb | 140 | pc.printf("Error - ret = %d - HTTP return code = %d \r\n", tmpBuffer, http.getHTTPResponseCode()); |
SolderSplashLabs | 0:0bce3a738bcb | 141 | } |
SolderSplashLabs | 0:0bce3a738bcb | 142 | |
SolderSplashLabs | 0:0bce3a738bcb | 143 | //POST data |
SolderSplashLabs | 0:0bce3a738bcb | 144 | HTTPMap map; |
SolderSplashLabs | 0:0bce3a738bcb | 145 | HTTPText inText(tmpBuffer, 512); |
SolderSplashLabs | 0:0bce3a738bcb | 146 | map.put("Hello", "World"); |
SolderSplashLabs | 0:0bce3a738bcb | 147 | map.put("test", "1234"); |
SolderSplashLabs | 0:0bce3a738bcb | 148 | pc.printf(" \r\nTrying to post data... \r\n"); |
SolderSplashLabs | 0:0bce3a738bcb | 149 | ret = http.post("http://httpbin.org/post", map, &inText); |
SolderSplashLabs | 0:0bce3a738bcb | 150 | if (!ret) |
SolderSplashLabs | 0:0bce3a738bcb | 151 | { |
SolderSplashLabs | 0:0bce3a738bcb | 152 | pc.printf("Executed POST successfully - read %d characters \r\n", strlen(tmpBuffer)); |
SolderSplashLabs | 0:0bce3a738bcb | 153 | pc.printf("Result: %s \r\n", tmpBuffer); |
SolderSplashLabs | 0:0bce3a738bcb | 154 | } |
SolderSplashLabs | 0:0bce3a738bcb | 155 | else |
SolderSplashLabs | 0:0bce3a738bcb | 156 | { |
SolderSplashLabs | 0:0bce3a738bcb | 157 | pc.printf("Error - ret = %d - HTTP return code = %d \r\n", ret, http.getHTTPResponseCode()); |
SolderSplashLabs | 0:0bce3a738bcb | 158 | } |
SolderSplashLabs | 0:0bce3a738bcb | 159 | |
SolderSplashLabs | 0:0bce3a738bcb | 160 | //PUT data |
SolderSplashLabs | 0:0bce3a738bcb | 161 | strcpy(tmpBuffer, "This is a PUT test!"); |
SolderSplashLabs | 0:0bce3a738bcb | 162 | HTTPText outText(tmpBuffer); |
SolderSplashLabs | 0:0bce3a738bcb | 163 | //HTTPText inText(tmpBuffer, 512); |
SolderSplashLabs | 0:0bce3a738bcb | 164 | pc.printf(" \r\nTrying to put resource... \r\n"); |
SolderSplashLabs | 0:0bce3a738bcb | 165 | ret = http.put("http://httpbin.org/put", outText, &inText); |
SolderSplashLabs | 0:0bce3a738bcb | 166 | if (!ret) |
SolderSplashLabs | 0:0bce3a738bcb | 167 | { |
SolderSplashLabs | 0:0bce3a738bcb | 168 | pc.printf("Executed PUT successfully - read %d characters \r\n", strlen(tmpBuffer)); |
SolderSplashLabs | 0:0bce3a738bcb | 169 | pc.printf("Result: %s \r\n", tmpBuffer); |
SolderSplashLabs | 0:0bce3a738bcb | 170 | } |
SolderSplashLabs | 0:0bce3a738bcb | 171 | else |
SolderSplashLabs | 0:0bce3a738bcb | 172 | { |
SolderSplashLabs | 0:0bce3a738bcb | 173 | pc.printf("Error - ret = %d - HTTP return code = %d \r\n", ret, http.getHTTPResponseCode()); |
SolderSplashLabs | 0:0bce3a738bcb | 174 | } |
SolderSplashLabs | 0:0bce3a738bcb | 175 | |
SolderSplashLabs | 0:0bce3a738bcb | 176 | //DELETE data |
SolderSplashLabs | 0:0bce3a738bcb | 177 | //HTTPText inText(tmpBuffer, 512); |
SolderSplashLabs | 0:0bce3a738bcb | 178 | pc.printf(" \r\nTrying to delete resource... \r\n"); |
SolderSplashLabs | 0:0bce3a738bcb | 179 | ret = http.del("http://httpbin.org/delete", &inText); |
SolderSplashLabs | 0:0bce3a738bcb | 180 | if (!ret) |
SolderSplashLabs | 0:0bce3a738bcb | 181 | { |
SolderSplashLabs | 0:0bce3a738bcb | 182 | pc.printf("Executed DELETE successfully - read %d characters \r\n", strlen(tmpBuffer)); |
SolderSplashLabs | 0:0bce3a738bcb | 183 | pc.printf("Result: %s \r\n", tmpBuffer); |
SolderSplashLabs | 0:0bce3a738bcb | 184 | } |
SolderSplashLabs | 0:0bce3a738bcb | 185 | else |
SolderSplashLabs | 0:0bce3a738bcb | 186 | { |
SolderSplashLabs | 0:0bce3a738bcb | 187 | pc.printf("Error - ret = %d - HTTP return code = %d \r\n", ret, http.getHTTPResponseCode()); |
SolderSplashLabs | 0:0bce3a738bcb | 188 | } |
SolderSplashLabs | 0:0bce3a738bcb | 189 | } |
SolderSplashLabs | 0:0bce3a738bcb | 190 | |
SolderSplashLabs | 0:0bce3a738bcb | 191 | // ------------------------------------------------------------------------------------------------------------ |
SolderSplashLabs | 0:0bce3a738bcb | 192 | /*! |
SolderSplashLabs | 0:0bce3a738bcb | 193 | @brief Open a WebSocket, send a string |
SolderSplashLabs | 0:0bce3a738bcb | 194 | */ |
SolderSplashLabs | 0:0bce3a738bcb | 195 | // ------------------------------------------------------------------------------------------------------------ |
SolderSplashLabs | 4:ce953c80c5b3 | 196 | void WebSocketAdcStream ( void ) |
SolderSplashLabs | 4:ce953c80c5b3 | 197 | { |
SolderSplashLabs | 4:ce953c80c5b3 | 198 | |
SolderSplashLabs | 4:ce953c80c5b3 | 199 | int res = 0; |
SolderSplashLabs | 4:ce953c80c5b3 | 200 | uint16_t counter = 0; |
SolderSplashLabs | 4:ce953c80c5b3 | 201 | uint16_t reconnects = 0; |
SolderSplashLabs | 4:ce953c80c5b3 | 202 | uint8_t myMAC[8]; |
SolderSplashLabs | 4:ce953c80c5b3 | 203 | |
SolderSplashLabs | 4:ce953c80c5b3 | 204 | wifi.get_mac_address(myMAC); |
SolderSplashLabs | 4:ce953c80c5b3 | 205 | |
SolderSplashLabs | 4:ce953c80c5b3 | 206 | Websocket ws((char *)WEB_SOCKET_URL); |
SolderSplashLabs | 4:ce953c80c5b3 | 207 | if ( ws.connect() ) |
SolderSplashLabs | 4:ce953c80c5b3 | 208 | { |
SolderSplashLabs | 4:ce953c80c5b3 | 209 | pc.printf("Connected to websocket server.\r\n"); |
SolderSplashLabs | 4:ce953c80c5b3 | 210 | |
SolderSplashLabs | 4:ce953c80c5b3 | 211 | pc.printf("\r\n!! Press any key to stop sending !!\r\n\r\n"); |
SolderSplashLabs | 4:ce953c80c5b3 | 212 | while (1) |
SolderSplashLabs | 4:ce953c80c5b3 | 213 | { |
SolderSplashLabs | 4:ce953c80c5b3 | 214 | counter ++; |
SolderSplashLabs | 4:ce953c80c5b3 | 215 | sprintf(tmpBuffer, "{\"id\":\"wifly_acc\",\"ax\":\"%d\",\"ay\":\"%d\",\"az\":\"%d\"}", adc0.read_u16(), adc1.read_u16(), adc2.read_u16()); |
SolderSplashLabs | 4:ce953c80c5b3 | 216 | |
SolderSplashLabs | 4:ce953c80c5b3 | 217 | if ( wifi.is_connected() ) |
SolderSplashLabs | 4:ce953c80c5b3 | 218 | { |
SolderSplashLabs | 4:ce953c80c5b3 | 219 | res = ws.send(tmpBuffer); |
SolderSplashLabs | 4:ce953c80c5b3 | 220 | pc.printf("Reconnects : %05d, Messages Sent : %05d, Websocket send returned : %d.\r\n", reconnects, counter, res); |
SolderSplashLabs | 4:ce953c80c5b3 | 221 | |
SolderSplashLabs | 4:ce953c80c5b3 | 222 | if ( -1 == res ) |
SolderSplashLabs | 4:ce953c80c5b3 | 223 | { |
SolderSplashLabs | 4:ce953c80c5b3 | 224 | pc.printf("Websocket Failure, reconnecting .... \r\n"); |
SolderSplashLabs | 4:ce953c80c5b3 | 225 | ws.close(); |
SolderSplashLabs | 4:ce953c80c5b3 | 226 | if ( ws.connect() ) |
SolderSplashLabs | 4:ce953c80c5b3 | 227 | { |
SolderSplashLabs | 4:ce953c80c5b3 | 228 | // Reconnected |
SolderSplashLabs | 4:ce953c80c5b3 | 229 | reconnects ++; |
SolderSplashLabs | 4:ce953c80c5b3 | 230 | } |
SolderSplashLabs | 4:ce953c80c5b3 | 231 | else |
SolderSplashLabs | 4:ce953c80c5b3 | 232 | { |
SolderSplashLabs | 4:ce953c80c5b3 | 233 | // Failure! |
SolderSplashLabs | 4:ce953c80c5b3 | 234 | break; |
SolderSplashLabs | 4:ce953c80c5b3 | 235 | } |
SolderSplashLabs | 4:ce953c80c5b3 | 236 | } |
SolderSplashLabs | 4:ce953c80c5b3 | 237 | |
SolderSplashLabs | 4:ce953c80c5b3 | 238 | wait_ms(1000); |
SolderSplashLabs | 4:ce953c80c5b3 | 239 | } |
SolderSplashLabs | 4:ce953c80c5b3 | 240 | else |
SolderSplashLabs | 4:ce953c80c5b3 | 241 | { |
SolderSplashLabs | 4:ce953c80c5b3 | 242 | pc.printf("WiFi Connection Lost .... \r\n"); |
SolderSplashLabs | 4:ce953c80c5b3 | 243 | } |
SolderSplashLabs | 4:ce953c80c5b3 | 244 | |
SolderSplashLabs | 4:ce953c80c5b3 | 245 | if ( pc.readable() ) |
SolderSplashLabs | 4:ce953c80c5b3 | 246 | { |
SolderSplashLabs | 4:ce953c80c5b3 | 247 | pc.printf("Closing Socket \r\n"); |
SolderSplashLabs | 4:ce953c80c5b3 | 248 | pc.getc(); |
SolderSplashLabs | 4:ce953c80c5b3 | 249 | break; |
SolderSplashLabs | 4:ce953c80c5b3 | 250 | } |
SolderSplashLabs | 4:ce953c80c5b3 | 251 | } |
SolderSplashLabs | 4:ce953c80c5b3 | 252 | |
SolderSplashLabs | 4:ce953c80c5b3 | 253 | ws.close(); |
SolderSplashLabs | 4:ce953c80c5b3 | 254 | pc.printf("Websocket Closed \r\n"); |
SolderSplashLabs | 4:ce953c80c5b3 | 255 | } |
SolderSplashLabs | 4:ce953c80c5b3 | 256 | else |
SolderSplashLabs | 4:ce953c80c5b3 | 257 | { |
SolderSplashLabs | 4:ce953c80c5b3 | 258 | pc.printf("Websocket connection failed\r\n"); |
SolderSplashLabs | 4:ce953c80c5b3 | 259 | } |
SolderSplashLabs | 4:ce953c80c5b3 | 260 | } |
SolderSplashLabs | 4:ce953c80c5b3 | 261 | |
SolderSplashLabs | 4:ce953c80c5b3 | 262 | // ------------------------------------------------------------------------------------------------------------ |
SolderSplashLabs | 4:ce953c80c5b3 | 263 | /*! |
SolderSplashLabs | 4:ce953c80c5b3 | 264 | @brief Open a WebSocket, send a string |
SolderSplashLabs | 4:ce953c80c5b3 | 265 | */ |
SolderSplashLabs | 4:ce953c80c5b3 | 266 | // ------------------------------------------------------------------------------------------------------------ |
SolderSplashLabs | 0:0bce3a738bcb | 267 | void WebSocketTest ( void ) |
SolderSplashLabs | 0:0bce3a738bcb | 268 | { |
SolderSplashLabs | 0:0bce3a738bcb | 269 | |
SolderSplashLabs | 0:0bce3a738bcb | 270 | int res = 0; |
SolderSplashLabs | 0:0bce3a738bcb | 271 | uint16_t counter = 0; |
SolderSplashLabs | 0:0bce3a738bcb | 272 | uint16_t reconnects = 0; |
SolderSplashLabs | 0:0bce3a738bcb | 273 | uint8_t myMAC[8]; |
SolderSplashLabs | 0:0bce3a738bcb | 274 | |
SolderSplashLabs | 0:0bce3a738bcb | 275 | wifi.get_mac_address(myMAC); |
SolderSplashLabs | 0:0bce3a738bcb | 276 | |
SolderSplashLabs | 0:0bce3a738bcb | 277 | Websocket ws((char *)WEB_SOCKET_URL); |
SolderSplashLabs | 0:0bce3a738bcb | 278 | if ( ws.connect() ) |
SolderSplashLabs | 0:0bce3a738bcb | 279 | { |
SolderSplashLabs | 0:0bce3a738bcb | 280 | pc.printf("Connected to websocket server.\r\n"); |
SolderSplashLabs | 0:0bce3a738bcb | 281 | |
SolderSplashLabs | 0:0bce3a738bcb | 282 | pc.printf("\r\n!! Press any key to stop sending !!\r\n\r\n"); |
SolderSplashLabs | 0:0bce3a738bcb | 283 | while (1) |
SolderSplashLabs | 0:0bce3a738bcb | 284 | { |
SolderSplashLabs | 0:0bce3a738bcb | 285 | counter ++; |
SolderSplashLabs | 0:0bce3a738bcb | 286 | sprintf(tmpBuffer, "WiFi DipCortex / CC3000 - %05d - %02x:%02x:%02x:%02x:%02x:%02x\r\n", counter, myMAC[0], myMAC[1], myMAC[2], myMAC[3], myMAC[4], myMAC[5]); |
SolderSplashLabs | 0:0bce3a738bcb | 287 | |
SolderSplashLabs | 0:0bce3a738bcb | 288 | if ( wifi.is_connected() ) |
SolderSplashLabs | 0:0bce3a738bcb | 289 | { |
SolderSplashLabs | 0:0bce3a738bcb | 290 | res = ws.send(tmpBuffer); |
SolderSplashLabs | 0:0bce3a738bcb | 291 | pc.printf("Reconnects : %05d, Messages Sent : %05d, Websocket send returned : %d.\r\n", reconnects, counter, res); |
SolderSplashLabs | 0:0bce3a738bcb | 292 | |
SolderSplashLabs | 0:0bce3a738bcb | 293 | if ( -1 == res ) |
SolderSplashLabs | 0:0bce3a738bcb | 294 | { |
SolderSplashLabs | 0:0bce3a738bcb | 295 | pc.printf("Websocket Failure, reconnecting .... \r\n"); |
SolderSplashLabs | 0:0bce3a738bcb | 296 | ws.close(); |
SolderSplashLabs | 0:0bce3a738bcb | 297 | if ( ws.connect() ) |
SolderSplashLabs | 0:0bce3a738bcb | 298 | { |
SolderSplashLabs | 0:0bce3a738bcb | 299 | // Reconnected |
SolderSplashLabs | 0:0bce3a738bcb | 300 | reconnects ++; |
SolderSplashLabs | 0:0bce3a738bcb | 301 | } |
SolderSplashLabs | 0:0bce3a738bcb | 302 | else |
SolderSplashLabs | 0:0bce3a738bcb | 303 | { |
SolderSplashLabs | 0:0bce3a738bcb | 304 | // Failure! |
SolderSplashLabs | 0:0bce3a738bcb | 305 | break; |
SolderSplashLabs | 0:0bce3a738bcb | 306 | } |
SolderSplashLabs | 0:0bce3a738bcb | 307 | } |
SolderSplashLabs | 0:0bce3a738bcb | 308 | |
SolderSplashLabs | 0:0bce3a738bcb | 309 | wait_ms(1000); |
SolderSplashLabs | 0:0bce3a738bcb | 310 | } |
SolderSplashLabs | 0:0bce3a738bcb | 311 | else |
SolderSplashLabs | 0:0bce3a738bcb | 312 | { |
SolderSplashLabs | 0:0bce3a738bcb | 313 | pc.printf("WiFi Connection Lost .... \r\n"); |
SolderSplashLabs | 0:0bce3a738bcb | 314 | } |
SolderSplashLabs | 0:0bce3a738bcb | 315 | |
SolderSplashLabs | 0:0bce3a738bcb | 316 | if ( pc.readable() ) |
SolderSplashLabs | 0:0bce3a738bcb | 317 | { |
SolderSplashLabs | 0:0bce3a738bcb | 318 | pc.printf("Closing Socket \r\n"); |
SolderSplashLabs | 0:0bce3a738bcb | 319 | pc.getc(); |
SolderSplashLabs | 0:0bce3a738bcb | 320 | break; |
SolderSplashLabs | 0:0bce3a738bcb | 321 | } |
SolderSplashLabs | 0:0bce3a738bcb | 322 | } |
SolderSplashLabs | 0:0bce3a738bcb | 323 | |
SolderSplashLabs | 0:0bce3a738bcb | 324 | ws.close(); |
SolderSplashLabs | 0:0bce3a738bcb | 325 | pc.printf("Websocket Closed \r\n"); |
SolderSplashLabs | 0:0bce3a738bcb | 326 | } |
SolderSplashLabs | 3:15828ac052f1 | 327 | else |
SolderSplashLabs | 3:15828ac052f1 | 328 | { |
SolderSplashLabs | 3:15828ac052f1 | 329 | pc.printf("Websocket connection failed\r\n"); |
SolderSplashLabs | 3:15828ac052f1 | 330 | } |
SolderSplashLabs | 0:0bce3a738bcb | 331 | } |
SolderSplashLabs | 0:0bce3a738bcb | 332 | |
SolderSplashLabs | 0:0bce3a738bcb | 333 | // ------------------------------------------------------------------------------------------------------------ |
SolderSplashLabs | 0:0bce3a738bcb | 334 | /*! |
SolderSplashLabs | 2:8f5776c287c5 | 335 | @brief Open a WebSocket, send a string |
SolderSplashLabs | 2:8f5776c287c5 | 336 | */ |
SolderSplashLabs | 2:8f5776c287c5 | 337 | // ------------------------------------------------------------------------------------------------------------ |
SolderSplashLabs | 2:8f5776c287c5 | 338 | void WebSocketReadTest ( void ) |
SolderSplashLabs | 2:8f5776c287c5 | 339 | { |
SolderSplashLabs | 2:8f5776c287c5 | 340 | |
SolderSplashLabs | 2:8f5776c287c5 | 341 | int res = 0; |
SolderSplashLabs | 2:8f5776c287c5 | 342 | uint16_t counter = 0; |
SolderSplashLabs | 2:8f5776c287c5 | 343 | uint16_t reconnects = 0; |
SolderSplashLabs | 2:8f5776c287c5 | 344 | uint8_t myMAC[8]; |
SolderSplashLabs | 2:8f5776c287c5 | 345 | |
SolderSplashLabs | 2:8f5776c287c5 | 346 | wifi.get_mac_address(myMAC); |
SolderSplashLabs | 2:8f5776c287c5 | 347 | |
SolderSplashLabs | 2:8f5776c287c5 | 348 | Websocket ws((char *)WEB_SOCKET_URL); |
SolderSplashLabs | 2:8f5776c287c5 | 349 | if ( ws.connect() ) |
SolderSplashLabs | 2:8f5776c287c5 | 350 | { |
SolderSplashLabs | 2:8f5776c287c5 | 351 | pc.printf("Connected to websocket server.\r\n"); |
SolderSplashLabs | 2:8f5776c287c5 | 352 | |
SolderSplashLabs | 2:8f5776c287c5 | 353 | pc.printf("\r\n!! Press any key to stop receiving !!\r\n\r\n"); |
SolderSplashLabs | 2:8f5776c287c5 | 354 | while (1) |
SolderSplashLabs | 2:8f5776c287c5 | 355 | { |
SolderSplashLabs | 2:8f5776c287c5 | 356 | counter ++; |
SolderSplashLabs | 2:8f5776c287c5 | 357 | //sprintf(tmpBuffer, "WiFi DipCortex / CC3000 - %05d - %02x:%02x:%02x:%02x:%02x:%02x\r\n", counter, myMAC[0], myMAC[1], myMAC[2], myMAC[3], myMAC[4], myMAC[5]); |
SolderSplashLabs | 2:8f5776c287c5 | 358 | |
SolderSplashLabs | 2:8f5776c287c5 | 359 | if ( wifi.is_connected() ) |
SolderSplashLabs | 2:8f5776c287c5 | 360 | { |
SolderSplashLabs | 2:8f5776c287c5 | 361 | res = ws.read(tmpBuffer); |
SolderSplashLabs | 2:8f5776c287c5 | 362 | |
SolderSplashLabs | 2:8f5776c287c5 | 363 | if ( res ) |
SolderSplashLabs | 2:8f5776c287c5 | 364 | { |
SolderSplashLabs | 2:8f5776c287c5 | 365 | pc.printf("Websocket Received : %s (Reconnects : %05d, Messages Recv : %05d)\r\n", tmpBuffer, reconnects, counter); |
SolderSplashLabs | 2:8f5776c287c5 | 366 | } |
SolderSplashLabs | 2:8f5776c287c5 | 367 | |
SolderSplashLabs | 2:8f5776c287c5 | 368 | if ( ws.is_connected() ) |
SolderSplashLabs | 2:8f5776c287c5 | 369 | { |
SolderSplashLabs | 2:8f5776c287c5 | 370 | |
SolderSplashLabs | 2:8f5776c287c5 | 371 | } |
SolderSplashLabs | 2:8f5776c287c5 | 372 | else |
SolderSplashLabs | 2:8f5776c287c5 | 373 | { |
SolderSplashLabs | 2:8f5776c287c5 | 374 | pc.printf("Websocket Closed, reconnecting .... \r\n"); |
SolderSplashLabs | 2:8f5776c287c5 | 375 | ws.close(); |
SolderSplashLabs | 2:8f5776c287c5 | 376 | if ( ws.connect() ) |
SolderSplashLabs | 2:8f5776c287c5 | 377 | { |
SolderSplashLabs | 2:8f5776c287c5 | 378 | // Reconnected |
SolderSplashLabs | 2:8f5776c287c5 | 379 | reconnects ++; |
SolderSplashLabs | 2:8f5776c287c5 | 380 | } |
SolderSplashLabs | 2:8f5776c287c5 | 381 | else |
SolderSplashLabs | 2:8f5776c287c5 | 382 | { |
SolderSplashLabs | 2:8f5776c287c5 | 383 | // Failure! |
SolderSplashLabs | 2:8f5776c287c5 | 384 | break; |
SolderSplashLabs | 2:8f5776c287c5 | 385 | } |
SolderSplashLabs | 2:8f5776c287c5 | 386 | } |
SolderSplashLabs | 2:8f5776c287c5 | 387 | } |
SolderSplashLabs | 2:8f5776c287c5 | 388 | else |
SolderSplashLabs | 2:8f5776c287c5 | 389 | { |
SolderSplashLabs | 2:8f5776c287c5 | 390 | pc.printf("WiFi Connection Lost .... \r\n"); |
SolderSplashLabs | 2:8f5776c287c5 | 391 | } |
SolderSplashLabs | 2:8f5776c287c5 | 392 | |
SolderSplashLabs | 2:8f5776c287c5 | 393 | if ( pc.readable() ) |
SolderSplashLabs | 2:8f5776c287c5 | 394 | { |
SolderSplashLabs | 2:8f5776c287c5 | 395 | pc.printf("Closing Socket \r\n"); |
SolderSplashLabs | 2:8f5776c287c5 | 396 | pc.getc(); |
SolderSplashLabs | 2:8f5776c287c5 | 397 | break; |
SolderSplashLabs | 2:8f5776c287c5 | 398 | } |
SolderSplashLabs | 2:8f5776c287c5 | 399 | } |
SolderSplashLabs | 2:8f5776c287c5 | 400 | |
SolderSplashLabs | 2:8f5776c287c5 | 401 | ws.close(); |
SolderSplashLabs | 2:8f5776c287c5 | 402 | pc.printf("Websocket Closed \r\n"); |
SolderSplashLabs | 2:8f5776c287c5 | 403 | } |
SolderSplashLabs | 3:15828ac052f1 | 404 | else |
SolderSplashLabs | 3:15828ac052f1 | 405 | { |
SolderSplashLabs | 3:15828ac052f1 | 406 | pc.printf("Websocket connection failed\r\n"); |
SolderSplashLabs | 3:15828ac052f1 | 407 | } |
SolderSplashLabs | 2:8f5776c287c5 | 408 | } |
SolderSplashLabs | 2:8f5776c287c5 | 409 | |
SolderSplashLabs | 2:8f5776c287c5 | 410 | // ------------------------------------------------------------------------------------------------------------ |
SolderSplashLabs | 2:8f5776c287c5 | 411 | /*! |
SolderSplashLabs | 0:0bce3a738bcb | 412 | @brief Open a TCP port send a string and wait for a reply |
SolderSplashLabs | 0:0bce3a738bcb | 413 | */ |
SolderSplashLabs | 0:0bce3a738bcb | 414 | // ------------------------------------------------------------------------------------------------------------ |
SolderSplashLabs | 0:0bce3a738bcb | 415 | void TcpClientTest ( void ) |
SolderSplashLabs | 0:0bce3a738bcb | 416 | { |
SolderSplashLabs | 0:0bce3a738bcb | 417 | uint16_t counter = 0; |
SolderSplashLabs | 0:0bce3a738bcb | 418 | TCPSocketConnection socket; |
SolderSplashLabs | 0:0bce3a738bcb | 419 | int n = 0; |
SolderSplashLabs | 0:0bce3a738bcb | 420 | |
SolderSplashLabs | 0:0bce3a738bcb | 421 | if (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT_TCP) < 0) |
SolderSplashLabs | 0:0bce3a738bcb | 422 | { |
SolderSplashLabs | 0:0bce3a738bcb | 423 | pc.printf("\r\nUnable to connect to (%s) on port (%d)\r\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT_TCP); |
SolderSplashLabs | 0:0bce3a738bcb | 424 | } |
SolderSplashLabs | 0:0bce3a738bcb | 425 | else |
SolderSplashLabs | 0:0bce3a738bcb | 426 | { |
SolderSplashLabs | 0:0bce3a738bcb | 427 | // Block for 1 second |
SolderSplashLabs | 0:0bce3a738bcb | 428 | socket.set_blocking( true, 1000 ); |
SolderSplashLabs | 0:0bce3a738bcb | 429 | |
SolderSplashLabs | 0:0bce3a738bcb | 430 | pc.printf("\r\n!! Press any key to stop sending !!\r\n\r\n"); |
SolderSplashLabs | 0:0bce3a738bcb | 431 | while (1) |
SolderSplashLabs | 0:0bce3a738bcb | 432 | { |
SolderSplashLabs | 0:0bce3a738bcb | 433 | counter ++; |
SolderSplashLabs | 0:0bce3a738bcb | 434 | |
SolderSplashLabs | 0:0bce3a738bcb | 435 | n = socket.send_all(hello, sizeof(hello) - 1); |
SolderSplashLabs | 0:0bce3a738bcb | 436 | |
SolderSplashLabs | 0:0bce3a738bcb | 437 | if ( n > 0 ) |
SolderSplashLabs | 0:0bce3a738bcb | 438 | { |
SolderSplashLabs | 0:0bce3a738bcb | 439 | pc.printf("%05d : TCP Socket Sent : Hello World\r\n", counter); |
SolderSplashLabs | 0:0bce3a738bcb | 440 | } |
SolderSplashLabs | 0:0bce3a738bcb | 441 | else |
SolderSplashLabs | 0:0bce3a738bcb | 442 | { |
SolderSplashLabs | 0:0bce3a738bcb | 443 | pc.printf("Failed to send\r\n"); |
SolderSplashLabs | 0:0bce3a738bcb | 444 | break; |
SolderSplashLabs | 0:0bce3a738bcb | 445 | } |
SolderSplashLabs | 0:0bce3a738bcb | 446 | |
SolderSplashLabs | 0:0bce3a738bcb | 447 | n = socket.receive(tmpBuffer, 256); |
SolderSplashLabs | 0:0bce3a738bcb | 448 | |
SolderSplashLabs | 0:0bce3a738bcb | 449 | if ( n > 0 ) |
SolderSplashLabs | 0:0bce3a738bcb | 450 | { |
SolderSplashLabs | 0:0bce3a738bcb | 451 | pc.printf("TCP Socket Recv'd : %s \r\n", tmpBuffer); |
SolderSplashLabs | 0:0bce3a738bcb | 452 | tmpBuffer[n] = '\0'; |
SolderSplashLabs | 0:0bce3a738bcb | 453 | } |
SolderSplashLabs | 0:0bce3a738bcb | 454 | else |
SolderSplashLabs | 0:0bce3a738bcb | 455 | { |
SolderSplashLabs | 0:0bce3a738bcb | 456 | tmpBuffer[0] = '\0'; |
SolderSplashLabs | 0:0bce3a738bcb | 457 | pc.printf("TCP : Failed to Recv\r\n"); |
SolderSplashLabs | 0:0bce3a738bcb | 458 | break; |
SolderSplashLabs | 0:0bce3a738bcb | 459 | } |
SolderSplashLabs | 0:0bce3a738bcb | 460 | |
SolderSplashLabs | 0:0bce3a738bcb | 461 | wait_ms(50); |
SolderSplashLabs | 0:0bce3a738bcb | 462 | |
SolderSplashLabs | 0:0bce3a738bcb | 463 | // Should we stop? |
SolderSplashLabs | 0:0bce3a738bcb | 464 | if ( pc.readable() ) |
SolderSplashLabs | 0:0bce3a738bcb | 465 | { |
SolderSplashLabs | 0:0bce3a738bcb | 466 | pc.printf("Closing Socket \r\n"); |
SolderSplashLabs | 0:0bce3a738bcb | 467 | pc.getc(); |
SolderSplashLabs | 0:0bce3a738bcb | 468 | break; |
SolderSplashLabs | 0:0bce3a738bcb | 469 | } |
SolderSplashLabs | 0:0bce3a738bcb | 470 | } |
SolderSplashLabs | 0:0bce3a738bcb | 471 | if ( wifi.is_connected() ) |
SolderSplashLabs | 0:0bce3a738bcb | 472 | { |
SolderSplashLabs | 0:0bce3a738bcb | 473 | socket.close(); |
SolderSplashLabs | 0:0bce3a738bcb | 474 | } |
SolderSplashLabs | 0:0bce3a738bcb | 475 | pc.printf("Completed.\r\n"); |
SolderSplashLabs | 0:0bce3a738bcb | 476 | } |
SolderSplashLabs | 0:0bce3a738bcb | 477 | } |
SolderSplashLabs | 0:0bce3a738bcb | 478 | |
SolderSplashLabs | 0:0bce3a738bcb | 479 | |
SolderSplashLabs | 0:0bce3a738bcb | 480 | // ------------------------------------------------------------------------------------------------------------ |
SolderSplashLabs | 0:0bce3a738bcb | 481 | /*! |
SolderSplashLabs | 0:0bce3a738bcb | 482 | @brief Opens a sockets to listen for connections, upon connection a message is sent and the |
SolderSplashLabs | 0:0bce3a738bcb | 483 | client disconnected |
SolderSplashLabs | 0:0bce3a738bcb | 484 | */ |
SolderSplashLabs | 0:0bce3a738bcb | 485 | // ------------------------------------------------------------------------------------------------------------ |
SolderSplashLabs | 0:0bce3a738bcb | 486 | void TcpServerTest ( void ) |
SolderSplashLabs | 0:0bce3a738bcb | 487 | { |
SolderSplashLabs | 0:0bce3a738bcb | 488 | int32_t status; |
SolderSplashLabs | 0:0bce3a738bcb | 489 | TCPSocketServer server; |
SolderSplashLabs | 0:0bce3a738bcb | 490 | TCPSocketConnection client; |
SolderSplashLabs | 0:0bce3a738bcb | 491 | |
SolderSplashLabs | 0:0bce3a738bcb | 492 | server.bind(80); |
SolderSplashLabs | 0:0bce3a738bcb | 493 | server.listen(); |
SolderSplashLabs | 0:0bce3a738bcb | 494 | pc.printf("\r\n!! Press any key to stop listening !!\r\n\r\n"); |
SolderSplashLabs | 0:0bce3a738bcb | 495 | while (1) |
SolderSplashLabs | 0:0bce3a738bcb | 496 | { |
SolderSplashLabs | 0:0bce3a738bcb | 497 | status = server.accept(client); |
SolderSplashLabs | 0:0bce3a738bcb | 498 | if (status >= 0) |
SolderSplashLabs | 0:0bce3a738bcb | 499 | { |
SolderSplashLabs | 0:0bce3a738bcb | 500 | client.set_blocking(false, 1500); // Timeout after (1.5)s |
SolderSplashLabs | 0:0bce3a738bcb | 501 | pc.printf("Connection from: %s \r\n", client.get_address()); |
SolderSplashLabs | 0:0bce3a738bcb | 502 | //client.receive(buffer, sizeof(buffer)); |
SolderSplashLabs | 0:0bce3a738bcb | 503 | //pc.printf("Received: %s \r\n",buffer); |
SolderSplashLabs | 0:0bce3a738bcb | 504 | pc.printf("Sending the message to the server. \r\n"); |
SolderSplashLabs | 0:0bce3a738bcb | 505 | client.send_all(hello, sizeof(hello)); |
SolderSplashLabs | 0:0bce3a738bcb | 506 | client.close(); |
SolderSplashLabs | 0:0bce3a738bcb | 507 | } |
SolderSplashLabs | 0:0bce3a738bcb | 508 | |
SolderSplashLabs | 0:0bce3a738bcb | 509 | // Should we stop? |
SolderSplashLabs | 0:0bce3a738bcb | 510 | if ( pc.readable() ) |
SolderSplashLabs | 0:0bce3a738bcb | 511 | { |
SolderSplashLabs | 0:0bce3a738bcb | 512 | pc.printf("Closing Socket \r\n"); |
SolderSplashLabs | 0:0bce3a738bcb | 513 | pc.getc(); |
SolderSplashLabs | 0:0bce3a738bcb | 514 | break; |
SolderSplashLabs | 0:0bce3a738bcb | 515 | } |
SolderSplashLabs | 0:0bce3a738bcb | 516 | |
SolderSplashLabs | 0:0bce3a738bcb | 517 | if (! wifi.is_connected() ) |
SolderSplashLabs | 0:0bce3a738bcb | 518 | { |
SolderSplashLabs | 0:0bce3a738bcb | 519 | pc.printf("WiFi Connection lost \r\n"); |
SolderSplashLabs | 0:0bce3a738bcb | 520 | break; |
SolderSplashLabs | 0:0bce3a738bcb | 521 | } |
SolderSplashLabs | 0:0bce3a738bcb | 522 | } |
SolderSplashLabs | 0:0bce3a738bcb | 523 | |
SolderSplashLabs | 0:0bce3a738bcb | 524 | if ( wifi.is_connected() ) |
SolderSplashLabs | 0:0bce3a738bcb | 525 | { |
SolderSplashLabs | 0:0bce3a738bcb | 526 | server.close(); |
SolderSplashLabs | 0:0bce3a738bcb | 527 | } |
SolderSplashLabs | 0:0bce3a738bcb | 528 | } |