Skip to content

Commit 2785e61

Browse files
authored
Merge pull request #314 from brentru/io-house-series
IO Home: Security Guide
2 parents 4017559 + 3719537 commit 2785e61

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed
156 KB
Binary file not shown.
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
"""
2+
'io_home_security.py'
3+
=======================================
4+
Secure and monitor your home with
5+
Adafruit IO.
6+
7+
Learning System Guide: https://learn.adafruit.com/adafruit-io-home-security
8+
9+
Author(s): Brent Rubell for Adafruit Industries, 2018.
10+
11+
Dependencies:
12+
- Adafruit_Blinka
13+
(https://github.com/adafruit/Adafruit_Blinka)
14+
- Adafruit_CircuitPython_SGP30
15+
(https://github.com/adafruit/Adafruit_CircuitPython_SGP30)
16+
- Adafruit_CircuitPython_NeoPixel
17+
(https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel)
18+
- picamera
19+
(https://github.com/waveform80/picamera)
20+
"""
21+
# Import standard python modules
22+
import time
23+
import base64
24+
# import Adafruit IO REST client
25+
from Adafruit_IO import Client, RequestError
26+
27+
# import SGP30, NeoPixel and picam libraries
28+
import neopixel
29+
import adafruit_sgp30
30+
import picamera
31+
32+
# import Adafruit Blinka
33+
from board import SCL, SDA, D18, D22, D24
34+
from busio import I2C
35+
import digitalio
36+
37+
# Number of NeoPixels connected to the strip
38+
NUM_PIXELS_STRIP = 60
39+
# Number of NeoPixels connected to the NeoPixel Jewel
40+
NUM_PIXELS_JEWEL = 6
41+
RED = (255, 0, 0)
42+
43+
# Set to the hour at which to arm the alarm system, 24hr time
44+
ALARM_HOUR = 16
45+
46+
# Set to the interval between loop execution, in seconds
47+
LOOP_INTERVAL = 2
48+
49+
# Set to your Adafruit IO key.
50+
# Remember, your key is a secret,
51+
# so make sure not to publish it when you publish this code!
52+
ADAFRUIT_IO_KEY = 'YOUR_IO_KEY'
53+
54+
# Set to your Adafruit IO username.
55+
# (go to https://accounts.adafruit.com to find your username)
56+
ADAFRUIT_IO_USERNAME = 'YOUR_IO_USERNAME'
57+
58+
# Create an instance of the REST client
59+
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
60+
61+
# set up Adafruit IO feeds
62+
tvoc_feed = aio.feeds('tvoc')
63+
eco2_feed = aio.feeds('eco2')
64+
door_feed = aio.feeds('front-door')
65+
motion_feed = aio.feeds('motion-detector')
66+
alarm_feed = aio.feeds('home-alarm')
67+
outdoor_lights_feed = aio.feeds('outdoor-lights')
68+
indoor_lights_Feed = aio.feeds('indoor-lights')
69+
picam_feed = aio.feeds('picam')
70+
71+
# set up picamera
72+
camera = picamera.PiCamera()
73+
# set the resolution of the pi camera
74+
# note: you can only send images <100kb to feeds
75+
camera.resolution = (200, 200)
76+
77+
# set up door sensor
78+
door_sensor = digitalio.DigitalInOut(D24)
79+
door_sensor.direction = digitalio.Direction.INPUT
80+
81+
# set up motion sensor
82+
pir_sensor = digitalio.DigitalInOut(D22)
83+
pir_sensor.direction = digitalio.Direction.INPUT
84+
prev_pir_value = pir_sensor.value
85+
is_pir_activated = False
86+
87+
# set up sgp30
88+
i2c_bus = I2C(SCL, SDA, frequency=100000)
89+
sgp30 = adafruit_sgp30.Adafruit_SGP30(i2c_bus)
90+
91+
# set up the neopixel strip
92+
pixels = neopixel.NeoPixel(D18, NUM_PIXELS_STRIP)
93+
pixels.fill((0, 0, 0))
94+
pixels.show()
95+
96+
def alarm_trigger():
97+
"""Alarm is triggered by the dashboard toggle
98+
and a sensor detecting movement.
99+
"""
100+
print('* SYSTEM ALARM!')
101+
for j in range(NUM_PIXELS_JEWEL):
102+
pixels[j] = RED
103+
pixels.show()
104+
time.sleep(0.5)
105+
# turn pixels off after alarm
106+
pixels.fill((0, 0, 0))
107+
pixels.show()
108+
109+
print('Adafruit IO Home: Security')
110+
111+
while True:
112+
# read SGP30
113+
co2eq, tvoc = sgp30.iaq_measure()
114+
print("CO2eq = %d ppm \t TVOC = %d ppb" % (co2eq, tvoc))
115+
# send SGP30 values to Adafruit IO
116+
aio.send(eco2_feed.key, co2eq)
117+
aio.send(tvoc_feed.key, tvoc)
118+
time.sleep(0.5)
119+
120+
# read/send door sensor
121+
if door_sensor.value:
122+
print('Door Open!')
123+
# change indicator block to red
124+
aio.send(door_feed.key, 3)
125+
else:
126+
print('Door Closed.')
127+
# reset indicator block to green
128+
aio.send(door_feed.key, 0)
129+
130+
# read/send motion sensor
131+
if door_sensor.value:
132+
if not prev_pir_value:
133+
print('Motion detected!')
134+
is_pir_activated = True
135+
# change indicator block to red
136+
aio.send(motion_feed.key, 3)
137+
else:
138+
if prev_pir_value:
139+
print('Motion ended.')
140+
is_pir_activated = False
141+
# reset indicator block to green
142+
aio.send(motion_feed.key, 0)
143+
144+
camera.capture('picam.jpg')
145+
print('snap!')
146+
with open("picam.jpg", "rb") as imageFile:
147+
image = base64.b64encode(imageFile.read())
148+
send_str = image.decode("utf-8")
149+
try:
150+
aio.send(picam_feed.key, send_str)
151+
print('sent to AIO!')
152+
except RequestError:
153+
print('Sending camera image failed...')
154+
155+
# Alarm System
156+
is_alarm = aio.receive(alarm_feed.key)
157+
158+
if is_alarm.value == "ON":
159+
# sample the current hour
160+
cur_time = time.localtime()
161+
cur_hour = time.tm_hour
162+
if (cur_hour > ALARM_HOUR and is_pir_activated is True):
163+
alarm_trigger()
164+
165+
prev_pir_value = door_sensor.value
166+
time.sleep(LOOP_INTERVAL)
110 KB
Binary file not shown.

0 commit comments

Comments
 (0)