← index #8405Issue #5790
Related · medium · value 0.589
QUERY · ISSUE

[esp32] MQTT connect delays

openby CdeMillsopened 2022-03-10updated 2025-10-03
port-esp32

Hello
I have code on a WEMOS ESP32 WiFi and Bluetooth Battery Development Tool where I installed MicroPython v1.18. Code implements a classical producer (sensor reading) / consumer (dumping to MQTT) scenario using coroutines and synchronisation mechanism.

In order to spare power, the WIFI is desactivated, a few measurements are accumulated, and the WIFI is restarted. I observe quite variable delays in MQTT connect. The code performing it after WIFI reconnect is:

import time
from umqtt.simple import MQTTClient
import ubinascii
import machine
from machine import Timer
import uasyncio as asyncio
from uasyncio import Lock

async def connect_mqtt():
  global client_id, mqtt_server
  client = MQTTClient(client_id, mqtt_server)
  try:
    before = time.ticks_ms()
    print(end="* ")
    print(.1*round(.01*before), end = " *")
    await asyncio.sleep(0)
    client.connect(clean_session=False)
    after = time.ticks_ms()
    print(" {0} * [{1}] *".format(.1*round(.01*after), .1*round(.01*(after-before))),  end = "")
  except IndexError:
    client.disconnect()  
    client = None
  return client

This routine is used by another coroutine when it needs to dump data. The ESP32 is on the WIFI network, the MQTT broker is on a wired network. Server is version 1.6.15, running on CentOS 8 Stream, on a desktop computer. The average time around client.connect is about 8 seconds, but may range from 0.2 seconds to 1 minute.

The try / except clause was added as sometimes this error is produced: seems a socket is awaiting some data, and nothing is received.

Using "mosquitto_pub" on another computer to send a single message takes about 100 milliseconds. What may explain this strange behavior on the ESP32 ?
TIA

Pascal

CANDIDATE · ISSUE

Esp32 using MQTT help

closedby mohammedabujaradopened 2020-03-24updated 2020-03-25

hello everyone,

i have an Esp32 that is connected to a dht22 sensor which publish the data to an mqtt server, also i have another esp32 that subscribe to the topic where the data are published, but the esp32 that subscribe to the topic it disply an error says ( mqtt: OSError(-1,) ). i am using micro python for the esp32 and both of them are connected to a raspberry pi 3 as server. also both of the board have two script. the first one connect too the WIFI, and for the second script. the first one publish the data and the second one subscribe to the topic.

here are the codes:

the code that publish the data:

from time import sleep
from umqtt.robust import MQTTClient
from machine import Pin
from dht import DHT22

SERVER = '172.20.10.11' # MQTT Server Address 
CLIENT_ID = 'ESP32_DHT22_Sensor'
TOPIC = b'temp_humidity'

client = MQTTClient(CLIENT_ID, SERVER)
client.connect() # Connect to MQTT broker

sensor = DHT22(Pin(15, Pin.IN, Pin.PULL_UP)) # DHT-22 on GPIO 15 (input with internal pull-up resistor)

while True:
try:
sensor.measure() # Poll sensor
t = sensor.temperature()
h = sensor.humidity()
if isinstance(t, float) and isinstance(h, float): # Confirm sensor results are numeric
msg = (b'{0:3.1f},{1:3.1f}'.format(t, h))
client.publish(TOPIC, msg) # Publish sensor data to MQTT topic
print(msg)
else:
print('Invalid sensor readings.')
except OSError:
print('Failed to read sensor.')
sleep(2)

the code that subscribe to the topic:

import time
from umqtt.robust import MQTTClient

SERVER = '172.20.10.11' # MQTT Server Address 
CLIENT_ID = 'ESP32_DHT22_Sensor'
TOPIC = b'temp_humidity'

def sub_cb(topic, msg):
print((topic, msg))

c = MQTTClient(CLIENT_ID, SERVER)

c.DEBUG = True
c.set_callback(sub_cb)

if not c.connect(clean_session=False):
print("New session being set up")
c.subscribe(TOPIC)

while 1:
c.wait_msg()

c.disconnect()

here is the error that i get whenever i ran the second code:

mqtt: OSError(-1,)
I (27817) wifi: ampdu: ignore deleting tx BA0
mqtt: OSError(-1,)
mqtt: OSError(-1,)
mqtt: OSError(-1,)
I (41297) wifi: ampdu: ignore deleting tx BA0
mqtt: OSError(-1,)
mqtt: OSError(-1,)
mqtt: OSError(-1,)
I (55127) wifi: ampdu: ignore deleting tx BA0
mqtt: OSError(-1,)
mqtt: OSError(-1,)
mqtt: OSError(-1,)
mqtt reconnect: OSError(23,)
I (68337) wifi: ampdu: ignore deleting tx BA0
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)
mqtt reconnect: OSError(23,)

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied