Serial Monitor output for the following sketch:
TimeInput set as ON time 11:20 and OFF time as 11:25. Initially without Wednesday enabled and then enabled just prior to 11:20. Confirmed with LED on between 11:20 and 11:25.
The important lines in the code are:
65 compare Time library days of the week (Day 1 is Sunday) with Blynk days of the week (Day 1 is Monday). So -1 deducted from Time library day.
90 and 104 has a 90s window to check the current time with the 60s SimpleTimer interval to ensure the relay is turned ON and OFF just once at the start and stop times.
Starting
[17892] Connecting to MTN WIFI 19996
[18893] Connected to WiFi
[18893] IP: 192.168.10.171
[18893] Blynk v0.3.9 on ESP8266
[18893] Connecting to 192.168.10.229:8442
[19081] Ready (ping: 10ms).
[19103] Time sync: OK
Checked schedule at: 11:16:13
Schedule INACTIVE today
Checked schedule at: 11:17:13
Schedule INACTIVE today
Checked schedule at: 11:18:13
Schedule INACTIVE today
Checked schedule at: 11:19:13
Schedule INACTIVE today
Checked schedule at: 11:19:32
Schedule ACTIVE today
Start: 11:20
Stop : 11:25
Time zone: Europe/Nicosia
Time zone offset: 10800
Day 1 is selected
Day 2 is selected
Day 3 is selected
Day 4 is selected
Day 5 is selected
Motor NOT STARTED today
Checked schedule at: 11:20:13
Schedule ACTIVE today
Start: 11:20
Stop : 11:25
Time zone: Europe/Nicosia
Time zone offset: 10800
Day 1 is selected
Day 2 is selected
Day 3 is selected
Day 4 is selected
Day 5 is selected
Motor STARTED at 11:20
Motor is still RUNNING
[319218] Time sync: OK
Checked schedule at: 11:21:13
Schedule ACTIVE today
Start: 11:20
Stop : 11:25
Time zone: Europe/Nicosia
Time zone offset: 10800
Day 1 is selected
Day 2 is selected
Day 3 is selected
Day 4 is selected
Day 5 is selected
Motor STARTED at 11:20
Motor is still RUNNING
Checked schedule at: 11:22:12
Schedule ACTIVE today
Start: 11:20
Stop : 11:25
Time zone: Europe/Nicosia
Time zone offset: 10800
Day 1 is selected
Day 2 is selected
Day 3 is selected
Day 4 is selected
Day 5 is selected
Motor STARTED at 11:20
Motor is still RUNNING
Checked schedule at: 11:23:13
Schedule ACTIVE today
Start: 11:20
Stop : 11:25
Time zone: Europe/Nicosia
Time zone offset: 10800
Day 1 is selected
Day 2 is selected
Day 3 is selected
Day 4 is selected
Day 5 is selected
Motor STARTED at 11:20
Motor is still RUNNING
Checked schedule at: 11:24:13
Schedule ACTIVE today
Start: 11:20
Stop : 11:25
Time zone: Europe/Nicosia
Time zone offset: 10800
Day 1 is selected
Day 2 is selected
Day 3 is selected
Day 4 is selected
Day 5 is selected
Motor STARTED at 11:20
Motor is still RUNNING
Checked schedule at: 11:25:13
Schedule ACTIVE today
Start: 11:20
Stop : 11:25
Time zone: Europe/Nicosia
Time zone offset: 10800
Day 1 is selected
Day 2 is selected
Day 3 is selected
Day 4 is selected
Day 5 is selected
Motor STARTED at 11:20
Motor STOPPED at 11:25
[619262] Time sync: OK
Checked schedule at: 11:26:13
Schedule ACTIVE today
Start: 11:20
Stop : 11:25
Time zone: Europe/Nicosia
Time zone offset: 10800
Day 1 is selected
Day 2 is selected
Day 3 is selected
Day 4 is selected
Day 5 is selected
Motor STARTED at 11:20
Motor STOPPED at 11:25
Checked schedule at: 11:27:12
Schedule ACTIVE today
Start: 11:20
Stop : 11:25
Time zone: Europe/Nicosia
Time zone offset: 10800
Day 1 is selected
Day 2 is selected
Day 3 is selected
Day 4 is selected
Day 5 is selected
Motor STARTED at 11:20
Motor STOPPED at 11:25
Sketch:
/**************************************************************
* timeinput.ino Demonstrate interaction of Time library with
* Blynk's TimeInput widget, Costas 14/9/16
* App project setup:
* RTC on V0 and Time Input widget on V1.
*
**************************************************************/
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
SimpleTimer timer;
WidgetRTC rtc;
BLYNK_ATTACH_WIDGET(rtc, V0);
#define server "192.168.10.229" // or "blynk.cloud-com" for Blynk's cloud server
#define TestLED 2 // on board LED pin assignment
char Date[16];
char Time[16];
char auth[] = "*******************";
char ssid[] = "MTN WIFI 19996";
char pass[] = "******************I";
long startsecondswd; // weekday start time in seconds
long stopsecondswd; // weekday stop time in seconds
long nowseconds; // time now in seconds
void setup()
{
pinMode(TestLED, OUTPUT);
digitalWrite(TestLED, HIGH); // set LED OFF
Serial.begin(115200);
Serial.println("\Starting");
Blynk.begin(auth, ssid, pass, server);
int mytimeout = millis() / 1000;
while (Blynk.connect() == false) { // try to connect to server for 10 seconds
if((millis() / 1000) > mytimeout + 8){ // try local server if not connected within 9 seconds
break;
}
}
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
}
}
BLYNK_WRITE(V1) {
sprintf(Date, "%02d/%02d/%04d", day(), month(), year());
sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
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");
if (t.hasStartTime()) // Process start time
{
Serial.println(String("Start: ") + t.getStartHour() + ":" + t.getStartMinute());
}
if (t.hasStopTime()) // Process stop time
{
Serial.println(String("Stop : ") + t.getStopHour() + ":" + t.getStopMinute());
}
// Display timezone details, for information purposes only
Serial.println(String("Time zone: ") + t.getTZ()); // Timezone is already added to start/stop time
Serial.println(String("Time zone offset: ") + t.getTZ_Offset()); // Get timezone offset (in seconds)
for (int i = 1; i <= 7; i++) { // Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)
if (t.isWeekdaySelected(i)) {
Serial.println(String("Day ") + i + " is selected");
}
}
nowseconds = ((hour() * 3600) + (minute() * 60) + second());
startsecondswd = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
//Serial.println(startsecondswd); // used for debugging
if(nowseconds >= startsecondswd){
Serial.print("Motor STARTED at");
Serial.println(String(" ") + t.getStartHour() + ":" + t.getStartMinute());
if(nowseconds <= startsecondswd + 90){ // 90s on 60s timer ensures 1 trigger command is sent
digitalWrite(TestLED, LOW); // set LED ON
// code here to switch the relay ON
}
}
else{
Serial.println("Motor NOT STARTED today");
// nothing more to do here, waiting for motor to be turned on later today
}
stopsecondswd = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
//Serial.println(stopsecondswd); // used for debugging
if(nowseconds >= stopsecondswd){
Serial.print("Motor STOPPED at");
Serial.println(String(" ") + t.getStopHour() + ":" + t.getStopMinute());
if(nowseconds <= stopsecondswd + 90){ // 90s on 60s timer ensures 1 trigger command is sent
digitalWrite(TestLED, HIGH); // set LED OFF
// code here to switch the relay OFF
}
}
else{
if(nowseconds >= startsecondswd){ // only show if motor has already started today
Serial.println("Motor is still RUNNING");
// 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");
}
}
}
void loop()
{
if (Blynk.connected()) {
Blynk.run();
}
timer.run();
}