timum-viw / socket.io-client

A socket.io-client implementation for ESP8266 and Arduino
228 stars 90 forks source link

Why when I try to connect my ESP8266 to my node sever in my localhost it keep getting disconnect and connect? #99

Closed Bunvath closed 3 years ago

Bunvath commented 3 years ago

I think the issue is within the library because I have try to implement this code according to the tutorial that I found in YouTube Youtube. The discussion of this tutorial is the first they implement the node js server for listening to specific event from socket io, second part they build a simple iOS application using swift to control an RGB light on the ESP8266 and the last part they make ESP8266 to listen to the event that have payload as RGB values. However, in my implementation, I use angular web application to control ON-OFF light on the Arduino but the problem when I try to listen to event that on and off, my ESP8266 don't connect to my server and print all of that message on the serial.

#include <SocketIoClient.h>
#include<ESP8266WiFi.h>
#include<Arduino.h>
#define USER_SERIAL Serial 

SocketIoClient socket;
const char* ssid = "bunvath_guest";
const char* pass = "*************";

void setup() {
  USER_SERIAL.begin(115200);
  searchWifi();
  initWiFi();
  socket.on("message", handleMessage);
  socket.on("light_on",handleMessage);
  socket.emit("message","hello from esp");
  socket.begin("192.168.0.102",3000);
}

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

//For handle the message
void handleMessage(const char* message, size_t length){
  USER_SERIAL.println(message);
}

//For establish connection to the WiFi network.
  void initWiFi(){
      WiFi.mode(WIFI_STA);
      WiFi.hostname("ESP-host");
      WiFi.setPhyMode(WIFI_PHY_MODE_11G);
      WiFi.enableInsecureWEP(true);
      WiFi.begin(ssid,pass);
      while(WiFi.status() != WL_CONNECTED){
        USER_SERIAL.print(".");
        delay(1000);
      }
      USER_SERIAL.print("");
      USER_SERIAL.println("WiFi connected");
      USER_SERIAL.print("IP Address: ");
      USER_SERIAL.println(WiFi.localIP());
      WiFi.setAutoReconnect(true);
      WiFi.persistent(true);

  }

//Searching for WiFi signal
 void searchWifi(){
  int numberOfNetwork = WiFi.scanNetworks();
  USER_SERIAL.println("-----");
  for ( int i = 0; i< numberOfNetwork;i++){
    USER_SERIAL.print("Network name: ");
    USER_SERIAL.println(WiFi.SSID(i));
    USER_SERIAL.print("Signal Strength: ");
    USER_SERIAL.println(WiFi.RSSI(i));
    USER_SERIAL.println("-------------");
  }
}

Here is my node server code for receiving all connection from client and print the client id when new client is connecting. Moreover, this node server will listen to all the event that client emit and broadcast to other client.

var port = process.env.PORT||3000
const http = require('http')
const app = require('./app')

const cors = require('cors');
let message = ''
const server = http.createServer(app)
const io = require('socket.io')(server,{cors:{
    origin:'http://localhost:4200',
    credentials: true
}}
    )

server.listen(port,()=>{
    console.log("server run on ", port)
})

io.on("connection",(socket)=>{
    console.log(socket.id)
    socket.on('message', data =>{
        // console.log(data)
        socket.broadcast.emit('message',data)
    })
    socket.on('open_light',data=>{
        socket.broadcast.emit('open_light',data)
        console.log(data)
    })

})

Here is the message that I got from serial monitor, I feel like my Arduino has already connected to my server but it keep getting reconnect. Moreover, in my sever I did not see any new client connect to server so I am really what going on with this. Plus, I am sure that my node server is working perfectly fine because I have implement angular web application to emit event, and I saw that event printing in server.

12:57:13.083 -> Network name: Meas Nakry
12:57:13.083 -> Signal Strength: -85
12:57:13.083 -> -------------
12:57:13.083 -> Network name: bunvath_guest
12:57:13.083 -> Signal Strength: -57
12:57:13.083 -> -------------
12:57:13.083 -> Network name: BLaNKxD
12:57:13.083 -> Signal Strength: -89
12:57:13.121 -> -------------
12:57:13.121 -> Network name: Tuy vanny1
12:57:13.121 -> Signal Strength: -85
12:57:13.121 -> -------------
12:57:13.121 -> ....WiFi connected
12:57:17.165 -> IP Address: 192.168.0.100
12:57:17.165 -> [SIoC] add packet 42["message",hello from esp]
12:57:17.269 -> [SIoC] Disconnected!
12:57:17.269 -> [SIoC] event disconnected not found. 2 events available
12:57:17.787 -> [SIoC] Disconnected!
12:57:17.787 -> [SIoC] event disconnected not found. 2 events available
12:57:18.376 -> [SIoC] Disconnected!
12:57:18.376 -> [SIoC] event disconnected not found. 2 events available
12:57:18.992 -> [SIoC] Disconnected!
12:57:18.992 -> [SIoC] event disconnected not found. 2 events available
12:57:19.633 -> [SIoC] Disconnected!
12:57:19.633 -> [SIoC] event disconnected not found. 2 events available
12:57:20.217 -> [SIoC] Connected to url: /socket.io/?transport=websocket    //Is this really a connection?
12:57:20.217 -> [SIoC] packet "42["message",hello from esp]" emitted
12:57:20.251 -> [SIoC] Disconnected!                                                                  // It keep getting disconnect like that.
12:57:20.251 -> [SIoC] event disconnected not found. 2 events available
12:57:20.771 -> [SIoC] Connected to url: /socket.io/?transport=websocket
12:57:20.771 -> [SIoC] Disconnected!
12:57:20.771 -> [SIoC] event disconnected not found. 2 events available
12:57:21.435 -> [SIoC] Disconnected!
12:57:21.435 -> [SIoC] event disconnected not found. 2 events available
12:57:22.044 -> [SIoC] Disconnected!
12:57:22.044 -> [SIoC] event disconnected not found. 2 events available
12:57:22.596 -> [SIoC] Connected to url: /socket.io/?transport=websocket
12:57:22.596 -> [SIoC] Disconnected!
12:57:22.596 -> [SIoC] event disconnected not found. 2 events available
12:57:23.178 -> [SIoC] Connected to url: /socket.io/?transport=websocket
12:57:23.178 -> [SIoC] Disconnected!
12:57:23.178 -> [SIoC] event disconnected not found. 2 events available
12:57:23.798 -> [SIoC] Disconnected!
12:57:23.798 -> [SIoC] event disconnected not found. 2 events available
12:57:24.403 -> [SIoC] Disconnected!
12:57:24.403 -> [SIoC] event disconnected not found. 2 events available
12:57:24.945 -> [SIoC] Disconnected!
12:57:24.945 -> [SIoC] event disconnected not found. 2 events available
12:57:25.533 -> [SIoC] Disconnected!
12:57:25.533 -> [SIoC] event disconnected not found. 2 events available
12:57:26.201 -> [SIoC] Connected to url: /socket.io/?transport=websocket
12:57:26.201 -> [SIoC] Disconnected!
12:57:26.201 -> [SIoC] event disconnected not found. 2 events available
12:57:26.792 -> [SIoC] Disconnected!
12:57:26.792 -> [SIoC] event disconnected not found. 2 events available
12:57:27.380 -> [SIoC] Disconnected!
12:57:27.380 -> [SIoC] event disconnected not found. 2 events available
12:57:27.894 -> [SIoC] Disconnected!
12:57:27.894 -> [SIoC] event disconnected not found. 2 events available
12:57:28.711 -> [SIoC] Disconnected!
12:57:28.711 -> [SIoC] event disconnected not found. 2 events available
12:57:29.426 -> [SIoC] Disconnected!
12:57:29.426 -> [SIoC] event disconnected not found. 2 events available
12:57:30.131 -> [SIoC] Disconnected!
12:57:30.131 -> [SIoC] event disconnected not found. 2 events available
12:57:30.655 -> [SIoC] Disconnected!
12:57:30.655 -> [SIoC] event disconnected not found. 2 events available
12:57:31.254 -> [SIoC] Disconnected!
12:57:31.254 -> [SIoC] event disconnected not found. 2 events available
12:57:31.787 -> [SIoC] Disconnected!
12:57:31.787 -> [SIoC] event disconnected not found. 2 events available
12:57:32.375 -> [SIoC] Disconnected!
12:57:32.375 -> [SIoC] event disconnected not found. 2 events available
12:57:33.171 -> [SIoC] Disconnected!
12:57:33.171 -> [SIoC] event disconnected not found. 2 events available
12:57:33.731 -> [SIoC] Disconnected!
12:57:33.731 -> [SIoC] event disconnected not found. 2 events available
12:57:34.435 -> [SIoC] Connected to url: /socket.io/?transport=websocket
12:57:34.435 -> [SIoC] Disconnected!
12:57:34.435 -> [SIoC] event disconnected not found. 2 events available
12:57:34.978 -> [SIoC] Connected to url: /socket.io/?transport=websocket
12:57:34.978 -> [SIoC] Disconnected!
12:57:34.978 -> [SIoC] event disconnected not found. 2 events available
12:57:35.581 -> [SIoC] Disconnected!
12:57:35.581 -> [SIoC] event disconnected not found. 2 events available
12:57:36.180 -> [SIoC] Disconnected!
12:57:36.180 -> [SIoC] event disconnected not found. 2 events available. 
PasTrik commented 2 years ago

Update ?