The use of the ESP8266 in the world of IoT
User avatar
By Josep112
#71768 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();
}
User avatar
By philbowles
#71769 You have to edit the espasynctcp.h file to select espasynctcp mode. Also a lot depends on your html...what errors are you getting?

PS use proper capitalisation. I'm on a tablet and can't be bothered :x