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

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

ESP32 Firebase

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 *