Welcome to a detailed exploration of creating a sophisticated home automation system using the ESP32 WROOM 32D SMD and Firebase. This comprehensive guide will walk you through the entire process, from designing the circuit and PCB to 3D printing an enclosure, understanding the code, and deploying a live working demo. This project is ideal for electronics and communication enthusiasts, especially those between the ages of 16 to 30, who are eager to dive into practical and innovative projects.
Table of Contents
- Introduction
- Components and Tools Required
- Circuit Design
- PCB Design and Fabrication
- 3D Printing the Enclosure
- Understanding the Code
- Live Working Demo and Deployment
- Resources and Links
- Conclusion
Introduction
Home automation integrates electronics, communication, and programming to create intelligent environments. In this guide, we will use the ESP32 WROOM 32D SMD, a versatile microcontroller, to control home appliances via Firebase, a cloud-based real-time database. This project will teach you how to design circuits, create PCBs, print custom enclosures, and understand the code required for a functional home automation system.
Components and Tools Required
Buy Components
Here’s a list of the components you’ll need for this project, with links to purchase them:
- BUY COMPLETE IOT DEVELOPMENT KIT– Buy Here
- ESP32-WROOM-32 – Buy Here
- 5V 7A Relay (4 pcs) – Buy Here
- PC817 Optocoupler (4 pcs) – Buy Here
- S8050 Transistor (4 pcs) – Buy Here
- 1kΩ Resistor (10 pcs) – Buy Here
- 220µF 16V Electrolytic Capacitor (2 pcs) – Buy Here
- 5mm Red LED (5 pcs) – Buy Here
- 10D561K Thermistor (1 pc) – Buy Here
- Male Strip Connector (1 pc) – Buy Here
- Female Strip Connector (1 pc) – Buy Here
- 0.1µF Capacitor (104) (5 pcs) – Buy Here
- 1N4007 Diode (5 pcs) – Buy Here
- Hi-Link HLK-5M05 5V/5W Switch Power Supply Module (1 pc) – Buy Here
- AMS1117 3.3V Regulator (2 pcs) – Buy Here
- 3-Pin Fixed Screw Terminal Block Connector (4 pcs)
- 2-Pin Fixed Screw Terminal Block Connector (1 pc)
- Single Sided Copper Clad PCB (1 pc) – Buy Here
- Resistor (2 pcs) – Buy Here
Circuit Design
The circuit design is the backbone of our home automation system. Below is a detailed overview of the components and their connections:
Components and Connections
- ESP32 WROOM 32D SMD:
- Acts as the central microcontroller, managing input and output.
- Connect GPIO pins to control relays.
2. Relays:
- 5V 7A Relays will control high-power devices like lights or fans.
- Use PC817 optocouplers to provide isolation between the ESP32 and the high-voltage circuits.
3. Optocouplers (PC817):
- Provides electrical isolation between the low-voltage ESP32 and the high-voltage relay.
4. Transistors (S8050):
- Transistors drive the relays using the ESP32 GPIO pins.
5. Resistors and Capacitors:
- 1kΩ Resistors: Placed in series with the base of each transistor.
- 220µF Capacitors: Smooth out the power supply to the relays.
- 0.1µF Capacitors: Filter out noise and stabilize the voltage.
6. Power Supply:
- Hi-Link HLK-5M05: Provides stable 5V power for the relays.
- AMS1117 3.3V: Regulates the voltage for the ESP32.
7. LEDs:
- 5mm Red LEDs: Indicate the status of the relays.
Circuit Diagram
To design the circuit, you can use EasyEDA. Here’s a simplified outline:
- Connect the ESP32 GPIO pins to the base of each S8050 transistor through 1kΩ resistors.
- Connect the emitters of the transistors to ground.
- Connect the collectors to one terminal of each relay coil.
- Connect the other terminal of the relay coils to the 5V power supply.
- Use PC817 optocouplers to isolate the ESP32 from the relay circuits.
PCB Design and Fabrication
Designing a PCB enhances the robustness and professionalism of your project. We’ll use EasyEDA for design and fabrication.
Designing the PCB
- Create a New Project:
- Open EasyEDA and start a new PCB project.
2. Add Components:
- Place the ESP32, relays, optocouplers, transistors, resistors, capacitors, and connectors onto the PCB layout.
3. Route the Connections:
- Connect the components according to your circuit diagram.
4. Design Rules Check:
- Perform a Design Rules Check (DRC) to identify and correct any errors.
5. Generate Gerber Files:
- Export the Gerber files necessary for PCB fabrication.
Fabricating the PCB
- Choose a PCB Manufacturer:
- Send your Gerber files to a PCB fabrication service like JLCPCB or PCBWay.
2. Solder the Components:
- Once the PCB arrives, solder the components onto the board carefully.
3D Printing the Enclosure
A 3D-printed enclosure protects your electronics and gives your project a polished look. We’ll use Tinkercad for designing the model and Cura for slicing.
Designing the Enclosure
- Create a 3D Model:
- Design a custom enclosure using Tinkercad. Ensure you include cutouts for connectors and ventilation.
2. Export the Design:
- Export your design as an STL file for 3D printing.
3D Printing
- Prepare the Model:
- Import the STL file into Cura.
2. Slice the Model:
- Configure print settings such as layer height, infill density, and supports.
3. Print the Enclosure:
- Print the enclosure using a 3D printer with durable material like PLA or ABS.
Understanding the Code
The code manages communication between the ESP32 and Firebase and controls the relays based on Firebase updates.
Code Overview
- Setup:
- Initialize Wi-Fi and Firebase connections.
- Configure GPIO pins for relay control.
2. Firebase Communication:
- Use the Firebase ESP32 library to connect to Firebase.
- Read and write data to control the relays.
3. Relay Control:
- Use GPIO pins to switch the relays on and off based on Firebase data.
Code Example
Here’s a basic code snippet to get you started:
#include <iostream>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "esp_event.h"
#include "esp_netif.h"
#include "esp_wifi.h"
#include "jsoncpp/value.h"
#include "jsoncpp/json.h"
#include "esp_firebase/app.h"
#include "esp_firebase/rtdb.h"
#include "wifi_utils.h"
#include "firebase_config.h"
#include "cJSON.h"
using namespace ESPFirebase;
extern "C" void app_main(void)
{
gpio_set_direction(GPIO_NUM_32, GPIO_MODE_OUTPUT);
gpio_set_direction(GPIO_NUM_33, GPIO_MODE_OUTPUT);
gpio_set_direction(GPIO_NUM_25, GPIO_MODE_OUTPUT);
gpio_set_direction(GPIO_NUM_26, GPIO_MODE_OUTPUT);
gpio_set_level(GPIO_NUM_32,1);
gpio_set_level(GPIO_NUM_33,1);
gpio_set_level(GPIO_NUM_25,1);
gpio_set_level(GPIO_NUM_26,1);
wifiInit(SSID, PASSWORD); // blocking until it connects
// Config and Authentication
user_account_t account = {USER_EMAIL, USER_PASSWORD};
FirebaseApp app = FirebaseApp(API_KEY);
app.loginUserAccount(account);
RTDB db = RTDB(&app, DATABASE_URL);
// vTaskDelay(500/portTICK_PERIOD_MS);
// Construct a new json object manually
Json::Value new_data;
new_data["Appliances1"] = 0;
new_data["Appliances2"] = 0;
new_data["Appliances3"] = 0;
new_data["Appliances4"] = 0;
db.putData("Bedroom", new_data);
// vTaskDelay(500/portTICK_PERIOD_MS);
Json::FastWriter writer;
std::string person3_string_prev = "";
while (1)
{
Json::Value root = db.getData("Bedroom"); // retrieve person3 from database, set it to "" to get entire database
std::string person3_string = writer.write(root); // convert it to json string
if(strcmp(person3_string_prev.c_str(), person3_string.c_str())){
ESP_LOGI("MAIN", "Bedroom as json string: \n%s", person3_string.c_str());
cJSON *root1 = cJSON_Parse(person3_string.c_str());
if (root1 != NULL)
{
cJSON *appliance1 = cJSON_GetObjectItem(root1, "Appliances1");
cJSON *appliance2 = cJSON_GetObjectItem(root1, "Appliances2");
cJSON *appliance3 = cJSON_GetObjectItem(root1, "Appliances3");
cJSON *appliance4 = cJSON_GetObjectItem(root1, "Appliances4");
if (appliance1 && appliance1->type == cJSON_Number)
{
if (appliance1->valueint)
{
gpio_set_level(GPIO_NUM_32,0);
ESP_LOGI("Relay 1", "Relay 1 Turned ON");
}
else{
gpio_set_level(GPIO_NUM_32,1);
ESP_LOGI("Relay 1", "Relay 1 Turned Off");
}
}
if (appliance2 && appliance2->type == cJSON_Number)
{
if (appliance2->valueint)
{
gpio_set_level(GPIO_NUM_33,0);
ESP_LOGI("Relay 2", "Relay 2 Turned ON");
}
else{
gpio_set_level(GPIO_NUM_33,1);
ESP_LOGI("Relay 2", "Relay 2 Turned Off");
}
}
if (appliance3 && appliance3->type == cJSON_Number)
{
if (appliance3->valueint)
{
gpio_set_level(GPIO_NUM_25,0);
ESP_LOGI("Relay 3", "Relay 3 Turned ON");
}
else{
gpio_set_level(GPIO_NUM_25,1);
ESP_LOGI("Relay 3", "Relay 3 Turned Off");
}
}
if (appliance4 && appliance4->type == cJSON_Number)
{
if (appliance4->valueint)
{
gpio_set_level(GPIO_NUM_26,0);
ESP_LOGI("Relay 4", "Relay 4 Turned On");
}
else{
gpio_set_level(GPIO_NUM_26,1);
ESP_LOGI("Relay 4", "Relay 4 Turned Off");
}
}
}
// You can also print entire Json Value object with std::cout with converting to string
// you cant print directly with printf or LOGx because Value objects can have many type. << is overloaded and can print regardless of the type of the Value
std::cout << root << std::endl;
// print the members (Value::Members is a vector)
Json::Value::Members members = root.getMemberNames();
for (const auto& member : members)
{
std::cout << member << ", ";
}
std::cout << std::endl;
person3_string_prev = person3_string;
vTaskDelay(100);
}
}
}
Download All Resources
Live Working Demo and Deployment
Live Demo
- Upload the Code:
- Upload the code to the ESP32 using the Arduino IDE or ESP-IDF.
2. Test the System:
- Verify that the relays respond to commands sent from Firebase. Use the Firebase Console to control the relays and observe the real-time response.
Deployment
- Set Up Firebase:
- Configure your Firebase project to include authentication and database rules.
2. Integrate with Home Appliances:
- Connect your high-power appliances to the relay outputs.
3. Monitor and Adjust:
- Use Firebase’s real-time capabilities to monitor and adjust the system as needed.
Resources and Links
Here are some additional resources to help with your project:
- EasyEDA – For circuit and PCB design.
- Tinkercad – For 3D modeling.
- Cura – For slicing 3D models.
- Firebase Documentation – For Firebase setup and integration.
Conclusion
This comprehensive guide has walked you through the process of creating a home automation system with ESP32 and Firebase. From circuit design and PCB fabrication to 3D printing and coding, you now have the knowledge to build and deploy a functional system. Experiment with different configurations and expand your project to include more features.
Also, check out our other playlist Rasa Chatbot, Internet of things, Docker, Python Programming, Machine Learning, Natural Language Processing, MQTT, Tech News, ESP-IDF etc.
Become a member of our social family on youtube here.
Stay tuned and Happy Learning. ✌🏻😃
Happy coding, and may your NLP endeavors be both enlightening and rewarding! ❤️🔥🚀🛠️🏡💡