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
main.cpp@15:b3e0f8ccd08f, 2015-10-21 (annotated)
- Committer:
- kentaroallen
- Date:
- Wed Oct 21 14:30:37 2015 +0000
- Revision:
- 15:b3e0f8ccd08f
- Parent:
- 14:4ee468971ae0
The mbed software for controlling a robot through wifi websockets from a Python Tornado Web Server.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
michaeljkoster | 0:6a891da014a3 | 1 | #include "mbed.h" |
michaeljkoster | 0:6a891da014a3 | 2 | #include "ESP8266Interface.h" |
mbedAustin | 9:91fe783e1dd4 | 3 | #include "TCPSocketConnection.h" |
sarahmarshy | 12:978788c2156c | 4 | #include "TCPSocketServer.h" |
sarahmarshy | 12:978788c2156c | 5 | #include "Websocket.h" |
kentaroallen | 15:b3e0f8ccd08f | 6 | #include "motordriver.h" |
kentaroallen | 15:b3e0f8ccd08f | 7 | #include "HTTPClient.h" |
sarahmarshy | 12:978788c2156c | 8 | |
kentaroallen | 15:b3e0f8ccd08f | 9 | ESP8266Interface wifi(p28,p27,p26,"HOME-0392","D64D858A064D14BF",115200); // TX,RX,Reset,SSID,Password,Baud |
kentaroallen | 15:b3e0f8ccd08f | 10 | Motor r(p22, p18, p17, 1); |
kentaroallen | 15:b3e0f8ccd08f | 11 | Motor m(p21, p20, p19, 1); // pwm, fwd, rev |
kentaroallen | 15:b3e0f8ccd08f | 12 | DigitalOut led1(LED1); |
kentaroallen | 15:b3e0f8ccd08f | 13 | DigitalOut led2(LED2); |
kentaroallen | 15:b3e0f8ccd08f | 14 | DigitalOut led3(LED3); |
kentaroallen | 15:b3e0f8ccd08f | 15 | DigitalOut led4(LED4); |
kentaroallen | 15:b3e0f8ccd08f | 16 | Serial pc(USBTX, USBRX); |
sarahmarshy | 12:978788c2156c | 17 | |
kentaroallen | 15:b3e0f8ccd08f | 18 | void forward() |
kentaroallen | 15:b3e0f8ccd08f | 19 | { |
kentaroallen | 15:b3e0f8ccd08f | 20 | led1 = 1; |
kentaroallen | 15:b3e0f8ccd08f | 21 | r.speed(.6f); |
kentaroallen | 15:b3e0f8ccd08f | 22 | m.speed(.6f); |
kentaroallen | 15:b3e0f8ccd08f | 23 | wait(1.5); |
kentaroallen | 15:b3e0f8ccd08f | 24 | r.coast(); |
kentaroallen | 15:b3e0f8ccd08f | 25 | m.coast(); |
kentaroallen | 15:b3e0f8ccd08f | 26 | led1 = 0; |
kentaroallen | 15:b3e0f8ccd08f | 27 | } |
kentaroallen | 15:b3e0f8ccd08f | 28 | void backward() |
kentaroallen | 15:b3e0f8ccd08f | 29 | { |
kentaroallen | 15:b3e0f8ccd08f | 30 | led2 = 1; |
kentaroallen | 15:b3e0f8ccd08f | 31 | r.speed(-.6f); |
kentaroallen | 15:b3e0f8ccd08f | 32 | m.speed(-.6f); |
kentaroallen | 15:b3e0f8ccd08f | 33 | wait(1.5); |
kentaroallen | 15:b3e0f8ccd08f | 34 | r.coast(); |
kentaroallen | 15:b3e0f8ccd08f | 35 | m.coast(); |
kentaroallen | 15:b3e0f8ccd08f | 36 | led2 = 0; |
kentaroallen | 15:b3e0f8ccd08f | 37 | } |
kentaroallen | 15:b3e0f8ccd08f | 38 | void left() |
kentaroallen | 15:b3e0f8ccd08f | 39 | { |
kentaroallen | 15:b3e0f8ccd08f | 40 | led3 = 1; |
kentaroallen | 15:b3e0f8ccd08f | 41 | wait(.5); |
kentaroallen | 15:b3e0f8ccd08f | 42 | r.speed(.6f); |
kentaroallen | 15:b3e0f8ccd08f | 43 | m.speed(-.6f); |
kentaroallen | 15:b3e0f8ccd08f | 44 | wait(1.5); |
kentaroallen | 15:b3e0f8ccd08f | 45 | r.coast(); |
kentaroallen | 15:b3e0f8ccd08f | 46 | m.coast(); |
kentaroallen | 15:b3e0f8ccd08f | 47 | led3 = 0; |
kentaroallen | 15:b3e0f8ccd08f | 48 | } |
kentaroallen | 15:b3e0f8ccd08f | 49 | void right() |
kentaroallen | 15:b3e0f8ccd08f | 50 | { |
kentaroallen | 15:b3e0f8ccd08f | 51 | led4 = 1; |
kentaroallen | 15:b3e0f8ccd08f | 52 | wait(.5); |
kentaroallen | 15:b3e0f8ccd08f | 53 | r.speed(-.6f); |
kentaroallen | 15:b3e0f8ccd08f | 54 | m.speed(.6f); |
kentaroallen | 15:b3e0f8ccd08f | 55 | wait(1.5); |
kentaroallen | 15:b3e0f8ccd08f | 56 | r.coast(); |
kentaroallen | 15:b3e0f8ccd08f | 57 | m.coast(); |
kentaroallen | 15:b3e0f8ccd08f | 58 | led4 = 0; |
kentaroallen | 15:b3e0f8ccd08f | 59 | } |
kentaroallen | 15:b3e0f8ccd08f | 60 | |
kentaroallen | 15:b3e0f8ccd08f | 61 | int main() |
kentaroallen | 15:b3e0f8ccd08f | 62 | { |
sarahmarshy | 12:978788c2156c | 63 | wifi.init(); //Reset |
sarahmarshy | 12:978788c2156c | 64 | wifi.connect(); //Use DHCP |
kentaroallen | 15:b3e0f8ccd08f | 65 | pc.printf("IP Address is %s\n", wifi.getIPAddress()); |
kentaroallen | 15:b3e0f8ccd08f | 66 | |
kentaroallen | 15:b3e0f8ccd08f | 67 | Websocket ws("ws://10.0.0.16:8080/ws"); |
kentaroallen | 15:b3e0f8ccd08f | 68 | if(!ws.connect()) { |
sarahmarshy | 12:978788c2156c | 69 | ws.close(); |
kentaroallen | 15:b3e0f8ccd08f | 70 | } else { |
kentaroallen | 15:b3e0f8ccd08f | 71 | char str[10]; |
kentaroallen | 15:b3e0f8ccd08f | 72 | char a; |
kentaroallen | 15:b3e0f8ccd08f | 73 | while(1) { |
kentaroallen | 15:b3e0f8ccd08f | 74 | //ready for new control |
kentaroallen | 15:b3e0f8ccd08f | 75 | sprintf(str, "1"); |
kentaroallen | 15:b3e0f8ccd08f | 76 | pc.printf("%d",str); |
kentaroallen | 15:b3e0f8ccd08f | 77 | ws.send(str); //send to server stating ready for new instruction |
kentaroallen | 15:b3e0f8ccd08f | 78 | //memset(str,0,10); |
sarahmarshy | 12:978788c2156c | 79 | wait(0.5f); |
kentaroallen | 15:b3e0f8ccd08f | 80 | |
kentaroallen | 15:b3e0f8ccd08f | 81 | if(ws.read(str)) { |
kentaroallen | 15:b3e0f8ccd08f | 82 | pc.printf("%s\n", *str); |
kentaroallen | 15:b3e0f8ccd08f | 83 | a = str[0]; |
kentaroallen | 15:b3e0f8ccd08f | 84 | pc.printf("%s",a); |
kentaroallen | 15:b3e0f8ccd08f | 85 | if(a=='1') { |
kentaroallen | 15:b3e0f8ccd08f | 86 | forward(); |
kentaroallen | 15:b3e0f8ccd08f | 87 | } |
kentaroallen | 15:b3e0f8ccd08f | 88 | if(a=='2') { |
kentaroallen | 15:b3e0f8ccd08f | 89 | backward(); |
kentaroallen | 15:b3e0f8ccd08f | 90 | } |
kentaroallen | 15:b3e0f8ccd08f | 91 | if(a=='3') { |
kentaroallen | 15:b3e0f8ccd08f | 92 | left(); |
kentaroallen | 15:b3e0f8ccd08f | 93 | } |
kentaroallen | 15:b3e0f8ccd08f | 94 | if(a=='4') { |
kentaroallen | 15:b3e0f8ccd08f | 95 | right(); |
kentaroallen | 15:b3e0f8ccd08f | 96 | } |
kentaroallen | 15:b3e0f8ccd08f | 97 | } else { |
kentaroallen | 15:b3e0f8ccd08f | 98 | //led1=led2=led3=led4=1; |
sarahmarshy | 12:978788c2156c | 99 | } |
kentaroallen | 15:b3e0f8ccd08f | 100 | //memset(str,0,10); |
kentaroallen | 15:b3e0f8ccd08f | 101 | wait(3); |
sarahmarshy | 12:978788c2156c | 102 | } |
sarahmarshy | 12:978788c2156c | 103 | } |
kentaroallen | 15:b3e0f8ccd08f | 104 | |
mbedAustin | 9:91fe783e1dd4 | 105 | } |