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