-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain_ledonly.py
41 lines (33 loc) · 1.27 KB
/
main_ledonly.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
# This code is adapted from
# https://github.com/RuiSantosdotme/ESP-MicroPython/tree/master/code/MQTT/Node_RED_Client
def sub_cb(topic, msg):
if msg == b'led0on':
led0.value(1)
elif msg == b'led0off':
led0.value(0)
def connect_and_subscribe(client_id, mqtt_server, mqtt_port, mqtt_user, mqtt_passwd, topic_sub):
client = MQTTClient(client_id, mqtt_server, mqtt_port, mqtt_user, mqtt_passwd)
client.set_callback(sub_cb)
client.connect()
client.subscribe(topic_sub)
print('Connected to %s MQTT broker, subscribed to %s topic' % (mqtt_server, topic_sub))
return client
def restart_and_reconnect():
print('Failed to connect to MQTT broker. Reconnecting...')
time.sleep(10)
machine.reset()
try:
client = connect_and_subscribe(client_id, mqtt_server, mqtt_port, mqtt_user, mqtt_passwd, topic_sub)
#client = connect_and_subscribe(client_id, mqtt_server, mqtt_port, mqtt_user, mqtt_passwd, topic_sub)
except OSError as e:
restart_and_reconnect()
last_sensor_reading = 0
while True:
try:
client.check_msg()
if (time.time() - last_sensor_reading) > readings_interval:
msg = b'led0={0:1d}.'.format(led0.value())
client.publish(topic_pub, msg)
last_sensor_reading = time.time()
except OSError as e:
restart_and_reconnect()