Hello everyone! ![]()
TL;DR
How can I initiate a non-blocking connection to a server (e.g., Telegram) so the code doesn't freeze while waiting for the result, and I can check later if the connection was successful?
I'm working on a project where I use an ESP32 to communicate with Telegram through a bot. This part works fine: I can exchange messages without problems.
However, the ESP32 also controls some temperature sensors and applies a filter to the data, which requires a constant sampling period.
Here’s the issue:
When the Telegram server is not reachable (ESP connected to the WiFi without internet for eg.), calling:
Serv_Telegram.connect(Serveur_Telegram, 443);
can block the code for up to 20 seconds before it times out.
I’ve already tried using:
Serv_Telegram.setHandshakeTimeout(3);
which reduces the blocking time, but it’s still blocking nonetheless.
What I’m trying to achieve is something like how the WiFi connection works:
WiFi.begin(SSID, PASS); // Initiates connection
// Later in the loop:
if (WiFi.status() == WL_CONNECTED) { ... }
This way the connection the WiFi could take 20sec but it doesn't matter because in the meantime the ESP is free to do something else.
But with WiFiClientSecure, the .connect() call is blocking.
So:
Is there a way to start the connection without blocking, and later check if the connection succeeded?
If needed, I can give more details.
Thanks a lot for any ideas or tips!