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:
The board connects to my wifi network when it boots. Everything works well.
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.replace("C", "") pressure = bme.values.replace("hPa", "") humidity = bme.values.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)
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?