Basic websocket demo esp8266

Dependencies:   ESP8266Interface HTTPClient-SSL WebSocketClient mbed-rtos mbed-dev

Fork of ESP8266_HTTP_HelloWorld by ESP8266

Committer:
readysteadygo2006
Date:
Tue Aug 09 08:11:54 2016 +0000
Revision:
19:15351a791e29
Parent:
18:3209e51b731a
Child:
20:42f15f50e2ee
Baseline, working with ws_srv_console.js

Who changed what in which revision?

UserRevisionLine numberNew 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 19:15351a791e29 13 ESP8266Interface wifi(D8,D2,D7,"EE-mehrg7","MFPHOME0708",115200); // TX,RX,Reset,SSID,Password,Baud
readysteadygo2006 19:15351a791e29 14
readysteadygo2006 19:15351a791e29 15 int main() {
readysteadygo2006 19:15351a791e29 16 pc.baud(921600);
sarahmarshy 12:978788c2156c 17 wifi.init(); //Reset
sarahmarshy 12:978788c2156c 18 wifi.connect(); //Use DHCP
readysteadygo2006 19:15351a791e29 19 wait(2);
readysteadygo2006 19:15351a791e29 20 printf("IP Address is %s\n\r", wifi.getIPAddress());
readysteadygo2006 19:15351a791e29 21 wait(2);
readysteadygo2006 19:15351a791e29 22
readysteadygo2006 19:15351a791e29 23 printf("connect ws\n\r", wifi.getIPAddress());
readysteadygo2006 19:15351a791e29 24 Websocket ws("ws://192.168.1.84:8000/");
readysteadygo2006 19:15351a791e29 25 wait(20);
mbedAustin 7:d2c97b20d237 26
readysteadygo2006 19:15351a791e29 27 //Check for successful socket connection
readysteadygo2006 19:15351a791e29 28 if(!ws.connect())
readysteadygo2006 19:15351a791e29 29 ws.close();
readysteadygo2006 19:15351a791e29 30 else{
readysteadygo2006 19:15351a791e29 31 char str[100];
readysteadygo2006 19:15351a791e29 32
readysteadygo2006 19:15351a791e29 33 for(int i=0; i<0x7fffffff; ++i) {
readysteadygo2006 19:15351a791e29 34 // string with a message
readysteadygo2006 19:15351a791e29 35 sprintf(str, "%d WebSocket Hello World over wifi", i);
readysteadygo2006 19:15351a791e29 36 printf("send: %s\n\r",str);
readysteadygo2006 19:15351a791e29 37 ws.send(str);
readysteadygo2006 19:15351a791e29 38
readysteadygo2006 19:15351a791e29 39 // clear the buffer and wait a sec...
readysteadygo2006 19:15351a791e29 40 memset(str, 0, 100);
readysteadygo2006 19:15351a791e29 41 wait(2);
readysteadygo2006 19:15351a791e29 42
readysteadygo2006 19:15351a791e29 43 // websocket server should echo whatever we sent it
readysteadygo2006 19:15351a791e29 44 if (ws.read(str)) {
readysteadygo2006 19:15351a791e29 45 printf("rcv'd: %s\n", str);
readysteadygo2006 19:15351a791e29 46 }
readysteadygo2006 19:15351a791e29 47 }
sarahmarshy 12:978788c2156c 48 }
readysteadygo2006 19:15351a791e29 49 ws.close();
readysteadygo2006 19:15351a791e29 50
readysteadygo2006 19:15351a791e29 51 while(true) {}
mbedAustin 9:91fe783e1dd4 52 }
readysteadygo2006 19:15351a791e29 53