Support Team
Feedback:
support@nextpcb.com
Hello students! Welcome to a new tutorial on the ESP32 microcontroller board. In this era of technology, where everything is becoming more automated, a home automation system is considered one of the most frequently designed projects in the Internet of Things(IoT) world. Today, we are presenting a simple home automation project using the ESP32, where the user can control four bulbs remotely. For demonstration, we have used simple bulbs but the scope of the project goes beyond this. We can control home appliances with ESP32 using the same procedure. This project is not only useful in daily household life but it is also considered one of the most appropriate projects for students who are searching for projects related to circuits, microcontrollers, the Internet of Things, or embedded systems.
In this tutorial, we’ll start with a detailed description of the project and understand the workings of each basic component. After that, we’ll see the code and study it thoroughly for clear concepts. We’ll see all the material required for this project and the step-by-step procedure to complete it. Let’s start with the project description.
Before starting the preparation for the project, it is important to understand the workings of each component. Each one of these has a specific duty and it is important to include all of them in the project. Here are the main components of the home automation project:
A transformer is used to change the voltage level of its input. There are different types of transformers that are:
Out of these, we are only interested in the working of a step-down transformer. It is defined as:
“A step-down transformer is a device that reduces the voltage level from its input to the output.”
It is a highly efficient device that is used in multiple home appliances and is one of the basic components of home automation projects.
Here are some of the functions of the step-down transformer in this project:
Till now, we have seen the conversion of the voltage level but we know that the electronic components we are using here use direct current (DC). There are four diodes used in this project to convert the AC into DC. Hence, the wires connected to the input of the transformer have 20 volts of AC, whereas the wires connecting to the output of these diodes have 12 volts of AC. This is important because the components coming next run on DC.
A buck converter also acts like a step-down transformer but there are some differences here. This device takes the rectified current, which is direct current (DC). It further lowers the voltage level to make it appropriate for the ESP32. In home automation projects, a buck converter is often seen before the microcontroller because these devices require a steady and stable voltage level with the least fluctuation. Here are some important points about its structure:
The ESP32 is a microcontroller that acts as the heart of this project. This is one of the most popular choices for home automation and other Internet of Things (IoT) projects. It is because it is versatile, low cost, and has the robust capabilities to handle a great number of components working together. It has multiple features for wireless connectivity, such as WiFi and Bluetooth, that make it ideal for the home automation system because it can virtually connect and control these devices. Here is the working of this microcontroller in the project we are discussing:
A relay is an electromechanical switch used in circuits to control the flow of electricity in them. Till now, the ESP32 has 5V AC but this cannot be fed into the bulbs directly. To convert it into the bulb user form, we require 220V AC. This may be confusing for you but the components we have used till now in this circuit are only to make the voltage compatible with the ESP32. Yet, there is a need to regain the real voltage values to use them with the final components of the system. Moreover, the relay also acts as a protecting device for the ESP32 because it stops the back EMF from reaching the ESP32.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
At the end of this circuit, the current passes to the bulb. These are controlled by the user through the browser connected to the network. The important thing to notice here is that we are using bulbs for easy representation of the circuit. Any component that can be run on AC power, such as fans, can be controlled through the project. In such a case, the circuit will not be as clear and open as it is given here in this project.
The whole project is controlled with the help of code in ESP32. The outputs on the pins of ESP32 and the interface to control the bulbs are specified in the code. Here is the whole code that simply specifies the information for the user and the ESP32:
// Load Wi-Fi library
#include
// Network credentials Here
const char* ssid = "ESP32-Network";
const char* password = "Esp32-Password";
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
//variables to store the current LED states
String statePin16 = "off";
String statePin17 = "off";
String statePin4 = "off";
String statePin5 = "off";
//Output variable to GPIO pins
const int ledPin16 = 16;
const int ledPin17 = 17;
const int ledPin4 = 4;
const int ledPin5 = 5;
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds
const long timeoutTime = 2000;
void setup() {
Serial.begin(115200);
pinMode(ledPin16, OUTPUT); // set the LED pin mode
digitalWrite(ledPin16, 0); // turn LED off by default
pinMode(ledPin17, OUTPUT); // set the LED pin mode
digitalWrite(ledPin17, 0); // turn LED off by default
pinMode(ledPin4, OUTPUT); // set the LED pin mode
digitalWrite(ledPin4, 0); // turn LED off by default
pinMode(ledPin5, OUTPUT); // set the LED pin mode
digitalWrite(ledPin5, 0); // turn LED off by default
WiFi.softAP(ssid,password);
// Print IP address and start web server
Serial.println("");
Serial.println("IP address: ");
Serial.println(WiFi.softAPIP());
server.begin();
}
void loop() {
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
currentTime = millis();
previousTime = currentTime;
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected() && currentTime - previousTime <= timeoutTime) {
// loop while the client's connected
currentTime = millis();
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// turns the GPIOs on and off
if (header.indexOf("GET /16/on") >= 0) {
statePin16 = "on";
digitalWrite(ledPin16, HIGH); // turns the LED on
} else if (header.indexOf("GET /16/off") >= 0) {
statePin16 = "off";
digitalWrite(ledPin16, LOW); //turns the LED off
}
if (header.indexOf("GET /17/on") >= 0) {
statePin17 = "on";
digitalWrite(ledPin17, HIGH); // turns the LED on
} else if (header.indexOf("GET /17/off") >= 0) {
statePin17 = "off";
digitalWrite(ledPin17, LOW); //turns the LED off
}
if (header.indexOf("GET /4/on") >= 0) {
statePin4 = "on";
digitalWrite(ledPin4, HIGH); // turns the LED on
} else if (header.indexOf("GET /4/off") >= 0) {
statePin4 = "off";
digitalWrite(ledPin4, LOW); //turns the LED off
}
if (header.indexOf("GET /5/on") >= 0) {
statePin5 = "on";
digitalWrite(ledPin5, HIGH); // turns the LED on
} else if (header.indexOf("GET /5/off") >= 0) {
statePin5 = "off";
digitalWrite(ledPin5, LOW); //turns the LED off
}
// Display the HTML web page
client.println(" ");
client.println(" ");
client.println(" ");
// CSS to style the on/off buttons
client.println(" ");
client.println("ESP32 Home Automation");
client.println("Control LED State");
// Display the HTML web page
client.println(" ");
client.println(" ");
client.println(" ");
// CSS to style the on/off buttons
client.println(" ");
if (statePin16 == "off") {
client.println("ON1");
} else {
client.println("OFF1");
}
if (statePin17 == "off") {
client.println("ON2");
} else {
client.println("OFF2");
}
if (statePin4 == "off") {
client.println("ON3");
} else {
client.println("OFF3");
}
if (statePin5 == "off") {
client.println("ON4");
} else {
client.println("OFF4");
}
client.println("");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
Code Explanation for Home Automation Project
For better understanding, we are dividing the code into different parts. Once you have understood the code in detail, you will understand the workings of each component in detail:
The first step is to include the library in the code. This library has built-in functions and other necessary information that we can use for the best functioning of the project.
In the next step, we’ll set some special variables that we’ll use throughout the code. Here is an explanation of each step:
The setup function is the one where the main body of the code starts. This is the part of the code that will run only once when the user hits the compile button.
The void loop is checked again and again in the code when it is executed. Here are the steps performed in this part of the code:
Here, simple HTML code is used to make things more attractive and user-friendly. At every point, the client function is used to print the details of all the things happening on the HTML page.
For the appealing interface, CSS is used. We need four buttons to handle the functionality of four bulbs. The labels and the functionality match here. The if-else loops match the state of the button and the output at the GPOI pins. In the end, the stop function is used by the client to stop the working of anything in the code. This is called when the user does not do anything and there is no web page interface. At this point, anything turned on/off in the system remains in its current situation and if the user wants to change something, he has to be connected to the network again.
Now that we know the workings of all the important components of the project, we can move on to the practical implementation and construction of the project. Let’s start with the materials used in the project and after that, we’ll see the procedure in detail.
The experiments involving microcontrollers consist of two basic parts: preparing the software as well as the hardware. Let’s start with the software part.
Once the code is loaded successfully, we need the IP address specific to the ESP32 to get the interface of the experiment.
Go to the serial monitor from the upper right corner of the screen:
Right now, it does not show any output. Click on the EN button on the ESP32 to get the output.
Now that we know that ESP32 is working fine, we can move on to the hardware formation of the project. To do this, you have to plug the ESP32 out of the system. Don’t worry, because if the ESP32 was running fine at this point, it will work best with the project as well and you can reconnect it later.
If all the steps given above are complete, then the project is now ready to be used. Here are the steps to check if it is working fine:
Now, we’ll control the state of the bulbs remotely through the interface we have just shown you. Here is the output when all the bulbs are turned on through the website interface:
Now, when the first light is turned off, the following output is shown:
When two lights are turned off, the following output is seen:
Similarly, the output for three lights is shown in the following way:
When all the lights are turned off, the results are as expected:
Hence, in this way, we have created a project in which we are controlling four bulbs at a time with the help of ESP32. The user can control the state of the bulbs through their mobile device or any other device that has a browser. We started with a simple project description and studied all the basic components and their workings. After that, we have seen the code and its details to understand the concepts. In the next step, we saw all the points by following which, we can set the hardware part of this project. This is the best example of using the basic components to show the working of ESP32. I hope your project is running well because I have tried my level best to explain each point in detail. Still, if you are facing any issues, you can ask in the comment section.
- ESP32 BLE(Bluetooth Low Energy) Control in Arduino IDE
- Free Worldwide Shipping on Over 600,000 Electronics Components with HQ Online
- Free PCB Assembly Offer is Now Live: Experience Reliable PCB Assembly from HQ NextPCB
- HQ NextPCB Introduces New PCB Gerber Viewer: HQDFM Online Lite Edition
Still, need help? Contact Us: support@nextpcb.com
Need a PCB or PCBA quote? Quote now
Dimensions: (mm) |
|
Quantity: (pcs) |
|
Layers: |
Thickness: |
Quote now |