/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "TMPL6uxgTRxrZ"
#define BLYNK_TEMPLATE_NAME "Quickstart Template"
#define BLYNK_AUTH_TOKEN "QeQtZYovnFcCeRgFHFzxLCEs5Sf4ZKbG"
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "time.h"
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;
// NTP server details
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 8 * 3600; // GMT+8 for MY Standard Time
const int daylightOffset_sec = 0; // No daylight saving time in MY
const int delaytime = 60 * 1000;
int lasttime = 0;
bool manualMode = false;
bool autoMode = true;
bool beforeSchool = true;
bool afterSchool = false;
bool autotem = false;
bool autotembeforeSchool = false;
bool autotemafterSchool = false;
bool manualtem = false;
bool manualtembeforeSchool = false;
bool manualtemafterSchool = false;
// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
// Set incoming value from pin V0 to a variable
if (param.asInt() == 1) {
manualMode = true;
autoMode = false;
if (autotem) {
afterSchool = autotemafterSchool;
beforeSchool = autotembeforeSchool;
//autotem = false;
manualtem = false;
} else {
if (manualtem) {
afterSchool = manualtemafterSchool;
beforeSchool = manualtembeforeSchool;
//manualtem = false;
}
}
} else {
manualMode = false;
autoMode = true;
autoM();
}
}
BLYNK_WRITE(V1)
{
// any code you place here will execute when the virtual pin value changes
Serial.println("Blynk.Cloud is writing something to V1");
if(param.asInt() == 2 && manualMode)
{
// execute this code if the switch widget is now ON
//digitalWrite(2,HIGH); // Set digital pin 2 HIGH
afterSchool = true;
beforeSchool = false;
manualtemafterSchool = true;
manualtembeforeSchool = false;
manualtem = true;
autotem = false;
}
else
{
if (manualMode && param.asInt() == 0) {
// execute this code if the switch widget is now OFF
//digitalWrite(2,LOW); // Set digital pin 2 LOW
afterSchool = false;
beforeSchool = true;
manualtemafterSchool = false;
manualtembeforeSchool = true;
manualtem = true;
autotem = false;
} else {
if (param.asInt() == 1 && manualMode) {
afterSchool = false;
beforeSchool = false;
manualtemafterSchool = false;
manualtembeforeSchool = false;
manualtem = true;
autotem = false;
} else {
if (autoMode && param.asInt() == 1) {
autotemafterSchool = false;
autotembeforeSchool = false;
autotem = true;
} else {
if (autoMode && param.asInt() == 0) {
autotemafterSchool = false;
autotembeforeSchool = true;
autotem = true;
} else {
if (autoMode && param.asInt() == 2) {
autotemafterSchool = true;
autotembeforeSchool = false;
autotem = true;
}
}
}
}
}
}
}
// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
Blynk.syncVirtual(V0);
Blynk.syncVirtual(V1);
}
// This function sends Arduino's uptime every second to Virtual Pin 2.
void myTimerEvent() {}
void setup()
{
// Debug console
Serial.begin(115200);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// You can also specify server:
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);
// Setup a function to be called every second
timer.setInterval(1000L, myTimerEvent);
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
pinMode(2, OUTPUT); // Initialise digital pin 2 as an output pin
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
}
void loop()
{
Blynk.run();
timer.run();
led();
if (autoMode) {
if (lasttime == 0) {
lasttime = millis();
autoM();
} else {
if (millis() - lasttime >= delaytime) {
autoM();
lasttime = millis();
}
}
}
}
void led() {
if (manualMode) {
digitalWrite(4, HIGH);
} else {
digitalWrite(4, LOW);
}
if (afterSchool) {
digitalWrite(2, HIGH);
} else {
digitalWrite(2, LOW);
}
if (beforeSchool) {
digitalWrite(5, HIGH);
} else {
digitalWrite(5, LOW);
}
}
void autoM() {
// Get current time
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
return;
}
// Print current time
Serial.println(&timeinfo, "Current time: %Y-%m-%d %H:%M:%S");
// Check if time is after 3:40 PM
if (timeinfo.tm_hour > 15 || (timeinfo.tm_hour == 15 && timeinfo.tm_min >= 40)) {
Serial.println("Current time is after 3:40 PM");
afterSchool = true;
} else {
Serial.println("Current time is before 3:40 PM");
afterSchool = false;
}
// Check if time is before 7:30 AM
if (timeinfo.tm_hour < 7 || (timeinfo.tm_hour == 7 && timeinfo.tm_min < 30)) {
Serial.println("Current time is before 7:30 AM");
beforeSchool = true;
} else {
Serial.println("Current time is after 7:30 AM");
beforeSchool = false;
}
}