-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinky.py
68 lines (57 loc) · 1.49 KB
/
inky.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
import time
import machine
from picographics import PicoGraphics, DISPLAY_INKY_PACK
from umqtt.simple import MQTTClient
import network
import json
# Connect to network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
# mqtt
SERVER="192.168.1.150"
ClientID = f'raspberry-sub-{time.time_ns()}'
topic = "jsquared/inky"
topic = 'zigbee2mqtt/+'
# Fill in your network name (ssid) and password here:
ssid = 'LBW2gig'
password = 'castleton'
wlan.connect(ssid, password)
# Display
graphics = PicoGraphics(DISPLAY_INKY_PACK)
WIDTH, HEIGHT = graphics.get_bounds()
graphics.set_update_speed(3)
graphics.set_font("sans")
graphics.set_pen(15)
graphics.clear()
graphics.set_pen(0)
graphics.set_thickness(5)
graphics.update()
# callback for receiving mqtt messages
def sub(topic, msg):
print('received message %s on topic %s' % (msg, topic))
j = json.loads(msg)
t = j['temperature']
graphics.set_pen(15)
graphics.clear()
graphics.set_pen(0)
graphics.text(str(t), 20, 60, scale=4)
graphics.update()
def connect(client):
print('Connecting to MQTT Broker "%s"' % (SERVER))
client.connect()
def reconnect(client):
print('Failed to connect to MQTT broker, Reconnecting...')
time.sleep(5)
client.connect()
def main():
client = MQTTClient(ClientID, SERVER, 1883)
try:
connect(client)
except OSError as e:
reconnect(client)
client.set_callback(sub)
client.subscribe(topic)
while True:
client.check_msg()
time.sleep(1)
main()