// ==================== Blynk Template ====================
#define BLYNK_TEMPLATE_ID "TMPL6CGCA5sG4"
#define BLYNK_TEMPLATE_NAME "Time Input"
#define BLYNK_AUTH_TOKEN "HVlv2QAkAWjg9CY9Nrv59DXmGZsBFXEc"
// ==================== Board Detection ====================
#if defined(ESP32)
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h> // ไลบรารี Blynk สำหรับ ESP32
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h> // ไลบรารี Blynk สำหรับ ESP8266
#endif
#include <TimeLib.h> // ไลบรารีจัดการเวลา เช่น hour(), minute()
#include <WidgetRTC.h> // ใช้ Sync เวลาอินเทอร์เน็ตจาก Blynk
// ==================== WiFi Config ====================
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASS ""
// ==================== Blynk & Timer ====================
BlynkTimer timer; // ตัวจัดการเวลา (เรียกฟังก์ชันเป็นช่วง)
WidgetRTC rtc; // ใช้ sync เวลากับ Blynk Server
// ==================== Variables ====================
int startSeconds = 0; // เวลาเริ่ม (วินาทีตั้งแต่ 00:00)
int stopSeconds = 0; // เวลาเลิก
bool activeNow = false; // สถานะว่า LED เปิดอยู่หรือไม่
unsigned long lastHeartbeat = 0;
const unsigned long heartbeatInterval = 200;
bool heartbeatState = LOW;
// ==================== GPIO ====================
#define LED_PIN2 2 // ไฟหลักที่ควบคุมตามเวลา ESP8266 & WeMos D1 R1 -> GPIO2
#define LED_PIN13 13 // ไฟกระพริบ heartbeat ESP8266 & WeMos D1 R1 -> GPIO13
// ==================== Debug แสดงเวลาปัจจุบัน ====================
void printNow() {
Serial.printf("Now: %02d:%02d:%02d\n", hour(), minute(), second());
}
// ==================== รับค่าจาก Blynk Time Input (V1) ====================
BLYNK_WRITE(V1)
{
if (param.getLength() >= 2) {
// ดึงเวลา Start/Stop (วินาทีจากเที่ยงคืน)
startSeconds = param[0].asInt();
stopSeconds = param[1].asInt();
// แสดงผลแบบ HH:MM
int sh = startSeconds / 3600;
int sm = (startSeconds % 3600) / 60;
int eh = stopSeconds / 3600;
int em = (stopSeconds % 3600) / 60;
Serial.printf("Start: %02d:%02d\n", sh, sm);
Serial.printf("Stop: %02d:%02d\n", eh, em);
} else {
Serial.println("ยังไม่ได้ตั้งเวลาใน Time Input widget");
}
}
// ==================== เรียกเมื่อเชื่อมต่อ Blynk สำเร็จ ====================
BLYNK_CONNECTED()
{
rtc.begin(); // Sync เวลากับ Blynk Cloud
Blynk.syncVirtual(V1); // ดึงค่าเวลา Start/Stop ล่าสุดจาก Server
}
// ==================== ฟังก์ชันตรวจเวลาแล้วเปิด/ปิดไฟ ====================
void checkTime()
{
int nowSeconds = hour() * 3600 + minute() * 60 + second();
bool shouldActive;
if (startSeconds < stopSeconds) {
// เวลาอยู่ในวันเดียว เช่น 09:00 → 17:00
shouldActive = (nowSeconds >= startSeconds && nowSeconds <= stopSeconds);
} else {
// เวลาข้ามวัน เช่น 22:00 → 06:00
shouldActive = (nowSeconds >= startSeconds || nowSeconds <= stopSeconds);
}
// ถ้าควรเปิด แต่ยังไม่เปิด
if (shouldActive && !activeNow) {
activeNow = true;
digitalWrite(LED_PIN2, HIGH);
Serial.println("▶ LED ON");
}
// ถ้าควรปิด แต่ยังไม่ปิด
else if (!shouldActive && activeNow) {
activeNow = false;
digitalWrite(LED_PIN2, LOW);
Serial.println("⏹ LED OFF");
}
}
// ==================== Setup ====================
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println("Connecting to WiFi...");
pinMode(LED_PIN2, OUTPUT);
pinMode(LED_PIN13, OUTPUT);
digitalWrite(LED_PIN2, LOW);
digitalWrite(LED_PIN13, LOW);
// ========= เชื่อมต่อ WiFi =========
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(300);
Serial.print(".");
}
Serial.println("\nWiFi Connected");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// ========= เชื่อมต่อ Blynk =========
Blynk.config(BLYNK_AUTH_TOKEN);
Blynk.connect();
// ========= ตั้ง Timer =========
timer.setInterval(1000L, checkTime); // ตรวจเวลา → เปิด/ปิดไฟ
timer.setInterval(1000L, printNow); // Debug แสดงเวลาปัจจุบัน
Serial.println("ระบบพร้อมทำงานแล้ว.");
}
// ==================== Loop ====================
void loop()
{
unsigned long currentMillis = millis();
// LED heartbeat กระพริบทุก heartbeatInterval
if (currentMillis - lastHeartbeat >= heartbeatInterval) {
lastHeartbeat = currentMillis;
heartbeatState = !heartbeatState;
digitalWrite(LED_PIN13, heartbeatState);
}
Blynk.run(); // ประมวลผล Blynk
timer.run(); // เรียกฟังก์ชันตาม interval
}