|
| 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 | +This exampl us for the Espressif Soala Wrover with an OV2640 Camera |
| 8 | +
|
| 9 | +This example requires that your WIFI and Adafruit IO credentials be configured |
| 10 | +in CIRCUITPY/secrets.py, and that you have created a feed called "image" with |
| 11 | +history disabled. |
| 12 | +
|
| 13 | +The maximum image size is 100kB after base64 encoding, or about 65kB before |
| 14 | +base64 encoding. In practice, "SVGA" (800x600) images are typically around |
| 15 | +40kB even though the "capture_buffer_size" (theoretical maximum size) is |
| 16 | +(width*height/5) bytes or 96kB. |
| 17 | +""" |
| 18 | + |
| 19 | +import binascii |
| 20 | +import ssl |
| 21 | +import time |
| 22 | +from secrets import secrets # pylint: disable=no-name-in-module |
| 23 | + |
| 24 | +import board |
| 25 | +import busio |
| 26 | +import digitalio |
| 27 | +import wifi |
| 28 | +import socketpool |
| 29 | +import adafruit_minimqtt.adafruit_minimqtt as MQTT |
| 30 | +from adafruit_io.adafruit_io import IO_MQTT |
| 31 | +import adafruit_ov2640 |
| 32 | + |
| 33 | +feed_name = "image-saola-ov2640" |
| 34 | + |
| 35 | +print("Connecting to WIFI") |
| 36 | +wifi.radio.connect(secrets["ssid"], secrets["password"]) |
| 37 | +pool = socketpool.SocketPool(wifi.radio) |
| 38 | + |
| 39 | +print("Connecting to Adafruit IO") |
| 40 | +mqtt_client = MQTT.MQTT( |
| 41 | + broker="io.adafruit.com", |
| 42 | + username=secrets["aio_username"], |
| 43 | + password=secrets["aio_key"], |
| 44 | + socket_pool=pool, |
| 45 | + ssl_context=ssl.create_default_context(), |
| 46 | +) |
| 47 | +mqtt_client.connect() |
| 48 | +io = IO_MQTT(mqtt_client) |
| 49 | + |
| 50 | +bus = busio.I2C(scl=board.IO7, sda=board.IO8) |
| 51 | +cam = adafruit_ov2640.OV2640( |
| 52 | + bus, |
| 53 | + data_pins=( |
| 54 | + board.IO36, |
| 55 | + board.IO37, |
| 56 | + board.IO41, |
| 57 | + board.IO42, |
| 58 | + board.IO39, |
| 59 | + board.IO40, |
| 60 | + board.IO21, |
| 61 | + board.IO38, |
| 62 | + ), |
| 63 | + clock=board.IO33, |
| 64 | + vsync=board.IO2, |
| 65 | + href=board.IO3, |
| 66 | + mclk=board.IO1, |
| 67 | + mclk_frequency=20_000_000, |
| 68 | + size=adafruit_ov2640.OV2640_SIZE_QVGA, |
| 69 | +) |
| 70 | + |
| 71 | +cam.flip_x = False |
| 72 | +cam.flip_y = False |
| 73 | + |
| 74 | +cam.size = adafruit_ov2640.OV2640_SIZE_SVGA |
| 75 | +cam.colorspace = adafruit_ov2640.OV2640_COLOR_JPEG |
| 76 | +jpeg_buffer = bytearray(cam.capture_buffer_size) |
| 77 | +while True: |
| 78 | + jpeg = cam.capture(jpeg_buffer) |
| 79 | + print(f"Captured {len(jpeg)} bytes of jpeg data") |
| 80 | + |
| 81 | + # b2a_base64() appends a trailing newline, which IO does not like |
| 82 | + encoded_data = binascii.b2a_base64(jpeg).strip() |
| 83 | + print(f"Expanded to {len(encoded_data)} for IO upload") |
| 84 | + io.publish(feed_name, encoded_data) |
| 85 | + print("Waiting 10s") |
| 86 | + time.sleep(10) |
0 commit comments