subscribe function for mqtt is not working

Discussion related to "under the hood" OpenMV topics.
winccwolf
Posts: 6
Joined: Fri Oct 26, 2018 4:05 am

subscribe function for mqtt is not working

Postby winccwolf » Fri Nov 02, 2018 4:41 am

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 :)
Attachments
error message.png
error message
mqtt_example_2_MB.py
script for mqtt subscribe
(1.46 KiB) Downloaded 24 times
User avatar
iabdalkader
Posts: 653
Joined: Sun May 24, 2015 3:53 pm

Re: subscribe function for mqtt is not working

Postby iabdalkader » Fri Nov 02, 2018 7:12 pm

Will test it and get back to you.
User avatar
iabdalkader
Posts: 653
Joined: Sun May 24, 2015 3:53 pm

Re: subscribe function for mqtt is not working

Postby iabdalkader » Sat Nov 03, 2018 9:05 pm

Hi, this minimal example works:

Code: Select all

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

Code: Select all

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

Code: Select all

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..
winccwolf
Posts: 6
Joined: Fri Oct 26, 2018 4:05 am

Re: subscribe function for mqtt is not working

Postby winccwolf » Tue Nov 06, 2018 7:44 am

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
Attachments
error message3.png
error message
error message2.png
error message2
User avatar
iabdalkader
Posts: 653
Joined: Sun May 24, 2015 3:53 pm

Re: subscribe function for mqtt is not working

Postby iabdalkader » Tue Nov 06, 2018 12:24 pm

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.
winccwolf
Posts: 6
Joined: Fri Oct 26, 2018 4:05 am

Re: subscribe function for mqtt is not working

Postby winccwolf » Tue Dec 25, 2018 1:40 pm

Hi I upgrade firmware from 3.0.0 to 3.2.0 and mqtt subscribe is working i can receive successful messages :D
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 "<stdin>", line 65, in <module>
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
User avatar
kwagyeman
Posts: 2564
Joined: Sun May 24, 2015 2:10 pm

Re: subscribe function for mqtt is not working

Postby kwagyeman » Tue Dec 25, 2018 2:19 pm

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.
Nyamekye,

Return to “Technical Discussion”

Who is online

Users browsing this forum: No registered users and 2 guests