|
| 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 | +The Kaluga development kit comes in two versions (v1.2 and v1.3); this demo is |
| 8 | +tested on v1.3. |
| 9 | +
|
| 10 | +The audio board must be mounted between the Kaluga and the LCD, it provides the |
| 11 | +I2C pull-ups(!) |
| 12 | +
|
| 13 | +This example requires that your WIFI and Adafruit IO credentials be configured in CIRCUITPY/secrets.py, and that you have created a feed called "image" with history disabled. |
| 14 | +
|
| 15 | +The maximum image size is 100kB after base64 encoding, or about 65kB before base64 encoding. In practice, "SVGA" (800x600) images are typically around 40kB even though the "capture_buffer_size" (theoretical maximum size) is (width*height/5) bytes or 96kB. |
| 16 | +""" |
| 17 | + |
| 18 | +import os |
| 19 | +import time |
| 20 | +import binascii |
| 21 | + |
| 22 | +import board |
| 23 | +import busio |
| 24 | +import wifi |
| 25 | +import socketpool |
| 26 | +import ssl |
| 27 | +import adafruit_ov2640 |
| 28 | +from secrets import secrets |
| 29 | +import adafruit_minimqtt.adafruit_minimqtt as MQTT |
| 30 | +from adafruit_io.adafruit_io import IO_MQTT |
| 31 | + |
| 32 | +feed_name = "image" |
| 33 | + |
| 34 | +print("Connecting to WIFI") |
| 35 | +wifi.radio.connect(secrets["ssid"], secrets["password"]) |
| 36 | +pool = socketpool.SocketPool(wifi.radio) |
| 37 | + |
| 38 | +print("Connecting to Adafruit IO") |
| 39 | +mqtt_client = MQTT.MQTT( |
| 40 | + broker="io.adafruit.com", |
| 41 | + username=secrets["aio_username"], |
| 42 | + password=secrets["aio_key"], |
| 43 | + socket_pool=pool, |
| 44 | + ssl_context=ssl.create_default_context(), |
| 45 | +) |
| 46 | +mqtt_client.connect() |
| 47 | +io = IO_MQTT(mqtt_client) |
| 48 | + |
| 49 | +bus = busio.I2C(scl=board.CAMERA_SIOC, sda=board.CAMERA_SIOD) |
| 50 | +cam = adafruit_ov2640.OV2640( |
| 51 | + bus, |
| 52 | + data_pins=board.CAMERA_DATA, |
| 53 | + clock=board.CAMERA_PCLK, |
| 54 | + vsync=board.CAMERA_VSYNC, |
| 55 | + href=board.CAMERA_HREF, |
| 56 | + mclk=board.CAMERA_XCLK, |
| 57 | + mclk_frequency=20_000_000, |
| 58 | + size=adafruit_ov2640.OV2640_SIZE_QVGA, |
| 59 | +) |
| 60 | + |
| 61 | +cam.flip_x = False |
| 62 | +cam.flip_y = False |
| 63 | +cam.test_pattern = False |
| 64 | + |
| 65 | +cam.size = adafruit_ov2640.OV2640_SIZE_SVGA |
| 66 | +cam.colorspace = adafruit_ov2640.OV2640_COLOR_JPEG |
| 67 | +jpeg_buffer = bytearray(cam.capture_buffer_size) |
| 68 | +while True: |
| 69 | + jpeg = cam.capture(jpeg_buffer) |
| 70 | + print(f"Captured {len(jpeg)} bytes of jpeg data") |
| 71 | + |
| 72 | + # b2a_base64() appends a trailing newline, which IO does not like |
| 73 | + encoded_data = binascii.b2a_base64(jpeg).strip() |
| 74 | + print(f"Expanded to {len(encoded_data)} for IO upload") |
| 75 | + |
| 76 | + io.publish("image", encoded_data) |
| 77 | + |
| 78 | + print("Waiting 3s") |
| 79 | + time.sleep(3) |
0 commit comments