IoT Home Automation using Blynk App using ESP32

IoT Home Automation using Blynk App using ESP32

In previous article of controlling a LED with Blynk app using ESP32 we learned about the features of ESP32 and how you can use Blynk App with ESP32 for IoT projects.

In this project, we will control home AC appliances with Blynk app which will be very helpful in your IoT home automation projects.

In this article we will control a bulb connected to AC mains through relay module with Blynk app and ESP32. Blynk is very compatible app with Arduino IDE to create IoT based projects. This app can easily be downloaded from Play store and can easily be configured. This app is user friendly and can be used to make good IoT projects. We previously used Blynk app for IoT projects.

 

Components Required

  • ESP32 module
  • USB Cable
  • Relay module
  • Bulb
  • Blynk App
  • AC Mains supply
  • Connecting wires

 

Circuit Diagram

 Circuit Diagram for IoT Home Automation using Blynk App using ESP32

 

To connect your AC appliance to Relay Module use the below connection:

Relay Module

 

Configuring Blynk App for Home Automation

Following are the steps to configure Blynk app in your phone and use it for a project:

  1. Firstly, download Blynk app in your phone from Google play store and install it.
  2. After installing, you need to create an account in this app; you may use your current Gmail account.
  3. After creating account a window will open, in this click on New Project.

 Configuring Blynk App for Home Automation using ESP32

 

  1. Now give project name according to your choice and in device choose ESP32 Dev Board and in Connection type choose Wi-Fi and then click on Create.
  2. Now a window will come which shows that your authentication token which you will need later sent to your concerned mail id to which you created your account. You can open your email to check authentication key.

 Authorize Blynk App for Home Automation using ESP32

 

  1. After clicking on OK, you will find canvas window.
  2. Now, tap anywhere on the canvas to open the widget box. All the available widgets are located here. Now choose a button.

 Setup Blynk App for Home Automation using ESP32

 

  1. Click on the Button widget to change the setting.
  2. Set output pin to gp2 as I am taking output here from GPIO2 pin, you can change according to you. In mode select to switch.
  3. When you are done with this setting, you are ready to work with this app. On pressing Play button it will switch you from EDIT mode to PLAY mode where you can interact with the hardware. In PLAY mode, you will not be able to drag or set new widgets, press STOP to get back to STOP mode.

 

Programming ESP32 for controlling IoT Home Appliances:

Here ESP32 will be programmed using Arduino IDE. First of all, you have to download the Blynk library from the given link.

https://github.com/blynkkk/blynk-library

It is a zip file, download it and extract it and then copy this library file to Arduino Library files. You can find your Arduino library file in Documents--> Arduino--> libraries. Copy this library file here.

Now in programming the first thing you have to do is include the required libraries.

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

 

Now define your output pin, here I am taking my output from GPIO 2 so include pin 2.

int pin = 2;

 

Now you have to enter your Authentication token and your network credentials inside double inverted commas.

char auth[] = "             ";
char ssid[] = "     ";
char pass[] = "      ";

 

In void setup() function, we will initialize the baud rate, LED output and will connect the module with the Wi-Fi using WiFi.begin(ssid,password); function. This function begins the Wi-Fi connection then enter authentication token in Blynk.begin function.

void setup() { 
  pinMode(pin, OUTPUT);
  pinMode(pin, HIGH);
  Serial.begin(115200);
  delay(10);
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, pass);
  int wifi_ctr = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 Serial.println("WiFi connected"); 
  Blynk.begin("66d1e7b99b92458295f8257b80fXXXXX", ssid, pass);
}

 

In the loop function include Blynk.run() command.

void loop(){
    Blynk.run();
}

 

The complete code is given below. Once your code is ready with modifications, you are ready to upload it to your ESP32 from Arduino IDE.

 

Output

After uploading of code open your serial monitor and your serial monitor should look like this:

 Run Python Code for Home Automation using ESP32

 

Now open your Blynk app and go to the project you have created earlier and tap on PLAY, you can see that changing the button state will change the state of your Bulb.

 IoT Home Automation using Blynk App and ESP32

 

This is how you are successfully able to control your home appliances with Blynk App using ESP32

Code

#define BLYNK_PRINT Serial

#include <WiFi.h>

#include <WiFiClient.h>

#include <BlynkSimpleEsp32.h>

int pin = 2;

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

// Go to the Project Settings (nut icon).

char auth[] = "a355a3a0baa248e391d6eb78411XXXXX";

// Your WiFi credentials.

// Set password to "" for open networks.

char ssid[] = "Ashish";

char pass[] = "12345678";

void setup() { 

  pinMode(pin, OUTPUT);

  pinMode(pin, HIGH);

  Serial.begin(115200);

  delay(10);

  Serial.print("Connecting to ");

  Serial.println(ssid);

  WiFi.begin(ssid, pass);

  int wifi_ctr = 0;

  while (WiFi.status() != WL_CONNECTED) {

    delay(500);

    Serial.print(".");

  }

  Serial.println("WiFi connected"); 

  Blynk.begin("a355a3a0baa248e391d6eb7841186195", ssid, pass);

}

void loop(){

    Blynk.run();

}