2025年5月21日 星期三

ESP32 DHT22 + Telegram start - 開始對話 help - 顯示指令清單 status - 查詢目前溫濕度 mute - 暫停自動變化通知 resume - 恢復自動變化通知

ESP32 DHT22 + Telegram

Telegram Bot 指令列表

/start - 開始對話

/help - 顯示指令清單

/status - 查詢目前溫濕度

/mute - 暫停自動變化通知

/resume - 恢復自動變化通知







//  /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 "80231906815:A1AE0KApbm5Ng00VCvO57JWA_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 "791652118469"
// DHT22 設定
#define DHTPIN 15
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

// Telegram 設定
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);

// 時間控制
unsigned long lastSendTime = 0;
const unsigned long sendInterval = 3000;

unsigned long lastBotCheck = 0;
const unsigned long botCheckInterval = 1000;

// 上一次的溫濕度值
float lastTemp = NAN;
float lastHum = NAN;

// 是否啟用變化通報
bool notifyEnabled = true;

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());

  // Telegram SSL 憑證略過
  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)) {
      Serial.println("讀取 DHT22 失敗");
      return;
    }

    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, "");
      Serial.println("變化 -> 傳送");
      Serial.println(msg);
    }
  }

  // 2. 處理 Telegram 指令
  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") {
          String welcome = "哈囉 " + user_name + ",我是 ESP32 環境監控 Bot!\n";
          welcome += "輸入 /help 查看可用指令。";
          bot.sendMessage(chat_id, welcome, "");
        }
        else if (text == "/help") {
          String helpMsg = "**可用指令:**\n";
          helpMsg += "/start - 開始對話\n";
          helpMsg += "/help - 顯示指令說明\n";
          helpMsg += "/status - 查詢目前溫濕度\n";
          helpMsg += "/mute - 暫停變化通知\n";
          helpMsg += "/resume - 恢復變化通知";
          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 {
          bot.sendMessage(chat_id, "無效指令,請輸入 /help 查看可用指令。", "");
        }
      }
      numNewMessages = bot.getUpdates(bot.last_message_received + 1);
    }
    lastBotCheck = now;
  }
}

沒有留言:

張貼留言

ESP32 (ESP-IDF in VS Code) MFRC522 + MQTT + PYTHON TKinter +SQLite

 ESP32 (ESP-IDF in VS Code) MFRC522 + MQTT + PYTHON TKinter +SQLite  ESP32 VS Code 程式 ; PlatformIO Project Configuration File ; ;   Build op...