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
http://www.soldersplash.co.uk/products/wifi-dipcortex/
Please Note, this example uses the serial port so you will need an external RS232 TTL to USB adapter.
Revision 4:4e5e094a81c0, committed 2013-10-09
- Comitter:
- SolderSplashLabs
- Date:
- Wed Oct 09 00:59:47 2013 +0000
- Parent:
- 3:d81f8a9f3733
- Child:
- 5:506f580e7ead
- Commit message:
- Added HTTP Client test
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HTTPClient.lib Wed Oct 09 00:59:47 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/donatien/code/HTTPClient/#1f743885e7de
--- a/main.cpp Tue Oct 08 22:42:04 2013 +0000 +++ b/main.cpp Wed Oct 09 00:59:47 2013 +0000 @@ -1,16 +1,13 @@ #include "mbed.h" #include "cc3000.h" - -#include "Websocket.h" #include "wifi.h" -#include "TCPSocketConnection.h" -#include "TCPSocketServer.h" #include "UDPSocket.h" #include "NTPClient.h" +#include "tcpTests.h" #include "main.h" using namespace mbed_cc3000; @@ -20,16 +17,12 @@ // List open sockets // Overkill mode 2 TCP 2 UDP echo ports? -#define SERIAL_BAUD_RATE 115200 Serial pc(p19, p20); //Serial pc(USBTX, USBRX); -const char* ECHO_SERVER_ADDRESS = "192.168.0.10"; -const int ECHO_SERVER_PORT_TCP = 80; const int ECHO_SERVER_PORT_UDP = 81; uint8_t *HostToPing = (uint8_t *)"google.com"; -char hello[] = "Hello World\r\n"; -const char WEB_SOCKET_URL[] = {"ws://sockets.mbed.org/ws/SolderSplashLabs/wo"}; +tNetappIpconfigRetArgs ipinfo; MENU_LEVEL currentMenu = MENU_TOP; @@ -90,168 +83,6 @@ // ------------------------------------------------------------------------------------------------------------ /*! - @brief Open a WebSocket, send a string -*/ -// ------------------------------------------------------------------------------------------------------------ -void WebSocketTest ( void ) -{ -int res = 0; -uint16_t counter = 0; -uint16_t reconnects = 0; -uint8_t myMAC[8]; -char websocketstr[100]; - - wifi.get_mac_address(myMAC); - - Websocket ws((char *)WEB_SOCKET_URL); - if ( ws.connect() ) - { - printf("Connected to websocket server.\r\n"); - - printf("\r\n!! Press any key to stop sending !!\r\n\r\n"); - while (1) - { - counter ++; - 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]); - res = ws.send(websocketstr); - printf("Reconnects : %05d, Messages Sent : %05d, Websocket send returned : %d.\r\n", reconnects, counter, res); - - if ( -1 == res ) - { - printf("Websocket Failure, reconnecting .... \r\n"); - ws.close(); - if ( ws.connect() ) - { - // Reconnected - reconnects ++; - } - else - { - // Failure! - break; - } - } - - wait_ms(1000); - - if ( pc.readable() ) - { - pc.getc(); - break; - } - } - - ws.close(); - printf("Websocket Closed \r\n"); - } -} - -// ------------------------------------------------------------------------------------------------------------ -/*! - @brief Open a TCP port send a string and wait for a reply -*/ -// ------------------------------------------------------------------------------------------------------------ -void TcpClientTest ( void ) -{ -uint16_t counter = 0; -TCPSocketConnection socket; -char buf[256]; -int n = 0; - - if (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT_TCP) < 0) - { - printf("Unable to connect to (%s) on port (%d)\r\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT_TCP); - } - else - { - // Block for 1 second - socket.set_blocking( true, 1000 ); - - printf("\r\n!! Press any key to stop sending !!\r\n\r\n"); - while (1) - { - counter ++; - - n = socket.send_all(hello, sizeof(hello) - 1); - - if ( n > 0 ) - { - printf("%05d : TCP Socket Sent : Hello World\r\n", counter); - } - else - { - printf("Failed to send\r\n"); - break; - } - - n = socket.receive(buf, 256); - - if ( n > 0 ) - { - printf("TCP Socket Recv'd : %s \r\n", buf); - buf[n] = '\0'; - } - else - { - buf[0] = '\0'; - printf("Failed to Recv\r\n"); - break; - } - - wait_ms(50); - - // Should we stop? - if ( pc.readable() ) - { - pc.getc(); - break; - } - } - socket.close(); - printf("Completed.\r\n"); - } -} - -// ------------------------------------------------------------------------------------------------------------ -/*! - @brief Opens a sockets to listen for connections -*/ -// ------------------------------------------------------------------------------------------------------------ -void TcpServerTest ( void ) -{ -int32_t status; -char buffer[256]; -TCPSocketServer server; -TCPSocketConnection client; - - server.bind(15000); - server.listen(); - printf("\r\n!! Press any key to stop listening !!\r\n\r\n"); - while (1) - { - status = server.accept(client); - if (status >= 0) - { - client.set_blocking(false, 1500); // Timeout after (1.5)s - printf("Connection from: %s \r\n", client.get_address()); - //client.receive(buffer, sizeof(buffer)); - //printf("Received: %s \r\n",buffer); - printf("Sending the message to the server. \r\n"); - client.send_all(hello, sizeof(hello)); - client.close(); - } - - // Should we stop? - if ( pc.readable() ) - { - pc.getc(); - break; - } - } -} - -// ------------------------------------------------------------------------------------------------------------ -/*! @brief Send a UDP Packet, wait for response */ // ------------------------------------------------------------------------------------------------------------ @@ -355,8 +186,6 @@ // ------------------------------------------------------------------------------------------------------------ void Menu_PrintHeader ( void ) { -tNetappIpconfigRetArgs ipinfo; - if ( wifi.is_dhcp_configured() ) { wifi.get_ip_config(&ipinfo); @@ -537,9 +366,10 @@ Menu_PrintHeader(); printf(" 1 - TCP Client, Connect to %s:%d\r\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT_TCP); - printf(" 2 - TCP Server, listen on port %d\r\n", ECHO_SERVER_PORT_TCP); + printf(" 2 - TCP Server, listen on %d.%d.%d.%d:%d\r\n", ipinfo.aucIP[3], ipinfo.aucIP[2], ipinfo.aucIP[1], ipinfo.aucIP[0], ECHO_SERVER_PORT_TCP); printf(" 3 - Web Socket Write \r\n"); printf(" 4 - Web Socket Read \r\n"); + printf(" 5 - HTTP Client \r\n"); printf(" x - Exit to top menu "); printf("\r\n"); printf("Enter command character : "); @@ -555,6 +385,12 @@ case '3': WebSocketTest(); break; + case '4': + + break; + case '5': + HttpClientTest(); + break; case 'x': currentMenu = MENU_TOP; break;
--- a/main.h Tue Oct 08 22:42:04 2013 +0000 +++ b/main.h Wed Oct 09 00:59:47 2013 +0000 @@ -2,6 +2,8 @@ #ifndef MAIN_H #define MAIN_H +#define SERIAL_BAUD_RATE 115200 + typedef enum MENU_LEVEL { MENU_TOP = 0,
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tcpTests.cpp Wed Oct 09 00:59:47 2013 +0000 @@ -0,0 +1,250 @@ +#include "mbed.h" +#include "cc3000.h" + +#include "TCPSocketConnection.h" +#include "TCPSocketServer.h" + +#include "HTTPClient.h" +#include "Websocket.h" + +extern cc3000 wifi; +extern Serial pc; +HTTPClient http; + +const char WEB_SOCKET_URL[] = {"ws://sockets.mbed.org/ws/SolderSplashLabs/wo"}; +const char* ECHO_SERVER_ADDRESS = "192.168.0.10"; +const int ECHO_SERVER_PORT_TCP = 80; +char hello[] = "Hello World\r\n"; +char str[512]; + +// ------------------------------------------------------------------------------------------------------------ +/*! + @brief Exercise the HTTP Client library +*/ +// ------------------------------------------------------------------------------------------------------------ +void HttpClientTest ( void ) +{ + //GET data + printf("\r\nTrying to fetch page... \r\n"); + int ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128); + if (!ret) + { + printf("Page fetched successfully - read %d characters \r\n", strlen(str)); + printf("Result: %s \r\n", str); + } + else + { + printf("Error - ret = %d - HTTP return code = %d \r\n", ret, http.getHTTPResponseCode()); + } + + //POST data + HTTPMap map; + HTTPText inText(str, 512); + map.put("Hello", "World"); + map.put("test", "1234"); + printf(" \r\nTrying to post data... \r\n"); + ret = http.post("http://httpbin.org/post", map, &inText); + if (!ret) + { + printf("Executed POST successfully - read %d characters \r\n", strlen(str)); + printf("Result: %s \r\n", str); + } + else + { + printf("Error - ret = %d - HTTP return code = %d \r\n", ret, http.getHTTPResponseCode()); + } + + //PUT data + strcpy(str, "This is a PUT test!"); + HTTPText outText(str); + //HTTPText inText(str, 512); + printf(" \r\nTrying to put resource... \r\n"); + ret = http.put("http://httpbin.org/put", outText, &inText); + if (!ret) + { + printf("Executed PUT successfully - read %d characters \r\n", strlen(str)); + printf("Result: %s \r\n", str); + } + else + { + printf("Error - ret = %d - HTTP return code = %d \r\n", ret, http.getHTTPResponseCode()); + } + + //DELETE data + //HTTPText inText(str, 512); + printf(" \r\nTrying to delete resource... \r\n"); + ret = http.del("http://httpbin.org/delete", &inText); + if (!ret) + { + printf("Executed DELETE successfully - read %d characters \r\n", strlen(str)); + printf("Result: %s \r\n", str); + } + else + { + printf("Error - ret = %d - HTTP return code = %d \r\n", ret, http.getHTTPResponseCode()); + } + +} + +// ------------------------------------------------------------------------------------------------------------ +/*! + @brief Open a WebSocket, send a string +*/ +// ------------------------------------------------------------------------------------------------------------ +void WebSocketTest ( void ) +{ +int res = 0; +uint16_t counter = 0; +uint16_t reconnects = 0; +uint8_t myMAC[8]; +char websocketstr[100]; + + wifi.get_mac_address(myMAC); + + Websocket ws((char *)WEB_SOCKET_URL); + if ( ws.connect() ) + { + printf("Connected to websocket server.\r\n"); + + printf("\r\n!! Press any key to stop sending !!\r\n\r\n"); + while (1) + { + counter ++; + 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]); + res = ws.send(websocketstr); + printf("Reconnects : %05d, Messages Sent : %05d, Websocket send returned : %d.\r\n", reconnects, counter, res); + + if ( -1 == res ) + { + printf("Websocket Failure, reconnecting .... \r\n"); + ws.close(); + if ( ws.connect() ) + { + // Reconnected + reconnects ++; + } + else + { + // Failure! + break; + } + } + + wait_ms(1000); + + if ( pc.readable() ) + { + pc.getc(); + break; + } + } + + ws.close(); + printf("Websocket Closed \r\n"); + } +} + +// ------------------------------------------------------------------------------------------------------------ +/*! + @brief Open a TCP port send a string and wait for a reply +*/ +// ------------------------------------------------------------------------------------------------------------ +void TcpClientTest ( void ) +{ +uint16_t counter = 0; +TCPSocketConnection socket; +char buf[256]; +int n = 0; + + if (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT_TCP) < 0) + { + printf("Unable to connect to (%s) on port (%d)\r\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT_TCP); + } + else + { + // Block for 1 second + socket.set_blocking( true, 1000 ); + + printf("\r\n!! Press any key to stop sending !!\r\n\r\n"); + while (1) + { + counter ++; + + n = socket.send_all(hello, sizeof(hello) - 1); + + if ( n > 0 ) + { + printf("%05d : TCP Socket Sent : Hello World\r\n", counter); + } + else + { + printf("Failed to send\r\n"); + break; + } + + n = socket.receive(buf, 256); + + if ( n > 0 ) + { + printf("TCP Socket Recv'd : %s \r\n", buf); + buf[n] = '\0'; + } + else + { + buf[0] = '\0'; + printf("Failed to Recv\r\n"); + break; + } + + wait_ms(50); + + // Should we stop? + if ( pc.readable() ) + { + pc.getc(); + break; + } + } + socket.close(); + printf("Completed.\r\n"); + } +} + +// ------------------------------------------------------------------------------------------------------------ +/*! + @brief Opens a sockets to listen for connections, upon connection a message is sent and the + client disconnected +*/ +// ------------------------------------------------------------------------------------------------------------ +void TcpServerTest ( void ) +{ +int32_t status; +char buffer[256]; +TCPSocketServer server; +TCPSocketConnection client; + + server.bind(15000); + server.listen(); + printf("\r\n!! Press any key to stop listening !!\r\n\r\n"); + while (1) + { + status = server.accept(client); + if (status >= 0) + { + client.set_blocking(false, 1500); // Timeout after (1.5)s + printf("Connection from: %s \r\n", client.get_address()); + //client.receive(buffer, sizeof(buffer)); + //printf("Received: %s \r\n",buffer); + printf("Sending the message to the server. \r\n"); + client.send_all(hello, sizeof(hello)); + client.close(); + } + + // Should we stop? + if ( pc.readable() ) + { + pc.getc(); + break; + } + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tcpTests.h Wed Oct 09 00:59:47 2013 +0000 @@ -0,0 +1,13 @@ +#ifndef TCPTESTS_H +#define TCPTESTS_H + +void HttpClientTest ( void ); +void WebSocketTest ( void ); +void TcpClientTest ( void ); +void TcpServerTest ( void ); + +extern const char *ECHO_SERVER_ADDRESS; +extern const int ECHO_SERVER_PORT_TCP = 80; +extern char *hello; + +#endif \ No newline at end of file