Malla de micros ESP* y sensores

Hola, estoy usando la libreria painlessmesh.h para hacer una malla de micros.
Entre los sensores conectados tengo un sensor ldr con salida digital.
El caso es que cuando tapo el ldr se detecta esta accion pero cuando los destapo no detecta que se ha expuesto a la luz de nuevo.
Se que algo no estoy haciendo bien pero no se que es.
Os adjunto el codigo por si me podeis ayudar.
Estoy utilizando un ESP-01S y el sensor va conectado al GPIO 0.
El codigo es el siguiente:

#include "painlessMesh.h"
// WiFi Credentials
#define   MESH_PREFIX     "nodo1"
#define   MESH_PASSWORD   "somethingSneaky"
#define   MESH_PORT       5555
//Pin Declaration
#define LUZ 0 // relay connected to  GPIO0
// const int LUZ = 0;
 int lluz = 0;
 String lluzo;
//Variables
Scheduler userScheduler; 
painlessMesh  mesh;
String estado ="LUZ";
void sendMessage() ;
Task taskSendMessage( TASK_SECOND * 1 , TASK_FOREVER, &sendMessage );
void sendMessage()
{
  DynamicJsonDocument doc(1024);
  doc["QUIEN"] = "LUZ";
  doc["QUE"] = lluzo; 
  doc["AQUIEN"] = "Maestro";
  String msg ;
  serializeJson(doc, msg);
  mesh.sendBroadcast( msg );
  taskSendMessage.setInterval((TASK_SECOND * 1));
}
// Needed for painless library

void receivedCallback( uint32_t from, String &msg ) {
  String json;
  DynamicJsonDocument doc(1024);
  json = msg.c_str();
  DeserializationError error = deserializeJson(doc, json);
// si recibo de maestro copruebo lo que pide
 String a = doc["QUIEN"];
 String b = doc["QUE"];
 String c = doc["AQUIEN"];
  if (error)
  {
    Serial.print("deserializeJson() failed: ");
    Serial.println(error.c_str());
  }

}
void newConnectionCallback(uint32_t nodeId) {
  Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId);
}
void changedConnectionCallback() {
  Serial.printf("Changed connections\n");
}
void nodeTimeAdjustedCallback(int32_t offset) {
  Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(), offset);
}
void setup() {
  Serial.begin(115200);
  pinMode(LUZ, INPUT);
  mesh.setDebugMsgTypes( ERROR | STARTUP );  // set before init() so that you can see startup messages
  mesh.init( MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT ,WIFI_AP_STA, 6  );
  mesh.onReceive(&receivedCallback);
  mesh.onNewConnection(&newConnectionCallback);
  mesh.onChangedConnections(&changedConnectionCallback);
  mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);
  userScheduler.addTask( taskSendMessage );
  taskSendMessage.enable();
}
void loop() {
  // it will run the user scheduler as well
  mesh.update();
  if(digitalRead(LUZ) == LOW){
    lluzo = "off";
    }else if(digitalRead(LUZ) == HIGH){
    lluzo = "on";
  };
}

Gracias y un saludo.
Javier

No encadenes los if , piensalo bien, como lo tienes es indetectable el HIGH. Mejor asi:

void loop() {
  // it will run the user scheduler as well
  mesh.update();
  if(digitalRead(LUZ) == LOW){lluzo = "off";}
  if(digitalRead(LUZ) == HIGH){lluzo = "on";}
}

O tambien:

void loop() {
  // it will run the user scheduler as well
  mesh.update();
  if (digitalRead(LUZ) == LOW) {
    lluzo = "off";
  }
  else {
    lluzo = "on";
  }//; Este ; sobra.
}

Todas son formas posibles.
A mi no me gusta que algo este enviando estados en cada ciclo del loop si no esta pasando algo. Solo espero recibir cambios de estado.

int estado, estadoAnt = LOW;

/*
Todo lo demas
*/
void loop() {
  // it will run the user scheduler as well
  mesh.update();
  estado = digitalRead(LUZ);
  if (estado != estadoAnt) {
      lluzo = estado?"on":"off";
  }
  estadoAnt = estado;
}

Buenas tardes.
Solucionado, muchas gracias a todos por vuestra ayuda.

Un saludo.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.