Home automation with ESP32 WiFi and Bluetooth

(Last Updated On: September 9, 2021)

 

Home Automation ESP32, Overview:

home automation esp32

Home automation with ESP32 WiFi + Bluetooth Module is very simple as compared to Nodemcu ESP8266 and Arduino boards. With ESP32 you can control different types of AC and DC loads over very long range using WiFi or short range using the ESP32 built-in Bluetooth module and this is the reason I prefer ESP32 over ESP8266 because ESP32 has a built-in Bluetooth module and moreover it has more analog and digital pins. I have a very detailed getting started tutorial on the ESP32 WiFi + Bluetooth module which explains the very basics including the ESP32 Pinout, technical specs, and how to install the ESP32 Board using the Arduino IDE.

home automation esp32

Anyhow with this circuit you can control your home appliances from anywhere around the world using your cell phone which has an application designed using Blynk IoT platform which I will explain later in this article. For the demonstration purposes I have connected a small 220Vac Fan, 12Vdc 775 DC Motor, and a 220Vac Bulb. These AC and DC loads can be controlled from anywhere around the world. I designed this circuit for only 4 loads but you can increase or decrease the number of loads as per your requirement.



About the Sponsor, JLCPCB:

home automation esp32

$2 for 1-4 Layer PCBs, sign up to get $18 new user coupons.

The PCB board used in this project is sponsored by JLCPCB. Feel free to visit their website JLCPCB to not only find out what awesome PCB and Assembly services they offer, but also to easily upload your Gerber files and thus order affordable and highly quality PCBs quickly. Besides this the new members also get a 5 dollars Coupon balance. So, what are you waiting for, go and get your first prototype order for free.

home automation esp32

I will also explain how to control the same electrical loads using the ESP32 built-in Bluetooth module and an Android Cell phone application designed in Android studio. There is no need to make any changes on the hardware side. The only change that is required is on the programming side.

home automation esp32

With this circuit you can convert your existing electrical switchboards into WiFi or Bluetooth controlled switchboards. I am sure now you have got the idea of what you are going to learn after reading this article. Without any further delay, let’s get started!!!




Amazon Purchase Links:

12v Adaptor:

ESP32 Wifi + Bluetooth Module:

775 DC Motor:

LM7805 Voltage Regulator:

470uf capacitor:

DC Female Power Jack:

Female Headers:

1n4007 diode:

10k Resistor:

2n2222 NPN transistor

12V SPDT Relay:

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Home Automation ESP32, Circuit Diagram:

home automation esp32

J1 is the female power jack and this is where we connect a 12v adaptor, battery or a solar panel. Two 470uf capacitors are connected at the input and output sides of the voltage regulator. The output of the voltage regulator is connected with the 5v pin of the ESP32 module and the ground of the power supply is connected with the ground of ESP32 module.

These are 12v SPDT type relays and cannot be directly controlled using the ESP32 Module, So, that’s why we need a driver to control these relays. You can use a relay driver IC or you can use 2n2222 NPN transistor and a 10k resistor. One pin of the relay coil is connected with the collector of the 2n2222 NPN transistor while the other pin of the relay coil is connected with the 12 volts. The emitter of the transistor is connected with the ground while the base is connected with the 10k ohm resistor.



Now to control these relays you simply need to connect these 10k resistors with the ESP32 I/O pins. In this project I am using the GPIO pins 13, 12, 14, and 27. I will be using the same pins in the programming.

These 4 relays can be used to control 4 different types of loads. The loads should be connected with the common and normally open legs of the relays.

ESP32 Home Automation Project PCB:

If you remember, in my previous Home automation project which was also based on the ESP32 module, I used a readymade relay module due to which the size and cost was increased. So, I decided to design another PCB and fixed all the electronics on a single PCB.

home automation esp32

Download: Gerber Files

I double checked all the connections and once satisfied I generated the Gerber files and placed an online order on the JLCPCB official website. I have a very detailed tutorial on how to generate the Gerber files, how to create a free account on JLCPCB, and then how to place an online order.

home automation esp32

This is the PCB board which I received from the JLCPCB as you can see the quality is really great. The silkscreen is quite clear and the Black Soldermask looks amazing. Next, I started off by placing the components and completed the soldering job.

home automation esp32

This is how the PCB board looks after soldering all the components. I also added holes on the left and right sides of the ESP32 if incase you want to connect some sensors.  You can connect AC or DC loads with these Green color terminal blocks.

home automation esp32

For the demonstration purposes I connected AC and DC loads. With relay1 I connected a 220Vac Fan, with relay2 I connected a 12Vdc 775 Motor, and with relay3 I connected a 220Vac Blub. My connections are completed and now it’s time to make the Blynk application.



Home automation ESP32 Blynk Application:

  • First of all open the blynk application.
  • Click on the new project and enter the project name as “Home Automation”.
  • Click on the choose device and select ESP32 Dev Board.
  • Make sure the connection type is set to WIFI.
  • Finally, click on the create button, an authentication token will be sent on your email id, which later will be used in the ESP32 Home Automation
  • Click anywhere on the screen and add 4 buttons. Drag and drop, if you want adjust the size.
  • Click on the first button, Set the name as Device1, select GP13, Set mode to SWITCH, and change the font size if you want.
  • Click on the second button, Set the name as Device2, select GP12, Set mode to SWITCH, and change the font size if you want.
  • Click on the third button, Set the name as Device3, select GP14, Set mode to SWITCH, and change the font size if you want.
  • Click on the fourth button, Set the name as Device4, select GP27, Set mode to SWITCH, and change the font size if you want.

That’s it our Home Automation Blynk application is ready.

home automation esp32

My application is ready and now let’s take a look at the home automation ESP32 programming.

Esp32 home automation Programming:


Home Automation ESP32 Code Explanation:

Before, you start the programming, first of all, make sure you download the BlynkSimpleEsp32 library.

// You should get Auth Token in the Blynk App.

char auth[] = “KIu6b5zMxIt07G2CBXgA0YJxlmiE10Km”;

This is the authentication code which was sent via email, I simply copied and paste it over here.

char ssid[] = “AndroidAP7DF8”;

char pass[] = “electroniclinic”;

AndroidAP7DF8 this is the name of the WiFi router and electroniclinic this is the password.

int device1 = 13; // gpio 13

int device2 = 12; // gpio 12

int device3 = 14; // gpio 14

int device4 = 27; // gpio 27

Next, I defined pins for the relays. 4 relays are connected with the ESP32 GPIO pins 13, 12, 14, and 27.

void setup()

{

// Debug console

Serial.begin(9600);

Blynk.begin(auth, ssid, pass);

pinMode(device1, OUTPUT);

pinMode(device2, OUTPUT);

pinMode(device3, OUTPUT);

pinMode(device4, OUTPUT);

}

Inside the void setup() function, I activated the Serial communication using the Serial.begin() function which is used for the debugging purposes, while 9600 is the baud rate. I set all the devices as output using the pinMode() function.

void loop()

{

Blynk.run();

}

Inside the void loop() function, we have only one function which is the Blynk.run(). So, that’s all about the Home automation ESP32 programming. I uploaded the code.

Warning:

Do not touch the wires as 220Vac can be really dangerous and may lead to death. Make this project on your own risk.

home automation esp32

I successfully controlled all the connected AC and DC loads. For the practical demonstration watch video given at the end of this article.



Now with Bluetooth:

As I said earlier there is no need to make any changes on the hardware side if incase you plan to use this circuit with a Bluetooth supported cell phone application. You will only need to write another program for the ESP32 module in order to be able to receive data wirelessly from the ESP32 using Bluetooth. Let’s take a look at the ESP32 Bluetooth Module programming.

 ESP32 Home Automation Bluetooth Code:


ESP32 Code Explanation:

This program uses only one header file “BluetoothSerial.h” and you don’t need to search for this header file because you will get this header file once you install your ESP32 board.

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)

#error Bluetooth is not enabled! Please run make menuconfig to and enable it

#endif

If you are using the ESP32 Built-in Bluetooth Module then you will need to add these lines of code.

long int Bluedata;

Next, I defined a variable Bluedata of the type long integer. I will be using this for storing the passwords which will be sent via Android Cell phone Application and will be compared with my predefined passwords.

int relay1 = 13;

long int password1 = 98421615;// Relay1 ON

long int password11 = 96951628;// Relay1 OFF

int relay2 = 12;

long int password2 = 74151525; // Relay2 ON

long int password21 = 45614787; // Relay2 OFF

int relay3 = 14;

long int password3 = 84515822; // relay3 ON

long int password31 = 81599922; // Relay3 OFF

int relay4 = 27;

long int password4 = 81426337;

long int password41 = 88428399;

4 relays are connected with the ESP32 pins 13, 12, 14, and 27. I selected strong passwords to control these relays.

void setup() {

Serial.begin(115200);

SerialBT.begin(“ESP32_Electronic_Clinic”); //Bluetooth device name

Serial.println(“The device started, now you can pair it with bluetooth!”);

pinMode(relay1, OUTPUT);

digitalWrite(relay1, LOW);

pinMode(relay2, OUTPUT);

digitalWrite(relay2, LOW);

pinMode(relay3, OUTPUT);

digitalWrite(relay3, LOW);

pinMode(relay4, OUTPUT);

digitalWrite(relay4, LOW);

}

In the void setup function I activated the serial communication and I also set the Bluetooth device name as the ESP32_Electronic_Clinic. Next, using the pinMode() function I set all the relays as the output and using the digitalWrite() function I turned off all the relays, as by default I want to keep all the relays in off state.



void loop() {

if (Serial.available()) {

SerialBT.write(Serial.read());

}

if (SerialBT.available()) {

Bluedata = SerialBT.parseInt();

}

delay(20);

// Serial.println(Bluedata);

if ( password1 == Bluedata )

{

digitalWrite(relay1, HIGH);

SerialBT.println(“Relay 1 is turned ON”);

Bluedata = 0;

delay(100);

}

if ( password11 == Bluedata )

{

digitalWrite(relay1, LOW);

SerialBT.println(“Relay 1 is turned OFF”);

Bluedata = 0;

delay(100);

}

// RELAY 2

if ( password2 == Bluedata )

{

digitalWrite(relay2, HIGH);

SerialBT.println(“Relay 2 is turned ON”);

Bluedata = 0;

delay(100);

}

if ( password21 == Bluedata )

{

digitalWrite(relay2, LOW);

SerialBT.println(“Relay 2 is turned OFF”);

Bluedata = 0;

delay(100);

}

// RELAY 3

if ( password3 == Bluedata )

{

digitalWrite(relay3, HIGH);

SerialBT.println(“Relay 3 is turned ON”);

Bluedata = 0;

delay(100);

}

if ( password31 == Bluedata )

{

digitalWrite(relay3, LOW);

SerialBT.println(“Relay 3 is turned OFF”);

Bluedata = 0;

delay(100);

}

// RELAY 4

if ( password4 == Bluedata )

{

digitalWrite(relay4, HIGH);

SerialBT.println(“Relay 4 is turned ON”);

Bluedata = 0;

delay(100);

}

if ( password41 == Bluedata )

{

digitalWrite(relay4, LOW);

SerialBT.println(“Relay 4 is turned OFF”);

Bluedata = 0;

delay(100);

}

}

In the void loop() function we are simply monitoring the Bluetooth module, so if the data is received then compare it with the predefined password values and if the received value is the same then turn ON or turn OFF the load.


ESP32 Bluetooth Android Application:

Download APK file:esp32-bluetooth

In order to control these relays wirelessly using Bluetooth you will need an application on your cell phone which you can download through the above link or you can design the one by yourself by reading my article in which I have explained everything. Now, let’s watch the ESP32 Bluetooth module based Home automation in action.

home automation esp32

I successfully controlled these AC and DC loads using my android cell phone application.

Watch Video Tutorial:

 

 

Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

%d bloggers like this: