-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.py
81 lines (65 loc) · 2.13 KB
/
code.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import alarm
import board
import time
import ssl
import socketpool
import wifi
import adafruit_minimqtt.adafruit_minimqtt as MQTT
from adafruit_minimqtt.adafruit_minimqtt import MMQTTException
# Must degrade to circuitpython 7.1.0 to use touchalarm
# Get wifi details and more from a secrets.py file
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
# Create a socket pool
pool = socketpool.SocketPool(wifi.radio)
# Create an alarm that will trigger if pin is touched.
touch_alarm = alarm.touch.TouchAlarm(pin=board.A2)
# Print out which alarm woke us up, if any.
print(alarm.wake_alarm)
def network_connect():
print("Connecting to %s"%secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["wifi_pw"])
print("Connected to %s!"%secrets["ssid"])
print("My IP address is", wifi.radio.ipv4_address)
def connected(client, userdata, flags, rc):
# This function will be called when the client is connected
# successfully to the broker.
print("Connected to broker!")
def disconnected(client, userdata, rc):
print("Disconnected from broker!")
def mqtt_connect():
global client
# Set up a MiniMQTT Client
client = MQTT.MQTT(
broker=secrets["broker"],
port=secrets["port"],
username=secrets["user"],
password=secrets["pw"],
client_id=secrets["client_id"],
socket_pool=pool,
ssl_context=ssl.create_default_context(),
keep_alive=60,
)
# Setup the callback methods above
client.on_connect = connected
client.on_disconnect = disconnected
# Connect the client to the MQTT broker.
print("Connecting to MQTT broker...")
client.connect()
print("Connecting WIFI")
network_connect()
print("Connecting MQTT")
mqtt_connect()
# Send a new message
print("Answering door...")
if alarm.wake_alarm:
client.publish("doorbell", "open me")
print("Sent!")
client.disconnect()
time.sleep(2)
# Exit the program, and then deep sleep until one of the alarms wakes us.
alarm.exit_and_deep_sleep_until_alarms(touch_alarm)
# Does not return, so we never get here.