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.
Diff: main.cpp
- Revision:
- 4:4e5e094a81c0
- Parent:
- 3:d81f8a9f3733
- Child:
- 5:506f580e7ead
--- 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;