2025年5月20日 星期二

ESP32 DHT22 + Telegram 每 3 秒檢查一次溫濕度 只有變化才發送至 Telegram 支援 /status 指令回報目前溫濕度

 ESP32 DHT22 + Telegram   

  1. 每 3 秒檢查一次溫濕度

  2. 只有變化才發送至 Telegram

  3. 支援 /status 指令回報目前溫濕度




//每 3 秒檢查一次溫濕度
//只有變化才發送至 Telegram
//支援 /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 "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"

// DHT 感測器設定
#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 連線
  WiFi.begin(ssid, password);
  Serial.print("連線中");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWiFi 已連線");
  Serial.println(WiFi.localIP());

  // Telegram HTTPS 憑證略過
  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) + " %";

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

        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, "");
          }
        }
      }
      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...