2025年5月20日 星期二

ESP32 DHT22 + Telegram /start:傳送歡迎訊息 /help:顯示可用指令清單 /status:立即查詢目前溫度與濕度

  ESP32 DHT22 + Telegram

 /start:傳送歡迎訊息  /help:顯示可用指令清單  /status:立即查詢目前溫度與濕度





Telegram 端指令使用 

1. 找到你的 Bot

在 Telegram 中搜尋你用 @BotFather 建立的 Bot 名稱,例如:

@MyEsp32MonitorBot

點擊開始對話後,按一下 /start,啟動 Bot。

2. 可用指令列表

以下是你可以在對話視窗輸入的指令:

指令功能說明
/start啟動 Bot,顯示歡迎訊息
/help顯示所有可用的指令說明
/status即時查詢目前的溫度與濕度



//   /start:傳送歡迎訊息
//   /help:顯示可用指令清單
//   /status:立即查詢目前溫度與濕度

#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 "802390216815:AAE0KA1pbm5Ng00VCvO571JWA_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 "719652184629"

// 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;

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 (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("變化 -> 傳送");
    }
  }

  // 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\n";
          welcome += "輸入 /help 來查看所有指令。";
          bot.sendMessage(chat_id, welcome, "");
        }
        else if (text == "/help") {
          String helpMsg = "**可用指令:**\n";
          helpMsg += "/start - 啟動對話\n";
          helpMsg += "/status - 查詢目前溫濕度\n";
          helpMsg += "/help - 顯示指令說明";
          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 {
          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...