arduinoWebSockets
arduinoWebSockets copied to clipboard
ESP8266 not connecting to SocketIO server
Please help me, I donot know where I am going wrong I have provided the server and client side code everything seems to be fine, but dont know where is the problem
///////////////////////*************************** SERVER CODE ON NODEJS ******************************* /////////////////////** var PORT = process.env.PORT || 5000; var express = require('express'); var app = express(); var http = require('http'); var server = http.Server(app);
app.get('/', (req, res) => { res.send('
Hello world
'); });server.listen(PORT, function () { console.log('listening on *:' + PORT); } );
var io = require('socket.io')(server);
io.on('connect', function (socket) {
console.log('a user connected');
socket.on('message', function (data) {
console.log(data);
io.emit('message', data);
});
});
**/////// ************************CLIENT CODE ON ESP32 ********************************/////////////
#include <SocketIoClient.h> #include <ArduinoJson.h> #include <Arduino.h> #include <ESP8266WiFi.h>
#define USER_SERIAL Serial
const char* ssid = "Net"; const char* pass = "123123123";
SocketIoClient webSocket;
void setup() {
USER_SERIAL.begin(115200);
searchWiFi(); connectWiFi();
// webSocket.begin("192.168.1.106", 5000); webSocket.begin("192.168.1.106", 5000, "/socket.io/?EIO=4"); webSocket.on("message", controlled); }
void loop() { webSocket.loop();
}
void controlled(const char* message, size_t length){ // USER_SERIAL.println(message);
DynamicJsonDocument doc(1024); deserializeJson(doc, message);
double r = doc["r"]; double g = doc["g"]; double b = doc["b"];
}
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("--------------"); } }
void connectWiFi(){ 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());
}
//////**************************** ERROR **************************/////
11:36:16.972 -> Network name: Net 11:36:16.972 -> Signal strength: -62 11:36:17.015 -> -------------- 11:36:17.015 -> ......WiFi connected 11:36:23.360 -> IP Address : 192.168.1.105 11:36:23.360 -> [SIoC] Disconnected! 11:36:23.866 -> [SIoC] Disconnected! 11:36:24.399 -> [SIoC] Disconnected! 11:36:24.909 -> [SIoC] Disconnected! 11:36:25.442 -> [SIoC] Disconnected! 11:36:25.942 -> [SIoC] Disconnected! 11:36:26.425 -> [SIoC] Disconnected!
Try using EIO=3
instead of EIO=4
.
webSocket.begin("192.168.1.106", 5000, "/socket.io/?EIO=3");