mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
commit
61a34a4a62
@ -1,11 +1,12 @@
|
|||||||
# MQTT Example.
|
# MQTT Example.
|
||||||
# This example shows how to use the MQTT library.
|
# This example shows how to use the MQTT library to publish to a topic.
|
||||||
#
|
#
|
||||||
# 1) Copy the mqtt.py library to OpenMV storage.
|
# 1) Copy the mqtt.py library to OpenMV storage.
|
||||||
# 2) Install the mosquitto client on PC and run the following command:
|
# 2) Run this script on the OpenMV camera.
|
||||||
|
# 3) Install the mosquitto client on PC and run the following command:
|
||||||
# mosquitto_sub -h test.mosquitto.org -t "openmv/test" -v
|
# mosquitto_sub -h test.mosquitto.org -t "openmv/test" -v
|
||||||
#
|
#
|
||||||
# Note: If the mosquitto broker is down (OSError -12) try a different one (ex: broker.hivemq.com)
|
# NOTE: If the mosquitto broker is unreachable, try another broker (For example: broker.hivemq.com)
|
||||||
import time, network
|
import time, network
|
||||||
from mqtt import MQTTClient
|
from mqtt import MQTTClient
|
||||||
|
|
||||||
37
scripts/examples/14-WiFi-Shield/mqtt_sub.py
Normal file
37
scripts/examples/14-WiFi-Shield/mqtt_sub.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
# MQTT 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) Run this script on the OpenMV camera.
|
||||||
|
# 3) Install the mosquitto client on PC and run the following command:
|
||||||
|
# mosquitto_pub -t "openmv/test" -m "Hello World!" -h test.mosquitto.org -p 1883
|
||||||
|
#
|
||||||
|
# NOTE: If the mosquitto broker is unreachable, try another broker (For example: broker.hivemq.com)
|
||||||
|
import time, network
|
||||||
|
from mqtt import MQTTClient
|
||||||
|
|
||||||
|
SSID='' # Network SSID
|
||||||
|
KEY='' # Network key
|
||||||
|
|
||||||
|
# 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.connect()
|
||||||
|
|
||||||
|
def callback(topic, msg):
|
||||||
|
print(topic, msg)
|
||||||
|
|
||||||
|
# must set callback first
|
||||||
|
client.set_callback(callback)
|
||||||
|
client.subscribe("openmv/test")
|
||||||
|
|
||||||
|
while (True):
|
||||||
|
client.check_msg() # poll for messages.
|
||||||
|
time.sleep(1000)
|
||||||
@ -167,11 +167,9 @@ class MQTTClient:
|
|||||||
# messages processed internally.
|
# messages processed internally.
|
||||||
def wait_msg(self):
|
def wait_msg(self):
|
||||||
res = self.sock.recv(1)
|
res = self.sock.recv(1)
|
||||||
self.sock.setblocking(True)
|
|
||||||
if res is None:
|
|
||||||
return None
|
|
||||||
if res == b"":
|
if res == b"":
|
||||||
raise OSError(-1)
|
return None
|
||||||
|
self.sock.setblocking(True)
|
||||||
if res == b"\xd0": # PINGRESP
|
if res == b"\xd0": # PINGRESP
|
||||||
sz = self.sock.recv(1)[0]
|
sz = self.sock.recv(1)[0]
|
||||||
assert sz == 0
|
assert sz == 0
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user