Tech Gyan Set
Published © MIT

ESP32-Based Smart Home Automation Using Firebase Realtime Da

ESP32 + Firebase smart home system with real-time sync between app, web, and manual switches—no ON/OFF mismatch.

BeginnerShowcase (no instructions)8 hours63
ESP32-Based Smart Home Automation Using Firebase Realtime Da

Things used in this project

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

1️⃣ Required Software (Pehle Install Karo)

C/C++
🔹 Arduino IDE

👉 Board Manager me ESP32 by Espressif Systems install karo

🔹 Firebase Library

Arduino IDE → Library Manager → install:
Firebase ESP Client by Mobizt

2️⃣ Firebase Setup (Important)

C/C++
🔹 Firebase Realtime Database

Firebase Console → New Project

Realtime Database → Create

Rules (Testing ke liye):
home
└── room1
├── light1 : false
└── fan1 : false
{
  "rules": {
    ".read": true,
    ".write": true
  }
}


home
 └── room1
     ├── light1 : false
     └── fan1   : false

3️⃣ ESP32 Code (🔥 MAIN CODE)

C/C++
📌 Replace:

WiFi Name & Password

Firebase URL

Firebase Secret
#include <WiFi.h>
#include <Firebase_ESP_Client.h>

// 🔹 WiFi Credentials
#define WIFI_SSID "YOUR_WIFI_NAME"
#define WIFI_PASSWORD "YOUR_WIFI_PASSWORD"

// 🔹 Firebase Credentials
#define DATABASE_URL "https://your-project-id.firebaseio.com/"
#define DATABASE_SECRET "YOUR_DATABASE_SECRET"

// 🔹 Firebase Objects
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;

// 🔹 Relay Pins
#define RELAY_LIGHT 26
#define RELAY_FAN   27

// 🔹 Manual Switch Pins
#define SWITCH_LIGHT 32
#define SWITCH_FAN   33

bool lastLightState = HIGH;
bool lastFanState   = HIGH;

void setup() {
  Serial.begin(115200);

  pinMode(RELAY_LIGHT, OUTPUT);
  pinMode(RELAY_FAN, OUTPUT);

  pinMode(SWITCH_LIGHT, INPUT_PULLUP);
  pinMode(SWITCH_FAN, INPUT_PULLUP);

  digitalWrite(RELAY_LIGHT, HIGH);
  digitalWrite(RELAY_FAN, HIGH);

  // 🔹 WiFi Connect
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("Connecting to WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println("\nWiFi Connected!");

  // 🔹 Firebase Config
  config.database_url = DATABASE_URL;
  config.signer.tokens.legacy_token = DATABASE_SECRET;

  Firebase.begin(&config, &auth);
  Firebase.reconnectWiFi(true);

  Serial.println("Firebase Connected!");
}

void loop() {

  // 🔹 Read Firebase Values
  if (Firebase.RTDB.getBool(&fbdo, "/home/room1/light1")) {
    bool lightState = fbdo.boolData();
    digitalWrite(RELAY_LIGHT, lightState ? LOW : HIGH);
  }

  if (Firebase.RTDB.getBool(&fbdo, "/home/room1/fan1")) {
    bool fanState = fbdo.boolData();
    digitalWrite(RELAY_FAN, fanState ? LOW : HIGH);
  }

  // 🔹 Manual Switch Light
  bool currentLightSwitch = digitalRead(SWITCH_LIGHT);
  if (currentLightSwitch != lastLightState) {
    lastLightState = currentLightSwitch;
    bool newState = currentLightSwitch == LOW;
    Firebase.RTDB.setBool(&fbdo, "/home/room1/light1", newState);
    delay(300);
  }

  // 🔹 Manual Switch Fan
  bool currentFanSwitch = digitalRead(SWITCH_FAN);
  if (currentFanSwitch != lastFanState) {
    lastFanState = currentFanSwitch;
    bool newState = currentFanSwitch == LOW;
    Firebase.RTDB.setBool(&fbdo, "/home/room1/fan1", newState);
    delay(300);
  }
}

Credits

Tech Gyan Set
6 projects • 1 follower
Thanks to Tech Gyan Set .

Comments