CTBot
CTBot copied to clipboard
Asynchronous requests
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.
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
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
Hello Daniel, recapping:
- you have several sensor and when triggered they send a Telegram message
- meanwhile, in the main loop, you have the check for new incoming messages
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:
- when triggered, all the sensors will enable his own flag. For example, when a fluid tank level sensor notify that is nearly empty, you could put a (global/object) boolean variable like
isTankEmpty
to true. In this way the computational time used is very short and you can manage it with interrupts. Just notify, don't manage it; - in the main loop:
- +you can check for all the sensors flag and manage it (for example, you can switch on an electropump/electrovalve to fill the tank). In this way if you need to send telegram messages, you can collect all the messages and send it in only one call concatenating them (in other words, you will send a string made up by several notifications). Remember to put the related flag to false as indication that the event have been processed.
-
- check for incoming messages
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 ;-)
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!
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!!