picamera2
picamera2 copied to clipboard
[HOW-TO] Example for MQTT
Hi,
I would like to use picamera2 to send images periodically or video stream from my headless raspberry pi 3b+ to a reactjs client that uses MQTT to receive data. Would the process be similar to https://github.com/raspberrypi/picamera2/blob/main/examples/capture_stream_udp.py ?
I just don't know how to change this.
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
sock.connect(("REMOTEIP", 10001))
stream = sock.makefile("wb")
picam2.start_recording(encoder, FileOutput(stream))
time.sleep(20)
picam2.stop_recording()
into something like this
def publish_image():
f=open("image_test.jpg", "rb") #3.7kiB in same folder
fileContent = f.read()
byteArr = bytearray(fileContent)
client.publish(topic, byteArr)
print("image published")
Thank you in advance.
Hi, I think it sounds like you might want to create your own Output object, with its own write method? Maybe take a look here, where it does something a bit like this. So perhaps in your __init__
method you want to store the "client" object, and in the write
method you then do self.client.publish
?
Thank you for the suggestion @davidplowman, I will explore your suggestion and see how far I'm able to get with. I will post back in here any progress or issues.
Hi, just wondering if there's any else to follow up on this one? Please post back if there is, otherwise I'll consider closing this issue in a few days. Thanks!
Closing this now. Please feel free to open a new issue if further help is needed. Thanks!