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 Carl - SolderSplash Labs

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:
2:6813713a64e1
Parent:
1:5fa4efb214e7
Child:
3:3fd4a328c7cf
--- a/main.cpp	Wed Jul 09 19:27:41 2014 +0000
+++ b/main.cpp	Thu Jul 10 16:52:10 2014 +0000
@@ -26,13 +26,15 @@
 // 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};
 
 // Multicast address - this causes issues with firmware version 1.28
-//const char* UDP_TARGET_IP = "224.0.0.251";
+const char* UDP_TARGET_IP = "224.0.0.251";
 
 // This is the broadcast address for the subnet 192.168.0.x
-const char* UDP_TARGET_IP = "192.168.0.255";
+//const char* UDP_TARGET_IP = "192.168.0.255";
 
 const int UDP_TARGET_PORT = 5353;
 UDPSocket UDP_TransmitSocket;
@@ -52,7 +54,7 @@
 {
 tNetappIpconfigRetArgs ipinfo;
     
-    while (! ( wifi.is_dhcp_configured() ) )
+    while (! ( wifi.is_connected() ) )
     {
         // Still not connected
         pc.printf("No Connection\r\n");
@@ -199,6 +201,30 @@
 
 // ------------------------------------------------------------------------------------------------------------
 /*!
+    @brief SetupSockets - Sets up the UDP sockets and resolves localhost to work around internal CC3000 issues
+*/
+// ------------------------------------------------------------------------------------------------------------
+void SetupSockets ( void )
+{
+uint32_t ip;
+
+    // Connected, v1.28 firmware workaround, ask for localhost to be resolved
+    wifi._socket.gethostbyname((uint8_t *)LOCALHOST,strlen((const char *)LOCALHOST), &ip);
+    
+    // Ignore the result, ask again
+    wifi._socket.gethostbyname((uint8_t *)LOCALHOST,strlen((const char *)LOCALHOST), &ip);
+    
+    // Ignore the result
+    
+    // set up a lisening socket
+    UdpRx_Init();
+    UdpTx_Init();
+
+}
+
+
+// ------------------------------------------------------------------------------------------------------------
+/*!
     @brief main loop
 */
 // ------------------------------------------------------------------------------------------------------------
@@ -217,27 +243,45 @@
     
     WaitForConnection();
     
-    // set up a lisening socket
-    UdpRx_Init();
-    UdpTx_Init();
+    SetupSockets();
     
     while (1)
     {        
-        // Check for UDP coms, non blocking
-        UdpRx();
+    
+        if ( wifi.is_connected() )
+        {
+            // Check for UDP coms, non blocking
+            UdpRx();
+                    
+            if ( tickCnt > 100 )
+            {
+                tickCnt = 0;
+                // And then send a UDP message
+                UdpTx();
+            }
+            
+            tickCnt ++;
+        }
+        else
+        {
+            // We have lost connection, tell the objects to close the sockets
+            // this clears down the socket handles
+            UDP_RecvSocket.close();
+            UDP_TransmitSocket.close();
+            
+            wifi.restart(0);
+            
+            // Block and wait for re-connection
+            WaitForConnection();
+    
+            // set up a lisening and transmit socket
+            SetupSockets();
+        }
         
         // TODO : Do other things, like service sensors etc..
         // For now lets simulate a task that lasts for 100ms
         wait(0.1);
         pc.printf(".");
-        
-        if ( tickCnt > 100 )
-        {
-            tickCnt = 0;
-            // And then send a UDP message
-            UdpTx();
-        }
-        tickCnt ++;
     }
    
 }
\ No newline at end of file