Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Longpoll - non blocking version #143

Open
fmarzocca opened this issue Feb 13, 2020 · 28 comments
Open

Longpoll - non blocking version #143

fmarzocca opened this issue Feb 13, 2020 · 28 comments

Comments

@fmarzocca
Copy link

fmarzocca commented Feb 13, 2020

My ESP will receive no more than 5 or 6 commands from Telegram in 24 hours, but in such a case, I need a fast response, action and notification.

I have some doubt about using Longpoll. Who can give me Pros & Cons on Longpoll?
In addition, would Long poll in some way delay the execution of other commands in the loop?

I am asking this question because I have noticed that when I set longPoll = 60, my device does not answer anymore to OTA, due to a timeout.

bot.longPoll = 60;

void loop() {
 server.handleClient();
 ArduinoOTA.handle();
 timeClient.update();
 
 if (millis() > Bot_lasttime + Bot_mtbs)  {
   int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
   while (numNewMessages) {
     handleNewMessages(numNewMessages);
     numNewMessages = bot.getUpdates(bot.last_message_received + 1);
   }
   Bot_lasttime = millis();
 }
}

@witnessmenow
Copy link
Owner

witnessmenow commented Feb 13, 2020 via email

@fmarzocca
Copy link
Author

fmarzocca commented Feb 13, 2020

Thank you Brian,
the problem is that my application is activated by:

  • a telegram command
  • a pin interrupt
  • a http post

and I am noticing that everything is slowed down (until timeout) if I use longPoll. On the other hand, without a longPoll, my ESP8266 is constantly sending/receiving bits (the blue led is almost always on). (I have edited my first post)

@RomanLut
Copy link

Longpool is implemented using busy wait in this library.
getUpdates() blocks for {longPoll} seconds.
Thus it is not usable in projects where you need to do some tasks in loop().
getUpdates() should just open connection and check if bytes are available in loop() without blocking.
I modified library this way, getUpdates() can be called with any frequency without blocking.
There still will be blocking when bot has to send responce. Or there is connection timeout. To fully fix this, bot has to be based on asynctcp library.

Attached is modified library.
esp8266firmware.zip

@fmarzocca
Copy link
Author

@RomanLut , do you mean that with your modified library I could set longPoll freely, without blocking other activities in the loop (such as ArduinoOTA.handle() or server.handleClient()?

@RomanLut
Copy link

Do not change longPool memeber, it is private now and set to 50 seconds.
Just call getUpdates() with any frequency without blocking.
There still will be blocking when you send messages.

BTW I have tried to implement bot based on ESPAsyncTCP, but it does not work because TLS 1.2 is not supported by ESPAsyncTCP. api.telegram.org requires TLS 1.2 now.

@fmarzocca
Copy link
Author

fmarzocca commented Feb 27, 2020

OK. I will give it a try and report back here. Should I have just to replace the .cpp and .h library files with yours?

@fmarzocca
Copy link
Author

fmarzocca commented Feb 27, 2020

I got this compiler error:

undefined reference to UniversalTelegramBot::sendMessage(String, String, String, bool)'`

@RomanLut
Copy link

Please double check that you use files from archive above. sendMessage() is properly declared():
.h
bool sendMessage(String chat_id, String text, String parse_mode = "", bool disable_notification = false);
.cpp:
bool UniversalTelegramBot::sendSimpleMessage(String chat_id, String text, String parse_mode, bool disable_notification)

@fmarzocca
Copy link
Author

Yes, I have looked at your code and I have seen it correctly declares the function.
The problem is that the compiler looks like still using the old library! I have replaced .cpp and .h files with yours in /libraries/UniversalTelegramBot/src

@fmarzocca
Copy link
Author

Fixed. I had to refresh the libry cache

@fmarzocca
Copy link
Author

Tested.
THIS IS GREAT! Finally I have get rid of all the huge network traffic, and the longPoll is not blocking my other functions in the loop!

Thank you very much, you should issue a Pull Request or fork this library with yours!
Great job, @RomanLut !

@fmarzocca
Copy link
Author

What is the use of disable_notification in sendMessage()?

@RomanLut
Copy link

You can pass true to disable notification sound.
I am sending log messages silently and alarm messages with notification sound, which I set to alarm sound in chat options in telegram app on phone.

@fmarzocca
Copy link
Author

You can pass true to disable notification sound.
I am sending log messages silently and alarm messages with notification sound, which I set to alarm sound in chat options in telegram app on phone.

Thanks

@RomanLut
Copy link

Note that this update does not solve all problems.
If you call sendMessage(), it will block for <5 seconds and next getUpdates() will block to <5 seconds once.
Worse, if you have connection problems, each getUpdates() will block for 5-50 seconds because client->connect() will block while trying to establish connection to api.telegram.org.
Also note issue #38
If you send a message with long text (1300 bytes), library will fall into endless update cycle generating huge traffic. There is a fix which is not merged.

@fmarzocca
Copy link
Author

fmarzocca commented Feb 27, 2020

Ok, I can accept that short block (<5sec) for my purposes. I hope not to have connection problems (the extender AP is close to the device) and I don't need to send long messages.
How can I subscribe or be notified of library's updates?
Do you have a github repo?

@RomanLut
Copy link

No, I do not have repo and there will be no more updates from me because I will not be using this library (I can not accept any blocking in my device).

@fmarzocca
Copy link
Author

No, I do not have repo and there will be no more updates from me because I will not be using this library (I can not accept any blocking in my device).

Maybe Brian @witnessmenow can include your mods into the official library...
There is no other solution to avoid a short block, unless remove the longPoll and accept 30/40 GB of junk network activity per month.
I had a very good experience with Raspberry and library telepot, but that's another story.

@fmarzocca
Copy link
Author

@RomanLut I noticed a little delay in responses (about 4-5 seconds). Is it normal or there is a way to expedite it?

@RomanLut
Copy link

Library maintains single connection because esp8266 does not have enough memory for two secured connections.
If you call sendMessage(), library closes connection opened for longpool getUpdates(), reopens connection and sends message. Secure connection handshaking requires ~2-5 seconds on esp8266. I do no think something can be improved here.

@fmarzocca
Copy link
Author

fmarzocca commented Feb 29, 2020 via email

@RomanLut
Copy link

I do not have experience with ESP32.

@fmarzocca
Copy link
Author

fmarzocca commented Mar 31, 2020

@RomanLut It's a month that I am using your modified library in my device. Once a week it happens that the device gets blocked: it is pingable, but it does not respond to Telegram or send messages. I have tried with FW 2.6.1 and 2.5.3, but it's the same. I have to recycle power. Do you have any idea?

@fmarzocca
Copy link
Author

@RomanLut since today Telegram servers has changed something and the library is not working anymore. I have seen that Brian release v.1.20. Do you have modified your code too? Thanks

@witnessmenow
Copy link
Owner

I'm not sure what the current status of this is, is it still worth looking into?

@witnessmenow witnessmenow changed the title Longpoll Longpoll - non blocking version Sep 30, 2020
@fmarzocca
Copy link
Author

fmarzocca commented Oct 1, 2020 via email

@fmarzocca
Copy link
Author

fmarzocca commented Jan 1, 2021

I'm not sure what the current status of this is, is it still worth looking into?

@witnessmenow Brian, is there a chance to embed this mod into the latest library update? The library is generating 25GB of traffic/month with LongPoll=0 and I need to use a non-blocking LongPoll.

@droidprova
Copy link

droidprova commented Feb 7, 2021

that would be the best, as the roman patch doesn't work with the latest release 1.3.0 and I don't want to lose the benefits of the latest version:

C:\Users\user\Documents\Arduino\libraries\Universal-Arduino-Telegram-Bot-master\src\UniversalTelegramBot.cpp:797:20: error: 'keyboardBuffer' was not declared in this scope DynamicJsonBuffer keyboardBuffer;
Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants