ESP-01S – Wi-fi Relay module for light remote control

In this tutorial, we will learn how to build a wi-fi switch, and use it remotely. If you want to remotely control the lights in your house or sun blinds, just have to copy this wifi switch and increase the number of switches. You can have access to schematics, PCB and firmware in this GitHub.
You can also see other projects like this one, that are more advanced, like BH OnOfre or sonoff.

Parts Required

ESP-01S Relay module

Figure 1

This WiFi relay based on ESP-01/ 01S WiFi module, we can use one GPIO of ESP-01/ 01S to control the relay by a low level. It’s easy to make your smart switch to any device by your phone anywhere with this smart relay.

Specifications

  • Working voltage: DC 5V-12V
  • Working current: ≥250mA
  • Communication: ESP01 or ESP 01S
  • WiFi module Transmission distance: the maximum transmission distance is 400m (open environment, mobile phone equipped with WiFi module)
  • Load: 10A/ 250VAC, 10A/ 30VDC, 10A/ 30VDC, 10A/ 28VDC
  • Size: 37 x 25mm

Blynk Android app

There are a lot of android applications for IoT. We will use blynk APP. Other application for desktop can be used, but we will keep it android for now.

Blynk is a hardware-agnostic IoT platform with white-label mobile apps, private clouds, device management, data analytics, and machine learning. You should check their Website to know more about Blynk.

Figure 2

So to install and configure the app, you must follow the tutorial on their website. However, if you follow the blynk tutorial, you will end up using the blynk.io server. A blynk.io server is a good option because, is safe, continuous and fast. Although blynk.io server is currently providing 2.000 of energy, which limits your Dashboard. To solve this problem, you can use public free blynk servers, from this GitHub. Remember that this server is unreliable and can have an intermittent shutdown. I will use the second server, but you can use any of them.

2º Server : blynk.iot-cm.com
App Port: 9443
Device Port: 8080
Status : Alive (seen 10 Nov 2019)
Energy : 1,000,000
Blynk Email Sending : unknown
Server location : Thailand (TH)
Admin : Krisana Meesuk Department of Electronic Science, Chiang Mai

To change the blynk.io server for the blynk.iot-cm.com, in the login in the page of the app, when choosing the server, you need to click on the “3 dots” button and copy the server name and App port, as shown in figure 2.

Figure 3

After making the instructions above, you will receive an email with a token. We will use that token in the code for access to the server. To begin, create a new project and name it as you desire. In this tutorial, it will be “Wifi Switch”.

Figure 4

Below the project name, you need to especify the hardware. Choose the ESP8266 option.

Figura 5

In the project settings menu, you can insert more devices. Each device will be an individual ESP-01S WiFi module, then the blynk app will generate a specific token for each one. We need the tokens, for future use in the code.

Figure 6

Go to the main screen again. Swipe the main screen to the left to open the widgets menu, and insert a button. Press the button to open the button settings, and choose the device, the output pin in the device and the operation mode. Create one or more buttons, for each device you have.

Figure 7

When you are choosing the output pin in the device, choose the virtual pin V0, to be equal to the code.

Figure 8

From now on, we just have to program our devices and connect them with the blynk app, and we are fine.

Circuit

Figure 9

Connect the DC output of the HI-LINK Power Supply to the 2 Pin Screw Terminal and connect the input to AC mains. Be careful, connect everything unplugged from the mains!! This is enough to power the board and starts programming. The 3 Pin Screw Terminal is the relay pins and must be the switch to your lamp or device.

A Proto-board can be used to mount and solder the parts. However, it is very good, if we use a PCB software design to build a PCB for this project. In this case, Eagle was selected. You can use other software like, Altium, Kicad or more. The final product is a small and compact PCB, that fit inside a wall socket. The Top and bottom layers can be seen in figure 10.

Figure 10

Coding

We will use a library from Blynk Git-Hub, to make them communicate with Blynk app. If you do not know how to instal a library or use the library manager in Arduino IDE, you can check Installing Additional Arduino Libraries.

Remember, that to be able to program the ESP-01/01S we need an FTDI Converter. If you use the FTDI mentioned above, just need to plug directly like Figure 11.

Figure 11

Copy the main sketch below to your Arduino IDE project and save it. Use the Serial Monitor to debug.


#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#define control_pin 0 //GPIO 0

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken"; //Itch device have their own

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

int On_Off = 0;
char flag = 0;
char estate_on = 0; 
char estate_off = 0;

BLYNK_WRITE (V0){ // On and Off Botton

  On_Off = param.asInt();

  if(On_Off){

    if(!estate_on){ // Condition to notify you just once every chance
      
      Serial.println("Bedroom light On");     
      
      estate_on = 1;
      estate_off = 0;
      flag = 1;  
    }
  }else{

    if(!estate_off){ // Condition to notify you just once every chance
      
      Serial.println("Bedroom light OFF");       

      estate_off = 1;
      estate_on = 0;
      flag = 0;
    }
  }

}

void setup()
{
  pinMode(control_pin,OUTPUT);
  
  // Debug console
  Serial.begin(9600);
  //Blynk.begin(auth, ssid, pass);
  Blynk.begin(auth, ssid, pass, "blynk.iot-cm.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

}

void loop()
{
  Blynk.run();

   if(flag){ //If botton On_Off is on

     /*Turn the relay ON*/
    digitalWrite(control_pin,LOW);//LOW Because the relay turns ON when the pin have GND
    
   }else{
    
     /*Turn the relay OFF*/
    digitalWrite(control_pin,HIGH);//HIGH Because the relay turns OFF when the pin have VCC

   }
}

4 thoughts on “ESP-01S – Wi-fi Relay module for light remote control

  1. Hello Ricardo
    Thank your for this nice tutorial.
    We use 220V power supply with two lines each one is 110V. there is no neutral or negative.
    When connected to relay, shall I connect one line from ac power supply to COM in relay and the other line to light bulb then to NO in relay.

    Is this correct?

    Thanks

    1. Hello!
      It depends on the voltage present in these lines, not only the value, but also whether it is AC or DC, and if it is AC, check the difference between the potentials present in the two lines.

Leave a Reply

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