Skip to content

Commit 456ab88

Browse files
committed
example switches between rgb565 and jpeg modes
1 parent 4de2566 commit 456ab88

File tree

1 file changed

+96
-0
lines changed
  • circuitpython-esp32-camera/esp32-s3-eye-adafruitio-and-lcd

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2021 Jeff Epler for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
5+
"""
6+
Show the live camera image on the viewfinder, then upload to adafruit IO when the 'BOOT' button is pressed.
7+
"""
8+
9+
import esp32_camera
10+
from terminalio import FONT
11+
import board
12+
import displayio
13+
import busio
14+
import struct
15+
import adafruit_requests
16+
import wifi
17+
import ssl
18+
import socketpool
19+
import keypad
20+
import board
21+
import dotenv
22+
import binascii
23+
24+
shutter_button = keypad.Keys((board.BOOT,), value_when_pressed=False)
25+
26+
aio_username = dotenv.get_key("/.env", "AIO_USERNAME")
27+
aio_key = dotenv.get_key("/.env", "AIO_KEY")
28+
29+
image_feed = "image"
30+
31+
pool = socketpool.SocketPool(wifi.radio)
32+
33+
import adafruit_minimqtt.adafruit_minimqtt as MQTT
34+
from adafruit_io.adafruit_io import IO_MQTT
35+
36+
print("Connecting to Adafruit IO")
37+
mqtt_client = MQTT.MQTT(
38+
broker="io.adafruit.com",
39+
username=aio_username,
40+
password=aio_key,
41+
socket_pool=pool,
42+
ssl_context=ssl.create_default_context(),
43+
)
44+
mqtt_client.connect()
45+
io = IO_MQTT(mqtt_client)
46+
47+
48+
print("Initializing camera")
49+
cam = esp32_camera.Camera(
50+
data_pins=board.CAMERA_DATA,
51+
external_clock_pin=board.CAMERA_XCLK,
52+
pixel_clock_pin=board.CAMERA_PCLK,
53+
vsync_pin=board.CAMERA_VSYNC,
54+
href_pin=board.CAMERA_HREF,
55+
pixel_format=esp32_camera.PixelFormat.RGB565,
56+
frame_size=esp32_camera.FrameSize.R240X240,
57+
i2c=board.I2C(),
58+
external_clock_frequency=20_000_000,
59+
framebuffer_count=2,
60+
)
61+
cam.vflip = True
62+
cam.hmirror = True
63+
64+
board.DISPLAY.auto_refresh = False
65+
display_bus = board.DISPLAY.bus
66+
67+
print(cam.width, cam.height)
68+
69+
ow = (board.DISPLAY.width - cam.width) // 2
70+
oh = (board.DISPLAY.height - cam.height) // 2
71+
display_bus.send(42, struct.pack(">hh", ow, cam.width + ow - 1))
72+
display_bus.send(43, struct.pack(">hh", oh, cam.height + ow - 1))
73+
74+
while True:
75+
frame = cam.take(1)
76+
display_bus.send(44, frame)
77+
if (ev := shutter_button.events.get()) and ev.pressed:
78+
cam.reconfigure(
79+
pixel_format=esp32_camera.PixelFormat.JPEG,
80+
frame_size=esp32_camera.FrameSize.SVGA,
81+
)
82+
frame = cam.take(1)
83+
if isinstance(frame, memoryview):
84+
jpeg = frame
85+
print(f"Captured {len(jpeg)} bytes of jpeg data")
86+
87+
# b2a_base64() appends a trailing newline, which IO does not like
88+
encoded_data = binascii.b2a_base64(jpeg).strip()
89+
print(f"Expanded to {len(encoded_data)} for IO upload")
90+
91+
io.publish("image", encoded_data)
92+
cam.reconfigure(
93+
pixel_format=esp32_camera.PixelFormat.RGB565,
94+
frame_size=esp32_camera.FrameSize.R240X240,
95+
)
96+
print(end=".")

0 commit comments

Comments
 (0)