ESP32 WebSocket Server Issue

Hello everyone,
I've been having a big issue lately with my ESP32 board and honestly, I don't know what else I should check... but to the point. I'm trying to establish WebSocketServer on my ESP32 DevKitCV4 board to connect from every device in a local network and get some HTML page in response. I had it up and running but after a short break when I came back to the development it wasn't working anymore. I couldn't find the problem so I tried to use some ready projects from the internet. Down below I copy-pasted code that I got from one article that apparently should work fine. In the serial monitor I get two messages: SPIFFS mounted successfully (nice) and it connects to the WiFi with some local IP assigned, however, I can't access that IP because I'm always getting timed out. Moreover, when I run arp -a in cmd there is no connected device with that IP! I even tested it on a second ESP32 board with no success. I checked other USB cables (I suspected a problem with the sketch import), and got the latest libraries versions - nothing. In Wireshark there's just ARP protocol being run with no success...

Here's the code:

#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include "SPIFFS.h"
#include <Arduino_JSON.h>

// Replace with your network credentials
const char* ssid = "someSSID";
const char* password = "somePassword";

// Create AsyncWebServer object on port 80
AsyncWebServer server(80);

// Create a WebSocket object
AsyncWebSocket ws("/ws");

String message = "";

// Initialize SPIFFS
void initFS() {
  if (!SPIFFS.begin()) {
    Serial.println("An error has occurred while mounting SPIFFS");
  }
  else{
   Serial.println("SPIFFS mounted successfully");
  }
}

// Initialise WiFi
void initWiFi() {
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi ..");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
  }
  Serial.println(WiFi.localIP());
}

void handleWebSocketMessage(void *arg, uint8_t *data, size_t len) {
  AwsFrameInfo *info = (AwsFrameInfo*)arg;
  if (info->final && info->index == 0 && info->len == len && info->opcode == WS_TEXT) {
    data[len] = 0;
    message = (char*)data;
  }
}

void onEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) {
  switch (type) {
    case WS_EVT_CONNECT:
      Serial.printf("WebSocket client #%u connected from %s\n", client->id(), client->remoteIP().toString().c_str());
      break;
    case WS_EVT_DISCONNECT:
      Serial.printf("WebSocket client #%u disconnected\n", client->id());
      break;
    case WS_EVT_DATA:
      handleWebSocketMessage(arg, data, len);
      break;
    case WS_EVT_PONG:
    case WS_EVT_ERROR:
      break;
  }
}

void initWebSocket() {
  ws.onEvent(onEvent);
  server.addHandler(&ws);
}


void setup() {
  Serial.begin(115200);

  initFS();
  initWiFi();
  initWebSocket();
  
  // Web Server Root URL
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/index.html", "text/html");
  });
  
  server.serveStatic("/", SPIFFS, "/");

  // Start server
  server.begin();
}

void loop() {
  ws.cleanupClients();
}

Index.html is just some dummy webpage to test. Does anyone have an idea what might be wrong with that solution? I really need this to work to establish communication with my mobile robot and it was working just fine until something weird happened...

Lets assume that ssid and password are both correct. Is the IP address being printed consistent with the IP address range provided by the DHCP server on your WiFi network? Does your router dashboard or WiFi station list show the mac address of your ESP as a connected client?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.