subscribe function for mqtt is not working

Hi
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:


mqtt_example_2_MB.py (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 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

#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
print(wlan.ifconfig())

client = MQTTClient("openmv", "test.mosquitto.org", port=1883)
client.set_callback(sub_cb)
client.connect()
client.subscribe("openmv/test")

while True:
    # Block waiting for messages
    client.wait_msg()

Tried the following command:

mosquitto_pub -h test.mosquitto.org -t "openmv/test"  -m "Hello World! From PC"

Output:

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…

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?

Thanks


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 “mqtt.py”, 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 (https://www.eclipse.org/paho/components/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.

Hello Team Openmv,
I want to use MQTT to publish and subscribe to data from AWS IoT broker. I have been able to run both mqtt_pub.py and mqtt_sub.py in the Examples. I want to combine both functionalities in the same script, most especially I want to use the unblocking function “check_msg” which allows foreground processing like the publish functionality to send messages to the broker. However, I get an error message “OSError: -27648”. How can I resolve the error and also is this the right way to use publish and subscribe in one script? Thanks

from mqtt import MQTTClient
from secret import SSID, KEY, user, password, server
import network
import time

KEY_PATH = "newkey.der"
CERT_PATH = "openmv.cert.der"
topic = "command"

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, KEY, security=wlan.WPA_PSK)

# We should have a valid IP now via DHCP
print("WiFi Connected ", wlan.ifconfig())


with open(KEY_PATH, 'r') as f:
    key1 = f.read()
with open(CERT_PATH, 'r') as f:
    cert1 = f.read()


def connect_and_subscribe():
    client = MQTTClient(client_id="openmv",
                        server=server,
                        port=8883,
                        keepalive=4000,
                        ssl=True,
                        ssl_params={"key": key1, "cert": cert1, "server_side": False})

    client.set_callback(callback)
    client.connect()
    print("Connected to a MQTT broker")
    client.subscribe(topic)
    print("Connected to %s MQTT broker, subcribed to %s topic" %
          (server, topic))
    return client


def callback(topic, msg):
    print(topic, msg)


client = connect_and_subscribe()

 #For publish and subscribe
while(True):
    client.check_msg() # poll for messages.
    client.publish("openmv/test", "Hello World!")
    time.sleep_ms(1000)

Error log:

Connected to a MQTT broker
Connected to a1fz198l1vhieb-ats.iot.us-east-2.amazonaws.com MQTT broker, subcribed to command topic

Traceback (most recent call last):
  File "<stdin>", line 49, in <module>
  File "mqtt.py", line 203, in check_msg
  File "mqtt.py", line 169, in wait_msg
OSError: -27648
MicroPython: v1.18-omv OpenMV: v4.3.0 HAL: v1.9.0 BOARD: OPENMV4P-STM32H743
Type "help()" for more information.
>>> 

@kwagyeman and @iabdalkader please can I get any help with the above?

That error is MBEDTLS_ERR_SSL_INTERNAL_ERROR not sure what’s going on. How did you convert the key/cert to DER ? This is known to work:

openssl ec -in key.pem -out key.der -outform DER
openssl x509 -in cert.pem -out cert.der -outform DER

Also you should try the latest firmware the development firmware from github.