2025年7月14日 星期一

Send a Telegram message using ESP32

 Send a Telegram message using ESP32







<<WOKWI ESP32程式>>

#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>

// --- WiFi Configuration ---
const char* ssid = "Wokwi-GUEST"; // Replace with your network credentials
const char* password = "";

// --- Telegram Bot Configuration ---
#define BOTtoken "801227100986:AAGymymK9_d1HcTGJWl3mtqHmilxB64_5Zw" // your Bot Token (Get from Botfather)
#define CHAT_ID "7925218469"
//需修改


WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);

// Task handle for the periodic message task
TaskHandle_t messageTaskHandle = NULL;

// Function for the periodic message task
void sendPeriodicMessage(void *pvParameters) {
  for (;;) {
    bot.sendMessage(CHAT_ID, "Hello World!");
    Serial.println("bot.sendMessage Hello World!");
    vTaskDelay(pdMS_TO_TICKS(20000)); // Delay for 20 seconds
  }
}

void setup() {
  Serial.begin(115200);

  // Connect to Wi-Fi
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // For ESP32/ESP8266

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Create the periodic message task
  // INCREASE STACK SIZE HERE from 2048 to something larger, e.g., 4096 or 8192
  xTaskCreate(
    sendPeriodicMessage,     // Task function
    "MessageTask",           // Name of the task
    4096,                    // <--- INCREASED STACK SIZE (try 4096 first, if still error, try 8192)
    NULL,                    // Parameter to pass to the task
    1,                       // Priority (0 is the lowest)
    &messageTaskHandle       // Task handle
  );
}

void loop() {
  // FreeRTOS tasks manage their own scheduling, so the loop can be empty.
}

沒有留言:

張貼留言

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...