WebSocket Server ASYNC
Posted: Sat Nov 11, 2017 7:43 am
I need a WebSocket Server in Esp Async, I can not get it to work and I do not find an example of the server where I'm going wrong?
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESPAsyncTCP.h>
#include <WebSocketsServer.h>
WebSocketsServer webSocket = WebSocketsServer(81);
const char* ssid = "*****";
const char* password = "********";
String texto;
void setup()
{
Serial.begin(115200);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED)
{
Serial.printf("WiFi Failed!\n");
return;
}
Serial.printf("WiFi Connected!\n");
Serial.println(WiFi.localIP());
webSocket.begin();
webSocket.onEvent(webSocketEvent);
}
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
Serial.printf(" >>> [WebSockets] Event(%d, %d, ...)\r\n", num, type);
switch(type) {
case WStype_DISCONNECTED:
Serial.printf("[%u] Desconectado!\r\n", num);
break;
case WStype_CONNECTED:
{
IPAddress ip = webSocket.remoteIP(num);
Serial.printf(">>> [WebSockets] [%u] Conectado IP: %d.%d.%d.%d url: %s\r\n", num, ip[0], ip[1], ip[2], ip[3], payload);
Serial.println("Passou pelo conect do WEBSOCKET");
}
break;
case WStype_TEXT:
Serial.printf(">>> [WebSockets] [%u] Recebendo comando: %s\r\n", num, payload);
break;
}
}
void loop()
{
webSocket.loop();
}
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESPAsyncTCP.h>
#include <WebSocketsServer.h>
WebSocketsServer webSocket = WebSocketsServer(81);
const char* ssid = "*****";
const char* password = "********";
String texto;
void setup()
{
Serial.begin(115200);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED)
{
Serial.printf("WiFi Failed!\n");
return;
}
Serial.printf("WiFi Connected!\n");
Serial.println(WiFi.localIP());
webSocket.begin();
webSocket.onEvent(webSocketEvent);
}
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
Serial.printf(" >>> [WebSockets] Event(%d, %d, ...)\r\n", num, type);
switch(type) {
case WStype_DISCONNECTED:
Serial.printf("[%u] Desconectado!\r\n", num);
break;
case WStype_CONNECTED:
{
IPAddress ip = webSocket.remoteIP(num);
Serial.printf(">>> [WebSockets] [%u] Conectado IP: %d.%d.%d.%d url: %s\r\n", num, ip[0], ip[1], ip[2], ip[3], payload);
Serial.println("Passou pelo conect do WEBSOCKET");
}
break;
case WStype_TEXT:
Serial.printf(">>> [WebSockets] [%u] Recebendo comando: %s\r\n", num, payload);
break;
}
}
void loop()
{
webSocket.loop();
}