/*
功能: 物聯網鬧鐘
*/
#include <WiFi.h>
#include <TM1637Display.h>
#include <time.h>
// Wi-Fi 設定
const char* ssid = "你的WiFi名稱"; // ⚠️請修改
const char* password = "你的WiFi密碼"; // ⚠️請修改
// TM1637 腳位設定
#define CLK 26
#define DIO 27
TM1637Display display(CLK, DIO);
// 鬧鐘輸出腳位(蜂鳴器或 LED)
#define ALARM_PIN 25
// 台灣時區 (GMT+8)
const long gmtOffset_sec = 8 * 3600;
const int daylightOffset_sec = 0;
// 鬧鐘設定時間(24 小時制)
const int alarmHour = 8;
const int alarmMinute = 18;
// 控制鬧鐘響鈴的旗標
bool alarmTriggered = false;
void setup() {
Serial.begin(115200);
display.setBrightness(7);
display.clear();
// 設定鬧鐘腳位
pinMode(ALARM_PIN, OUTPUT);
digitalWrite(ALARM_PIN, LOW); // 初始關閉
// Wi-Fi 連線
//WiFi.begin(ssid, password);
WiFi.begin("Wokwi-GUEST", "", 6); // wokwi提供的虛擬 WiFi 接入點
Serial.print("正在連接 Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi 連線成功!");
// 初始化 NTP 時間
configTime(gmtOffset_sec, daylightOffset_sec, "pool.ntp.org", "time.nist.gov");
struct tm timeinfo;
while (!getLocalTime(&timeinfo)) {
Serial.println("無法取得時間,重試中...");
delay(1000);
}
Serial.println("時間同步完成!");
}
void loop() {
struct tm timeinfo;
if (getLocalTime(&timeinfo)) {
int hour = timeinfo.tm_hour;
int minute = timeinfo.tm_min;
// 顯示時間
int displayTime = hour * 100 + minute;
static bool showColon = true;
uint8_t colon = showColon ? 0b01000000 : 0;
display.showNumberDecEx(displayTime, colon, true);
showColon = !showColon;
// 檢查鬧鐘時間
if (hour == alarmHour && minute == alarmMinute) {
if (!alarmTriggered) {
Serial.println("⏰ 鬧鐘響起!");
for (int i = 0; i < 10; i++) {
tone(ALARM_PIN, 1000); // 發出 1000 Hz 聲音
delay(500);
noTone(ALARM_PIN); // 停止聲音
delay(500);
}
alarmTriggered = true; // 當日只響一次
}
} else {
alarmTriggered = false; // 時間錯過後,重設觸發旗標
}
} else {
Serial.println("時間更新失敗!");
}
delay(1000); // 每秒更新
}
/*
功能: 物聯網時鐘
*/
/*
#include <WiFi.h>
#include <TM1637Display.h>
#include <time.h>
// Wi-Fi 設定
const char* ssid = "你的WiFi名稱"; // ⚠️請修改
const char* password = "你的WiFi密碼"; // ⚠️請修改
// TM1637 腳位設定
#define CLK 26
#define DIO 27
TM1637Display display(CLK, DIO);
// 時區設定(GMT+8:台灣)
const long gmtOffset_sec = 8 * 3600;
const int daylightOffset_sec = 0;
void setup() {
Serial.begin(115200);
display.setBrightness(7);
display.clear();
// Wi-Fi 連線
//WiFi.begin(ssid, password);
WiFi.begin("Wokwi-GUEST", "", 6); // wokwi提供的虛擬 WiFi 接入點
Serial.print("正在連接 Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi 連線成功!");
// 初始化 NTP 時間
configTime(gmtOffset_sec, daylightOffset_sec, "pool.ntp.org", "time.nist.gov");
struct tm timeinfo;
while (!getLocalTime(&timeinfo)) {
Serial.println("無法取得時間,重試中...");
delay(1000);
}
Serial.println("時間同步完成!");
}
void loop() {
struct tm timeinfo;
if (getLocalTime(&timeinfo)) {
// 取得小時與分鐘
int hour = timeinfo.tm_hour;
int minute = timeinfo.tm_min;
// 將時間轉換成 4 位數:HHMM
int displayTime = hour * 100 + minute;
// 顯示時間,冒號閃爍
static bool showColon = true;
uint8_t colon = showColon ? 0b01000000 : 0;
display.showNumberDecEx(displayTime, colon, true);
showColon = !showColon;
} else {
Serial.println("時間更新失敗!");
}
delay(1000); // 每秒更新一次
}
*/