Smart Home Control: Automate 16 Relays with ESP32 and Firebase via Smartphone | Own Android App | Own Android App

Smart Home Control: Automate 16 Relays with ESP32 and Firebase via Smartphone | Own Android App

Smart

Welcome to Yarana IoT Guru! 🌟

Imagine being able to control 16 different appliances in your home with a single smartphone app! With ESP32, Firebase, and an Android app, you can automate your entire home lighting, fans, and other electrical devices wirelessly.

In this tutorial, we will guide you step-by-step on how to:
Control 16 relays using an ESP32 microcontroller
Use Firebase for real-time relay switching
Build an Android app to manage the relays remotely
Create a complete smart home automation system

👉 📥 Download Complete Source Code:
🔗 GitHub Repository


🎯 What You Will Learn in This Project

✔️ Connecting ESP32 to a 16-channel relay module
✔️ Using Firebase Realtime Database for real-time updates
✔️ Creating a custom Android app for controlling relays
✔️ Automating home appliances with IoT and cloud technology


🛠 Components & Technologies Used

ESP32 Microcontroller – Brain of the project
16-Channel Relay Module – Controls appliances remotely
Firebase Realtime Database – Cloud-based relay control
Android App (Kotlin/Java) – Sends commands to ESP32
WiFi Network – Enables remote access


📌 Why Automate Your Home Using ESP32 & Firebase?

Traditional switch-based controls are outdated. With smart automation, you can:
🔹 Turn appliances ON/OFF remotely using a smartphone
🔹 Schedule devices based on time or conditions
🔹 Monitor power usage and improve energy efficiency
🔹 Make your home smarter with IoT-based automation

This system is affordable, scalable, and beginner-friendly! 🚀


🚀 Step-by-Step Guide to Building a Smart Home Automation System

Step 1: Setting Up the 16-Channel Relay with ESP32

The 16-channel relay module allows you to control multiple home appliances using low-voltage signals.

📥 Wiring Diagram for ESP32 & Relay

ESP32 PinRelay Module PinFunction
GPIO 4IN1Relay 1
GPIO 5IN2Relay 2
GPIO 18IN3Relay 3
GPIO 19IN4Relay 4
GPIO 21IN5Relay 5
GPIO 22IN6Relay 6
GPIO 23IN7Relay 7
GPIO 25IN8Relay 8
GPIO 26IN9Relay 9
GPIO 27IN10Relay 10
GPIO 32IN11Relay 11
GPIO 33IN12Relay 12
GPIO 12IN13Relay 13
GPIO 13IN14Relay 14
GPIO 14IN15Relay 15
GPIO 15IN16Relay 16

Power Connections:

  • Connect VCC of Relay Module to 5V of ESP32
  • Connect GND of Relay Module to GND of ESP32

Step 2: Writing the ESP32 Code for Firebase Integration

The ESP32 will retrieve relay states from Firebase and turn appliances ON/OFF accordingly.

📥 ESP32 Code to Control 16 Relays via Firebase

cppCopyEdit#include <WiFi.h>
#include <FirebaseESP32.h>

#define FIREBASE_HOST "your-firebase-url.firebaseio.com"
#define FIREBASE_AUTH "your-firebase-secret"

FirebaseData firebaseData;
WiFiClient client;

// Define relay pins
int relayPins[] = {4, 5, 18, 19, 21, 22, 23, 25, 26, 27, 32, 33, 12, 13, 14, 15};
String relayPaths[] = {"/relay1", "/relay2", "/relay3", "/relay4", "/relay5", "/relay6", "/relay7", "/relay8", "/relay9", "/relay10",
                       "/relay11", "/relay12", "/relay13", "/relay14", "/relay15", "/relay16"};

void setup() {
    Serial.begin(115200);
    WiFi.begin("your-SSID", "your-PASSWORD");

    while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.print(".");
    }

    Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);

    for (int i = 0; i < 16; i++) {
        pinMode(relayPins[i], OUTPUT);
        digitalWrite(relayPins[i], HIGH);  // Default OFF state
    }
}

void loop() {
    for (int i = 0; i < 16; i++) {
        if (Firebase.getInt(firebaseData, relayPaths[i])) {
            int state = firebaseData.intData();
            digitalWrite(relayPins[i], state);
        }
    }
    delay(500);
}

Step 3: Creating an Android App for Smart Home Control

Now, let’s build an Android app to:
Send ON/OFF commands to Firebase
Retrieve real-time relay status

📥 Android Code to Read Firebase Data

javaCopyEditDatabaseReference databaseRef = FirebaseDatabase.getInstance().getReference("relay1");

databaseRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        int relayState = dataSnapshot.getValue(Integer.class);
        if (relayState == 1) {
            relaySwitch.setChecked(true);
        } else {
            relaySwitch.setChecked(false);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.w("Firebase", "Failed to read value.", databaseError.toException());
    }
});

📥 Android Code to Send Relay Control Command

javaCopyEditrelaySwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
    int state = isChecked ? 1 : 0;
    databaseRef.setValue(state);
});

Step 4: Testing & Demonstration

1️⃣ Upload ESP32 code and ensure it connects to Firebase.
2️⃣ Run the Android app and control relays via Firebase.
3️⃣ Check appliance switching and verify remote operation.

Congratulations! You’ve built a fully functional smart home automation system. 🚀


📺 Watch the Full Video Tutorial

🔴 ESP32 16-Relay Smart Home Control Video on Yarana IoT Guru

📢 Check out our GitHub Repo for the full source code!
👉 Download Code


📌 Frequently Asked Questions (FAQs)

❓ Can I add more than 16 relays?
✅ Yes! You can use I2C relay boards for expansion.

❓ Does this system work without the internet?
✅ No, Firebase requires an active WiFi connection.

❓ Can I schedule relays to turn ON/OFF automatically?
✅ Yes! Add time-based scheduling in Firebase.


🚀 Final Thoughts

This ESP32-based smart home system is a powerful yet simple IoT solution. With Firebase cloud integration, you can remotely control appliances from anywhere.

Try this project and make your home smarter today! 🚀

Leave a Reply

Your email address will not be published. Required fields are marked *