-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt2pins_wiznet5k.py
executable file
·382 lines (321 loc) · 11.4 KB
/
mqtt2pins_wiznet5k.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import gc
import json
import asyncio
import board
import busio
import digitalio
import supervisor
import microcontroller
import neopixel
import time
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket
import adafruit_logging as logging
from adafruit_led_animation import color
import adafruit_minimqtt.adafruit_minimqtt as MQTT
from queue import Queue, QueueFull
try:
from secrets import secrets
except ImportError:
print("Required info is kept in secrets.py, please add them there!")
raise
try:
from secrets import PINS, DEBUG
except ImportError:
print("Required PINS to manage is kept in secrets.py, please add them there!")
raise
class State:
def __init__(self):
self.debug = DEBUG
self.pixels = neopixel.NeoPixel(board.NEOPIXEL, 1, auto_write=True)
self.pins = [digitalio.DigitalInOut(x) for x in PINS]
for pin in self.pins:
pin.switch_to_output()
self.mqtt_connected = False
self.mqtt_client = None
self.eth = None
self.uptime_mins = 0
self.soft_dog = 0
self.counters = {}
self.mqtt_subs = {}
self.status_queue = Queue(maxsize=1)
def inc_counter(self, name):
curr_value = self.counters.get(name, 0)
self.counters[name] = curr_value + 1
def handle_message_ping(self, _topic, _message):
print("Handling ping")
send_status_now(self)
def handle_message_ports(self, _topic, message):
print(f"Handling ports: {message}")
for pin_index, value_str in enumerate(message):
if pin_index >= len(PINS):
break
new_value_dict = {
"0": False,
"1": True,
"!": not self.pins[pin_index].value,
}
self.pins[pin_index].value = new_value_dict.get(
value_str, self.pins[pin_index].value
)
def handle_message_port(self, topic, message):
if not message:
return
try:
pin_index = int(topic.split("/")[-1])
except Exception as e:
print(f"Failed to get pin_index from message: {e}")
return
assert pin_index >= 0 and pin_index < len(
PINS
), f"Unexpected pin_index {topic} from message: {message}"
value = message.lower() in {
"1",
"yes",
"yeah",
"yay",
"yup",
"y",
"on",
"up",
"go",
}
if not value and message.lower() in {
"!",
"not",
"flip",
"other",
"reverse",
"change",
}:
value = not self.pins[pin_index].value
print(f"Setting pin {pin_index} ({PINS[pin_index]}) to {value}")
self.pins[pin_index].value = value
def boom(message):
print(f"Handling boom: {message}")
time.sleep(5)
# bye bye cruel world
microcontroller.reset()
# Define MQTT callback methods which are called when events occur
def connected(client, state, flags, rc):
# This function will be called when the client is connected
# successfully to the broker.
print(f"Connected to MQTT BROKER")
# Subscribe to all feeds
topic = secrets["topic_prefix"] + "/ping"
client.subscribe(topic)
state.mqtt_subs[topic] = state.handle_message_ping
topic = secrets["topic_prefix"] + "/ports"
client.subscribe(topic)
state.mqtt_subs[topic] = state.handle_message_ports
for index in range(len(PINS)):
topic = secrets["topic_prefix"] + f"/{index}"
client.subscribe(topic)
state.mqtt_subs[topic] = state.handle_message_port
state.inc_counter("connect")
state.mqtt_connected = True
def disconnected(_client, state, rc):
# This method is called when the client is disconnected
print(f"Disconnected from MQTT BROKER rc: {rc}")
state.mqtt_connected = False
state.inc_counter("disconnected")
def subscribe(_client, state, topic, granted_qos):
# This method is called when the client subscribes to a new feed.
print(f"Subscribed to {topic} with QOS level {granted_qos}")
state.inc_counter("subscribe")
def publish(_client, state, topic, pid):
# This method is called when the client publishes data to a feed.
print(f"Published to {topic} with PID {pid}")
state.inc_counter("publish")
def message(client, topic, message):
# This method is called when a topic the client is subscribed to
# has a new message.
print("New message on topic {0}: {1}".format(topic, message))
state = client._user_data
if topic in state.mqtt_subs:
state.mqtt_subs[topic](topic, message)
state.inc_counter("message")
async def neo_status(state):
def cycle(items):
while True:
for item in items:
yield item
pixels_disc = cycle([color.RED, color.BLACK])
pixels_conn = cycle(
[
color.ORANGE,
color.YELLOW,
color.GREEN,
color.BLUE,
color.PURPLE,
color.MAGENTA,
color.TEAL,
color.CYAN,
color.WHITE,
color.GOLD,
color.PINK,
color.AQUA,
color.JADE,
color.AMBER,
color.OLD_LACE,
]
)
while True:
pixel_color = (
next(pixels_conn) if state.mqtt_connected else next(pixels_disc)
)
state.pixels.fill(pixel_color)
await asyncio.sleep(1)
async def bump_uptime(state):
while True:
await asyncio.sleep(60)
state.uptime_mins += 1
async def trigger_send_status(state):
while True:
# every 10 minutes and 10 seconds
await asyncio.sleep(60 * 10 + 10)
await state.status_queue.put(True)
def send_status_now(state):
try:
state.status_queue.put_nowait(True)
except QueueFull:
pass
async def send_status(state):
mqtt_pub_status = secrets["topic_prefix"] + "/status"
while True:
while not state.mqtt_connected:
await asyncio.sleep(1)
# Block until status is needed, which can be periodic or responding to ping msg
_ = await state.status_queue.get()
ports = ""
for pin_index in range(len(PINS)):
ports += "1" if state.pins[pin_index].value else "0"
value = {
"uptime_mins": state.uptime_mins,
"ip": state.eth.pretty_ip(state.eth.ip_address),
"ports": ports,
"counters": str(state.counters),
"mem_free": gc.mem_free(),
}
try:
state.mqtt_client.publish(mqtt_pub_status, json.dumps(value))
state.inc_counter("status")
if state.debug:
print(f"send_status: {mqtt_pub_status}: {value}")
except Exception as e:
print(f"Failed to send status: {e}")
async def soft_dogwatch(state):
# Note: this is mostly used to handle cases when there is an exception in
# net_monitor that could not be handled. When this happens, state.soft_dog will
# stop increasing and we will know it is time to panic.
soft_dogwatch_interval = 60
while True:
before_soft_dog = state.soft_dog
await asyncio.sleep(soft_dogwatch_interval)
if before_soft_dog == state.soft_dog:
boom(
f"state.soft_dog stuck at {before_soft_dog} after {soft_dogwatch_interval} seconds"
)
state.soft_dog = 0
async def net_monitor(state):
cs = digitalio.DigitalInOut(board.D10)
spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
try:
print("Connecting ethernet")
state.eth = WIZNET5K(spi_bus, cs, mac=secrets["mac"])
except Exception as e:
boom(f"Failed setup WIZNET5K: {e}")
print("Chip Version:", state.eth.chip)
print("MAC Address:", [hex(i) for i in state.eth.mac_address])
print("My IP address is:", state.eth.pretty_ip(state.eth.ip_address))
# Set up a MiniMQTT
MQTT.set_socket(socket, state.eth)
# https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT/issues/129
broker_user = secrets["broker_user"] if secrets["broker_user"] else None
broker_pass = secrets["broker_pass"] if secrets["broker_pass"] else None
state.mqtt_client = MQTT.MQTT(
broker=secrets["broker"],
port=1883,
username=broker_user,
password=broker_pass,
is_ssl=False,
)
if state.debug:
state.mqtt_client.enable_logger(logging, logging.DEBUG)
state.mqtt_client._user_data = state
state.mqtt_client.on_connect = connected
state.mqtt_client.on_disconnect = disconnected
state.mqtt_client.on_subscribe = subscribe
state.mqtt_client.on_publish = publish
state.mqtt_client.on_message = message
connect_backoff = 0
while True:
state.soft_dog += 1
await asyncio.sleep(0)
state.eth.maintain_dhcp_lease()
if not state.mqtt_connected:
try:
print("Connecting to MQTT broker...")
state.mqtt_client.connect()
assert (
state.mqtt_connected
), "connected callback should have happened"
connect_backoff = 0
# send status when MQTT gets connected
send_status_now(state)
except Exception as e:
print(f"Failed mqtt connect: {e}")
await asyncio.sleep(connect_backoff)
if connect_backoff < 18:
connect_backoff += 1
continue
try:
if not state.mqtt_client.loop(timeout=0.2):
# Take a little break if nothing really happened
await asyncio.sleep(0.123)
except Exception as e:
if state.debug:
print(f"Failed MQTT client loop: {e}")
await _try_disconnect(state)
async def _try_disconnect(state):
print("MQTT is disconnecting")
state.inc_counter("fail_loop")
await asyncio.sleep(3)
if not state.mqtt_connected:
if state.debug:
print("MQTT disconnect not needed, because broker is not connected")
return
# Force state to be disconnected
state.mqtt_connected = False
disc_ok = False
try:
state.mqtt_client.disconnect()
disc_ok = True
print("MQTT disconnect completed")
except Exception as e:
print(f"Failed mqtt disconnect: {e}")
try:
if not disc_ok:
state.inc_counter("eth_reset")
assert state.eth.sw_reset() == 0, f"Reset WIZNET5K did not go well"
print("Reset WIZNET5K completed")
except Exception as e:
boom(f"FATAL! Failed eth reset: {e}")
async def main():
state = State()
neo_status_task = asyncio.create_task(neo_status(state))
bump_uptime_task = asyncio.create_task(bump_uptime(state))
trigger_send_status_task = asyncio.create_task(trigger_send_status(state))
send_status_task = asyncio.create_task(send_status(state))
soft_dogwatch_task = asyncio.create_task(soft_dogwatch(state))
net_monitor_task = asyncio.create_task(net_monitor(state))
await asyncio.gather(
neo_status_task,
bump_uptime_task,
trigger_send_status_task,
send_status_task,
soft_dogwatch_task,
net_monitor_task,
)
supervisor.disable_autoreload()
asyncio.run(main())