I'm actually working on project with esp32 and firebase real time database where I store the temperature and the humidity and the led state, I have control over the led with an app when I press on it changes led value to 1 and when off to 0
What I want is that whenever I press a button the logic of the LED executes immediately something like setting up a listener to LED
here is the structure of my firebase { "LED": 0, "humidity": 34, "temp": 23.4 }
and here is my logic for the esp 32
#include <ESP32Firebase.h>
#include <WiFi.h>
#include <DHT.h>
#include <ArduinoJson.h>
#define DHTPIN 4
#define DHTTYPE DHT11
#define _SSID "wifi" // Your WiFi SSID
#define _PASSWORD "code" // Your WiFi Password
#define REFERENCE_URL "url"
const int ledPin = 5;
Firebase firebase(REFERENCE_URL);
DHT dht(DHTPIN, DHTTYPE);
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
dht.begin();
WiFi.mode(WIFI_STA);
WiFi.disconnect();
// Connect to WiFi
Serial.print("Connecting to: ");
Serial.println("Wifi1811");
WiFi.begin(_SSID, _PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("-");
}
Serial.println("");
Serial.println("WiFi Connected");
}
void loop() {
firebase.json(true);
String data = firebase.getString("HouseInfos");
Serial.println(data);
StaticJsonDocument<96> doc;
deserializeJson(doc, data);
float temp = doc["temp"];
float humid = doc["humidity"];
int LED = doc["LED"];
if (LED == 0) {
digitalWrite(ledPin, LOW);
} else if (LED == 1) {
digitalWrite(ledPin, HIGH);
}
readSensor();
}
void readSensor() {
float humidity = dht.readHumidity(); // Read humidity
float temperature = dht.readTemperature();
firebase.setFloat("HouseInfos/humidity", humidity);
firebase.setFloat("HouseInfos/temp", temperature);
Serial.println("data written");
}