Skip to content

Commit 43a842b

Browse files
authored
Merge pull request adafruit#2711 from brentru/add-memento-doorbell-camera
Add Memento IOT Doorbell Camera
2 parents 21d5653 + 745405b commit 43a842b

File tree

1 file changed

+101
-0
lines changed
  • MEMENTO/Memento_IOT_Doorbell

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# SPDX-FileCopyrightText: 2023 Brent Rubell for Adafruit Industries
2+
#
3+
# An open-source IoT doorbell with the Adafruit MEMENTO camera and Adafruit IO
4+
#
5+
# SPDX-License-Identifier: Unlicense
6+
import os
7+
import time
8+
import ssl
9+
import binascii
10+
import digitalio
11+
import adafruit_pycamera
12+
import board
13+
import wifi
14+
import socketpool
15+
import adafruit_requests
16+
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
17+
18+
print("CircuitPython Doorbell Camera")
19+
20+
### WiFi ###
21+
# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys
22+
# with your WiFi credentials. DO NOT share that file or commit it into Git or other
23+
# source control.
24+
25+
# Set your Adafruit IO Username, Key and Port in settings.toml
26+
# (visit io.adafruit.com if you need to create an account,
27+
# or if you need your Adafruit IO key.)
28+
aio_username = os.getenv("ADAFRUIT_AIO_USERNAME")
29+
aio_key = os.getenv("ADAFRUIT_AIO_KEY")
30+
31+
print(f"Connecting to {os.getenv('CIRCUITPY_WIFI_SSID')}")
32+
wifi.radio.connect(
33+
os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")
34+
)
35+
print(f"Connected to {os.getenv('CIRCUITPY_WIFI_SSID')}!")
36+
37+
pool = socketpool.SocketPool(wifi.radio)
38+
requests = adafruit_requests.Session(pool, ssl.create_default_context())
39+
40+
# Initialize an Adafruit IO HTTP API object
41+
io = IO_HTTP(os.getenv("ADAFRUIT_AIO_USERNAME"), os.getenv("ADAFRUIT_AIO_KEY"), requests)
42+
43+
# Adafruit IO feed configuration
44+
try:
45+
# Get the 'camera' feed from Adafruit IO
46+
feed_camera = io.get_feed("camera")
47+
except AdafruitIO_RequestError:
48+
# If no 'camera' feed exists, create one
49+
feed_camera = io.create_new_feed("camera")
50+
51+
# Initialize memento camera
52+
pycam = adafruit_pycamera.PyCamera()
53+
# Turn off TFT backlight
54+
pycam.display.brightness = 0.0
55+
# Deinitialize the MEMENTO's NeoPixels
56+
# Why? The pixels use board.A1 and we want to use it to control the doorbell LED
57+
pycam.pixels.deinit()
58+
59+
# Set up the button
60+
pin_button = digitalio.DigitalInOut(board.A0)
61+
pin_button.direction = digitalio.Direction.INPUT
62+
pin_button.pull = digitalio.Pull.UP
63+
64+
# Set up the button's LED
65+
led = digitalio.DigitalInOut(board.A1)
66+
led.direction = digitalio.Direction.OUTPUT
67+
led.value = True
68+
print("Doorbell ready to be pressed!")
69+
70+
def capture_send_image():
71+
"""Captures an image and send it to Adafruit IO."""
72+
# Force autofocus and capture a JPEG image
73+
pycam.autofocus()
74+
jpeg = pycam.capture_into_jpeg()
75+
print("Captured image!")
76+
if jpeg is not None:
77+
# Encode JPEG data into base64 for sending to Adafruit IO
78+
print("Encoding image...")
79+
encoded_data = binascii.b2a_base64(jpeg).strip()
80+
# Send encoded_data to Adafruit IO camera feed
81+
print("Sending image to Adafruit IO...")
82+
io.send_data(feed_camera["key"], encoded_data)
83+
print("Sent image to IO!")
84+
else:
85+
print("ERROR: JPEG frame capture failed!")
86+
print("DONE, waiting for next press..")
87+
# Turn the LED on to signal that the doorbell is ready to be pressed again
88+
led.value = True
89+
90+
91+
while True:
92+
# Wait until the doorbell is pressed
93+
if not pin_button.value:
94+
print("Doorbell pressed!")
95+
# Turn the doorbell LED off to signal that it has been pressed
96+
led.value = False
97+
# Play a doorbell tone using the speaker
98+
pycam.tone(95, 0.5)
99+
pycam.tone(70, 0.5)
100+
capture_send_image()
101+
time.sleep(0.01)

0 commit comments

Comments
 (0)