// Wokwi-Compatible ESP32 Code dengan HTTP Request ke Firebase REST API
// Author: AFU ICHSAN PRADANA
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <DHTesp.h>
// WiFi Configuration
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// Firebase Configuration (ganti dengan project Anda)
#define FIREBASE_HOST "https://led-11.firebaseio.com/"
#define FIREBASE_AUTH "qO7zxYbz4sYxtmuXzNJay4IjMmNcU2DPfDwSu3Js"
// Pin Configuration sesuai diagram Wokwi
const int DHT_PIN = 13; // DHT22 SDA pin
#define LED_PIN 14 // LED pin (melalui resistor 1kΩ)
// Objects
DHTesp dhtSensor;
HTTPClient http;
// Variables
int kondisiLED = 0;
unsigned long lastSensorUpdate = 0;
unsigned long lastControlCheck = 0;
const unsigned long SENSOR_INTERVAL = 5000; // 5 detik
const unsigned long CONTROL_INTERVAL = 2000; // 2 detik
void setup() {
Serial.begin(115200);
Serial.println("=== Wokwi ESP32 Firebase REST API ===");
Serial.println("Author: AFU ICHSAN PRADANA");
Serial.println("=====================================");
// Setup pins
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
Serial.println("✓ DHT22 (Pin 13) & LED (Pin 14) initialized");
// Connect to WiFi
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("✓ Connected! IP: ");
Serial.println(WiFi.localIP());
Serial.println("=====================================");
}
void loop() {
// Update sensor data
if (millis() - lastSensorUpdate > SENSOR_INTERVAL) {
lastSensorUpdate = millis();
updateSensorData();
}
// Check LED control
if (millis() - lastControlCheck > CONTROL_INTERVAL) {
lastControlCheck = millis();
checkLEDControl();
}
delay(100);
}
// Fungsi untuk mengirim data sensor ke Firebase via REST API
void updateSensorData() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
if (isnan(data.temperature) || isnan(data.humidity)) {
Serial.println("❌ Failed to read DHT22 sensor!");
return;
}
Serial.println("--- Sensor Reading ---");
Serial.printf("Temperature: %.1f°C\n", data.temperature);
Serial.printf("Humidity: %.1f%%\n", data.humidity);
// Send temperature
sendToFirebase("sensor/temperature", data.temperature);
// Send humidity
sendToFirebase("sensor/humidity", data.humidity);
Serial.println("---------------------");
}
// Fungsi untuk cek kontrol LED dari Firebase
void checkLEDControl() {
String response = getFromFirebase("control/led");
if (response != "") {
int newKondisi = response.toInt();
if (newKondisi != kondisiLED) {
kondisiLED = newKondisi;
if (kondisiLED == 1) {
digitalWrite(LED_PIN, HIGH);
Serial.println("🔴 LED ON");
} else {
digitalWrite(LED_PIN, LOW);
Serial.println("⚫ LED OFF");
}
// Update status confirmation
sendToFirebase("status/led", kondisiLED);
sendToFirebase("status/timestamp", millis());
}
}
}
// Fungsi untuk mengirim data ke Firebase REST API
void sendToFirebase(String path, float value) {
if (WiFi.status() == WL_CONNECTED) {
http.begin(FIREBASE_HOST + "/" + path + ".json?auth=" + FIREBASE_AUTH);
http.addHeader("Content-Type", "application/json");
String payload = String(value);
int httpResponseCode = http.PUT(payload);
if (httpResponseCode > 0) {
Serial.printf("✓ %s: %.1f sent to Firebase\n", path.c_str(), value);
} else {
Serial.printf("❌ Error sending %s: %d\n", path.c_str(), httpResponseCode);
}
http.end();
} else {
Serial.println("❌ WiFi not connected!");
}
}
// Overload untuk integer
void sendToFirebase(String path, int value) {
if (WiFi.status() == WL_CONNECTED) {
http.begin(FIREBASE_HOST + "/" + path + ".json?auth=" + FIREBASE_AUTH);
http.addHeader("Content-Type", "application/json");
String payload = String(value);
int httpResponseCode = http.PUT(payload);
if (httpResponseCode > 0) {
Serial.printf("✓ %s: %d updated in Firebase\n", path.c_str(), value);
} else {
Serial.printf("❌ Error sending %s: %d\n", path.c_str(), httpResponseCode);
}
http.end();
}
}
// Overload untuk long (timestamp)
void sendToFirebase(String path, unsigned long value) {
sendToFirebase(path, (int)value);
}
// Fungsi untuk membaca data dari Firebase REST API
String getFromFirebase(String path) {
if (WiFi.status() == WL_CONNECTED) {
http.begin(FIREBASE_HOST + "/" + path + ".json?auth=" + FIREBASE_AUTH);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
response.replace("\"", ""); // Remove quotes
http.end();
return response;
} else {
Serial.printf("❌ Error reading %s: %d\n", path.c_str(), httpResponseCode);
}
http.end();
}
return "";
}
// Alternative: Manual control via Serial Monitor
void serialControl() {
if (Serial.available()) {
String command = Serial.readString();
command.trim();
if (command == "led_on" || command == "1") {
digitalWrite(LED_PIN, HIGH);
kondisiLED = 1;
Serial.println("🔴 LED ON (Manual)");
sendToFirebase("control/led", 1);
} else if (command == "led_off" || command == "0") {
digitalWrite(LED_PIN, LOW);
kondisiLED = 0;
Serial.println("⚫ LED OFF (Manual)");
sendToFirebase("control/led", 0);
} else if (command == "status") {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.printf("Temperature: %.1f°C, Humidity: %.1f%%, LED: %s\n",
data.temperature, data.humidity,
kondisiLED ? "ON" : "OFF");
}
}
}