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');
});