Skip to content

Commit 0e9cf83

Browse files
authored
Create code.py
1 parent 0e9e86d commit 0e9cf83

File tree

1 file changed

+265
-0
lines changed

1 file changed

+265
-0
lines changed

Feather_Freezer_Alarm/code.py

Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
# SPDX-FileCopyrightText: 2022 Matt Desmarais for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import ssl
7+
import microcontroller
8+
import socketpool
9+
import wifi
10+
import board
11+
import neopixel
12+
import adafruit_minimqtt.adafruit_minimqtt as MQTT
13+
from adafruit_io.adafruit_io import IO_MQTT
14+
import digitalio
15+
from adafruit_debouncer import Debouncer
16+
import supervisor
17+
18+
#setup buzzer1
19+
buzzer1 = digitalio.DigitalInOut(board.D13)
20+
buzzer1.direction = digitalio.Direction.OUTPUT
21+
22+
#setup buzzer2
23+
buzzer2 = digitalio.DigitalInOut(board.D11)
24+
buzzer2.direction = digitalio.Direction.OUTPUT
25+
26+
#setup left door switch
27+
leftdoor = digitalio.DigitalInOut(board.D5)
28+
leftdoor.direction = digitalio.Direction.INPUT
29+
leftdoor.pull = digitalio.Pull.UP
30+
leftswitch = Debouncer(leftdoor)
31+
32+
#setup right door switch
33+
rightdoor = digitalio.DigitalInOut(board.D9)
34+
rightdoor.direction = digitalio.Direction.INPUT
35+
rightdoor.pull = digitalio.Pull.UP
36+
rightswitch = Debouncer(rightdoor)
37+
38+
#setup motion sensor
39+
pir = digitalio.DigitalInOut(board.D6)
40+
pir.direction = digitalio.Direction.INPUT
41+
motion = Debouncer(pir)
42+
43+
try:
44+
from secrets import secrets
45+
except ImportError:
46+
print("WiFi and Adafruit IO credentials are kept in secrets.py - please add them there!")
47+
raise
48+
49+
# Add your Adafruit IO Username and Key to secrets.py
50+
# (visit io.adafruit.com if you need to create an account,
51+
# or if you need to obtain your Adafruit IO key.)
52+
aio_username = secrets["aio_username"]
53+
aio_key = secrets["aio_key"]
54+
55+
# WiFi
56+
try:
57+
print("Connecting to %s" % secrets["ssid"])
58+
wifi.radio.connect(secrets["ssid"], secrets["password"])
59+
print("Connected to %s!" % secrets["ssid"])
60+
# Wi-Fi connectivity fails with error messages, not specific errors, so this except is broad.
61+
except Exception as e: # pylint: disable=broad-except
62+
print("Failed to connect to WiFi. Error:", e, "\nBoard will restart in 5 seconds.")
63+
time.sleep(5)
64+
microcontroller.reset()
65+
66+
# Create a socket pool
67+
pool = socketpool.SocketPool(wifi.radio)
68+
69+
# Initialize a new MQTT Client object
70+
mqtt_client = MQTT.MQTT(
71+
broker="io.adafruit.com",
72+
username=secrets["aio_username"],
73+
password=secrets["aio_key"],
74+
socket_pool=pool,
75+
ssl_context=ssl.create_default_context(),
76+
)
77+
78+
# Define callback functions which will be called when certain events happen.
79+
def connected(client):
80+
print("Connected to Adafruit IO! Listening for Freezer changes...")
81+
82+
# Initialize Adafruit IO MQTT "helper"
83+
io = IO_MQTT(mqtt_client)
84+
85+
# Set up the callback methods above
86+
io.on_connect = connected
87+
88+
#start time for timed uploads
89+
start = int(time.time()/300)
90+
#door timers set start times to now
91+
start1 = time.monotonic()
92+
start2 = time.monotonic()
93+
#door alarms set to False
94+
prealarm = False
95+
alarm1 = False
96+
alarm2 = False
97+
98+
door1feed = "unit-6.door1"
99+
door2feed = "unit-6.door2"
100+
motionfeed = "unit-6.motion"
101+
alarmfeed = "unit-6.alarm"
102+
resolvedfeed = "unit-6.resolved"
103+
#reconnectedfeed = "unit-6.reconnected"
104+
105+
#initial publishes all zeros
106+
try:
107+
io.connect()
108+
# Adafruit IO fails with internal error types and WiFi fails with specific messages.
109+
# This except is broad to handle any possible failure.
110+
except Exception as e: # pylint: disable=broad-except
111+
print("Failed to get or send data, or connect. Error:", e,
112+
"\nBoard will restart in 20 seconds.")
113+
time.sleep(20)
114+
microcontroller.reset()
115+
io.publish(alarmfeed, 0)
116+
io.publish(resolvedfeed, 0)
117+
#io.publish(reconnectedfeed, 1)
118+
#time.sleep(.25)
119+
#io.publish(reconnectedfeed, 0)
120+
121+
while True:
122+
try:
123+
# If Adafruit IO is not connected...
124+
if not io.is_connected:
125+
# Connect the client to the MQTT broker.
126+
print("Connecting to Adafruit IO...")
127+
io.connect()
128+
129+
time.sleep(1)
130+
#update leftswitch
131+
leftswitch.update()
132+
#if door closed upload to IO
133+
if leftswitch.fell:
134+
print('left closed')
135+
io.publish(door1feed, 1)
136+
time.sleep(.25)
137+
io.publish(door1feed, 0)
138+
#if door opened upload to IO, set start1 to now
139+
if leftswitch.rose:
140+
print('left opened')
141+
io.publish(door1feed, 1)
142+
start1 = time.monotonic()
143+
#if door remains open
144+
if leftswitch.value:
145+
print('still left open')
146+
#if door remains closed, reset start1
147+
else:
148+
#door still closed reset timer
149+
print('still left closed')
150+
start1 = time.monotonic()
151+
152+
#update rightswitch
153+
rightswitch.update()
154+
#if door closed upload to IO
155+
if rightswitch.fell:
156+
print('right closed')
157+
io.publish(door2feed, 1)
158+
time.sleep(.25)
159+
io.publish(door2feed, 0)
160+
#if door opened upload to IO, set start2 to now
161+
if rightswitch.rose:
162+
print('right opened')
163+
io.publish(door2feed, 1)
164+
start2 = time.monotonic()
165+
if rightswitch.value:
166+
print('still right open')
167+
#door still closed reset timer
168+
else:
169+
print('still right closed')
170+
start2 = time.monotonic()
171+
172+
#if a door closes update both switches
173+
if rightswitch.fell or leftswitch.fell:
174+
rightswitch.update()
175+
leftswitch.update()
176+
#if both doors are closed
177+
if not rightswitch.value and not leftswitch.value:
178+
print('doors just closed')
179+
#if prelarm is true then set it to False
180+
if(prealarm == True):
181+
buzzer1.value = False
182+
#if an alarm is true then upload to IO alarm resolved
183+
if alarm1 or alarm2:
184+
#publish 0 to alarm feed
185+
io.publish(alarmfeed, 0)
186+
#buzzers off/Alarms to False
187+
buzzer1.value = False
188+
buzzer2.value = False
189+
alarm1 = False
190+
alarm2 = False
191+
#toggle alarm resolved feed to send email notification
192+
io.publish(resolvedfeed, 1)
193+
time.sleep(5)
194+
io.publish(resolvedfeed, 0)
195+
196+
#check motion sensor if there is no alarm
197+
if(alarm1 == False and alarm2 == False and prealarm == False):
198+
#update pir sensor
199+
motion.update()
200+
#if motion stopped
201+
if motion.fell:
202+
print('motion stopped')
203+
#publish 0 to motion feed
204+
io.publish(motionfeed, 1)
205+
time.sleep(.25)
206+
io.publish(motionfeed, 0)
207+
#if motion started
208+
if motion.rose:
209+
print('motion detected')
210+
#reset start times
211+
start1 = time.monotonic()
212+
start2 = time.monotonic()
213+
#if continued motion
214+
elif motion.value:
215+
print('still motion')
216+
io.publish(motionfeed, 1)
217+
time.sleep(5)
218+
#if continued no motion
219+
else:
220+
print('no motion')
221+
222+
print("\n")
223+
224+
# Explicitly pump the message loop.
225+
io.loop()
226+
227+
#check difference between time now and start times if more than N seconds start beeping
228+
if (((time.monotonic() - start1) >= 300) or ((time.monotonic() - start2) >= 300)):
229+
prealarm = True
230+
#beeping
231+
buzzer1.value = True
232+
time.sleep(.5)
233+
buzzer1.value = False
234+
235+
#check if difference between time now and start1 if more than X seconds turn on buzzer2
236+
if (alarm1 == False and ((time.monotonic() - start1) >= 600)):
237+
alarm1 = True
238+
buzzer2.value = True
239+
#publish 1 to alarm feed
240+
io.publish(alarmfeed, 1)
241+
242+
#check if difference between time now and start2 if more than X seconds turn on buzzer2
243+
if (alarm2 == False and ((time.monotonic() - start2) >= 600)):
244+
alarm2 = True
245+
buzzer2.value = True
246+
#publish 1 to alarm feed
247+
io.publish(alarmfeed, 1)
248+
249+
print(str(time.time()))
250+
print(str(int(time.time()/300)))
251+
#check if 300 seconds have passed compared to start time, if so publish values
252+
if(int(time.time()/300) > start):
253+
print("PUBLISH EVERY FIVE MINUTES")
254+
start = int(time.time()/300)
255+
io.publish(door1feed, int(leftswitch.value))
256+
io.publish(door2feed, int(rightswitch.value))
257+
io.publish(motionfeed, int(motion.value))
258+
259+
# Adafruit IO fails with internal error types and WiFi fails with specific messages.
260+
# This except is broad to handle any possible failure.
261+
except Exception as e: # pylint: disable=broad-except
262+
print("Failed to get or send data, or connect. Error:", e,
263+
"\nBoard will restart in 20 seconds.")
264+
time.sleep(20)
265+
microcontroller.reset()

0 commit comments

Comments
 (0)