ESP32IOT Projects

ESP32 Bluetooth & Wifi together for Smart House / Home Technology

Description:

ESP32 Bluetooth & Wifi together- ESP32 Bluetooth + Wifi Module by the Espressif systems come with the built-in Low Energy Bluetooth Module which can be used just like the standard HC-05 or HC-06 Bluetooth Modules. The ESP32 Bluetooth module can be paired with all types of cell phones, and special android cell phone applications can be designed for monitoring and controlling different types of sensors and electrical loads.

I will start with the very basics explaining how to make a simple wireless text messaging system using the ESP32 Bluetooth module and the android cell phone application. After explaining the basic programming and circuit diagram then I will explain how to make a Smart House and control electrical loads using the ESP32 Bluetooth and an android cell phone application designed in Android Studio.


Later, in the end, I will explain how to use the ESP32 Bluetooth and Wifi together to make a Smart House/Home automation system. You will be able to control the electrical loads using the android cell phone application without using the internet and moreover it’s best for short-range communication. For the worldwide control system, we will make an application using the Blynk IoT platform. With the help of this application, the electrical loads can be controlled from anywhere around the world. Any load which is turned ON using the Blynk application can be turned OFF using the Android cell phone application and vice versa.

ESP32 Bluetooth

For every load to control strong passwords are used which can be manually entered using my designed android cell phone application, or you can make your own application, add some buttons and you don’t need to write the passwords.

ESP32 Bluetooth



Download Android Apps

Both the application can be downloaded by clicking on the download links given below.

Download Cell phone App1:Blueserial app

Download Cell phone App2:esp32 bluetooth

If you want to design your own android cell phone application then read my article “Android app development to control Arduino over Bluetooth using Android Studio” which explains step by step how to make your own android cell phone application using the Android Studio.

In this tutorial, we will cover,

  1. Circuit diagram explanation
  2. Basic, intermediate, and advanced code explanation
  3. Blynk application designing, and finally
  4. Testing

Without any further delay, let’s get started!!!

Note: For the step-by-step explanation and practical demonstration watch video tutorial given at the end of this article.

The components and tools used in this project can be purchased from Amazon and Banggood, the components Purchase links are given below:

Purchase from Amazon:

ESP32 WiFi + Bluetooth Module(Recommended)

LM7805 Voltage Regulator:

470uf capacitor:

DC Female Power Jack:

Female Headers:

Male Headers:

12v SPDT type 4-channel relay Module:

Other Tools and Components:

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!


ESP32 Bluetooth and Wifi Module Power Supply:

ESP32 Bluetooth

Let’s first start with the 5V regulated power supply based on the LM7805 voltage regulator. This is the same 5v regulated power supply I have been using for the Nodemcu ESP8266 Wifi module.

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. A 330-ohm resistor is connected in series with a 2.5v led. This is a current limiting resistor. 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.

ESP32 Power Supply and Development Board:

For the easy interfacing, I designed my own ESP32 based development board as per the circuit diagram already explained. After I was done with the designing, I generated the Gerber files using the PCBWay online Gerber files generator tool, which hardly takes 2 to 3 minutes, and finally uploaded my Gerber files.

ESP32 Bluetooth
ESP32 Power Supply PCB board, ESPRESSIF ESP32 Devkit WROOM


This is the ESP32 power supply board after completing the Soldering Job, As you can see The PCB quality is really great. The silkscreen is quite clear and the Black Solder mask looks amazing, I am 100% satisfied with their work.

The PCB board used in this project is sponsored by the PCBWay Company. PCBWay is quite professional in the field of PCB manufacturing; you can try their services at extremely low prices, Only 5 dollars for 10 PCBs and 30 dollars in total for 20 PCBs assembly, besides this the new members also get a 5 Dollars bonus. The Gerber files of the ESP32 Wifi + Bluetooth Module can be downloaded by clicking on the link below.

Download: ESP32 Power Supply Gerber files

High quality & Only 24 Hours Build time:

Connect the ESP32 Bluetooth + Wifi Module with the laptop.

ESP32 Bluetooth

Before you start the programming, first of all, make sure you download the BluetoothSerial.h Library by clicking on the download libraries link given below, the library is available at 29 number.


Download Libraries:

ESP32 Bluetooth Example code:

//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

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!");
}

void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {
    Serial.write(SerialBT.read());
  }
  delay(20);
}

Upload this simple sketch developed by Evandro Copercini, which I will modify in a minute but first the basics. So, at this point I changed the name of the Bluetooth device to ESP32 Electronic Clinic. The only purpose of this code is to send a text message from the computer serial monitor to the Android cell phone Bluetooth Application or from the android cell phone Bluetooth App to the computer serial monitor. After you upload the above program and While the ESP32 Bluetooth + Wifi Module is connected with the laptop, open the serial monitor, then open the android application1, the download link is already given above. Search for the paired devices, click on the  ESP32 Electronic Clinic, and click on the connect button. At this point, you should be able to send and receive messages.

ESP32 Bluetooth

This was a very basic example, which is perfect for testing the ESP32 Bluetooth module.

Now I will modify the circuit diagram and programming to control 220Vac light bulbs using only the ESP32 Bluetooth and an android cell phone application. Which I will further modify to make a Smart House/Home automation system using ESP32 Bluetooth & Wifi together.



ESP32 Bluetooth Smart House Circuit Diagram:

ESP32 Bluetooth

This is the final modified circuit diagram. It’s totally up to you if you want to control these relays using only the Bluetooth or using only the Wifi, or using Bluetooth and Wifi together.

Currently, I have connected a 4-channel relay module. You can use a readymade relay module or you can follow the same exact connections and build a one by yourself. All the relays used in this project are of the type SPDT “Single Pole and double throw”. As you can see the connections of all the relays are exactly the same. This relay has a total of 5 pins, two relay coil pins, common, Normally Closed, and Normally Open.

These are 12v 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 a 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.

A Neutral wire from the 110/220Vac is connected directly with the neutral point of the Light Bulbs while the Live wire is connected with the Bulbs through the relays.


About the 4-Channel Relay Module:

ESP32 Bluetooth

This is a 4-channel relay module which I will use for controlling the light bulbs.

ESP32 Bluetooth

Finally, I connected everything as per the circuit diagram already explained. Now let’s have a look at the modified ESP32 Bluetooth programming.

ESP32 Bluetooth Smart House/Home Automation programming:

//Programmer Name: Engr Fahad
// https://www.electroniclinic.com/

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

long int Bluedata;

int relay1 = 32; 
long int password1 = 98421615;// Relay1 ON
long int password11 = 96951628;// Relay1 OFF

int relay2 = 33; 
long int password2 = 74151525; // Relay2 ON
long int password21 = 45614787; // Relay2 OFF

int relay3 = 34; 
long int password3 = 84515822; // relay3 ON
long int password31 = 81599922; // Relay3 OFF

int relay4 = 35; 
long int password4 = 81426337;
long int password41 = 88428399;



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);


}

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);
  }
}

ESP32 Bluetooth Smart House/Home Automation Code explanation:

As you can see this is the same exact program, which I modified for controlling Home appliances. I defined pins for the relays and selected strong passwords so that nobody else can control the appliances. One password is used to turn ON the relay while the other password is used to turn OFF the relay. This is the same basic programming I used in the Car automation and anti-theft system which was based on the HC-05 or HC-06 Bluetooth Module. So for the detailed explanation read my article on the Car automation and anti-theft system. Finally, I uploaded the code into the ESP32 Wifi + Bluetooth Module.


Warning:

While everything is powered Up, never touch the relays as 220Vac can be really dangerous, make sure you wear the protective gloves. Make this project at your own risk.

ESP32 Bluetooth

You can use this simple android application for controlling electrical loads, if you can remember passwords, you can also use short passwords, these are basically the commands which are used to control the relays, I know this is not user friendly, it wastes a lot of time, that’s why we designed another cell phone application using android studio.

ESP32 Bluetooth

This app is actually developed by my brother Fawad who is currently running another website programmingdigest.com. So if you have any questions regarding the android application development you can visit his website.

Now, let’s modify the ESP32 programming so that the relays can be controlled using the Android Bluetooth cell phone application and also the Wifi technology. So for the worldwide control system, we will make an application using the Blynk application. So let’s start with the Blynk application. Follow the same exact steps.


Note: this old version of the Blynk app is no more functional. For the blynk mobile App setup and Blynk.cloud dashboard setup ready my article on the New Blynk V2.0. In this article I have explained how to migrate your projects from Blynk 1.0 to the new Blynk V2.0. You can also watch the video.

ESP32 Wifi Smart House/home automation 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 to 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.

ESP32 Bluetooth

ESP32 Bluetooth and Wifi Based Smart House Programming:

//Programmer Name: Engr Fahad
// https://www.electroniclinic.com/

#include "BluetoothSerial.h"
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp32.h>

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

long int Bluedata;

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;

// for IOT

// You should get Auth Token in the Blynk App.
char auth[] = "szETZohB3MtHIL0y7BJGarXPQbZCkafa";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "AndroidAP7DF8";
char pass[] = "jamshaid";


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);

Blynk.begin(auth, ssid, pass);

}

void loop() {
    Blynk.run();
  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);
  }
}

This is the final ESP32 Bluetooth and wifi Based Smart House programming. As you can see this time I added the library for the Blynk while the pins and passwords remain the same. I added the authentication code and the Wifi credentials.



In the void setup function, I added the blynk.begin() function and finally, in the void loop() function I added the Blynk.run() function. The rest of the programming remains the same. So that’s all about the programming.

ESP32 Bluetooth

I successfully controlled the 110/220Vac light Bulbs using the Blynk application and also the Android cell phone Bluetooth application. If you have any questions regarding this project or any other project, let me know in a comment. Subscribe to my website and YouTube channel if you want to be notified each time I upload a new article or a new video on my YouTube channel “Electronic Clinic”.

Watch Video Tutorial:

 


Related Projects:

Download ESP32 Board Manager URL link, Programming, ESP32 Eagle library, ESP32 Datasheet:

https://www.electroniclinic.com/esp32-arduino-ide-board-manager-installation-espressif-esp32-wroom/

ESP32 IoT based Home Automation Project “Previous Project”

https://www.electroniclinic.com/esp32-home-automation-project-espressif-home-automation/

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

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

Back to top button