-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
152 lines (120 loc) · 4.24 KB
/
code.py
File metadata and controls
152 lines (120 loc) · 4.24 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import time
import board
import neopixel
from analogio import AnalogOut
import ipaddress
import wifi
import socketpool
import adafruit_minimqtt.adafruit_minimqtt as MQTT
# Define callback methods which are called when events occur
# pylint: disable=unused-argument, redefined-outer-name
def connect(mqtt_client, userdata, flags, rc):
# This function will be called when the mqtt_client is connected
# successfully to the broker.
print("Connected to MQTT Broker!")
print("Flags: {0}\n RC: {1}".format(flags, rc))
def disconnect(mqtt_client, userdata, rc):
# This method is called when the mqtt_client disconnects
# from the broker.
print("Disconnected from MQTT Broker!")
led.fill(ORANGE)
wifi_connected = False
def subscribe(mqtt_client, userdata, topic, granted_qos):
# This method is called when the mqtt_client subscribes to a new feed.
print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))
def unsubscribe(mqtt_client, userdata, topic, pid):
# This method is called when the mqtt_client unsubscribes from a feed.
print("Unsubscribed from {0} with PID {1}".format(topic, pid))
def publish(mqtt_client, userdata, topic, pid):
# This method is called when the mqtt_client publishes data to a feed.
print("Published to {0} with PID {1}".format(topic, pid))
def message(client, topic, message):
# Method called when a client's subscribed feed has a new value.
print("New message on topic {0}: {1}".format(topic, message))
if len(message.split(":")) == 2:
Key = message.split(":")[0]
KeyValue = message.split(":")[1]
if Key == "router":
if KeyValue == "reboot":
try:
led.fill(BLUE)
analog_out.value = 65535
mqtt_client.publish(secrets["mqtt_topic"], "status:power_off")
time.sleep(secrets["reboot_time"])
analog_out.value = 0
mqtt_client.publish(secrets["mqtt_topic"], "status:reboot_done")
led.fill(GREEN)
print ("Reboot complete")
except:
print ("Failed to complete reboot")
analog_out.value = 0
led.fill(ORANGE)
def connect_wifi():
try:
print("My MAC addr:", [hex(i) for i in wifi.radio.mac_address])
print("Connecting to %s"%secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to %s!"%secrets["ssid"])
print("My IP address is", wifi.radio.ipv4_address)
except:
print ("Failed to connect to wifi")
led.fill(ORANGE)
return False, None
# Create a socket pool
pool = socketpool.SocketPool(wifi.radio)
# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
broker=secrets["mqtt_broker"],
port=secrets["mqtt_port"],
socket_pool=pool,
)
# Connect callback handlers to mqtt_client
mqtt_client.on_connect = connect
mqtt_client.on_disconnect = disconnect
mqtt_client.on_subscribe = subscribe
mqtt_client.on_unsubscribe = unsubscribe
mqtt_client.on_publish = publish
mqtt_client.on_message = message
try:
print("Attempting to connect to %s" % mqtt_client.broker)
mqtt_client.connect()
except:
print ("Failed to connect to MQTT broker")
led.fill(ORANGE)
return False, None
print("Subscribing to %s" % secrets["mqtt_topic"])
mqtt_client.subscribe(secrets["mqtt_topic"])
led.fill(GREEN)
return True, mqtt_client
# 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
#Onboard neopixel is used as a status indicator
BLACK = (0,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
ORANGE = (255,127,0)
led = neopixel.NeoPixel(board.NEOPIXEL, 1)
led.fill(BLACK)
#Pin A0 is connected to IoT Relay switch
analog_out = AnalogOut(board.A0)
analog_out.value = 0
wifi_connected = False
while True:
if not wifi_connected:
wifi_connected, mqtt_client = connect_wifi()
if wifi_connected:
if not mqtt_client.is_connected():
wifi_connected = False
led.fill(ORANGE)
if wifi_connected:
try:
mqtt_client.loop()
except:
print ("Lost connection to MQTT broker")
wifi_connected = False
led.fill(ORANGE)
time.sleep(0.5)