BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Controll lights at home also modify the switch


#1

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

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
*/
//**************************************************************************************simpletimer
#include <SimpleTimer.h>
//**************************************************************************************simpletimer
//************************************************************************************** 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";

//**************************************************************************************blynk
//**************************************************************************************simpletimer
SimpleTimer timer;
//const int   checkInterval = 30000;
//**************************************************************************************simpletimer

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
int LIGHTSTATE = 0;
int LIGHTSTATE1=0;

//*****************************************************debounce
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean lightState = false;

boolean lastButton2 = LOW;
boolean currentButton2 = LOW;
boolean fanState = false;
//****************************************************debounce

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


void setup()
{
  //*************************************************blynk
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  //*************************************************blynk
  pinMode(switchLight, INPUT);
  pinMode(switchFan,INPUT);
  pinMode(light, OUTPUT);
  pinMode(fan,OUTPUT);
  pinMode(LIGHT,INPUT);
  pinMode(LIGHT2,INPUT);
//digitalWrite(light,LOW);
//digitalWrite(fan,LOW);
  timer.setInterval(30000, reconnect);

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

}

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

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

//********************************************************debounce

boolean debounce(boolean last)
{
  boolean current = digitalRead(switchLight);
  if (last != current)
  {
    delay(5);
    current = digitalRead(switchLight);
  }
  return current;
}

boolean debounce2(boolean last2)
{
  boolean current2 = digitalRead(switchFan);
  if (last2 != current2)
  {
    delay(5);
    current2 = digitalRead(switchFan);
  }
  return current2;
}
 //*************************************************debounce

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

  LIGHTSTATE1 = digitalRead(LIGHT2);
  if(LIGHTSTATE1==HIGH){
     Blynk.virtualWrite(V4,ON);
      Blynk.virtualWrite(V6,"LIGHT ON");
  }
  else{
     Blynk.virtualWrite(V4,OFF);
      Blynk.virtualWrite(V6,"LIGHT OFF");
  }
  ///****************************************** //input to chech the status of the relay on/off

   timer.run();
  //***************************************************************debounce
  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);

//***************************************************************debounce
if(Blynk.connected()){
    Blynk.run();
  }
//****************************************************************blynk
//Blynk.run();
//****************************************************************blynk
}

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

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.


#3

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
Thanks


#4

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?


#5

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

Pete.


#6

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


#7

Thank you, I will wait.


#8

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
Thanks
Dan


#9

Look what I found on AliExpress
http://s.aliexpress.com/MreMVNNn
This is the mini power supply I got it from AliExpress
It’s very small and fit in the socket wall


#10

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


#11

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


#12

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); " ??


#13

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


#14

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.


#15

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


#16

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.


#17

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


#18

#19

blynk application can I learn the button information?

button = v1
button = v2
lcd = v5


#20

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.