MQTT starts too slowly

richardwuday
Posts: 7
Joined: Wed Nov 10, 2021 5:59 am

MQTT starts too slowly

Postby richardwuday » Fri Aug 05, 2022 10:02 am

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?

MMliam
Posts: 11
Joined: Thu Aug 04, 2022 12:31 am

Re: MQTT starts too slowly

Postby MMliam » Sat Aug 06, 2022 12:56 am

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

richardwuday
Posts: 7
Joined: Wed Nov 10, 2021 5:59 am

Re: MQTT starts too slowly

Postby richardwuday » Sun Aug 07, 2022 4:22 pm

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.

MMliam
Posts: 11
Joined: Thu Aug 04, 2022 12:31 am

Re: MQTT starts too slowly

Postby MMliam » Sun Aug 07, 2022 5:48 pm

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

tommeyers
Posts: 184
Joined: Tue Apr 17, 2018 1:51 pm
Location: Santiago, Dominican Republic

Re: MQTT starts too slowly

Postby tommeyers » Sun Aug 07, 2022 11:57 pm

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
IT Professional, Maker
Santiago, Dominican Republic

MMliam
Posts: 11
Joined: Thu Aug 04, 2022 12:31 am

Re: MQTT starts too slowly

Postby MMliam » Mon Aug 08, 2022 2:03 am

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

tommeyers
Posts: 184
Joined: Tue Apr 17, 2018 1:51 pm
Location: Santiago, Dominican Republic

Re: MQTT starts too slowly

Postby tommeyers » Fri Aug 12, 2022 12:48 am

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
IT Professional, Maker
Santiago, Dominican Republic

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot] and 52 guests