WiFi DipCortex / CC3000 Demo - Contains a menu driven set of tests to initalise and control the CC3000 radio. Also allowing you to test various TCP and UDP connections.
Dependencies: NTPClient WebSocketClient cc3000_hostdriver_mbedsocket mbed HTTPClient
tcpTests.cpp
00001 #include "mbed.h" 00002 #include "cc3000.h" 00003 00004 #include "TCPSocketConnection.h" 00005 #include "TCPSocketServer.h" 00006 00007 #include "HTTPClient.h" 00008 #include "Websocket.h" 00009 00010 extern cc3000 wifi; 00011 extern Serial pc; 00012 HTTPClient http; 00013 00014 const char WEB_SOCKET_URL[] = {"ws://sockets.mbed.org/ws/SolderSplashLabs/wo"}; 00015 const char* ECHO_SERVER_ADDRESS = "192.168.0.10"; 00016 const int ECHO_SERVER_PORT_TCP = 80; 00017 char hello[] = "Hello World\r\n"; 00018 char str[512]; 00019 00020 // ------------------------------------------------------------------------------------------------------------ 00021 /*! 00022 @brief Exercise the HTTP Client library 00023 */ 00024 // ------------------------------------------------------------------------------------------------------------ 00025 void HttpClientTest ( void ) 00026 { 00027 //GET data 00028 printf("\r\nTrying to fetch page... \r\n"); 00029 int ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128); 00030 if (!ret) 00031 { 00032 printf("Page fetched successfully - read %d characters \r\n", strlen(str)); 00033 printf("Result: %s \r\n", str); 00034 } 00035 else 00036 { 00037 printf("Error - ret = %d - HTTP return code = %d \r\n", ret, http.getHTTPResponseCode()); 00038 } 00039 00040 //POST data 00041 HTTPMap map; 00042 HTTPText inText(str, 512); 00043 map.put("Hello", "World"); 00044 map.put("test", "1234"); 00045 printf(" \r\nTrying to post data... \r\n"); 00046 ret = http.post("http://httpbin.org/post", map, &inText); 00047 if (!ret) 00048 { 00049 printf("Executed POST successfully - read %d characters \r\n", strlen(str)); 00050 printf("Result: %s \r\n", str); 00051 } 00052 else 00053 { 00054 printf("Error - ret = %d - HTTP return code = %d \r\n", ret, http.getHTTPResponseCode()); 00055 } 00056 00057 //PUT data 00058 strcpy(str, "This is a PUT test!"); 00059 HTTPText outText(str); 00060 //HTTPText inText(str, 512); 00061 printf(" \r\nTrying to put resource... \r\n"); 00062 ret = http.put("http://httpbin.org/put", outText, &inText); 00063 if (!ret) 00064 { 00065 printf("Executed PUT successfully - read %d characters \r\n", strlen(str)); 00066 printf("Result: %s \r\n", str); 00067 } 00068 else 00069 { 00070 printf("Error - ret = %d - HTTP return code = %d \r\n", ret, http.getHTTPResponseCode()); 00071 } 00072 00073 //DELETE data 00074 //HTTPText inText(str, 512); 00075 printf(" \r\nTrying to delete resource... \r\n"); 00076 ret = http.del("http://httpbin.org/delete", &inText); 00077 if (!ret) 00078 { 00079 printf("Executed DELETE successfully - read %d characters \r\n", strlen(str)); 00080 printf("Result: %s \r\n", str); 00081 } 00082 else 00083 { 00084 printf("Error - ret = %d - HTTP return code = %d \r\n", ret, http.getHTTPResponseCode()); 00085 } 00086 00087 } 00088 00089 // ------------------------------------------------------------------------------------------------------------ 00090 /*! 00091 @brief Open a WebSocket, send a string 00092 */ 00093 // ------------------------------------------------------------------------------------------------------------ 00094 void WebSocketTest ( void ) 00095 { 00096 int res = 0; 00097 uint16_t counter = 0; 00098 uint16_t reconnects = 0; 00099 uint8_t myMAC[8]; 00100 char websocketstr[100]; 00101 00102 wifi.get_mac_address(myMAC); 00103 00104 Websocket ws((char *)WEB_SOCKET_URL); 00105 if ( ws.connect() ) 00106 { 00107 printf("Connected to websocket server.\r\n"); 00108 00109 printf("\r\n!! Press any key to stop sending !!\r\n\r\n"); 00110 while (1) 00111 { 00112 counter ++; 00113 sprintf(websocketstr, "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]); 00114 00115 if ( wifi.is_connected() ) 00116 { 00117 res = ws.send(websocketstr); 00118 printf("Reconnects : %05d, Messages Sent : %05d, Websocket send returned : %d.\r\n", reconnects, counter, res); 00119 00120 if ( -1 == res ) 00121 { 00122 printf("Websocket Failure, reconnecting .... \r\n"); 00123 ws.close(); 00124 if ( ws.connect() ) 00125 { 00126 // Reconnected 00127 reconnects ++; 00128 } 00129 else 00130 { 00131 // Failure! 00132 break; 00133 } 00134 } 00135 00136 wait_ms(1000); 00137 } 00138 else 00139 { 00140 printf("WiFi Connection Lost .... \r\n"); 00141 } 00142 00143 if ( pc.readable() ) 00144 { 00145 pc.getc(); 00146 break; 00147 } 00148 } 00149 00150 ws.close(); 00151 printf("Websocket Closed \r\n"); 00152 } 00153 } 00154 00155 // ------------------------------------------------------------------------------------------------------------ 00156 /*! 00157 @brief Open a TCP port send a string and wait for a reply 00158 */ 00159 // ------------------------------------------------------------------------------------------------------------ 00160 void TcpClientTest ( void ) 00161 { 00162 uint16_t counter = 0; 00163 TCPSocketConnection socket; 00164 char buf[256]; 00165 int n = 0; 00166 00167 if (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT_TCP) < 0) 00168 { 00169 printf("Unable to connect to (%s) on port (%d)\r\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT_TCP); 00170 } 00171 else 00172 { 00173 // Block for 1 second 00174 socket.set_blocking( true, 1000 ); 00175 00176 printf("\r\n!! Press any key to stop sending !!\r\n\r\n"); 00177 while (1) 00178 { 00179 counter ++; 00180 00181 n = socket.send_all(hello, sizeof(hello) - 1); 00182 00183 if ( n > 0 ) 00184 { 00185 printf("%05d : TCP Socket Sent : Hello World\r\n", counter); 00186 } 00187 else 00188 { 00189 printf("Failed to send\r\n"); 00190 break; 00191 } 00192 00193 n = socket.receive(buf, 256); 00194 00195 if ( n > 0 ) 00196 { 00197 printf("TCP Socket Recv'd : %s \r\n", buf); 00198 buf[n] = '\0'; 00199 } 00200 else 00201 { 00202 buf[0] = '\0'; 00203 printf("TCP : Failed to Recv\r\n"); 00204 break; 00205 } 00206 00207 wait_ms(50); 00208 00209 // Should we stop? 00210 if ( pc.readable() ) 00211 { 00212 pc.getc(); 00213 break; 00214 } 00215 } 00216 if ( wifi.is_connected() ) 00217 { 00218 socket.close(); 00219 } 00220 printf("Completed.\r\n"); 00221 } 00222 } 00223 00224 // ------------------------------------------------------------------------------------------------------------ 00225 /*! 00226 @brief Opens a sockets to listen for connections, upon connection a message is sent and the 00227 client disconnected 00228 */ 00229 // ------------------------------------------------------------------------------------------------------------ 00230 void TcpServerTest ( void ) 00231 { 00232 int32_t status; 00233 char buffer[256]; 00234 TCPSocketServer server; 00235 TCPSocketConnection client; 00236 00237 server.bind(15000); 00238 server.listen(); 00239 printf("\r\n!! Press any key to stop listening !!\r\n\r\n"); 00240 while (1) 00241 { 00242 status = server.accept(client); 00243 if (status >= 0) 00244 { 00245 client.set_blocking(false, 1500); // Timeout after (1.5)s 00246 printf("Connection from: %s \r\n", client.get_address()); 00247 //client.receive(buffer, sizeof(buffer)); 00248 //printf("Received: %s \r\n",buffer); 00249 printf("Sending the message to the server. \r\n"); 00250 client.send_all(hello, sizeof(hello)); 00251 client.close(); 00252 } 00253 00254 // Should we stop? 00255 if ( pc.readable() ) 00256 { 00257 pc.getc(); 00258 break; 00259 } 00260 00261 if ( wifi.is_connected() ) 00262 { 00263 break; 00264 } 00265 } 00266 00267 if ( wifi.is_connected() ) 00268 { 00269 server.close(); 00270 } 00271 }
Generated on Wed Jul 13 2022 02:13:28 by 1.7.2