ESP32 DHT22 + Telegram
Telegram Bot 指令列表
/start - 開始對話
/help - 指令說明
/status - 查詢目前溫濕度
/mute - 暫停變化通知
/resume - 恢復變化通知
/stopreport - 停止定時回報
/startreport - 啟用定時回報
// /start - 開始對話
// /help - 顯示指令清單
// /status - 查詢目前溫濕度
// /mute - 暫停自動變化通知
// /resume - 恢復自動變化通知
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <DHT.h>
// WiFi 設定
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Telegram Bot 資訊
// Initialize Telegram BOT
#define BOT_TOKEN "8023906815:AAE0KApbm5Ng00VCvO57JWA_XKtbx6a4IXM"
// your Bot Token (Get from Botfather)
// Use @myidbot to find out the chat ID of an individual or a group
// Also note that you need to click "start" on a bot before it can
// message you
#define CHAT_ID "7965218469"
// DHT22 設定
#define DHTPIN 15
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Telegram 設定
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
// 控制變數
bool notifyEnabled = true;
bool autoReportEnabled = true;
// 定時回報時間(秒轉毫秒)
unsigned long reportInterval = 60000; // 預設 60 秒
unsigned long lastReportTime = 0;
const unsigned long sendInterval = 3000;
unsigned long lastSendTime = 0;
const unsigned long botCheckInterval = 1000;
unsigned long lastBotCheck = 0;
float lastTemp = NAN;
float lastHum = NAN;
void setup() {
Serial.begin(115200);
dht.begin();
WiFi.begin(ssid, password);
Serial.print("WiFi 連線中");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi 已連線!");
Serial.println(WiFi.localIP());
secured_client.setInsecure();
Serial.println("\nESP32 開機完成,監控啟動中!");
bot.sendMessage(CHAT_ID, "ESP32 啟動完成,開始監控!", "");
}
void loop() {
unsigned long now = millis();
// 1. 溫濕度變化通知
if (notifyEnabled && (now - lastSendTime >= sendInterval)) {
lastSendTime = now;
float t = dht.readTemperature();
float h = dht.readHumidity();
if (!isnan(t) && !isnan(h)) {
if (t != lastTemp || h != lastHum) {
lastTemp = t;
lastHum = h;
String msg = "溫濕度變化:\n";
msg += "溫度:" + String(t, 1) + " °C\n";
msg += "濕度:" + String(h, 1) + " %";
bot.sendMessage(CHAT_ID, msg, "");
}
}
}
// 2. 定時回報
if (autoReportEnabled && (now - lastReportTime >= reportInterval)) {
lastReportTime = now;
float t = dht.readTemperature();
float h = dht.readHumidity();
if (!isnan(t) && !isnan(h)) {
String msg = "定時回報:\n";
msg += "溫度:" + String(t, 1) + " °C\n";
msg += "濕度:" + String(h, 1) + " %";
bot.sendMessage(CHAT_ID, msg, "");
}
}
// 3. 處理指令
if (now - lastBotCheck >= botCheckInterval) {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
for (int i = 0; i < numNewMessages; i++) {
String text = bot.messages[i].text;
String chat_id = bot.messages[i].chat_id;
String user_name = bot.messages[i].from_name;
if (text == "/start") {
bot.sendMessage(chat_id, "哈囉 " + user_name + ",我是 ESP32 環境監控 Bot!\n輸入 /help 查看可用指令。", "");
}
else if (text == "/help") {
String helpMsg = "**可用指令:**\n";
helpMsg += "/start - 開始對話\n";
helpMsg += "/help - 顯示指令說明\n";
helpMsg += "/status - 查詢目前溫濕度\n";
helpMsg += "/mute - 暫停變化通知\n";
helpMsg += "/resume - 恢復變化通知\n";
helpMsg += "/stopreport - 停止定時回報\n";
helpMsg += "/startreport - 啟用定時回報\n";
helpMsg += "/setreport x - 設定定時回報間隔為 x 秒(5~3600)";
bot.sendMessage(chat_id, helpMsg, "");
}
else if (text == "/status") {
float t = dht.readTemperature();
float h = dht.readHumidity();
if (isnan(t) || isnan(h)) {
bot.sendMessage(chat_id, "讀取感測器失敗,請稍後再試。", "");
} else {
String msg = "目前環境數據:\n";
msg += "溫度:" + String(t, 1) + " °C\n";
msg += "濕度:" + String(h, 1) + " %";
bot.sendMessage(chat_id, msg, "");
}
}
else if (text == "/mute") {
notifyEnabled = false;
bot.sendMessage(chat_id, "變化通知已暫停。", "");
}
else if (text == "/resume") {
notifyEnabled = true;
bot.sendMessage(chat_id, "變化通知已恢復。", "");
}
else if (text == "/stopreport") {
autoReportEnabled = false;
bot.sendMessage(chat_id, "定時回報已關閉。", "");
}
else if (text == "/startreport") {
autoReportEnabled = true;
bot.sendMessage(chat_id, "定時回報已啟用。", "");
}
else if (text.startsWith("/setreport ")) {
String valStr = text.substring(11);
int seconds = valStr.toInt();
if (seconds >= 5 && seconds <= 3600) {
reportInterval = seconds * 1000;
bot.sendMessage(chat_id, "定時回報間隔已設定為 " + String(seconds) + " 秒。", "");
} else {
bot.sendMessage(chat_id, "請輸入 5 ~ 3600 之間的數字(秒)。", "");
}
}
else {
bot.sendMessage(chat_id, "無效指令,請輸入 /help 查看可用指令。", "");
}
}
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
lastBotCheck = now;
}
}






沒有留言:
張貼留言