This is the mbed code for handling Wifi websocket control from a Python based Tornado Web Server.

Dependencies:   ESP8266Interface HTTPClient Motordriver WebSocketClient mbed

Fork of ESP8266_WebSockets_HelloWorld by ESP8266

Files at this revision

API Documentation at this revision

Comitter:
kentaroallen
Date:
Wed Oct 21 14:30:37 2015 +0000
Parent:
14:4ee468971ae0
Commit message:
The mbed software for controlling a robot through wifi websockets from a Python Tornado Web Server.

Changed in this revision

HTTPClient.lib Show annotated file Show diff for this revision Revisions of this file
Motordriver.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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPClient.lib	Wed Oct 21 14:30:37 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/donatien/code/HTTPClient/#1f743885e7de
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Motordriver.lib	Wed Oct 21 14:30:37 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/littlexc/code/Motordriver/#3110b9209d3c
--- a/main.cpp	Mon Jun 08 21:32:56 2015 +0000
+++ b/main.cpp	Wed Oct 21 14:30:37 2015 +0000
@@ -3,42 +3,103 @@
 #include "TCPSocketConnection.h"
 #include "TCPSocketServer.h"
 #include "Websocket.h"
+#include "motordriver.h"
+#include "HTTPClient.h"
 
-
+ESP8266Interface wifi(p28,p27,p26,"HOME-0392","D64D858A064D14BF",115200); // TX,RX,Reset,SSID,Password,Baud
+Motor r(p22, p18, p17, 1);
+Motor m(p21, p20, p19, 1); // pwm, fwd, rev
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+Serial pc(USBTX, USBRX);
 
-ESP8266Interface wifi(D1,D0,D10,"demossid","password",115200); // TX,RX,Reset,SSID,Password,Baud
- 
-int main() {
+void forward()
+{
+    led1 = 1;
+    r.speed(.6f);
+    m.speed(.6f);
+    wait(1.5);
+    r.coast();
+    m.coast();
+    led1 = 0;
+}
+void backward()
+{
+    led2 = 1;
+    r.speed(-.6f);
+    m.speed(-.6f);
+    wait(1.5);
+    r.coast();
+    m.coast();
+    led2 = 0;
+}
+void left()
+{
+    led3 = 1;
+    wait(.5);
+    r.speed(.6f);
+    m.speed(-.6f);
+    wait(1.5);
+    r.coast();
+    m.coast();
+    led3 = 0;
+}
+void right()
+{
+    led4 = 1;
+    wait(.5);
+    r.speed(-.6f);
+    m.speed(.6f);
+    wait(1.5);
+    r.coast();
+    m.coast();
+    led4 = 0;
+}
+
+int main()
+{
     wifi.init(); //Reset
     wifi.connect(); //Use DHCP
-    printf("IP Address is %s\n", wifi.getIPAddress());
-    
-    Websocket ws("ws://192.168.1.20:8888/ws");
-    
-    //Check for successful socket connection
-    if(!ws.connect())
+    pc.printf("IP Address is %s\n", wifi.getIPAddress());
+
+    Websocket ws("ws://10.0.0.16:8080/ws");
+    if(!ws.connect()) {
         ws.close();
-    else{
-        char str[100];
-        
-        for(int i=0; i<0x7fffffff; ++i) {
-            // string with a message
-            sprintf(str, "%d WebSocket Hello World over wifi", i);
-            printf("send: %s\n",str);
-            ws.send(str);
-        
-            // clear the buffer and wait a sec...
-            memset(str, 0, 100);
+    } else {
+        char str[10];
+        char a;
+        while(1) {
+            //ready for new control
+            sprintf(str, "1");
+            pc.printf("%d",str);
+            ws.send(str); //send to server stating ready for new instruction
+            //memset(str,0,10);
             wait(0.5f);
-        
-            // websocket server should echo whatever we sent it
-            if (ws.read(str)) {
-                printf("rcv'd: %s\n", str);
+
+            if(ws.read(str)) {
+                pc.printf("%s\n", *str);
+                a = str[0];
+                pc.printf("%s",a);
+                if(a=='1') {
+                    forward();
+                }
+                if(a=='2') {
+                    backward();
+                }
+                if(a=='3') {
+                    left();
+                }
+                if(a=='4') {
+                    right();
+                }
+            } else {
+                //led1=led2=led3=led4=1;
             }
+            //memset(str,0,10);
+            wait(3);
         }
     }
-    ws.close();
-    
-    while(true) {}
+
 }
- 
\ No newline at end of file