-
Notifications
You must be signed in to change notification settings - Fork 1
/
allnet_B18_light_barrier.py
48 lines (35 loc) · 1.16 KB
/
allnet_B18_light_barrier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from micropython import const
from machine import Pin
from utime import ticks_ms, time, localtime
from ntptime import settime
import urequests
UTC_OFFSET = const(7200)
BARRIER_GPIO_PIN = const(23)
PUSHOVER_TOKEN = 'YOUR PUSHOVER TOKEN'
PUSHOVER_USER_KEY = 'YOUR PUSHOVER USER KEY'
def irq_handler(pin) -> None:
"""
irq handler
:param pin: pin object
:return: None
"""
global last_time
_ = pin
new_time = ticks_ms()
if (new_time - last_time) > 500:
last_time = new_time
msg = f"token={PUSHOVER_TOKEN}&user={PUSHOVER_USER_KEY}"
msg += f"&title=Alert&message=Light Barrier Interrupted"
msg += f"&sound=siren×tamp={time()}"
r = urequests.post(url="https://api.pushover.net/1/messages.json", data=msg)
print(f'[INFO] Response: {r.text}')
r.close()
if __name__ == '__main__':
last_time = 0
try:
settime()
except Exception as t_err:
print(f'[ERROR] Connection to NTP server failed: {t_err}')
actual = localtime(time() + UTC_OFFSET)
barrier = Pin(BARRIER_GPIO_PIN, Pin.IN, Pin.PULL_DOWN)
barrier.irq(handler=irq_handler, trigger=Pin.IRQ_RISING)