Issue with ESP-NOW functionality when connecting to WebSocket server in Node.js

agoktugaydin
Posts: 1
Joined: Wed Jan 10, 2024 4:28 pm

Issue with ESP-NOW functionality when connecting to WebSocket server in Node.js

Postby agoktugaydin » Wed Jan 10, 2024 4:32 pm

Hello everyone,

I'm currently working on a project where I have a server written in Node.js that utilizes WebSocket functionality. However, when I connect my ESP32 device (running the Arduino code provided below) to this server, the ESP-NOW functionality stops working.
  1. #include <Arduino.h>
  2. #include <Wire.h>
  3. #include <WiFi.h>
  4. #include <esp_now.h>
  5. #include <WebSocketsClient.h>
  6. #include <ArduinoJson.h>
  7. #include <Adafruit_GFX.h>
  8. #include <Adafruit_SSD1306.h>
  9. #include <config.h>
  10.  
  11. IPAddress webSocketServer(WEBSOCKET_SERVER_IP1, WEBSOCKET_SERVER_IP2, WEBSOCKET_SERVER_IP3, WEBSOCKET_SERVER_IP4);
  12. int webSocketPort = WEBSOCKET_SERVER_PORT;
  13. const char * ssid = WIFI_SSID;
  14. const char * password = WIFI_PASSWORD;
  15.  
  16. WebSocketsClient wsClient;
  17.  
  18. const int analogIn = A0;
  19. int rawValue = 0;
  20. double voltage = 0;
  21. double rawSum = 0;
  22. int limit = 2000;
  23. int percentage = 0;
  24.  
  25. #define OLED_RESET 4
  26. Adafruit_SSD1306 display(OLED_RESET);
  27.  
  28. typedef struct struct_message {
  29.   int id;
  30.   int x;
  31.   int y;
  32. }
  33. struct_message;
  34.  
  35. struct_message myData;
  36. struct_message board1;
  37. struct_message boardsStruct[1] = {
  38.   board1
  39. };
  40.  
  41. void OnDataRecv(const uint8_t * mac_addr,
  42.   const uint8_t * incomingData, int len) {
  43.   char macStr[18];
  44.   Serial.print("Packet received from: ");
  45.   snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
  46.     mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  47.   Serial.println(macStr);
  48.   memcpy( & myData, incomingData, sizeof(myData));
  49.   Serial.printf("Board ID %u: %u bytes\n", myData.id, len);
  50.   boardsStruct[myData.id - 1].x = myData.x;
  51.   boardsStruct[myData.id - 1].y = myData.y;
  52.   Serial.printf("x value: %d \n", boardsStruct[myData.id - 1].x);
  53.   Serial.printf("y value: %d \n", boardsStruct[myData.id - 1].y);
  54.   Serial.println("Data received from ESP-NOW");
  55.  
  56. }
  57.  
  58. void displayValues() {
  59.   rawSum = analogRead(analogIn);
  60.  
  61.   for (int i = 0; i < 500; i++) {
  62.     rawSum += analogRead(analogIn);
  63.   }
  64.  
  65.   rawValue = rawSum / 500;
  66.   voltage = (rawValue / 4096.0) * 3300;
  67.   percentage = map(rawValue, 1400, 4096, 0, 100);
  68.  
  69.   display.setTextColor(WHITE);
  70.   display.setTextSize(1);
  71.   display.setCursor(0, 0);
  72.  
  73.   if (rawValue < limit) {
  74.     digitalWrite(LED_BUILTIN, LOW);
  75.     digitalWrite(18, LOW);
  76.     display.print("NORMAL");
  77.     display.setCursor(0, 10);
  78.     display.print("raw value:");
  79.     display.print(rawValue);
  80.     display.print("\n");
  81.     display.print("received value:");
  82.     display.print(boardsStruct[0].x);
  83.     display.print("\n");
  84.     display.print("Percentage: ");
  85.     display.print(percentage);
  86.     display.print("%");
  87.   } else {
  88.     digitalWrite(LED_BUILTIN, HIGH);
  89.     digitalWrite(18, HIGH);
  90.     display.print("LEAK");
  91.     display.setCursor(0, 10);
  92.     display.print("raw value:");
  93.     display.print(rawValue);
  94.     display.print("\n");
  95.     display.print("received value:");
  96.     display.print(boardsStruct[0].x);
  97.     display.print("\n");
  98.     display.print("Percentage: ");
  99.     display.print(percentage);
  100.     display.print("%");
  101.  
  102.   }
  103.  
  104.   display.display();
  105.   delay(300);
  106.   display.clearDisplay();
  107. }
  108.  
  109. void sendData() {
  110.  
  111.   StaticJsonDocument < 200 > jsonData;
  112.  
  113.   jsonData["deviceId"] = "3bf76280-6ca9-4d83-9ffb-db112de00c24";
  114.   jsonData["gasIntensity"] = percentage;
  115.   jsonData["zone"] = "43C72";
  116.  
  117.   String serializedData;
  118.   serializeJson(jsonData, serializedData);
  119.  
  120.   wsClient.sendTXT(serializedData);
  121.   Serial.println(serializedData);
  122.  
  123. }
  124.  
  125. void wsEvent(WStype_t type, uint8_t * payload, size_t length) {
  126.   if (type == WStype_DISCONNECTED) {
  127.     Serial.println("Disconnected from WebSocket server.");
  128.   } else if (type == WStype_CONNECTED) {
  129.     Serial.println("Connected to WebSocket server.");
  130.   } else if (type == WStype_TEXT) {
  131.     Serial.println("Received data from WebSocket server:");
  132.     Serial.println((char * ) payload);
  133.   }
  134. }
  135.  
  136. void setup() {
  137.   pinMode(LED_BUILTIN, OUTPUT);
  138.   pinMode(18, OUTPUT);
  139.  
  140.   Wire.begin();
  141.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  142.  
  143.   Serial.begin(115200);
  144.  
  145.   WiFi.mode(WIFI_AP_STA);
  146.  
  147.   WiFi.begin(ssid, password);
  148.   while (WiFi.status() != WL_CONNECTED) {
  149.     delay(1000);
  150.     Serial.println("Connecting to WiFi...");
  151.   }
  152.  
  153.   if (esp_now_init() != ESP_OK) {
  154.     Serial.println("Error initializing ESP-NOW");
  155.     return;
  156.   }
  157.  
  158.   esp_now_register_recv_cb(OnDataRecv);
  159.  
  160.   delay(300);
  161.   WiFi.begin(ssid, password);
  162.  
  163.   wsClient.begin(webSocketServer, webSocketPort);
  164.   wsClient.onEvent(wsEvent);
  165.   Serial.println("WebSocket client started...");
  166.  
  167. }
  168.  
  169. void loop() {
  170.   displayValues();
  171.   delay(300);
  172.   wsClient.loop();
  173.   static unsigned long lastSent = 0;
  174.   unsigned long now = millis();
  175.  
  176.   if (wsClient.isConnected() && now - lastSent >= 2000) {
  177.     sendData();
  178.  
  179.   }
Problem:

The ESP32 device is designed to communicate using ESP-NOW, which works perfectly when not connected to the WebSocket server.

Upon connecting to the WebSocket server using the WebSocketsClient library, the ESP-NOW functionality seems to cease, and no data is received through ESP-NOW.

Expected Behavior:

The ESP-NOW functionality should continue to work seamlessly even when the WebSocket connection is established.

Additional Information:

The WebSocket server is implemented in Node.js.

The WebSocket library used on the ESP32 side is WebSocketsClient

The ESP-NOW functionality is used for communication between multiple ESP32 devices.

Possible Causes:

The WebSocket library may be interfering with ESP-NOW operations.

Attempts to Resolve:

I have checked for any conflicts in network configurations.

I've tried adjusting the order of initialization for ESP-NOW and WebSocket.

I appreciate any insights or suggestions on how to troubleshoot and resolve this issue. If you need additional details, feel free to ask.

Thank you in advance!

Who is online

Users browsing this forum: No registered users and 25 guests