subscribe function for mqtt is not working

Discussion related to "under the hood" OpenMV topics.
winccwolf
Posts: 5
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 2 times
User avatar
iabdalkader
Posts: 593
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: 593
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: 5
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: 593
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.

Return to “Technical Discussion”

Who is online

Users browsing this forum: No registered users and 6 guests