|
| 1 | +# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +import time |
| 6 | +import os |
| 7 | +import ssl |
| 8 | +import io |
| 9 | +import binascii |
| 10 | +import jpegio |
| 11 | +import microcontroller |
| 12 | +import wifi |
| 13 | +import socketpool |
| 14 | +import displayio |
| 15 | +from adafruit_qualia.graphics import Graphics, Displays |
| 16 | +import adafruit_minimqtt.adafruit_minimqtt as MQTT |
| 17 | + |
| 18 | +aio_username = os.getenv("ADAFRUIT_AIO_USERNAME") |
| 19 | +aio_key = os.getenv("ADAFRUIT_AIO_KEY") |
| 20 | + |
| 21 | +print(f"Connecting to {os.getenv('CIRCUITPY_WIFI_SSID')}") |
| 22 | +wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")) |
| 23 | +print(f"Connected to {os.getenv('CIRCUITPY_WIFI_SSID')}!") |
| 24 | + |
| 25 | +camera_feed = aio_username + "/feeds/camera" |
| 26 | + |
| 27 | +graphics = Graphics(Displays.ROUND40, default_bg=None, auto_refresh=True) |
| 28 | + |
| 29 | +def center(g, b): |
| 30 | + # center the image |
| 31 | + g.x -= ((b.width * 2) - 720) // 4 |
| 32 | + g.y -= ((b.height * 2) - 720) // 4 |
| 33 | + |
| 34 | +def decode_image(base64_msg): |
| 35 | + # Decode the base64 image into raw binary JPEG data |
| 36 | + decoded_image = binascii.a2b_base64(base64_msg) |
| 37 | + # Create a JpegDecoder instance |
| 38 | + decoder = jpegio.JpegDecoder() |
| 39 | + # Use io.BytesIO to treat the decoded image as a file-like object |
| 40 | + jpeg_data = io.BytesIO(decoded_image) |
| 41 | + # Open the JPEG data source from the BytesIO object |
| 42 | + width, height = decoder.open(jpeg_data) |
| 43 | + print(width, height) |
| 44 | + # Create a Bitmap with the dimensions of the JPEG image |
| 45 | + bitmap = displayio.Bitmap(width, height, 65536) # Use 65536 colors for RGB565 |
| 46 | + # Decode the JPEG into the bitmap |
| 47 | + decoder.decode(bitmap) |
| 48 | + # pylint: disable=line-too-long |
| 49 | + grid = displayio.TileGrid(bitmap, pixel_shader=displayio.ColorConverter(input_colorspace=displayio.Colorspace.RGB565_SWAPPED)) |
| 50 | + center(grid, bitmap) |
| 51 | + group = displayio.Group(scale=2) |
| 52 | + group.append(grid) |
| 53 | + graphics.display.root_group = group |
| 54 | + graphics.display.refresh() |
| 55 | + |
| 56 | + |
| 57 | +# Define callback methods which are called when events occur |
| 58 | +def connected(client, userdata, flags, rc): # pylint: disable=unused-argument |
| 59 | + # This function will be called when the client is connected |
| 60 | + # successfully to the broker. |
| 61 | + print(f"Connected to Adafruit IO! Listening for topic changes on {camera_feed}") |
| 62 | + # Subscribe to all changes on the onoff_feed. |
| 63 | + client.subscribe(camera_feed) |
| 64 | + |
| 65 | + |
| 66 | +def disconnected(client, userdata, rc): # pylint: disable=unused-argument |
| 67 | + # This method is called when the client is disconnected |
| 68 | + print("Disconnected from Adafruit IO!") |
| 69 | + |
| 70 | + |
| 71 | +def message(client, topic, msg): # pylint: disable=unused-argument |
| 72 | + # This method is called when a topic the client is subscribed to |
| 73 | + # has a new message. |
| 74 | + print(f"New message on topic {topic}") |
| 75 | + decode_image(msg) |
| 76 | + |
| 77 | +pool = socketpool.SocketPool(wifi.radio) |
| 78 | +ssl_context = ssl.create_default_context() |
| 79 | +# Initialize an Adafruit IO HTTP API object |
| 80 | +mqtt_client = MQTT.MQTT( |
| 81 | + broker="io.adafruit.com", |
| 82 | + port=1883, |
| 83 | + username=aio_username, |
| 84 | + password=aio_key, |
| 85 | + socket_pool=pool, |
| 86 | + ssl_context=ssl_context, |
| 87 | +) |
| 88 | +# Setup the callback methods above |
| 89 | +mqtt_client.on_connect = connected |
| 90 | +mqtt_client.on_disconnect = disconnected |
| 91 | +mqtt_client.on_message = message |
| 92 | + |
| 93 | +# Connect the client to the MQTT broker. |
| 94 | +print("Connecting to Adafruit IO...") |
| 95 | +mqtt_client.connect() |
| 96 | +while True: |
| 97 | + # Poll the message queue |
| 98 | + try: |
| 99 | + mqtt_client.loop(timeout=1) |
| 100 | + time.sleep(5) |
| 101 | + except Exception as error: # pylint: disable=broad-except |
| 102 | + print(error) |
| 103 | + time.sleep(5) |
| 104 | + microcontroller.reset() |
0 commit comments