A WiFiDipCortex based robot. Control is via sockets over WiFi. See also: https://github.com/mfurseman/robo-android

Dependencies:   Motordriver USBDevice cc3000_hostdriver_mbedsocket_hacked mbed

Revision:
3:ba11f6207550
Parent:
2:50c151183047
Child:
4:1b5c2a2cdeb7
--- a/main.cpp	Sat Oct 25 17:34:45 2014 +0000
+++ b/main.cpp	Fri Oct 31 23:27:35 2014 +0000
@@ -3,16 +3,24 @@
 #include "TCPSocketConnection.h"
 #include "TCPSocketServer.h"
 
-/* Quickly change debug flag to remove blocking serial code */
-#define DEBUG
+/* MAC 08:00:28:57:43:b8 */
+
+/* Quickly change debug flag to remove USB serial code */
+//#define DEBUG
 #ifdef DEBUG
 #include "USBSerial.h"
-USBSerial serial;
+USBSerial serial;  // This must be instantiated in main
 #define debug(x, ...) serial.printf(x, ##__VA_ARGS__);
 #else
 #define debug(x, ...)
 #endif
 
+/* Client commands */
+#define CMD_NULL 0
+#define CMD_C_ECHO 97
+#define CMD_C_LED_ON 98
+#define CMD_C_LED_OFF 99
+
 
 using namespace mbed_cc3000;
 
@@ -28,44 +36,44 @@
 
 
 /* Prints CC3000 connection info */
-void printConnectionInfo() {
+void printConnectionInfo()
+{
     if (( wifi.is_enabled() ) && ( wifi.is_dhcp_configured() )) {
         wifi.get_ip_config(&ipinfo);
-    }    
+    }
     if (! wifi.is_enabled() ) {
-        debug("CC3000 Disabled\r\n"); 
-    }
-    else if ( wifi.is_dhcp_configured() ) {
+        debug("CC3000 Disabled\r\n");
+    } else if ( wifi.is_dhcp_configured() ) {
         debug("SSID : %-33s|\r\n", ipinfo.uaSSID);
-        debug("IP : %-35s|\r\n", wifi.getIPAddress());   
-    }
-    else if ( wifi.is_connected() ) {
-        debug("Connecting, waiting for DHCP\r\n"); 
-    }
-    else {
-        debug("Not Connected\r\n");   
+        debug("IP : %-35s|\r\n", wifi.getIPAddress());
+    } else if ( wifi.is_connected() ) {
+        debug("Connecting, waiting for DHCP\r\n");
+    } else {
+        debug("Not Connected\r\n");
     }
 }
 
 
 /* WiFi DipCortex board setup */
-void init() {
-    NVIC_SetPriority(SSP1_IRQn, 0x0); 
+void init()
+{
+    NVIC_SetPriority(SSP1_IRQn, 0x0);
     NVIC_SetPriority(PIN_INT0_IRQn, 0x1);
-    
+
     // SysTick set to lower priority than Wi-Fi SPI bus interrupt
-    NVIC_SetPriority(SysTick_IRQn, 0x2); 
-    
+    NVIC_SetPriority(SysTick_IRQn, 0x2);
+
     // Enable RAM1
     LPC_SYSCON->SYSAHBCLKCTRL |= (0x1 << 26);
-    
+
     // This may be neccassary for CC3000
     wait(1);
 }
 
 
 /* Connects WiFi assuming existing SmartConfig */
-void connectWifi() {
+void connectWifi()
+{
     wifi.start(0);
     wait_ms(750);
     wifi._wlan.ioctl_set_connection_policy(0, 0, 1);
@@ -73,47 +81,96 @@
     // TODO: Use static IP if possible
 }
 
-/* Opens a server on port 5678, waits for a connection, sends 'hello world'
-   to the client, then closes all sockets and returns. */
-void serverTest() {
-    TCPSocketServer server;
-    TCPSocketConnection client;
 
-    int32_t status;
-    char hello[] = "Hello World\r\n";
-    
-    /* Wait for a client connection on 5678 */
-    server.bind(5678);
-    debug("Before server.listen()\r\n");
-    server.listen();
-    status = server.accept(client); // This returns -1 with no waiting clients
-    
-    /* Send hello world message to client */
-    debug("Accepted client with status %d\r\n", status);
-    if(status >= 0) {
-        client.set_blocking(false, 1500); // Timeout after (1.5)s
-        debug("Connection from: %s \r\n", client.get_address());
-        client.send_all(hello, sizeof(hello));
-    }
-    
-    /* Disconnect all sockets */
-    client.close();
-    server.close();
-}
-
-int main(void) {
+/* Where it all begins */
+int main(void)
+{
     init();
     debug("Completed init()\r\n");
     printConnectionInfo();
+
     connectWifi();
     debug("Completed connectWifi()\r\n");
     printConnectionInfo();
 
     while(1) {
-        debug("\r\n  ::  One second test loop  ::  \r\n");
+        debug("\r\nOne second client attachment loop\r\n");
         printConnectionInfo();
-        serverTest();
+
+        debug("Creating server and client sockets\r\n");
+        wait_ms(15);
+        TCPSocketConnection client; // is_connected not reliable when using non-blocking sockets
+        TCPSocketServer server;
+        int32_t status;
+
+        server.bind(5678);
+        server.listen();
+        status = server.accept(client);
+        wait_ms(15);
+        int n_timeout = 1;
+        debug("Accept client returned with status %d\r\n", status);
+        if(status >= 0) {
+            client.set_blocking(false, 1000); //  5 ms time out is min for CC3000
+            debug("Connection from: %s \r\n", client.get_address());
+            wait_ms(15);
+
+            while(1) {
+                debug("\r\nClient connected loop - 0 ms\r\n");
+                
+                wait_ms(15);
+                char command = 0;
+                client.set_blocking(false, 5); //  5 ms time out is min for CC3000
+                status = client.receive(&command, 1);
+                if(status == 1) {
+                    debug("Recieved data from client: %d with status %d\r\n", command, status);
+                    switch(command) {
+                        case CMD_C_ECHO:
+                            wait_ms(15);
+                            char buffer[3];
+                            client.set_blocking(false, 2000);
+                            status = client.receive_all(buffer, sizeof(buffer));
+                            debug("Echo test recieved: %s Status: %d\r\n", buffer, status);
+                            
+                            wait_ms(15);
+                            status = client.send_all(buffer, sizeof(buffer));
+                            debug("Echo test send completed with status: %d\r\n");
+                            break;
+                            
+                        case CMD_C_LED_ON:
+                            led = 1;
+                            break;
+                            
+                        case CMD_C_LED_OFF:
+                            led = 0;
+                            break;
+                            
+                        default:
+                            debug("Command %d not recognised\r\n", command);
+                            break;
+                    }
+                }
+
+                wait_ms(15);
+                /* Check to see if the non-blocking socket is closed */
+                if((n_timeout++) % 100 == 0) {
+                    client.set_blocking(false, 1000); //  5 ms time out is min for CC3000
+                    status = client.send("abc\r\n", 5);
+                    debug("Single byte send returned with status %d\r\n", status);
+                    if(status < 0) {
+                        break;
+                    }
+                }
+            }
+            debug("Client connection lost\r\n");
+            wait_ms(15);
+        }
+        debug("Should now return to the top of while(1) after changing LED\r\n");
+        wait_ms(15);
+
+        debug("After wait_ms(15)\r\n");
         led = !led;
-        wait(1);
-   }
+        debug("After led = !led\r\n");
+        wait(1.);
+        debug("After wait(1.)\r\n");
+    }
 }