Send Jpeg Over MQTT Using An OpenMV Camera
MQTT
We can use [pycom]'s [mqtt library][mqtt-lib].
The basic usage:
from network import WINC
from mqtt import MQTTClient
import machine
import time
def settimeout(duration):
pass
# Init wlan module and connect to network
print("Trying to connect... (may take a while)...")
wlan = WINC()
wlan.connect(SSID, key=KEY, security=wlan.WPA_PSK)
# We should have a valid IP now via DHCP
print(wlan.ifconfig())
client = MQTTClient("demo", "broker.hivemq.com", port=1883)
client.settimeout = settimeout
client.connect()
print("Sending ON")
client.publish("/camera", "ON")
Fixes
getaddrinfo
: if we pass an IP save a network call.
Alternative MQTT Clients
[umqtt.simple][umqtt-simple]:
- QoS 0 and 1 are supported for both publish and subscribe. (QoS2 isn't supported to keep code size small)
- Besides ClientID, only "clean session" parameter is supported for connect as of now.
There's a separate umqtt.robust module which builds on umqtt.simple and adds automatic reconnect support in case of network errors. Please see its documentation for further details.
msg = b'{"MsgId":%u,"Mem":%u,"Celsius":%s,"Pressure":%s,"Humidity":%s}' % (count, gc.mem_free(), v[0][:-1], v[1][:-3], v[2][:-1])
client.publish(b"home/weather", msg)
Traceback (most recent call last):
File "<stdin>", line 48, in <module>
File "mqtt.py", line 63, in connect
TypeError: object 'NoneType' is not a tuple or list
HTTP POST: https://github.com/micropython/micropython-lib/blob/master/urequests/urequests.py
[pycom][] [mqtt-lib]:https://github.com/pycom/pycom-libraries/blob/master/lib/mqtt/mqtt.py
samples: https://github.com/gloveboxes/ESP32-MicroPython-BME280-MQTT-Sample https://www.hackster.io/bucknalla/mqtt-micropython-044e77 (uses same)