diff --git a/include/Telegram.h b/include/Telegram.h index 62064df..a66558c 100644 --- a/include/Telegram.h +++ b/include/Telegram.h @@ -31,5 +31,7 @@ public: void init(); void cyclic(unsigned long now); + + int sendMessage(const String& chat_id, const String& text); }; diff --git a/include/secrets.h_template b/include/secrets.h_template index a6e730c..6810dcf 100644 --- a/include/secrets.h_template +++ b/include/secrets.h_template @@ -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" diff --git a/platformio.ini b/platformio.ini index ad99ccb..2e0a0ff 100644 --- a/platformio.ini +++ b/platformio.ini @@ -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 diff --git a/src/Telegram.cpp b/src/Telegram.cpp index bed9f05..2ff3aee 100644 --- a/src/Telegram.cpp +++ b/src/Telegram.cpp @@ -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; } -} \ No newline at end of file +} + +int Telegram::sendMessage(const String& chat_id, const String& text) +{ + return bot.sendMessage(chat_id, text); +} diff --git a/src/main.cpp b/src/main.cpp index f60194e..14e4193 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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 */