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 Pablo2048
#71836 If you need websocket server in async mode, the simplest way is to use ESPAsyncWebServer which contain websocket plugin. Your'e not using async method in your code because of websocket.loop() in your loop() (async method are async so no more .loop() things...) I hope that your'e not a noob - async things are more complicated and require more knowledge than simlpe sync things from core...