subscribe function for mqtt is not working

I want to use mqtt to publish and subscribe some data so i can control events remotely such like turn on the LED at 3 (blue)
The example for publishing with mqtt is working perfect but when i try to use the subscribe function the entire program stacks at the line with subscribe function call and after some time i get an error message “OSerror -1” see attachments for my script and print screen of error,
Can you help me with the mqtt subscribe function, any example will be appreciated.

Thanks :slight_smile: (1.46 KB)

Will test it and get back to you.

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 library to OpenMV storage.
# 2) Install the mosquitto client on PC and run the following command:
#    mosquitto_pub -h -t "openmv/test"  -m "Hello World From PC!"
import time, network, pyb
from mqtt import MQTTClient

#Work WiFi
SSID='' # Network SSID
KEY=''  # Network key

def sub_cb(topic, msg):
    print((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", "", port=1883)

while True:
    # Block waiting for messages

Tried the following command:

mosquitto_pub -h -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', '']
(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…

Thanks, I tried your code it seems correct but it is not working on my openMV camera (I confirm the message is send to the broker using another mqtt subscribe client but nothing is coming in the openMV_IDE terminal) after I publish some messages from a publish client I get the subscribe code crashing in the openMV_IDE with some memory error message (see the figures) do you thing there is any problem with the firmware? or do i av to use a different library?


Hi, I can’t see the error, can you attach your script and the PC side code ? I need to be able to repeat this here.

Hi I upgrade firmware from 3.0.0 to 3.2.0 and mqtt subscribe is working i can receive successful messages :smiley:
However if I leave the M7 camera running with script waiting for some message after some time it stops and I get the following message
“Traceback (most recent call last):
File “”, line 65, in
File “”, line 145, in wait_msg
OSError: -13
MicroPython v1.9.4-4531-g65b367440 on 2018-11-04; OPENMV3 with STM32F765”
Is there any fix for that?

Just for info, i use mqtt-spy ( for a client to send and receive test messages

Thanks a lot

Mmm, I really hate how MicroPython just returns negative numbers without any context for errors. Looking up what they mean is a pain.

Ibrahim can help on this. It’s likely a socket being closed however. The best way to deal with this is to wrap the method call chain in try: except: logic and redo opening the socket on failure. This is the python way.

Anyway, please post the code and how long it takes so we can check if there’s anything we need to fix.