Controll lights at home also modify the switch


hello friends
my project is to control the Can lights at home using the wemos D1 mini and of course the amazing app

and also i tried to change the switches to push buttons very nice and easy to locate when the room is dark .

and also when u loose the connections for the wifi u are able to turn the light on and off from the physical buttons on the wall ,i used Extract in the setup from help and guide from Costas he helped to make it work
thank u sir

the code is below and if there is any improves u guys can make in it i would bevery glad and happy to change it for better .
thanks all

Arduino Tutorials
Episode 2
Switch3 Program (debounced)
Written by: Jeremy Blum
#include <SimpleTimer.h>
//************************************************************************************** blynk
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define ON 255
#define OFF 0

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

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

SimpleTimer timer;
//const int   checkInterval = 30000;

int switchLight = D1; //   input button for light 
int switchFan = D2;    //  input button for fan
int light = D6;      //   output to energize relay for light
int fan = D5;        // out put to enegrize relay for fan

int LIGHT = D7;    //input to check the status of the relay on/off
int LIGHT2= D8;    // //input to check the status of the relay on/off

boolean lastButton = LOW;
boolean currentButton = LOW;
boolean lightState = false;

boolean lastButton2 = LOW;
boolean currentButton2 = LOW;
boolean fanState = false;

void reconnect(){
  int mytimeout = millis() / 1000;
  while (Blynk.connect() == false) { 
    if((millis() / 1000) > mytimeout + 8){  // try for less than 9 seconds

void setup()
  Blynk.begin(auth, ssid, pass);
  pinMode(switchLight, INPUT);
  pinMode(light, OUTPUT);
  timer.setInterval(30000, reconnect);

  int mytimeout = millis() / 1000;
  while (Blynk.connect() == false) { 
    if((millis() / 1000) > mytimeout + 8){  // try for less than 9 seconds


BLYNK_WRITE(V0) //turn light with blynk
int state = param.asInt();
if (state ==1)
 lightState = !lightState;
else if (state ==0)

BLYNK_WRITE(V1) //turn fan with blynk
int state = param.asInt();
if (state ==1)
 fanState = !fanState;
else if (state ==0)


boolean debounce(boolean last)
  boolean current = digitalRead(switchLight);
  if (last != current)
    current = digitalRead(switchLight);
  return current;

boolean debounce2(boolean last2)
  boolean current2 = digitalRead(switchFan);
  if (last2 != current2)
    current2 = digitalRead(switchFan);
  return current2;

void loop()
  //******************************************** //input to chech the status of the relay on/off
  LIGHTSTATE = digitalRead(LIGHT);
    // turn LED on:
    Blynk.virtualWrite(V5,"LIGHT ON");
  } else {
    // turn LED off:
    Blynk.virtualWrite(V5,"LIGHT OFF");

  LIGHTSTATE1 = digitalRead(LIGHT2);
      Blynk.virtualWrite(V6,"LIGHT ON");
      Blynk.virtualWrite(V6,"LIGHT OFF");
  ///****************************************** //input to chech the status of the relay on/off;
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)
    lightState = !lightState;
    // Blynk.virtualWrite(V3,lightState, ON);
  lastButton = currentButton;
  digitalWrite(light, lightState);

   currentButton2 = debounce2(lastButton2);
  if (lastButton2 == LOW && currentButton2 == HIGH)
    fanState = !fanState;
  lastButton2 = currentButton2;
  digitalWrite(fan, fanState);


Two way switch one is physical one is blynk switch connected to the same relay. How to check light on or off

Really nice project. The thing I liked the most is availability of switching devices On or Off even without WiFi. Would you like to share the Schematics as well? I did similar with Sonoff but it does not have functionality like yours. Plus you claimed 65W and Sonoff max is 10W.


Yes sure brother I’ll draw it and post it
It’s been under test and it’s doing good really
.hasnt give me any problem or disconnecting
So if there is no wifi
It will try to connect to Internet for 8 seconds then it will work for 30 seconds till it connects to internet it’s pretty good so even if there is no wifi am
Able to turn it on or off


It’s exactly this project that I need, do you have the wiring diagram on the switches and what do I need for this project?


Sonoff is 10 AMPS maximum, not 10 Watts.
10A @ 250v = 2500W maximum load for a Sonoff.



Yes brother I’ll provide the diagram and the stuff u need


Thank you, I will wait.


Hello brother this is the circuit diagram sorry I did it the old style draw it with my hand I don’t have the app for the circuit designer
If u have any q ask
And if u are able to prove it for better don’t hesitate
I will be more than happy to improve the code also


Look what I found on AliExpress
This is the mini power supply I got it from AliExpress
It’s very small and fit in the socket wall


Thanks brother, I’ll analyze and set up the scheme, then tell you how it was.


Alright no problem :slight_smile:
The push buttons I also got them from AliExpress they are already 5vdc
There are some on eBay 3.3 u need to put resistor for it
Just to remind thank u


If the wifi adapter and the rooter are turned off (or unpluggled) the switch keeps working with the buttons on the wall? Or the sketch keeps in a infinit cycle on the instruction " Blynk.begin(auth, ssid, pass); " ??


If there is no wifi the physical button will work yes for 30 seconds then it stops working for 8 seconds in this 8 seconds it will try to connect to blynk if the connection made then ur fine the blynk will work and the physical button too
If it didn’t connect to blynk it will keep trying to connect


There´s any way to improved the SONOFF with the follow features??:

  • Could turn off and turn on the light with the physical button, without delay, even if wifi connections goes down or if the router in turned off. I say this cause most of codes of SONFF works with the physical button but have somes delays when connection goes down or wait for connection setting even if the button is pressed.

  • Use wifi manager with the features above.


To be honest I havnt tried sonoff yet
But i think it should have small of delay like mine I have 8 seconds delay to try to connect to blynk cloud when there is internet u need couple of seconds to try to connect in this couple of seconds u won’t be able to operate the physical buttoN


Exactly, si I would like to avoid that “dead time” for the physical button, but I dont know how.
There are two ways to read the physical button:

1- by polling. I think in this way you will always have the “dead time” issue.

2 - by Interruption. Im not sure how to use the interruption to change inmediately the switch state without dropping down the connection or causing another issue.


A post was split to a new topic: Blynk application can I learn the button information



blynk application can I learn the button information?

button = v1
button = v2
lcd = v5


You question doesn’t make any sense… and did not seem related to the 2+ year old topic you attached it to, so i moved you into your own topic.

Please clarify your question.