@Nitheesh_Mohan look through all the items marked with // COSTAS to see what you were missing out. Tested here and all fine on a WeMos D1 Mini Pro.
Few changes done by me:
- SPI.H is not needed for ESP’s
- 115200 baud is generally fine for ESP’s
- Must wait for Blynk to be connected or RTC may be skipped until next 5 minute cycle
- the 2 sprintf statements are important
- Vital you have #define BLYNK_PRINT Serial for debugging unless you are a perfect coder
- Recommend you define pins rather than using “16” as you only have one line of code to change if you want to change the pin.
// #include <SPI.h> // COSTAS
//#define BLYNK_DEBUG // COSTAS
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space // COSTAS
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <Time.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
char auth[] = "xxx";
char ssid[]= "xxx";
char pass[]= "xxx";
char Date[16];
char Time[16];
long startseconds; // start time in seconds
long stopseconds; // stop time in seconds
long nowseconds; // time now in seconds
int btnpin;
SimpleTimer timer;
WidgetRTC rtc;
BLYNK_ATTACH_WIDGET(rtc, V0);
void setup(){
Serial.begin(115200); // COSTAS
Serial.println("\nStarted"); // COSTAS
pinMode(16, OUTPUT);
Blynk.begin(auth, ssid, pass);
while (Blynk.connect() == false) { // COSTAS
// Wait until connected // COSTAS
} // COSTAS
Blynk.notify("Connected successfully");
Serial.println("Done");
rtc.begin();
timer.setInterval(60000L, activetoday); // check every minute if schedule should run today
timer.setInterval(30000L, reconnectBlynk); // check every 30s if still connected to server
}
void activetoday(){ // check if schedule should run today
if(year() != 1970){
Blynk.syncVirtual(V1); // sync timeinput widget
sprintf(Date, "%02d/%02d/%04d", day(), month(), year()); // COSTAS
sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second()); // COSTAS
}
}
BLYNK_WRITE(V1) {
TimeInputParam t(param);
Serial.print("Checked schedule at: ");
Serial.println(Time);
int dayadjustment = -1;
if(weekday() == 1){
dayadjustment = 6; // needed for Sunday, Time library is day 1 and Blynk is day 7
}
if(t.isWeekdaySelected((weekday() + dayadjustment))){ //Time library starts week on Sunday, Blynk on Monday
Serial.println("Schedule ACTIVE today");
nowseconds = ((hour() * 3600) + (minute() * 60) + second());
startseconds = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
if(nowseconds >= startseconds){
if(nowseconds <= startseconds + 90){ // 90s on 60s timer ensures 1 trigger command is sent
// code here to switch the relay ON
digitalWrite(16, HIGH); // set Relay ON
Blynk.virtualWrite(V2, 1);
}
}
else{
Serial.println("Relay not on");
// nothing more to do here, waiting for relay to be turned on later today
}
stopseconds = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
if(nowseconds >= stopseconds){
// 90s on 60s timer ensures 1 trigger command is sent
if(nowseconds <= stopseconds + 90){
// code here to switch the relay OFF
digitalWrite(16, LOW); // set Relay OFF
Blynk.virtualWrite(V2, 0);
}
}
else{
if(nowseconds >= startseconds){ // only show if motor has already started today
Serial.println("Relay is still ON");
// nothing more to do here, waiting for motor to be turned off later today
}
}
}
else{
Serial.println("Schedule INACTIVE today");
// nothing to do today, check again in 1 minutes time
}
Serial.println();
}
void reconnectBlynk() {
if (!Blynk.connected()) {
if(Blynk.connect()) {
BLYNK_LOG("Reconnected");
}
else {
BLYNK_LOG("Not reconnected");
}
}
}
BLYNK_WRITE(V2) {
btnpin = param.asInt();
digitalWrite(16, btnpin);
}
void loop()
{
if (Blynk.connected()) {
Blynk.run();
}
timer.run();
}