Commit a1651500 authored by Daniel Kolibár's avatar Daniel Kolibár
Browse files

Merge branch 'pi' into 'master'

Pi

See merge request tomas.gordon/hackathon-2021!4
parents ed486f1a e6a0e955
import paho.mqtt.client as mqtt
import picamera
import time
import io
import base64
import json
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected successfully to MQTT broker")
client.subscribe("birdie/rxtx/to/raspberry")
else:
print(f"Connecting to MQTT broker failed with code {rc}")
def on_message(client, userdata, msg):
messagedecoded = str(msg.payload.decode("utf-8"))
print(messagedecoded)
jsoned_message = json.loads(messagedecoded)
if jsoned_message["command"] == 'TAKE_PHOTO':
print('taking photo!')
photo = io.BytesIO()
with picamera.PiCamera() as camera:
camera.resolution = (320, 200)
camera.rotation = 180
camera.start_preview()
time.sleep(2)
camera.capture(photo, 'jpeg')
print('took photo!')
base64_bytes = base64.b64encode(photo.getvalue())
base64_message = base64_bytes.decode('ascii')
x = {'houseId':jsoned_message["houseId"], 'data': base64_message}
client.publish('birdie/rxtx/from/raspberry', payload=json.dumps(x), qos=0, retain=False)
def on_publish(client,userdata,result):
print("published", result)
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.on_publish = on_publish
client.tls_set()
client.username_pw_set("birdie", "tjhpnHMQb2021")
client.connect(
host = "50ffbd7e701c480ca097875a1621692c.s1.eu.hivemq.cloud",
port= 8883,
keepalive = 60
)
client.loop_forever()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment