|
| 1 | +# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2021 Jeff Epler for Adafruit Industries |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: Unlicense |
| 5 | + |
| 6 | +""" |
| 7 | +Upload a jpeg image to Adafruit IO at regular intervals |
| 8 | +
|
| 9 | +This example requires: |
| 10 | + * ESP32-S3-EYE development kit from Espressif |
| 11 | +
|
| 12 | +To use: |
| 13 | + * On io.adafruit.com, create a feed named "image" and turn OFF history |
| 14 | + * On io.adafruit.com, create a dashboard and add an "image" block using the feed "image" as its data |
| 15 | + * Set up CIRCUITPY/.env with WiFI and Adafruit IO credentials |
| 16 | + * Copy the project bundle to CIRCUITPY |
| 17 | +""" |
| 18 | + |
| 19 | +import os |
| 20 | +import time |
| 21 | + |
| 22 | +import dotenv |
| 23 | +import io |
| 24 | +import sys |
| 25 | +import binascii |
| 26 | +import esp32_camera |
| 27 | +import board |
| 28 | +import socketpool |
| 29 | +import wifi |
| 30 | +import ssl |
| 31 | + |
| 32 | +import adafruit_minimqtt.adafruit_minimqtt as MQTT |
| 33 | +from adafruit_io.adafruit_io import IO_MQTT |
| 34 | + |
| 35 | +aio_username = dotenv.get_key('/.env', 'AIO_USERNAME') |
| 36 | +aio_key = dotenv.get_key('/.env', 'AIO_KEY') |
| 37 | + |
| 38 | +image_feed = "image" |
| 39 | + |
| 40 | +cam = esp32_camera.Camera( |
| 41 | + data_pins=board.CAMERA_DATA, |
| 42 | + external_clock_pin=board.CAMERA_XCLK, |
| 43 | + pixel_clock_pin=board.CAMERA_PCLK, |
| 44 | + vsync_pin=board.CAMERA_VSYNC, |
| 45 | + href_pin=board.CAMERA_HREF, |
| 46 | + pixel_format=esp32_camera.PixelFormat.JPEG, |
| 47 | + frame_size=esp32_camera.FrameSize.SVGA, |
| 48 | + i2c=board.I2C(), |
| 49 | + external_clock_frequency=20_000_000, |
| 50 | + grab_mode=esp32_camera.GrabMode.WHEN_EMPTY) |
| 51 | +cam.vflip = True |
| 52 | + |
| 53 | + |
| 54 | +pool = socketpool.SocketPool(wifi.radio) |
| 55 | + |
| 56 | +print("Connecting to Adafruit IO") |
| 57 | +mqtt_client = MQTT.MQTT( |
| 58 | + broker="io.adafruit.com", |
| 59 | + username=aio_username, |
| 60 | + password=aio_key, |
| 61 | + socket_pool=pool, |
| 62 | + ssl_context=ssl.create_default_context(), |
| 63 | +) |
| 64 | +mqtt_client.connect() |
| 65 | +io = IO_MQTT(mqtt_client) |
| 66 | + |
| 67 | +while True: |
| 68 | + frame = cam.take(1) |
| 69 | + if isinstance(frame, memoryview): |
| 70 | + jpeg = frame |
| 71 | + print(f"Captured {len(jpeg)} bytes of jpeg data") |
| 72 | + |
| 73 | + # b2a_base64() appends a trailing newline, which IO does not like |
| 74 | + encoded_data = binascii.b2a_base64(jpeg).strip() |
| 75 | + print(f"Expanded to {len(encoded_data)} for IO upload") |
| 76 | + |
| 77 | + io.publish("image", encoded_data) |
| 78 | + |
| 79 | + time.sleep(10) |
0 commit comments