Problems with ESP8266

Good evening, I have a problem with my esp8266-01, I'm working with socket.io, create a server under windows in node.js, it works great, I manage to turn on a led and turn it off from the server node.js, I also made a PIR sensor will send me alarms without problems, the problem is that yesterday I went to a local linux Debian server and there it does not work, the server appears connected but does not turn off or turn on the led, also connects many times, attached the image of the server, the code js and the arduino code, thank you very much for your help

I only have the esp8266 connected, all those connections and disconnections are from the esp8266

this is my code in arduino ide

#include <SocketIOClient.h>
#define LedPin 2
#define pirPin 0
// VARIABLE LED
unsigned long tiempo;
unsigned long t=0;
int Dt = 5000;
unsigned long tiempo2;
unsigned long t2=0;
int Dt2 = 3000;
int ledState;
int previousReading = LOW;
const char* ssid = "ERNESTO";
const char* password = "delta098";

String host = "192.168.0.17";
int port = 3000;
//bool clicked = false;
SocketIOClient socket;
void setupNetwork() {  
    WiFi.begin(ssid, password);
    uint8_t i = 0;
    while (WiFi.status() != WL_CONNECTED && i++ < 20) delay(2000);
    if(i == 21){
      while(1) delay(2000);
    }
}

void light(String state) {
  Serial.println("[light] " + state);
  if (state == "\"state\":true") {
    Serial.println("[light] ON");
    digitalWrite(LedPin, HIGH);
    ledState = 1;    
  }
  else {
    Serial.println("[light] off");
    digitalWrite(LedPin, LOW);
    ledState = 0;    
  }
}
void pir(){
  if (digitalRead(pirPin) == HIGH){
    socket.emit("pir", "{\"state\":true}");
    Serial.println("[pir] on");
  }
}
//
// This code runs only once
//
void setup() {
  // set up our pins
  pinMode(LedPin, OUTPUT);
  pinMode(pirPin, INPUT);
  digitalWrite(LedPin, LOW);
  Serial.begin(115200);
  setupNetwork();
  socket.on("light", light);
  socket.connect(host, port);
}

//
// This code runs over and over again
//
void loop() {
  socket.monitor();
  tiempo = millis();
  if (tiempo-t > Dt){
    t = tiempo;
    if (ledState == 1){
      socket.emit("led", "{\"state\":true}");
    }else{
      socket.emit("led", "{\"state\":false}");
    }
  }
  int reading = digitalRead(pirPin);
  if (previousReading == LOW && reading == HIGH) {
    socket.emit("pir", "{\"state\":true}");
  }
  previousReading = reading;
  
}

and this is my code in node.js

var express = require('express');
var http = require('http');
var socket = require('socket.io');
//var light = {state:false};
var app = express();
var server = http.createServer(app);
var io = socket.listen(server);
app.use(express.static('public'));
app.use(express.static('node_modules'));
app.get('*', function(req, res) {
  res.sendFile(__dirname + '/views/index.html');
});

io.on('connection', function(socket) {
  console.log('User connected: ' + socket.id);
  //socket.emit('light', light);
  socket.on('disconnect', function(){
    console.log('User disconnected: ' + socket.id);
  });
  socket.on('toggle', function(state) {
    //light.state = !light.state;
    var light = state
    //console.log(light)
    //console.log('id: ' + socket.id + ' light: ' + light.state);
    io.emit('light', light);
  });
  socket.on('led', function(data){
    io.emit('ledEst', data);
  })
  socket.on('pir', function(data){
    if (data.state==true) {
      io.emit('pirEst', data);
      //console.log(data)
    }
  })
});

server.listen(3000, function() {
  console.log('listening on *:3000');
});

Weird that it works on Windows but not on your Raspberry Pi..

Can you tell us which version of the SocketIOClient library you are using? I found several forks on Github with slight differences.

Maybe the Raspberry Pi is slower to respond to socket.connect() so socket.monitor() is re-trying the connection several times. This would account for the multiple connections you see in your node.js logs. A simple test might be to add a delay() at the end of setup() to give things time to get connected before starting your loop.

thank you very much for answering, this is the link I use to guide me and download the libraries

regarding the delay I see it very possible so I left a delay of 2000 in the connection but still does not work, if I have done well? thanks again

Sorry to hear that the delay did not work. Perhaps the added code below might help instead?

void setup() {
  //etc etc
  socket.connect(host, port);
  while (! socket.connected()) {
    delay(1000);
  };
};

Do you have a serial monitor hooked up to the ESP8266? That might give you more infomation about what is happening.

thanks rw950431, i insert your code,

#include <SocketIOClient.h>
#define LedPin 2
#define pirPin 0
// VARIABLE LED
unsigned long tiempo;
unsigned long t=0;
int Dt = 5000;
unsigned long tiempo2;
unsigned long t2=0;
int Dt2 = 3000;
int ledState;
int previousReading = LOW;
const char* ssid = "ERNESTO";
const char* password = "delta098";

String host = "192.168.0.17";
int port = 3000;
//bool clicked = false;
SocketIOClient socket;
void setupNetwork() {  
    WiFi.begin(ssid, password);
    uint8_t i = 0;
    while (WiFi.status() != WL_CONNECTED && i++ < 20) delay(2000);
    if(i == 21){
      while(1) delay(2000);
    }
}

void light(String state) {
  Serial.println("[light] " + state);
  if (state == "\"state\":true") {
    Serial.println("[light] ON");
    digitalWrite(LedPin, HIGH);
    ledState = 1;    
  }
  else {
    Serial.println("[light] off");
    digitalWrite(LedPin, LOW);
    ledState = 0;    
  }
}
void pir(){
  if (digitalRead(pirPin) == HIGH){
    socket.emit("pir", "{\"state\":true}");
    Serial.println("[pir] on");
  }
}
//
// This code runs only once
//
void setup() {
  // set up our pins
  pinMode(LedPin, OUTPUT);
  pinMode(pirPin, INPUT);
  digitalWrite(LedPin, LOW);
  Serial.begin(115200);
  setupNetwork();
  socket.on("light", light);
  socket.connect(host, port);
  while (! socket.connected()) {
    delay(1000);
  };
}

//
// This code runs over and over again
//
void loop() {
  socket.monitor();
  tiempo = millis();
  if (tiempo-t > Dt){
    t = tiempo;
    if (ledState == 1){
      socket.emit("led", "{\"state\":true}");
    }else{
      socket.emit("led", "{\"state\":false}");
    }
  }
  int reading = digitalRead(pirPin);
  if (previousReading == LOW && reading == HIGH) {
    socket.emit("pir", "{\"state\":true}");
    //digitalWrite(LedPin, HIGH);
  }
  previousReading = reading;
 
}

on the server appears connected after about 10 seconds is disconnected, does not reconnect and in the com screen appears:

[readHandshake] Connected. SID=HaCvKq80q8rvhYxoAAAE
[readHandshake] Connecting via Websocket

and nothing else happens

With the old code, the com screen show:

[readHandshake] Connected. SID=H8S6fRfy3sf2l1K9AAAF
[readHandshake] Connecting via Websocket
[monitor] Client not connected.
[readHandshake] Connected. SID=4qXa6A0l1nBt6dDHAAAG
[readHandshake] Connecting via Websocket
[monitor] Can't connect. Aborting.
[monitor] Client not connected.
[readHandshake] Connected. SID=eP88mPbqarOQ9vdVAAAH
[readHandshake] Connecting via Websocket

Which suggests that the client never actually fully connects. Do you have any logs from the nodeJS process on the RPi at that stage?

What does the COM output look like when its connected to the windows version?

good afternoon, thanks for answering, i solve part of the problem, I do not have a raspberry then emule the software in virtualbox, as it did not work with the solution, I thought the problem could be the emulated system "raspbian" decided to install a ubuntu mate and there if everything worked perfectly, I think it could be user restrictions, I have a hosting where they have enabled node.js that does not work either, they must restrict access to third parties, I would believe, but anyway, it worked and hopefully when I have the raspberry work, I think I will install in it ubuntu mate too, thank you very much for your help, help me a lot, greetings from Colombia

Perhaps its a problem with the firewall not allowing the right ports through?

hello i need a help please

for the code arduino :

i have a error that they not have method socket.emit() socket.on in class socketioClient

i change the librairie many time , but no have idea , to let this code work

netico14 can you please tell me , how the code work with you

thanks

Hello, this problem is the library, chose another library maybe yo can use this, i do simple-socket-io-example/README.md at master · robojay/simple-socket-io-example · GitHub