shurillu / CTBot

A simple (and easy to use) Arduino Telegram BOT Library for ESP8266/ESP32
MIT License
147 stars 34 forks source link

Problem after calling soft reset (ESP.Restart()) #51

Closed mad-b closed 4 years ago

mad-b commented 4 years ago

Hello. I am making one of the commanbs to b e a reset command - a simple "reset" token that you can send from the telegram chat, to... soft reset the ESP8266. So, I write "reset" on the chat, send and it receives and restarts the ESP - as expected.

However, it enters an endless restart loop like if the last "reset" command keeps in the messages in put queue somehow, and soon after the soft restart, it ends up in the if-else containing the "reset" and restarts all over - and over and over ...

Is there some issue, like I should wait clear or check something before reset, to avoid leftovers of some sort inside the system?

Can you enlight me about what can be happening and how to solve this? Thanks

ivan140 commented 4 years ago

Hi,

Had the same issue with ESP 8266. My workaround was:

else if (m.text.indexOf("/reset") > -1) {
                m.text = "";
                if (!config.justResetet) {
                  bot.sendMessage(m, "reset");
                  config.justResetet = true;
                  EEPROM.put(0, config);
                  EEPROM.commit();
                  delay(3000);
                  ESP.restart();
                } else {
                  config.justResetet = false;
                  EEPROM.put(0, config);
                  EEPROM.commit();
                }
}

P.S.: it's maybe more of a feature(bug) of the ESP itself, rather than the CTBot library. There are some issues and comments about this function and how it works. If I'm not mistaken, it saves the complete state of variables and so on and repeats the whole iteration of the loop.

shurillu commented 4 years ago

Hello mad-b, hello ivan140,

the issue is relate to the method how Telegram knows if a message is corrected collected or not. Everytime a getNewMessage() method is called, it send to the Telegram server the last received message, so the Telegram server knows that message is corrected received and send all the new messages. Going back to your issue, this simple trick will resolve the problem: before calling the ESP.restart(), call the getNewMessage() method to confirm the reset message is arrived. You don't need to handle the message (if any) fetched by the getNewMessage() because it will not be confirmed so after the reset, it will be fetched one more time. Try this:

#include "CTBot.h"
CTBot myBot;
String ssid  = "mySSID"    ; // REPLACE mySSID WITH YOUR WIFI SSID
String pass  = "myPassword"; // REPLACE myPassword YOUR WIFI PASSWORD, IF ANY
String token = "myToken"   ; // REPLACE myToken WITH YOUR TELEGRAM BOT TOKEN
void setup() {
    Serial.begin(115200);
    Serial.println("Starting TelegramBot...");
    myBot.wifiConnect(ssid, pass);
    myBot.setTelegramToken(token);
}
void loop() {
    TBMessage msg;
    if (myBot.getNewMessage(msg)) {
     if (msg.text.equalsIgnoreCase("restart")) {
        myBot.getNewMessage(msg); // confirm the restart message is received
        ESP.restart();
     } else 
          myBot.sendMessage(msg.sender.id, msg.text);
    }    
    delay(500);
}
mad-b commented 4 years ago

Thanks for the info, Stefano. I was suspecting it was something of the sort :)