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