|
13 | 13 | (https://github.com/adafruit/Adafruit_Blinka)
|
14 | 14 | - Adafruit_CircuitPython_SGP30
|
15 | 15 | (https://github.com/adafruit/Adafruit_CircuitPython_SGP30)
|
| 16 | + - Adafruit_CircuitPython_NeoPixel |
| 17 | + (https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel) |
16 | 18 | - picamera
|
| 19 | + (https://github.com/waveform80/picamera) |
17 | 20 | """
|
18 | 21 | # Import standard python modules
|
19 | 22 | import time
|
20 | 23 | import base64
|
21 |
| -import os |
22 | 24 | # import Adafruit IO REST client
|
23 |
| -from Adafruit_IO import Client, Feed, RequestError |
| 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 |
24 | 31 |
|
25 | 32 | # import Adafruit Blinka
|
26 | 33 | from board import SCL, SDA, D18, D22, D24
|
27 | 34 | from busio import I2C
|
28 | 35 | import digitalio
|
29 |
| -import neopixel_write |
30 |
| - |
31 |
| -# import Adafruit_CircuitPython_SGP30 and picam |
32 |
| -import adafruit_sgp30 |
33 |
| -import picamera |
34 | 36 |
|
35 |
| -# NeoPixels Commands |
36 |
| -PIXELS_OFF = bytearray([0, 0, 0]) |
37 |
| -PIXELS_ON = bytearray([0, 255, 0]) |
| 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) |
38 | 42 |
|
39 | 43 | # Set to the hour at which to arm the alarm system, 24hr time
|
40 | 44 | ALARM_HOUR = 16
|
|
85 | 89 | sgp30 = adafruit_sgp30.Adafruit_SGP30(i2c_bus)
|
86 | 90 |
|
87 | 91 | # set up the neopixel strip
|
88 |
| -pixel_strip = digitalio.DigitalInOut(D18) |
89 |
| -pixel_strip.direction = digitalio.Direction.OUTPUT |
90 |
| -neopixel_write.neopixel_write(pixel_strip, PIXELS_OFF) |
91 |
| - |
92 |
| -print('Adafruit IO Home: Security') |
| 92 | +pixels = neopixel.NeoPixel(D18, NUM_PIXELS_STRIP) |
| 93 | +pixels.fill((0, 0, 0)) |
| 94 | +pixels.show() |
93 | 95 |
|
94 | 96 | def alarm_trigger():
|
95 |
| - """Alarm is triggered by the dashboard toggle |
96 |
| - and a sensor detecting movement. |
97 |
| - """ |
98 |
| - print('* SYSTEM ALARM!') |
99 |
| - for j in range(0, 5): |
100 |
| - neopixel_write.neopixel_write(pixel_strip, PIXELS_ON) |
| 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() |
101 | 104 | time.sleep(0.5)
|
102 |
| - neopixel_write.neopixel_write(pixel_strip, PIXELS_OFF) |
103 |
| - # turn pixels off after alarm animation |
104 |
| - neopixel_write.neopixel_write(pixel_strip, PIXELS_OFF) |
| 105 | + # turn pixels off after alarm |
| 106 | + pixels.fill((0, 0, 0)) |
| 107 | + pixels.show() |
105 | 108 |
|
106 |
| -while True: |
107 |
| - # read SGP30 |
108 |
| - co2eq, tvoc = sgp30.iaq_measure() |
109 |
| - print("CO2eq = %d ppm \t TVOC = %d ppb" % (co2eq, tvoc)) |
110 |
| - # send SGP30 values to Adafruit IO |
111 |
| - aio.send(eco2_feed.key, co2eq) |
112 |
| - aio.send(tvoc_feed.key, tvoc) |
113 |
| - time.sleep(0.5) |
114 |
| - |
115 |
| - # read/send door sensor |
116 |
| - if door_sensor.value: |
117 |
| - print('Door Open!') |
118 |
| - # change indicator block to red |
119 |
| - aio.send(door_feed.key, 3) |
120 |
| - else: |
121 |
| - print('Door Closed.') |
122 |
| - # reset indicator block to green |
123 |
| - aio.send(door_feed.key, 0) |
124 |
| - |
125 |
| - # read/send motion sensor |
126 |
| - if door_sensor.value: |
127 |
| - if not prev_pir_value: |
128 |
| - print('Motion detected!') |
129 |
| - is_pir_activated = True |
130 |
| - # change indicator block to red |
131 |
| - aio.send(motion_feed.key, 3) |
132 |
| - else: |
133 |
| - if prev_pir_value: |
134 |
| - print('Motion ended.') |
135 |
| - is_pir_activated = False |
136 |
| - # reset indicator block to green |
137 |
| - aio.send(motion_feed.key, 0) |
138 |
| - |
139 |
| - camera.capture('picam.jpg') |
140 |
| - print('snap!') |
141 |
| - with open("picam.jpg", "rb") as imageFile: |
142 |
| - image = base64.b64encode(imageFile.read()) |
143 |
| - send_str = image.decode("utf-8") |
144 |
| - try: |
145 |
| - aio.send(picam_feed.key, send_str) |
146 |
| - print('sent to AIO!') |
147 |
| - except: |
148 |
| - print('Sending camera image failed...') |
149 |
| - |
150 |
| - # Alarm System |
151 |
| - is_alarm = aio.receive(alarm_feed.key) |
152 |
| - |
153 |
| - if (is_alarm.value == "ON"): |
154 |
| - # sample the current hour |
155 |
| - cur_time = time.localtime() |
156 |
| - cur_hour = time.tm_hour |
157 |
| - if (cur_hour > ALARM_HOUR and is_pir_activated == True): |
158 |
| - alarm_trigger() |
159 |
| - |
160 |
| - prev_pir_value = door_sensor.value |
161 |
| - time.sleep(LOOP_INTERVAL) |
| 109 | +print('Adafruit IO Home: Security') |
162 | 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) |
163 | 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) |
0 commit comments