I have a generic ESP32 board running the latest Micropython. I have installed umqtt.robust with upip, and very quickly and easily I got publishing a message to my Mosquitto broker to work. However, trying to get the subscription part working has me stumped. From several websites I have cobbled the following together, which I think should work:
Code: Select all
from umqtt.robust import MQTTClient
def sub_callback(topic,payload):
if topic == "test/test":
print("message received")
print(payload)
mqtt = MQTTClient('esp32test', '192.168.178.84')
print("*** Connecting to MQTT broker...")
mqtt.connect()
print("*** MQTT broker connected")
mqtt.set_callback(sub_callback)
mqtt.subscribe("test/test")
while True:
mqtt.check_msg()
*** Connecting to MQTT broker...
*** MQTT broker connected
When I push a message to the test/test topic, the sub_callback does not appear to be called, because the text "message received" never appears.
The board is correctly connected to the wifi. I can ping it from my computer, and in any case, a script that publishes an mqtt message works fine. I push a message to the test/test topic with a python command on my Raspberry Pi, and on a mqtt client on my phone I see the message appear. I have other channels running on my broker which all work fine and fast, so my broker seems fine. I have tried different topic names, clientIDs, but it just doesn't work. Just to make sure it's not the board, I have tried a 2nd ESP32, same result.
I'm hoping (and expecting) it's just something I'm doing wrong that I'm not understanding. Can anyone point out my mistake?