Small demo to purely test UDP, depends on the module already being configured to auto connect to an access point
Dependencies: USBDevice cc3000_hostdriver_mbedsocket mbed
Fork of WifiDipCortex-UDPDemo by
Wifi-DipCortex - Test application that shows you how to listen for UDP message on one port while sending messages on another
- Listens on UDP_LISTEN_PORT, upon data being recvied it can be found in UDP_RecvBuffer - Transmits UDP_TX_Buffer on UDP_TARGET_PORT to UDP_TARGET_IP
Revision 3:3fd4a328c7cf, committed 2014-08-07
- Comitter:
- SolderSplashLabs
- Date:
- Thu Aug 07 00:40:04 2014 +0000
- Parent:
- 2:6813713a64e1
- Commit message:
- Added Smartconfig; Added disconnection detection; Re-connection after disconnection, not functioning; PC app here to exercise the UDP connection : https://github.com/SolderSplashLabs/PC-UDP-Example
Changed in this revision
cc3000_hostdriver_mbedsocket.lib | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/cc3000_hostdriver_mbedsocket.lib Thu Jul 10 16:52:10 2014 +0000 +++ b/cc3000_hostdriver_mbedsocket.lib Thu Aug 07 00:40:04 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/SolderSplashLabs/code/cc3000_hostdriver_mbedsocket/#ca8c234997c0 +http://mbed.org/users/SolderSplashLabs/code/cc3000_hostdriver_mbedsocket/#974ff993d863
--- a/main.cpp Thu Jul 10 16:52:10 2014 +0000 +++ b/main.cpp Thu Aug 07 00:40:04 2014 +0000 @@ -23,10 +23,11 @@ cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37)); Serial uart(p19, p20); +DigitalIn Button(P0_1); + // USB CDC serial port USBSerial pc; - char LOCALHOST[] = "localhost"; const char UDP_TX_Buffer[] = {0,0,0,0,0,1,0,0,0,0,0,0,6,0x61,0x61,0x61,0x61,0x61,0x61,5,0x6c,0x6f,0x63,0x61,0x6c,0,0,1,0,1}; @@ -36,15 +37,46 @@ // This is the broadcast address for the subnet 192.168.0.x //const char* UDP_TARGET_IP = "192.168.0.255"; -const int UDP_TARGET_PORT = 5353; +const int UDP_TARGET_PORT = 1900; UDPSocket UDP_TransmitSocket; -const int UDP_LISTEN_PORT = 11028; +const int UDP_LISTEN_PORT = 1900; UDPSocket UDP_RecvSocket; -#define UDP_RECV_BUF_LEN 512 +#define UDP_RECV_BUF_LEN 1400 uint8_t UDP_RecvBuffer[ UDP_RECV_BUF_LEN ]; + +// ------------------------------------------------------------------------------------------------------------ +/*! + @brief WaitForConnection +*/ +// ------------------------------------------------------------------------------------------------------------ +void SmartConfig ( void ) +{ + pc.printf("\r\nStarting Smart config, waiting for message from smartphone app ....\r\n"); + + // We dont want to auto reconnect to an access point + wifi._wlan.ioctl_set_connection_policy(0, 0, 0); + + // start smart config will disconnect, set the prefix + // wait for a message via a SmartConfig app, store it to the profile list + // finally it will reenable auto connection, triggering the module to connect to the new access point + wifi.start_smart_config(0); + + pc.printf("\r\nSmartConfig complete\r\n"); + + while (! Button ) + { + // Wait for release + pc.printf("Release the button\r\n"); + wait(1); + } + + // NOTE : normal once connected using SmartConfig you would enable the mdns server to tell the app your connected + // but that would interfere with this example using the mdns port +} + // ------------------------------------------------------------------------------------------------------------ /*! @brief WaitForConnection @@ -52,13 +84,20 @@ // ------------------------------------------------------------------------------------------------------------ void WaitForConnection ( void ) { +uint8_t buffer[8]; tNetappIpconfigRetArgs ipinfo; while (! ( wifi.is_connected() ) ) { // Still not connected - pc.printf("No Connection\r\n"); + pc.printf("No Connection - hold button for smartconfig\r\n"); wait(1); + + if (! Button ) + { + // Button has been pressed + SmartConfig(); + } } if (( wifi.is_enabled() ) && ( wifi.is_dhcp_configured() )) @@ -67,6 +106,14 @@ } pc.printf("Connected to (%s) %s \r\n", ipinfo.uaSSID, wifi.getIPAddress()); + + wifi.get_mac_address(buffer); + pc.printf(" MAC address : %02x:%02x:%02x:%02x:%02x:%02x\r\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5]); + + if (! wifi._nvmem.read_sp_version( (unsigned char*)&buffer[0] ) ) + { + pc.printf(" CC3000 Firmware Version : %u.%u \r\n", buffer[0], buffer[1]); + } } @@ -118,7 +165,7 @@ target.set_address(UDP_TARGET_IP, UDP_TARGET_PORT); - pc.printf("Sending UDP Message ..\r\n"); + pc.printf("Sending UDP Message to port (%i)..\r\n", UDP_TARGET_PORT); returnVal = UDP_TransmitSocket.sendTo(target, (char *)UDP_TX_Buffer, sizeof(UDP_TX_Buffer)); @@ -167,15 +214,21 @@ { Endpoint client; int returnVal = 0; - - returnVal = UDP_RecvSocket.receiveFrom(client, (char *)UDP_RecvBuffer, UDP_RECV_BUF_LEN); - - if ( returnVal > 0 ) + + do { - pc.printf("UDP Message Recv'd %i bytes from : %s \r\n", returnVal, client.get_address()); + returnVal = UDP_RecvSocket.receiveFrom(client, (char *)UDP_RecvBuffer, UDP_RECV_BUF_LEN); + + if ( returnVal > 0 ) + { + pc.printf("UDP Message Recv'd %i bytes from : %s \r\n", returnVal, client.get_address()); - // TODO : Process your UDP message here - } + // TODO : Process your UDP message here + // NOTE : a message larger than your CC3000 SPI buffer and the UDP_RECV_BUF_LEN will be split up into multiple receiveFrom calls + // NOTE : multiple messages on a UDP port seem to get merged. + } + + } while ( returnVal > 0 ); } // ------------------------------------------------------------------------------------------------------------ @@ -196,6 +249,8 @@ uart.baud(SERIAL_BAUD_RATE); + Button.mode(PullUp); + wait(1); } @@ -222,7 +277,6 @@ } - // ------------------------------------------------------------------------------------------------------------ /*! @brief main loop @@ -271,6 +325,8 @@ wifi.restart(0); + wifi._wlan.ioctl_set_connection_policy(0, 1, 1); + // Block and wait for re-connection WaitForConnection(); @@ -282,6 +338,12 @@ // For now lets simulate a task that lasts for 100ms wait(0.1); pc.printf("."); + + // If the user presses the button, send a message + if (! Button ) + { + UdpTx(); + } } } \ No newline at end of file