Hi, this minimal example works:
# MQTT Subscribe Example.
# This example shows how to use the MQTT library to subscribe to a topic.
# 1) Copy the mqtt.py library to OpenMV storage.
# 2) Install the mosquitto client on PC and run the following command:
# mosquitto_pub -h test.mosquitto.org -t "openmv/test" -m "Hello World From PC!"
import time, network, pyb
from mqtt import MQTTClient
SSID='' # Network SSID
KEY='' # Network key
def sub_cb(topic, msg):
# Init wlan module and connect to network
print("Trying to connect... (may take a while)...")
wlan = network.WINC()
wlan.connect(SSID, key=KEY, security=wlan.WPA_PSK)
# We should have a valid IP now via DHCP
client = MQTTClient("openmv", "test.mosquitto.org", port=1883)
# Block waiting for messages
Tried the following command:
mosquitto_pub -h test.mosquitto.org -t "openmv/test" -m "Hello World! From PC"
MicroPython v1.9.4-4531-g65b367440 on 2018-11-04; OPENMV3 with STM32F765
Type "help()" for more information.
>>> Trying to connect... (may take a while)...
Running in Station mode...
[-45, 2, 'mux', 'f8:f0:05:f0:b5:3e', '192.168.1.105']
(b'openmv/test', b'Hello World From PC!')
Note in your example you disconnect the client after publishing, so you should break, otherwise you will publish again on a disconnected client. Also note if you publish the callback will be called, and then you publish again (goes into a loop) etc…