#include <WiFi.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const int LED_PIN = 4;
const String fbURL = "https://led-controller-8a61d-default-rtdb.firebaseio.com/led.json?auth=a0070SrfgMuF9xfo5KTJa62FklguFtUBNaYlMtUb";
// We keep these global so they don't get deleted and recreated every loop
WiFiClientSecure client;
HTTPClient http;
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
WiFi.begin(ssid, password, 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println("\nConnected!");
client.setInsecure(); // Skip SSL certificate check for speed
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
// Instead of http.begin every time, we just call it once
// and use the keep-alive nature of HTTP 1.1
http.begin(client, fbURL);
int code = http.GET();
if (code == 200) {
String payload = http.getString();
// Fast cleaning of the data
payload.replace("\"", "");
payload.trim();
if (payload == "on") {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
}
// IMPORTANT: We do NOT call http.end() here.
// Keeping it open is what makes it "Instant."
}
// 100ms is enough to feel instant but not crash the ESP32
delay(100);
}