ESP32 Telegram「長輪詢 (Long Polling)」
/*******************************************************************
* An example of setting a long poll, this will mean the request
* for new messages will wait the specified amount of time before
* returning with no messages
*
* This should reduce amount of data used by the bot
*******************************************************************/
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
// Wifi network station credentials
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// Telegram BOT Token (Get from Botfather)
#define BOT_TOKEN "7738940254:AAHbrWu9ovb1BKPQyWsbNSjNxfCGCrEWU-o"
const unsigned long BOT_MTBS = 1000; // mean time between scan messages
unsigned long bot_lasttime; // last time messages' scan has been done
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
void handleNewMessages(int numNewMessages)
{
for (int i = 0; i < numNewMessages; i++)
{
bot.sendMessage(bot.messages[i].chat_id, bot.messages[i].text, "");
}
}
void setup()
{
Serial.begin(115200);
Serial.println();
// attempt to connect to Wifi network:
Serial.print("Connecting to Wifi SSID ");
Serial.print(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.print("\nWiFi connected. IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Retrieving time: ");
configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
time_t now = time(nullptr);
while (now < 24 * 3600)
{
Serial.print(".");
delay(100);
now = time(nullptr);
}
Serial.println(now);
bot.longPoll = 60;
}
void loop()
{
if (millis() - bot_lasttime > BOT_MTBS)
{
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages)
{
Serial.println("got response");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
Serial.println("I will happen much less often with a long poll");
bot_lasttime = millis();
}
}
這段程式碼的核心主題是 「長輪詢 (Long Polling)」。相較於一般的快速輪詢,長輪詢能大幅減少網路流量與資源消耗,對使用電池供電或有流量限制的 ESP32 專案來說非常重要。
以下是詳細的逐行解說:
1. 核心技術:什麼是 Long Polling?
在 setup() 函式中,這行是重點:
C++
bot.longPoll = 60; // 設定長輪詢時間為 60 秒
一般輪詢 (Short Polling):ESP32 問伺服器「有新訊息嗎?」,伺服器立刻回答「有」或「沒有」。這會產生大量的空封包交換。
長輪詢 (Long Polling):ESP32 問伺服器「有新訊息嗎?」。如果沒有,伺服器會「握著請求不放」,直到有新訊息進來或是 60 秒時間到才回覆。
2. 處理新訊息 (handleNewMessages)
這部分維持最基礎的 Echo 功能:
C++
void handleNewMessages(int numNewMessages) {
for (int i = 0; i < numNewMessages; i++) {
// 收到什麼文字,就回傳給同一個 chat_id
bot.sendMessage(bot.messages[i].chat_id, bot.messages[i].text, "");
}
}
3. 初始化設定 (setup)
WiFi 連線:連上模擬器的 Wokwi-GUEST。
憑證設定:
secured_client.setCACert用於 HTTPS 安全通訊。網路對時:
configTime同步 NTP 時間。設定 Long Poll:如前所述,設定為 60 秒,這意味著
bot.getUpdates函式在最糟情況下會讓程式卡住 60 秒等待回應(但這期間伺服器會保持連線,不會浪費流量)。
4. 主迴圈邏輯 (loop)
你會發現這個迴圈的行為與之前的範例大不相同:
C++
void loop() {
if (millis() - bot_lasttime > BOT_MTBS) {
// 這行會因為 longPoll = 60,而在此處停留等待最高 60 秒
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
Serial.println("got response");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
// 只有在收到訊息或 60 秒超時後,這行才會被印出
Serial.println("I will happen much less often with a long poll");
bot_lasttime = millis();
}
}
5. 長輪詢的優缺點分析
| 特性 | 優點 | 缺點 |
| 流量消耗 | 極低。減少了大量的重複請求標頭。 | 無。 |
| 反應速度 | 快。有訊息時伺服器會立刻推播回傳。 | 無。 |
| 程式架構 | 適合單純的機器人。 | 會阻塞程式。因為 bot.getUpdates 會等待,你的 loop 其他功能(如感測器讀取)也會跟著停擺。 |

沒有留言:
張貼留言