shurillu / CTBot

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

Asynchronous requests #15

Open Salomon-MH opened 5 years ago

Salomon-MH commented 5 years ago

Hello,

first of all, thank you for your really easy to use library. I was wondering if it's possible for you to implement an asynchronous callback for the getNewMessage function, as it stops the loop for up to 5 seconds for me sometimes.

I dont know if it's related to this issue, but I'm also experiencing weird "freezes" of my ESP8266, so that after a few hours it wont answer anymore and no other services such as my ESP8266Webserver is responding either, while the device is still logged into my WiFi and pinging back. Any clues?

Thanks.

shurillu commented 5 years ago

Hello Salomon, I encountered something similar when I left server connections "open". In other words, if you look inside the sendCommand() method I open and close the connection to the server within the method itself (with WiFiClientSecure::flush() and WiFiClientSecure::close() method). Without this trick (and bearing only on the destructor of the WiFiClientSecure object) the library occasionally will hang. This effect will be greater if I instantiate and connect the WiFiClientSecure object for the entire lifetime of the CTBot object (constructor time)... So my advice is: try to open and close connection in the same "time space" (open connection, fetch your data then close connection). Let me know if this will help.

Stefano

Salomon-MH commented 5 years ago

Hello Stefano,

thanks for your fast answer. Do you mind helping me out even a bit more? So I do have a CTBot instance (e.g. myBot). I'm calling myBot.getNewMessage(msg) every few seconds in my loop(). I do have some external triggers coded into my program (e.g. temperature sensors, ...) that also call myBot.sendMessage(ID, msg). So what exactly are you suggesting?

Do I have to call some methods after and/or before each call, including receiving and sending? Or when do I have to add which command?

I got it to work for much longer right now as I'm checking for new messages only every 5 seconds, but I'm not too sure if this fixed the issue or just makes the timespan until the next "hang" longer.

Thanks in advance.

Daniel

shurillu commented 5 years ago

Hello Daniel, recapping:

If the system that you realized is not a time critical system (like a nuclear plant ^_^) the first thing that pop out in mi mind is:

This could be a solution?

Stefano

P.S.: the idea of a non blocking asynchronous getNewMessage() method is a good one (maybe the sendMessage() too), but for now I'm a bit busy with another project (small Tank Battle, see mine others repositories). This will be a item in the to do list ;-)

Salomon-MH commented 5 years ago

Hey Stefano, while this is a good way doing it on non-time-critical applications (as you mentioned), this is not applicable in my case. While I'm obviously not running a nuclear plant, I use the library for a home made house alarm 😄 Thanks though. Great to hear that such feature is planned and thanks for your fast answers. Good luck on your projects!

pdor90 commented 5 years ago

hi , i have a similar problem , in my clock based on a wemos d1 and a neopixel matrix i'm trying to use your library for set the allarm and the time via a telegram bot , the library work with no problem , but the timeout of the getNewMessage(msg) is long , as the check of message is in the main loop. my question is , it's possible to reduce timeout time if no message are present to a few millisecond for example ?

Ty very much for the great library you have made!!