Basic websocket demo esp8266
Dependencies: ESP8266Interface HTTPClient-SSL WebSocketClient mbed-rtos mbed-dev
Fork of ESP8266_HTTP_HelloWorld by
main.cpp@22:82f71740b660, 2016-09-19 (annotated)
- Committer:
- readysteadygo2006
- Date:
- Mon Sep 19 16:30:17 2016 +0000
- Revision:
- 22:82f71740b660
- Parent:
- 20:42f15f50e2ee
fix mbed lib;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
michaeljkoster | 0:6a891da014a3 | 1 | #include "mbed.h" |
readysteadygo2006 | 19:15351a791e29 | 2 | |
sarahmarshy | 14:1f0a842f8750 | 3 | #include "HTTPClient.h" |
michaeljkoster | 0:6a891da014a3 | 4 | #include "ESP8266Interface.h" |
mbedAustin | 9:91fe783e1dd4 | 5 | #include "TCPSocketConnection.h" |
sarahmarshy | 12:978788c2156c | 6 | #include "Websocket.h" |
sarahmarshy | 12:978788c2156c | 7 | |
readysteadygo2006 | 19:15351a791e29 | 8 | |
readysteadygo2006 | 19:15351a791e29 | 9 | |
readysteadygo2006 | 19:15351a791e29 | 10 | DigitalOut myled(LED1); |
readysteadygo2006 | 19:15351a791e29 | 11 | Serial pc(USBTX, USBRX); // tx, rx |
readysteadygo2006 | 19:15351a791e29 | 12 | |
readysteadygo2006 | 20:42f15f50e2ee | 13 | //ESP8266Interface wifi(D8,D2,D7,"EE-mehrg7","MFPHOME0708",115200); // TX,RX,Reset,SSID,Password,Baud |
readysteadygo2006 | 20:42f15f50e2ee | 14 | ESP8266Interface wifi(PA_11,PA_12,PB_12,"EE-mehrg7","MFPHOME0708",115200); // TX,RX,Reset,SSID,Password,Baud |
readysteadygo2006 | 19:15351a791e29 | 15 | |
readysteadygo2006 | 19:15351a791e29 | 16 | int main() { |
readysteadygo2006 | 19:15351a791e29 | 17 | pc.baud(921600); |
sarahmarshy | 12:978788c2156c | 18 | wifi.init(); //Reset |
sarahmarshy | 12:978788c2156c | 19 | wifi.connect(); //Use DHCP |
readysteadygo2006 | 20:42f15f50e2ee | 20 | // wait(2); |
readysteadygo2006 | 19:15351a791e29 | 21 | printf("IP Address is %s\n\r", wifi.getIPAddress()); |
readysteadygo2006 | 20:42f15f50e2ee | 22 | // wait(2); |
readysteadygo2006 | 19:15351a791e29 | 23 | |
readysteadygo2006 | 19:15351a791e29 | 24 | printf("connect ws\n\r", wifi.getIPAddress()); |
readysteadygo2006 | 19:15351a791e29 | 25 | Websocket ws("ws://192.168.1.84:8000/"); |
readysteadygo2006 | 20:42f15f50e2ee | 26 | // wait(2); |
mbedAustin | 7:d2c97b20d237 | 27 | |
readysteadygo2006 | 19:15351a791e29 | 28 | //Check for successful socket connection |
readysteadygo2006 | 19:15351a791e29 | 29 | if(!ws.connect()) |
readysteadygo2006 | 19:15351a791e29 | 30 | ws.close(); |
readysteadygo2006 | 19:15351a791e29 | 31 | else{ |
readysteadygo2006 | 19:15351a791e29 | 32 | char str[100]; |
readysteadygo2006 | 19:15351a791e29 | 33 | |
readysteadygo2006 | 19:15351a791e29 | 34 | for(int i=0; i<0x7fffffff; ++i) { |
readysteadygo2006 | 19:15351a791e29 | 35 | // string with a message |
readysteadygo2006 | 20:42f15f50e2ee | 36 | sprintf(str, "{\"ts\":1, \"readings\": [{\"reading\": \"d\", \"value\": %d}]}", i); |
readysteadygo2006 | 19:15351a791e29 | 37 | printf("send: %s\n\r",str); |
readysteadygo2006 | 19:15351a791e29 | 38 | ws.send(str); |
readysteadygo2006 | 19:15351a791e29 | 39 | |
readysteadygo2006 | 19:15351a791e29 | 40 | // clear the buffer and wait a sec... |
readysteadygo2006 | 19:15351a791e29 | 41 | memset(str, 0, 100); |
readysteadygo2006 | 19:15351a791e29 | 42 | wait(2); |
readysteadygo2006 | 19:15351a791e29 | 43 | |
readysteadygo2006 | 19:15351a791e29 | 44 | // websocket server should echo whatever we sent it |
readysteadygo2006 | 19:15351a791e29 | 45 | if (ws.read(str)) { |
readysteadygo2006 | 19:15351a791e29 | 46 | printf("rcv'd: %s\n", str); |
readysteadygo2006 | 19:15351a791e29 | 47 | } |
readysteadygo2006 | 19:15351a791e29 | 48 | } |
sarahmarshy | 12:978788c2156c | 49 | } |
readysteadygo2006 | 19:15351a791e29 | 50 | ws.close(); |
readysteadygo2006 | 19:15351a791e29 | 51 | |
readysteadygo2006 | 19:15351a791e29 | 52 | while(true) {} |
mbedAustin | 9:91fe783e1dd4 | 53 | } |
readysteadygo2006 | 19:15351a791e29 | 54 |