umqtt.robust: ensure that broker received the message

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
djipey
Posts: 1
Joined: Sun Dec 01, 2019 3:04 pm

umqtt.robust: ensure that broker received the message

Post by djipey » Sun Dec 01, 2019 3:16 pm

Hi, this is my first post on the forum.

I'm familiar with Python and I'm trying to send data from my ESP32 board to a Mosquitto broker, with the MQTT "protocol".

This isn't really hard and I managed to get that working. Here is a short sample of code demonstrating how I send some environmental data to the broker:

Code: Select all

import machine
import time
from umqtt.robust import MQTTClient

import bme280

import max44009
import config

i2c = machine.I2C(scl=machine.Pin(config.SCL_PIN), sda=machine.Pin(config.SDA_PIN))

c = MQTTClient("umqtt_client", config.MQTT_SERVER)

bme = bme280.BME280(i2c=i2c)
max44009 = max44009.MAX44009(i2c)

while True:
    print(bme.values)
    print(max44009.illuminance_lux)

    temperature = bme.values[0].replace("C", "")
    pressure = bme.values[1].replace("hPa", "")
    humidity = bme.values[2].replace("%", "")
    illuminance = str(max44009.illuminance_lux)

    c.connect()

    c.publish(config.TOPIC_TEMPERATURE, temperature, qos=1)
    c.publish(config.TOPIC_PRESSURE, pressure, qos=1)
    c.publish(config.TOPIC_HUMIDITY, humidity, qos=1)
    c.publish(config.TOPIC_ILLUMINANCE, illuminance, qos=1)

    c.disconnect()

    time.sleep(5)
The board connects to my wifi network when it boots. Everything works well.

Now, let's say my bandwidth gets sparse (because I'm watching Netflix for example). The MQTT communication starts to struggle. Sometimes, I notice that the broker doesn't get any messages for like 30 min. Because I pass `qos=1`, the MQTT client will try again and again to publish to the broker. But in this case I might want to do something else, like reconnect to the wifi network even if I'm still connected to it (I checked, trying to do wlan.isconnected() will return True, it just seems like communication is hard between the board and the broker).

So I was wondering, is there a standard way to check if the broker received the message, and perform some actions if it's not the case?

Cheers

User avatar
pythoncoder
Posts: 3719
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: umqtt.robust: ensure that broker received the message

Post by pythoncoder » Mon Dec 02, 2019 7:20 am

Various thoughts come to mind. The best solution would be to run a separate WiFi AP for your MQTT stuff, then the problem would go away ;) Failing that I can think of two possibilities.

If you ran a second MQTT client it could subscribe to the topic and publish a response. Your main client could wait for the subscription and do something if it's not received in the requisite period. Note that your second client could run on the same box as the broker - you could write it in CPython using Paho MQTT. Or it could be a simple script calling mosquitto_sub and mosquitto_pub in a loop.

The other approach would be to use the resilient asynchronous MQTT library. Asynchronous code (based on uasyncio) makes this easy. But there is a learning curve for uasyncio: if you're not already familiar with it there is a tutorial in in this repo.
Peter Hinch

Post Reply