Cannot connect esp8266 to my deployed socket.io (nodejs) server

im trying to connect my esp8266 to my deployed socket.io server on render but it keep connecting and disconnecting , i tried nmap to check if i have the correct port number i get this “10000/tcp open snet-sensor-mgmt” and sometimes i get it filtered , does that cause a problem so it prevent esp8266 from connecting ? i tried to make react app and it works fine with socket.io
and this is my code

const express = require('express');
const http = require('http');
const cors = require('cors');
const { Server } = require('socket.io');

const app = express();

app.use(cors({
  origin: '*'
}));
const server = http.createServer(app);
const port = process.env.PORT || 5000;
server.listen(port, () => {
  console.log(`Server listening on port ${port}`);
});
const io = new Server(server, {
  cors: {
   origin: "*",
   methods: ["GET", "POST"],
   transports: ["websocket", "polling"],
   credentials: true,
  },
   allowEIO3: true,
  });

let brightness = 0;
io.on('connection', (socket) => {
  console.log('New WebSocket connection');
  socket.emit('brightness', brightness);
  socket.on('brightness', (newBrightness) => {
    brightness = newBrightness;
    console.log('Brightness updated:', brightness);
    io.emit('brightness', brightness);
  });
});

Hi there,

From my understanding the esp8266 won’t use HTTPS (WSS) by default, which is required for connecting to your Render service, as we redirect all plain HTTP requests to HTTPS. Have you configured your board so it will use HTTPS to make the connection?

Regards,

Keith
Render Support, UTC+10 :australia:

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsClient.h>
#include <Hash.h>

ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;

#define USE_SERIAL Serial1

void webSocketEvent(WStype_t type, uint8_t *payload, size_t length) {
  switch (type) {
    case WStype_DISCONNECTED:
      Serial.println("[WSc] Disconnected!");
      break;
    case WStype_CONNECTED:
      {
        Serial.println("[WSc] Connected to url: " + String((char *)payload));

        // send message to server when Connected
        webSocket.sendTXT("Connected");
      }
      break;
    case WStype_TEXT:
      Serial.println("[WSc] get text: " + String((char *)payload));

      // send message to server
      // webSocket.sendTXT("message here");
      break;
    case WStype_BIN:
      Serial.println("[WSc] get binary length: " + String(length));
      hexdump(payload, length);

      // send data to server
      // webSocket.sendBIN(payload, length);
      break;
  }
}

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.println();
  Serial.println();
  Serial.println("starting");

  for (uint8_t t = 4; t > 0; t--) {
    Serial.printf("[SETUP] BOOT WAIT %d...\n", t);
    Serial.flush();
    delay(1000);
  }

  WiFiMulti.addAP("ssid","12345678");

  while (WiFiMulti.run() != WL_CONNECTED) {
    Serial.println("not connected");
    delay(100);
  }

  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("Connected to the internet.");
  } else {
    Serial.println("Not connected to the internet.");
  }

  webSocket.beginSocketIOSSL("url",443,"/socket.io/?EIO=3","arduino");
 

  webSocket.onEvent(webSocketEvent);
}

void loop() {
  webSocket.loop();
}

im using this code , but the esp8266 keeps connecting and disconnecting

Hi there,

Take a look at this blog post, it may help using WebSockets with SSL: https://codigoparallevar.com/blog/2019/secure-websockets-esp8266-arduino/

Regards,

Keith
Render Support, UTC+10 :australia:

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