Bhupesh Varshney
Bhupesh Varshney

Reputation: 1660

socketio server not connecting to ESP8266

I am facing problems while connecting to my SocketIo server through my ESP8266. I have my ESP connected to my Wifi and my Node Server is running on localhost. I have the following code in ESP8266

#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <SocketIoClient.h>


//192.168.1.37 --My IP Address

SocketIoClient webSocket;

const char* ssid     = "ssid";      // SSID
const char* password = "pass";        // Password
const char* host = "192.168.1.37";        // Server IP (localhost)
const int   port = 8080;                  // Server Port
const char* url = "http://localhost:8080/test";

void event(const char * payload, size_t length) {
  Serial.println("Message");
}

void setup() {
  Serial.begin(115200);
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);     // 5ms Delay
    Serial.print(".");

  }

  Serial.print("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println("Connecting To Socket");
  webSocket.on("event", event);
  webSocket.begin("192.168.1.37", 8080);

}

void loop() {

  if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
     delay(3000);    //Send a request every 3 seconds  
     webSocket.loop();
     Serial.println("Retrying ....");
}

Also my Server side code is in JavaScript.

var io = socket(server);
io.on('connection', (socket) => {
    console.log('made socket connection', socket.id);
    socket.on("ESP", function(data){
        socket.emit('Hi ESP, ESP called', data);
        console.log("Socket Working !");
    });
    console.log('made socket connection', socket.id);
    socket.on("connect", function(data){
        socket.emit('Hi ESP connect called', data);
        console.log("Socket Working !");
    });

});

But i keep on receiving this output in the Serial Monitor. Also there is no request received on my server.

11:37:09.729 -> ..........WiFi connected
11:37:14.129 -> IP address: 
11:37:14.129 -> 192.168.1.13
11:37:14.129 -> Connecting To Socket
11:37:17.245 -> Retrying ....
11:37:17.245 -> [SIoC] Disconnected!
11:37:17.245 -> [SIoC] event disconnected not found. 1 events available
11:37:20.230 -> Retrying ....
11:37:20.230 -> [SIoC] Disconnected!
11:37:20.230 -> [SIoC] event disconnected not found. 1 events available

Upvotes: 1

Views: 2165

Answers (3)

Sajad Mga
Sajad Mga

Reputation: 31

i have same issue

[SIoC] Disconnected!
[SIoC] Connected to url: /socket.io/?transport=websocket
[SIoC] Disconnected!
[SIoC] Disconnected!
[SIoC] Disconnected!

i change server side socket io config to this :

const io = require("socket.io")(server, {
cors: {
 origin: "*",
 methods: ["GET", "POST"],
 transports: ["websocket", "polling"],
 credentials: true,
},
 allowEIO3: true,
});

and Fix !

Upvotes: 3

HưngDIY C&#244;ng
HưngDIY C&#244;ng

Reputation: 1

I had the same issue and changed:

WiFi.begin(ssid, password);

to

WiFiMulti.addAP(ssid, pass);

and it connects fine.

PS: I used <SocketIOclient.h>

Upvotes: 0

Sricharan Donkada
Sricharan Donkada

Reputation: 1

I have faced the same issue. It is fixed when I changed the socket.io version to ^2.3.0 Change the socket io version to ^2.3.0 in package.json, delete node modules and reinstall the packages using npm install command

Upvotes: 0

Related Questions