NodeMCU Blynk Feedback Switch with Physical Switch Status

Introduction

These days there are different types of switches available in the market. Some switches can be accessed by smartphones using certain apps. In this article we will see how to build a NodeMCU Blynk Feedback Switch with physical button feedback status.

This is simple and easy to build. It can be used at home in real time and can be useful for disabled and older people. Any beginner who has a knowledge of Arduino code and wifi modules can build this projects easily.

Materials Needed

For building this project, below components are needed. These are easily available in the market.

  1. ESP8266 NodeMCU x 1
  2. 2 Channel Relay Module x 1
  3. Push button switches x 1
  4. Led’s (Red & Green) x 2
  5. Resistor 330Ω (1/4w) x 2
  6. Resistor 10KΩ (1/4w) x 2
  7. Mobile Charger (5v/1A) as power supply
  8. Jumper Wires
  9. Breadboard

How it Works?

The working of this project is simple. Here NodeMCU is the brain of this project which takes action as per the code. This wifi module gets connected with internet and sends/receive data from blynk app.

Once the project is powered up you the code will check if internet is available. If internet is available and the connection is established then Green led will glow. In case internet is not available Red light will glow. Relays will control the AC appliances and switches can also be used to turn on/off the devices.

The state change in switches will be reflected in blynk app. Alternatively you can control the devices using blynk as well. In case of power failure or reset the device will get reconnected as sync the state from blynk.

Module Functions

ESP8266 NodeMCU

NodeMCU: 

This the used for controlling the appliances and other components connected. This module helps in connecting with internet and sending / receiving data form blynk app.

Relay Module:

In this project we are using 2 channel relay module. You can also use 4 channel if you wish to control 4 devices. This is used for switching AC devices as per the signal from NodeMCU.

Led’s are used for visual indications. Resistors are used as current limiting and switches are used for switching devices.

Circuit Diagram

Below circuit diagram can be built on a PCB. It is easy to build a relay module.

Blynk based Smart Switch with Physical button feedback status

Must Read:

Smart Switch using blynk

IoT Based temperature & Humidity Monitoring

Touch based Switch

Blynk Setup

Open Blynk app and tap on New Project

Blynk config

Give a project name and Select the Device Type

nodemcu switch

Select your hardware as NodeMCU

nodemcu switch blynk

After filling up all the details just tap on Create

nodemcu switch blynk

Auth Token will be sent to registered email address

Auth Token

Tap on the button below to add widgets

Blynk config

Select Button or Styled Button. Since we will be configuring two switches hence we will select in two numbers.

nodemcu switch blynk

Once you select switches, provide the name for that switch and configure the remaining a shown for both the switches.

nodemcu switch blynk

After configuration you can press the triangular button to connect your blynk project with your hardware.

nodemcu switch

Code

The code is available for download here. Just update SSID, Password and Auth Token and upload the code in NodeMCU. You will receive an email from blynk regarding auth tiken while setting up the project in Blynk app.

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Your Network SSID" ; // update SSID
char pass[] = "Network Password" ; // update password

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "Auth Token" ; // Update Auth Token

Complete Code

/***************************************************************
This project is based on NodeMcu ESP8266
This works with Internet or without Internet with physical switch status Feedback
Automatic and Manual switching Home Automation project using Blynk IoT Platform
***************************************************************/

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;

//Pin assignments
#define switch_1 14 // D5 Pin of NodeMcu
#define switch_2 12 // D6 Pin of NodeMcu

#define relay_1 16 //D0 Pin of NodeMcu
#define relay_2 5 //D1 Pin of NodeMcu

#define LED1 13 //D7 Pin of NodeMcu
#define LED2 15 //D8 Pin of NodeMcu

int MODE = 0;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "NEtwork SSID";
char pass[] = "Network Password";

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

int switch_1_ON = 0;
int switch_2_ON = 0;

BLYNK_WRITE(V1)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
digitalWrite(relay_1, pinValue); // process received value
}

BLYNK_WRITE(V2)
{
int pinValue = param.asInt(); // assigning incoming value from pin V2 to a variable
digitalWrite(relay_2, pinValue); // process received value
}

void internet() //When this device connects with internet
{
//**************************************
//Switch 1 configuration
//**************************************
if (digitalRead(switch_1) == LOW)
{
if (switch_1_ON == 0 )
{
digitalWrite(relay_1, LOW);
Blynk.virtualWrite(V1, 0);
switch_1_ON = 1;
}
}

if (digitalRead(switch_1) == HIGH )
{
if (switch_1_ON == 1)
{
digitalWrite(relay_1, HIGH);
Blynk.virtualWrite(V1, 1);
switch_1_ON = 0;
}
}
//**************************************
//Switch 2 configuration
//**************************************
if (digitalRead(switch_2) == LOW)
{
if (switch_2_ON == 0 )
{
digitalWrite(relay_2, LOW);
Blynk.virtualWrite(V2, 0);
switch_2_ON = 1;
}
}

if (digitalRead(switch_2) == HIGH )
{
if (switch_2_ON == 1)
{
digitalWrite(relay_2, HIGH);
Blynk.virtualWrite(V2, 1);
switch_2_ON = 0;
}
}
}
//********************************************

void no_internet() //When this device doesn't gets internet
{
digitalWrite(relay_1, digitalRead(switch_1));
digitalWrite(relay_2, digitalRead(switch_2));
}

void Blynk_status() // called every 3 seconds by SimpleTimer
{
bool isconnected = Blynk.connected();
if (isconnected == false)
{
MODE = 1;
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
}
if (isconnected == true)
{
MODE = 0;
digitalWrite(LED1, LOW);
digitalWrite(LED2, HIGH);
Blynk.syncAll();
}
}

void setup()
{
Serial.begin(9600);
pinMode(switch_1, INPUT);
pinMode(relay_1, OUTPUT);

pinMode(switch_2, INPUT);
pinMode(relay_2, OUTPUT);

pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);

//*****************************************

digitalWrite(LED1, HIGH);
delay(200);
digitalWrite(LED1, LOW);
delay(200);
digitalWrite(LED2, HIGH);
delay(200);
digitalWrite(LED2, LOW);
delay(200);
digitalWrite(LED1, HIGH);
delay(200);
digitalWrite(LED1, LOW);
delay(200);
digitalWrite(LED2, HIGH);
delay(200);
digitalWrite(LED2, LOW);
delay(200);
digitalWrite(LED1, HIGH);
delay(200);
digitalWrite(LED1, LOW);
delay(200);
digitalWrite(LED2, HIGH);
delay(200);
digitalWrite(LED2, LOW);
delay(200);
digitalWrite(LED1, HIGH);
delay(200);
digitalWrite(LED2, LOW);
delay(200);
digitalWrite(LED2, HIGH);
delay(200);
digitalWrite(LED1, LOW);
delay(200);

WiFi.begin(ssid, pass);
timer.setInterval(3000L, Blynk_status); // check if connected to Blynk server every 3 seconds
Blynk.config(auth);
}

void loop()
{
if (WiFi.status() == WL_CONNECTED)
{
Blynk.run();
}

timer.run(); // Initiates SimpleTimer
if (MODE == 0)
internet();
else
no_internet();
}

Construction

Building this project is simple and easy. Get all the required materials and connect it as per the circuit diagram.

Blynk based Smart Switch with Physical button feedback

Testing

When the project is powered on, first it will check for internet connectivity and tell you the status accordingly. You can refer the video below for complete project.

Conclusion

This project NodeMCU blynk feedback switch can be used at home and will be very effective. The major advantage of this project is getting a feedback status of the physical switch. This can be controlled via blynk app from your mobile. In case of power cuts it will restore the blynk switch state as well. The components used for building this project is easily available in stores.  It’s really a fun making this project.

3 thoughts on “NodeMCU Blynk Feedback Switch with Physical Switch Status”

    • Hi,

      Yes you can use normal ac switches. Just connect the 2 wires as same as connected with pushbutton switch.

      Reply

Leave a Comment