#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const String firebaseURL = "https://esp32-wokwi-demo-default-rtdb.firebaseio.com/led_status.json";
void setup() {
Serial.begin(115200);
pinMode(2, OUTPUT);
digitalWrite(2, LOW);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" Connected!");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(firebaseURL);
int httpCode = http.GET();
if (httpCode == 200) {
String payload = http.getString();
int status = payload.toInt();
digitalWrite(2, status);
Serial.println("LED: " + String(status));
} else {
Serial.println("Error reading Firebase");
}
http.end();
}
delay(2000); // 2 giây đọc 1 lần
}