This is my complete code:
#define DEBUG
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
// -- NodeMCU PIN to ESP8266 ESP-12 Pinout
#define LDRPIN A0 // ADC Pin for LDR Sensor
#define DHTPIN 12 // GPIO12 --> D6
#define D1 5 // LED 1 (Yellow)
#define D3 0 // LED 2 (Green)
#define D5 14 // LED 3 (Red)
#define D2 4 // LED 4 (White)
// -- Blynk Virtual PIN definition
#define VTimer_Pin V2
#define VLEDblink_Pin V3
#define VLampTimer_Pin V5
// -- Blynk Virtual Display PIN definition
#define VDisplayTemp_Pin V10
#define VDisplayHumid_Pin V11
#define VDisplayLDR_Pin V12
#define VDisplayUptime_Pin V20
#define VDisplayNetConnect_Pin V21
#define VDisplayNetConnectUp_Pin V22
#define VDisplayCurrentTime_Pin V23
// -- Blynk Virtual Button PIN definition
#define VButton1_Pin V31
#define VButton2_Pin V32 // with Timer
#define VButton3_Pin V33
#define VButton5_Pin V35
#define VButtonTimer1_Pin V36
#define VButtonTimer3_Pin V37
#define VButtonTimer5_Pin V38
#define VButtonLDR_Timer_Pin V39
#define VSliderLDR_Pin V40
// -- Blynk Virtual LED PIN definition
#define VLED1_Pin V41
#define VLED2_Pin V42
#define VLED3_Pin V43
#define VLED5_Pin V45
const char auth[] = "xxxxxxxxxxxxxxxx; //insert here your token generated by Blynk
const char ssid[] = "xxxxx;
const char pass[] = "xxxx";
DHT dht(DHTPIN, DHT11);
SimpleTimer timer;
WidgetLED VLED_SystemBlink(VLEDblink_Pin);
WidgetLED VLED1(VLED1_Pin);
WidgetLED VLED2(VLED2_Pin);
WidgetLED VLED3(VLED3_Pin);
WidgetLED VLED5(VLED5_Pin);
// ----- Global variable ------
int LDR_Treshold = 30; // Value trahshold for Lighto switch on/off (Auto Lamp by LDR)
int NetworkDisconectedTimes = 0; // Reset every start-up
int NetConnectUp; // Reset every network re-conected
boolean AutoLamp1_Set = false;
boolean AutoLamp3_Set = false;
boolean AutoLamp5_Set = false;
boolean AutobyLDRTimer = false;
// -- This function will run every time Blynk connection is established
BLYNK_CONNECTED() {
Blynk.syncAll();
NetworkDisconectedTimes++;
Blynk.virtualWrite(VDisplayNetConnect_Pin, NetworkDisconectedTimes); // -- Disconected time counter on V21
String EmailBodyMsg;
EmailBodyMsg = "Up Time: "+ClockFormat((millis() / 1000), false)+" == ";
EmailBodyMsg += String(NetworkDisconectedTimes)+" x == ";
EmailBodyMsg += ClockFormat(NetConnectUp, true);
Blynk.email("noersaleh@gmail.com", "BLYNK Noer Home Automation: ESP8266 reconnected to Wifi/Internet", EmailBodyMsg);
NetConnectUp = 0; // -- Reset Network Connection Uptime upon Network Disconected
#ifdef DEBUG
Serial.println("Email to noersaleh@gmail.com: ");
Serial.println("==============================");
Serial.println(EmailBodyMsg);
Serial.println("==============================");
#endif
}
BLYNK_WRITE(VTimer_Pin) // Virtual PIN 2 is tight with a timer widgets on apps
{
// You'll get HIGH/1 at startTime and LOW/0 at stopTime.
// this method will be triggered every day until you remove widget or stop project or
// clean stop/start fields of widget
int i=param.asInt();
if (i==1)
{
VLED2.on();
Blynk.virtualWrite(VButton2_Pin, HIGH);
}
else {
VLED2.off();
Blynk.virtualWrite(VButton2_Pin, LOW);
}
digitalWrite(D2, param.asInt()); // GPIO4 / D2 is the LED set to timer
#ifdef DEBUG
Serial.print("Got a value: ");
Serial.println(param.asInt());
#endif
}
/********************************************************************************
*
* Define all button related to Lamp ON-OFF
* Toggle
*
********************************************************************************/
BLYNK_WRITE(VButton1_Pin) //-- Lamp 1
{
digitalWrite(D1, param.asInt());
}
BLYNK_WRITE(VButton2_Pin) //-- Lamp 2
{
digitalWrite(D2, param.asInt());
}
BLYNK_WRITE(VButton3_Pin) //-- Lamp 3
{
digitalWrite(D3, param.asInt());
}
BLYNK_WRITE(VButton5_Pin) //-- Lamp 4
{
digitalWrite(D5, param.asInt());
}
/********************************************************************************
*
* Define all button related to Auto / Manual Lamp ON-OFF
* either by Timer or LDR
*
********************************************************************************/
BLYNK_WRITE(VSliderLDR_Pin) //-- Slider Widget
{
LDR_Treshold = param.asInt();
}
BLYNK_WRITE(VButtonTimer1_Pin) //-- Button Widget for Lamp 1 Auto (Timer or LDR)
{
AutoLamp1_Set = (param.asInt()==1);
}
BLYNK_WRITE(VButtonTimer3_Pin) //-- Button Widget for Lamp 3 Auto (Timer or LDR)
{
AutoLamp3_Set = (param.asInt()==1);
}
BLYNK_WRITE(VButtonTimer5_Pin) //-- Button Widget for Lamp 5 Auto (Timer or LDR)
{
AutoLamp5_Set = (param.asInt()==1);
}
BLYNK_WRITE(VButtonLDR_Timer_Pin) //-- Button Widget for Set Auto by LDR or Timer
{
AutobyLDRTimer = (param.asInt()==1); // -- if TRUE --> LDR
}
BLYNK_WRITE(VLampTimer_Pin) // Virtual PIN 5 is tight with a timer widgets on apps
{
if (!AutobyLDRTimer) {
int i=param.asInt();
if (i==1)
{
if (AutoLamp1_Set) {
VLED1.on();
Blynk.virtualWrite(VButton1_Pin, HIGH);
digitalWrite(D1, HIGH);
}
if (AutoLamp3_Set) {
VLED3.on();
Blynk.virtualWrite(VButton3_Pin, HIGH);
digitalWrite(D3, HIGH);
}
if (AutoLamp5_Set) {
VLED5.on();
Blynk.virtualWrite(VButton5_Pin, HIGH);
digitalWrite(D5, HIGH);
}
}
else {
if (AutoLamp1_Set) {
VLED1.off();
Blynk.virtualWrite(VButton1_Pin, LOW);
digitalWrite(D1, LOW);
}
if (AutoLamp3_Set) {
VLED3.off();
Blynk.virtualWrite(VButton3_Pin, LOW);
digitalWrite(D3, LOW);
}
if (AutoLamp5_Set) {
VLED5.off();
Blynk.virtualWrite(VButton5_Pin, LOW);
digitalWrite(D5, LOW);
}
}
}
}
String ClockFormat(int InputClock, boolean DisplaySecond)
{
int my_h,my_m,my_s;
my_s = InputClock;
my_m = my_s / 60;
my_h = my_s / 3600;
my_s = my_s - my_m * 60;
my_m = my_m - my_h * 60;
String MyClock = String(my_h) +":";
if(my_m < 10)
MyClock += "0";
MyClock += String(my_m);
if (DisplaySecond)
{
MyClock += ":";
if(my_s < 10)
MyClock += "0";
MyClock += String(my_s);
}
return MyClock;
} // End: ClockFormat()
void sendNetConnectTime()
{
NetConnectUp++; // --- Increment the time every second
if (Blynk.connected()) {
// -- Blink LED that in Sync
if (VLED_SystemBlink.getValue()) {
VLED_SystemBlink.off();
} else {
VLED_SystemBlink.on();
}
// Refresh all the VLED status accodirng to each H/W GPIO Status
if (digitalRead(D1)==1) {VLED1.on();} else {VLED1.off();}
if (digitalRead(D3)==1) {VLED3.on();} else {VLED3.off();}
if (digitalRead(D5)==1) {VLED5.on();} else {VLED5.off();}
if (digitalRead(D2)==1) {VLED2.on();} else {VLED2.off();}
#ifdef DEBUG
Serial.print("D1 D3 D5 D2 Status:" );
Serial.print(digitalRead(D1));
Serial.print(" ");
Serial.print(digitalRead(D3));
Serial.print(" ");
Serial.print(digitalRead(D5));
Serial.print(" ");
Serial.println(digitalRead(D2));
#endif
} // END: if (Blynk.connected())
} // End: sendNetConnectTime()
void sendUptime()
{
// ------------- Converting miliseconds to Clock and daisplay on Virtual Pin
String MyClock = ClockFormat((millis() / 1000), false);
Blynk.virtualWrite(VDisplayUptime_Pin, MyClock); //-- Display UpTime
Blynk.virtualWrite(VDisplayNetConnectUp_Pin, ClockFormat(NetConnectUp, false)); //-- Display Last Network Connect
#ifdef DEBUG
Serial.print("Uptime (s): ");
Serial.println(MyClock);
#endif
} // End: sendUptime()
void sendDHT11_LDR_AutoLDR()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
if (!isnan(t) && !isnan(h)) {
Blynk.virtualWrite(VDisplayTemp_Pin, t);
Blynk.virtualWrite(VDisplayHumid_Pin, h);
}
int LDR_value = analogRead(LDRPIN);
LDR_value = map(LDR_value, 0, 1023, 0, 100); // -- convert value of 0-1023 to 0-100
Blynk.virtualWrite(VDisplayLDR_Pin, LDR_value);
// ----- Excecute this when each Lamp is controlled with Auto Timer Mode
if (AutobyLDRTimer) {
if (LDR_value < LDR_Treshold) { //--- Light below the threshold --> Set lamp ON, and syn LED and Button
if (AutoLamp1_Set) {
VLED1.on();
Blynk.virtualWrite(VButton1_Pin, HIGH);
digitalWrite(D1, HIGH);
}
if (AutoLamp3_Set) {
VLED3.on();
Blynk.virtualWrite(VButton3_Pin, HIGH);
digitalWrite(D3, HIGH);
}
if (AutoLamp5_Set) {
VLED5.on();
Blynk.virtualWrite(VButton5_Pin, HIGH);
digitalWrite(D5, HIGH);
}
}
else { //--- Light above the threshold --> Set lamp OFF, and syn LED and Button
if (AutoLamp1_Set) {
VLED1.off();
Blynk.virtualWrite(VButton1_Pin, LOW);
digitalWrite(D1, LOW);
}
if (AutoLamp3_Set) {
VLED3.off();
Blynk.virtualWrite(VButton3_Pin, LOW);
digitalWrite(D3, LOW);
}
if (AutoLamp5_Set) {
VLED5.off();
Blynk.virtualWrite(VButton5_Pin, LOW);
digitalWrite(D5, LOW);
}
}
}
#ifdef DEBUG
Serial.print("Temp:" );
Serial.print(t);
Serial.print(" - Humidity:" );
Serial.println(h);
Serial.print("Light:" );
Serial.println(LDR_value);
#endif
} // End: sendDHT11_LDR()
/****************************************************
* SETUP: Initialize for the first Time
*****************************************************/
void setup()
{
Blynk.begin(auth, ssid, pass);
while (Blynk.connect() == false) { } // Wait until connected
// Wait until connected
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D5, OUTPUT);
pinMode(DHTPIN, INPUT);
dht.begin();
timer.setInterval(1000L, sendNetConnectTime);
timer.setInterval(10000L, sendDHT11_LDR_AutoLDR);
timer.setInterval(60000L, sendUptime);
Blynk.virtualWrite(VDisplayUptime_Pin, "0:00"); // Display initial UpTime
Blynk.virtualWrite(VDisplayNetConnectUp_Pin, "0:00"); // Display initial last Network Connect time
#ifdef DEBUG
Serial.begin(9600); // See the connection status in Serial Monitor
#endif
} // End: setup()
/****************************************************
* LOOP: all the time
*****************************************************/
void loop()
{
Blynk.run(); // Initiates Blynk
timer.run(); // Initiates SimpleTimer
} // End: loop()
As you can see I’m not using any delay function, or external button…