add rebooting the ESP when WiFi is disconnected and add debugger Telegram ID

This commit is contained in:
Marcel Walter 2026-02-17 00:30:25 +01:00
parent 5ce8d2ee3e
commit dd2185216a
5 changed files with 40 additions and 2 deletions

View File

@ -31,5 +31,7 @@ public:
void init();
void cyclic(unsigned long now);
int sendMessage(const String& chat_id, const String& text);
};

View File

@ -2,6 +2,7 @@
#define TELEGRAM_BOT_TOKEN "123123123123:BBCeAAewGN_wayA123VnbqRNJL4l3dpXy2S"
#define TELEGRAM_BOT_OWNER "321123321"
//#define DEBUGGER_TELEGRAM_ID "32121321" //address to send debugging information to via Telegram
#define WIFI_SSID "Wifi_SSID"
#define WIFI_PASSWORD "secretPassword"

View File

@ -17,7 +17,7 @@ lib_deps =
witnessmenow/UniversalTelegramBot @ ~1.3.0
adafruit/Adafruit GFX Library @ ~1.12.3
knolleary/PubSubClient @ ~2.8
build_flags=-DTELEGRAM_DEBUG
; build_flags=-DTELEGRAM_DEBUG
; OTA update path: hostname is flipdottelegram.fritz.box
upload_protocol = espota
upload_port = 172.30.30.50

View File

@ -15,6 +15,12 @@ void Telegram::handleNewMessages(int numNewMessages)
String chat_id = bot.messages[i].chat_id;
String text = bot.messages[i].text;
#ifdef DEBUGGER_TELEGRAM_ID
bot.sendMessage(DEBUGGER_TELEGRAM_ID, String("Ich habe von ") + bot.messages[i].from_name + "(" + bot.messages[i].from_id + ")" +
"im Chat " + bot.messages[i].chat_title + "(" + bot.messages[i].chat_id + ")" +
"folgenden Text empfangen:\n\n" + text);
#endif
String from_name = bot.messages[i].from_name;
if (from_name == "")
from_name = "Guest";
@ -55,6 +61,9 @@ void Telegram::handleNewMessages(int numNewMessages)
if (DisplayText.isEmpty())
{
bot.sendMessage(chat_id, "Display text ist leer!!");
#ifdef DEBUGGER_TELEGRAM_ID
bot.sendMessage(DEBUGGER_TELEGRAM_ID, String("Ich habe an ") + chat_id + " folgenden Text geschrieben:\n\n" + "Display text ist leer!!");
#endif
}
else
{
@ -62,6 +71,9 @@ void Telegram::handleNewMessages(int numNewMessages)
Answer += DisplayText;
Answer += "\".";
bot.sendMessage(chat_id, Answer);
#ifdef DEBUGGER_TELEGRAM_ID
bot.sendMessage(DEBUGGER_TELEGRAM_ID, String("Ich habe an ") + chat_id + " folgenden Text geschrieben:\n\n" + Answer);
#endif
}
break;
}
@ -79,6 +91,9 @@ void Telegram::handleNewMessages(int numNewMessages)
}
bot.sendMessage(chat_id, welcome);
#ifdef DEBUGGER_TELEGRAM_ID
bot.sendMessage(DEBUGGER_TELEGRAM_ID, String("Ich habe an ") + chat_id + " folgenden Text geschrieben:\n\n" + welcome);
#endif
}
}
}
@ -142,6 +157,9 @@ void Telegram::init()
bot.setMyCommands(jsonString);
bot.sendMessage(TELEGRAM_BOT_OWNER, "Hi, ich bin online.");
#ifdef DEBUGGER_TELEGRAM_ID
bot.sendMessage(DEBUGGER_TELEGRAM_ID, "Hi, ich bin online.");
#endif
bot_lasttime = millis();
}
@ -166,4 +184,9 @@ void Telegram::cyclic(unsigned long now)
bot_lasttime = now;
}
}
}
int Telegram::sendMessage(const String& chat_id, const String& text)
{
return bot.sendMessage(chat_id, text);
}

View File

@ -218,6 +218,7 @@ void loop()
unsigned long now = millis();
static unsigned long lastDisplayRefresh = now;
static unsigned long lastTelegramRefresh = now;
static unsigned long lastWifiDisconnect = now;
if (WiFi.status() == WL_CONNECTED)
{
@ -283,6 +284,9 @@ void loop()
// if wifi is not connected wait for connection
if (WiFi.status() != oldWifiStatus)
{
/* remember las disconnection time to restart if needed */
lastWifiDisconnect = now;
/* we freshly disconnected so tell everybody that we are offline */
Text = "WLAN \"";
Text += WIFI_SSID;
@ -294,6 +298,14 @@ void loop()
oldWifiStatus = WiFi.status();
}
if ((now - lastWifiDisconnect) > (5 /* min */ * 60 /* sec/min */ * 1000 /* millisec/sec */))
{
#ifdef DEBUGGER_TELEGRAM_ID
tele.sendMessage(DEBUGGER_TELEGRAM_ID, "Wifi disconnected - restarting now");
#endif
esp_restart();
}
}
/* FlipDot Handling */