picamera2 icon indicating copy to clipboard operation
picamera2 copied to clipboard

[HOW-TO] Example for MQTT

Open gbernal opened this issue 2 years ago • 2 comments

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.

gbernal avatar Aug 13 '22 22:08 gbernal

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?

davidplowman avatar Aug 15 '22 08:08 davidplowman

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.

gbernal avatar Aug 16 '22 02:08 gbernal

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!

davidplowman avatar Sep 13 '22 10:09 davidplowman

Closing this now. Please feel free to open a new issue if further help is needed. Thanks!

davidplowman avatar Sep 26 '22 12:09 davidplowman