Page 1 of 1

MQTT starts too slowly

Posted: Fri Aug 05, 2022 10:02 am
by richardwuday
When esp_mqtt_client_start was caleed,it takes 7 seconds to return connected state.Then I found that it took less than 1 second to connect from the beginning to return OK. Is there any way to shorten the time ahead?

Re: MQTT starts too slowly

Posted: Sat Aug 06, 2022 12:56 am
by MMliam
When you say it takes 7 seconds to re-connect that suggests you're using your wifi network for MQTT? While there are things that can be done to improve network connection time, it would be much better to use ESP-Now; reducing connection times from seconds to milliseconds. If you interested I can provide you with information how to implement ESP-Now with MQTT.

Mike

Re: MQTT starts too slowly

Posted: Sun Aug 07, 2022 4:22 pm
by richardwuday
Can ESP-NOW improve this situation? If so, i am interested in it, and what are the requirements for the MQTT server? As the MQTT server belongs to the operator,and can not be changed.

Re: MQTT starts too slowly

Posted: Sun Aug 07, 2022 5:48 pm
by MMliam
The first thing you'll need to do is create a Gateway using a single ESP32. The gateway receives ESP-Now from your sensors/devices (using the ESP32 AP interface), is connected to WiFi (using the ESP32 STA interface) and forwards the data to the MQTT broker. ESP-Now sending devices can request an acknowledgement (ACK) from the gateway. The total ESP-Now connect to ACK is around 25ms; this is constant whether a request for ACK is set "True", or "False". Because the Gateway always stays connected to the WiFi-network there is no network connection lag (your 7 secs).

Here are a couple examples of how to implement and ESP-Now gateway:
https://randomnerdtutorials.com/esp32-e ... eb-server/
https://microcontrollerslab.com/esp32-e ... FI_AP_STA.

These examples use Arduino; I use microPython. My gateway connects with an MQTT broker in Node-Red on a PC.
If you're interested in Node-Red here's a couple of links:
Node-Red: https://www.youtube.com/playlist?list=P ... qFcXBkHy-6
Although Node-Red & MQTT were created by IBM, MQTT is not native to Node-Red, but MQTT node-extensions can be added:
http://stevesnoderedguide.com/install-m ... r-node-red

Have fun,
Mike

Re: MQTT starts too slowly

Posted: Sun Aug 07, 2022 11:57 pm
by tommeyers
I am not sure where the bottleneck is from your description.
Connecting to an mqtt server involves the server.
Have you used verbose on the sever or used Wireshark or investigated where the roadblock is? I recommend you identify the problem first.

Tom Meyers

Re: MQTT starts too slowly

Posted: Mon Aug 08, 2022 2:03 am
by MMliam
Tom,

Richard is not having a "bottleneck" issue. As I understand it, the delay occurs every time his device re-connects to the wifi. Connections to wifi can take several seconds, and are also variable, as a function of distance from the router.

I had the same problem with remote sensors that needed to be in deep-sleep to save power. When an event occurred, bringing a sensor out of deep-sleep it needed to re-connect to the network. The synchronization of sensors required normalization of the connection time, slowing down the entire system. The solution provided by Espressif to address just this problem is ESP-Now.

Mike

Re: MQTT starts too slowly

Posted: Fri Aug 12, 2022 12:48 am
by tommeyers
I think the idea that investigating and defining the problem isn't necessary is an interesting approach to problem solving.

The wrong approach I also think.

It is incumbent of the poster to bound the problem. The methods of bounding are simple but necessary.

Before I choose a solution I assure I know the problem. It is a step that can't be skipped.

MQTT has some keep-alive parameters and they have not been discussed.

The WiFi connection also throws some 15+ events that have not been monitored.

The problem is not known.

I had no problems with my mqtt connections. But I had a lot of events fired.

Tom