In this user guide, we’ll use the Blynk application on our cellphones to control the output of the ESP32. We’ll demonstrate yet another helpful application that allows us to control any output linked to the ESP32’s GPIO pins.For the objectives of this post, we’ll use the Arduino IDE to control an LED linked to an output GPIO of our ESP32 module. The LED will be turned on and off using two-way communication between the ESP32 board and the Blynk project that you will construct using the app. After that, controlling the output of any other ESP32 module will be relatively simple. The buzzer, relay, motors, and other devices can also be controlled.

PROJECT OVERVIEW

  • Introduction of Blynk app
  • Installing the Blynk app and creating our project
  • Setting up Arduino IDE
  • Controlling LED using ESP32, Arduino IDE and Blynk application.

REQUIRED COMPONENTS

  1. ESP32 development board
  2. One 5mm LED
  3. One 220ohm resistor
  4. Breadboard
  5. Connecting Wires

SCHEMATIC

Blynk Application Introduction

Blynk is a highly accessible smartphone application that works on both Android and iOS devices. It is an interactive dashboard where users may place components to create their own IoT projects. As a result, a mobile app will be created that allows the user to operate microcontrollers connected via WIFI. LEDs, relays, electric motors, and a variety of other devices can be controlled. Blynk does not require any internal programming to develop your app. It’s as simple as dragging and dropping your electronic components into place. As a result, using Blynk to construct your IoT projects is really simple and involves very little work.

SETTING UP BLYNK APPLICATION

Installing and Getting Ready

We will use an Android smartphone for this project. So go to Google Play or Apple Store.

  1. Search for ‘Blynk’ and install the application.
  2. You will have to create an account to proceed further. You can also log in with your Facebook account if you possess that.

Creating project

  1. After you have successfully signed,Click ‘New project’.
  2. Now specify the name of your project, device and connection type. After that press the ‘Create’ button.
  3. You will receive a notification regarding your Authorization token. This can be accessed from the email account you signed in with and also through Project Settings.
  4. Press on the screen. The widget box will appear. Now we will build our project. Firstly, we will press the ‘Button’ widget.
  5. Press the button widget again on the canvas to change its parameters. In the output section, choose the GPIO pin through which your LED will be connected to the ESP32 module. 

Icons

At the top, you will find three icons. These are for Project settings, editing and play button.

Inside the play button, you can also find the stop button.

Setting up Arduino IDE

We will use Arduino IDE to program our ESP32 development board. Thus, you should have the latest version of Arduino IDE. Additionally, you also need to install the ESP32 plugin.

Installing Blynk Library

To use the Blynk app with our ESP32 board, we would have to install its library.

To download the library, click here. Click on ‘Code’ and then ‘Download Zip’.

You have to go to Sketch > Include Library > Add .zip Library inside the IDE to add the library as well. After installation of the library, restart your IDE.

Arduino Sketch (Connecting ESP32 with Blynk)

Open your Arduino IDE and go to File > New to open a new file. Copy the code given below in that file. You need to enter your network credentials. Additionally, you also have to provide your Authorization key which you obtained from the Blynk app.

CODE

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

int led_gpio = 2;

#define Authorization_key "hXBwHPyX-kvDSQAxkEq_R_A21x******"                    
#define SSID "Write_your_SSID_here"       // replace with your SSID
#define Password "********"           //replace with your password

void setup() {  
  pinMode(led_gpio, OUTPUT); 
  Serial.begin(115200);
  delay(10);
  WiFi.begin(SSID, Password);
  while (WiFi.status() != WL_CONNECTED) {
  delay(500);
  Serial.print(".");
  } 
  Blynk.begin(Authorization_key,SSID,Password);
}
void loop(){
    Blynk.run();
}

HOW CODE WORKS

Libraries are included.

To begin, we’ll incorporate all of the necessary libraries for this project. The WiFi.h library will assist in connecting the ESP32 module to a wireless network. The libraries for the Blynk application functionality will also be included.

#define BLYNK_PRINT Serial

#include <WiFi.h>

#include <WiFiClient.h>

#include <BlynkSimpleEsp32.h>

Output Variable

Then, we will define the variable of data type int called ‘led_gpio’. It will save the GPIO pin through which we will connect our LED. We are using GPIO25 in our case. Make sure to enter the same GPIO pin in the program code which you saved when creating the button in the Blynk app. Thus, we will control this pin through the app.

int led_gpio = 25;

Defining Authorization key

Thirdly, we will define the authorization key which was emailed to us through Blynk in our account through which we logged in. Keep this key safe with you for security concerns.

#define Authorization_key “hXBwHPyX-kvDSQAxkEq_R_A21x******”

Defining Network credentials

The network credentials will be defined next. The first is for the SSID, and the second is for the password. These are our wireless network credentials, which will be used to connect to our network. To ensure a successful connection, replace both of them with your credentials.

#define SSID “Write_your_SSID_here”                                       

#define Password “Write_your_password_here”

Setup() function

Inside the setup() function, we will open a serial connection at a baud rate of 115200.

Serial.begin(115200);

By using the pinMode() function, the ‘led_gpio’ variable (GPIO25) will be passed as a parameter inside the function. This will configure the pin as an output.

pinMode(led_gpio, OUTPUT);

The following section of code will connect our ESP32 board with the local network whose network credentials we already specified above. We will use the WiFi.begin() function. The arguments will be the SSID and the password which we defined earlier in the code.

WiFi.begin(SSID, Password);

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

  delay(500);

  Serial.print(“.”);

  }

We’ll now use the Blynk.begin() function to link our ESP32 module to the Blynk app. It considers three variables. The permission key is the first. The SSID is the second element, while the WIFI password is the third. All three of these were defined in the programme code before. A successful connection will be formed using the right credentials.

Blynk.begin(Authorization_key,SSID,Password);

loop() function

Inside the loop function we will use the Blynk.run() command to keep the connection running.

Blynk.run();

DEMONSTRATION

  1. Choose the correct board and COM port before uploading your code to the board.
  2. Go to Tools > Board and select ESP32 Dev Module.
  1. Next, go to Tools > Port and select the appropriate port through which your board is connected.
  1. Click on the upload button to upload the code to your ESP32 development board.
  2. After you have uploaded your code to the development board, press its ENABLE button.
  3. In your Arduino IDE, open up the serial monitor and you will be able to see the status of your WIFI connection. 
  4. When you receive the ‘ready’ message on the serial monitor, open your Blynk application. Open the project you created and press the ‘play’ button.
  1. Click the microcontroller icon at the top. You will receive a notification about the connection being established with your ESP32 board.
  1. Now, touch the Button widget to toggle the LED. By pressing ON/OFF, simultaneously the LED connected to GPIO25 will turn ON/OFF as well. To disrupt the connection, press the stop button.

CONCLUSION

We were able to control our ESP32 module’s output by creating a mobile app in Blynk. We controlled an LED connected to a pin configured as an output.

author avatar
Aravind S S