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
}