Hi,
I have a question regarding MQTT_Client component. When i use it in a loop, program is not going further until a message is received or a "timeout" occur (i measured ~80seconds)
I want to "leave" the MQTT_Client function after ~50seconds, is there another way, beside using an interrupt ?
Tiberiu
MQTT_Client
-
- Posts: 9
- http://meble-kuchenne.info.pl
- Joined: Thu Dec 10, 2020 6:04 pm
- Location: Romania
-
- Valued Contributor
- Posts: 489
- Joined: Mon Dec 07, 2020 1:00 pm
- Has thanked: 83 times
- Been thanked: 262 times
Re: MQTT_Client
I don't think any of the functions should be blocking, or wait that long.
What is it running on, that is, what target device and communication method?
Could you post your program?
I will look tomorrow to see if I still have any examples.
There might be an example on our component Wiki, I cannot check just now.
What is it running on, that is, what target device and communication method?
Could you post your program?
I will look tomorrow to see if I still have any examples.
There might be an example on our component Wiki, I cannot check just now.
-
- Valued Contributor
- Posts: 489
- Joined: Mon Dec 07, 2020 1:00 pm
- Has thanked: 83 times
- Been thanked: 262 times
Re: MQTT_Client
Hi,
Looks like most of it is correct.
Apart from, a return of zero from the Read function should not be treated as an error.
It simply means that the broker hasn't sent a message.
So a return value of zero is going to be more often than not.
So you don't need to try reconnecting in this case.
The only thing you need to do periodically is send a Ping
this will keep the connection alive, and also lets you know if the connection is alive
(a zero return means the connection has dropped)
Also, the reconnection code inside an interrupt is not a good idea.
You could either set a flag in the interrupt and do the reconnection in the main, triggered by the flag.
Or put a counter in your main loop, use that to send the Ping, then do a reconnect if that fails.
If you did that, I would also suggest reducing the 500mS delay, then use the counter to send the Ping say every 30 seconds.
Could you explain your initial question a bit more?
Do you think that one of the MQTT functions is taking too long, if so, which function?
The timeout you refer to could be related to the broker keep alive time, it might be aborting.
Hence my note about the Ping.
hope that helps
Looks like most of it is correct.
Apart from, a return of zero from the Read function should not be treated as an error.
It simply means that the broker hasn't sent a message.
So a return value of zero is going to be more often than not.
So you don't need to try reconnecting in this case.
The only thing you need to do periodically is send a Ping
this will keep the connection alive, and also lets you know if the connection is alive
(a zero return means the connection has dropped)
Also, the reconnection code inside an interrupt is not a good idea.
You could either set a flag in the interrupt and do the reconnection in the main, triggered by the flag.
Or put a counter in your main loop, use that to send the Ping, then do a reconnect if that fails.
If you did that, I would also suggest reducing the 500mS delay, then use the counter to send the Ping say every 30 seconds.
Could you explain your initial question a bit more?
Do you think that one of the MQTT functions is taking too long, if so, which function?
The timeout you refer to could be related to the broker keep alive time, it might be aborting.
Hence my note about the Ping.
hope that helps
Re: MQTT_Client
Hi,
My setup is:
raspberry pi with mosquitto mqtt broker
mqtt client running on node red (installed on the same raspberry pi)
mqtt client on ESP32-wroom-32d
node red mqtt client is requesting data from ESP32 mqtt client
"Do you think that one of the MQTT functions is taking too long, if so, which function?"
READ function;
when is called in a loop, for example, function is returning a result in 2 cases:
- a message is received
- a keep alive timeout
otherwise the main program is not going through the flow, is stuck in READ function
I tried to PING every 5seconds, but no effect, after ~80 seconds READ function is returning a "0",
when READ is returning "0", i have to disconnect-reconnect and everything is back to normal
In case raspberry pi is rebooted, client - broker connection is lost and main program is "stuck" in READ function.
After program is restarted (ESP32 reboot), connection with broker is reestablished.
For this case i did not find a "good" solution to reestablish the connection, besides using disconnect-reconnect inside an interrupt.
My setup is:
raspberry pi with mosquitto mqtt broker
mqtt client running on node red (installed on the same raspberry pi)
mqtt client on ESP32-wroom-32d
node red mqtt client is requesting data from ESP32 mqtt client
"Do you think that one of the MQTT functions is taking too long, if so, which function?"
READ function;
when is called in a loop, for example, function is returning a result in 2 cases:
- a message is received
- a keep alive timeout
otherwise the main program is not going through the flow, is stuck in READ function
I tried to PING every 5seconds, but no effect, after ~80 seconds READ function is returning a "0",
when READ is returning "0", i have to disconnect-reconnect and everything is back to normal
In case raspberry pi is rebooted, client - broker connection is lost and main program is "stuck" in READ function.
After program is restarted (ESP32 reboot), connection with broker is reestablished.
For this case i did not find a "good" solution to reestablish the connection, besides using disconnect-reconnect inside an interrupt.