Simple test¶
Ensure your device works with this simple test.
1# SPDX-FileCopyrightText: 2021 Dylan Herrada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5import ssl
6import board
7from digitalio import DigitalInOut, Direction, Pull
8import touchio
9import socketpool
10import wifi
11import adafruit_minimqtt.adafruit_minimqtt as MQTT
12from adafruit_io.adafruit_io import IO_MQTT
13from adafruit_dash_display import Hub
14
15up = DigitalInOut(board.BUTTON_UP)
16up.direction = Direction.INPUT
17up.pull = Pull.DOWN
18
19select = DigitalInOut(board.BUTTON_SELECT)
20select.direction = Direction.INPUT
21select.pull = Pull.DOWN
22
23down = DigitalInOut(board.BUTTON_DOWN)
24down.direction = Direction.INPUT
25down.pull = Pull.DOWN
26
27back = touchio.TouchIn(board.CAP7)
28submit = touchio.TouchIn(board.CAP8)
29
30try:
31 from secrets import secrets
32except ImportError:
33 print("WiFi secrets are kept in secrets.py, please add them there!")
34 raise
35
36display = board.DISPLAY
37
38# Set your Adafruit IO Username and Key in secrets.py
39# (visit io.adafruit.com if you need to create an account,
40# or if you need your Adafruit IO key.)
41aio_username = secrets["aio_username"]
42aio_key = secrets["aio_key"]
43
44print("Connecting to %s" % secrets["ssid"])
45wifi.radio.connect(secrets["ssid"], secrets["password"])
46print("Connected to %s!" % secrets["ssid"])
47
48# Create a socket pool
49pool = socketpool.SocketPool(wifi.radio)
50
51# Initialize a new MQTT Client object
52mqtt_client = MQTT.MQTT(
53 broker="io.adafruit.com",
54 username=secrets["aio_username"],
55 password=secrets["aio_key"],
56 socket_pool=pool,
57 ssl_context=ssl.create_default_context(),
58)
59
60# Initialize an Adafruit IO MQTT Client
61io = IO_MQTT(mqtt_client)
62
63
64def pub_lamp(lamp):
65 if isinstance(lamp, str):
66 lamp = eval(lamp) # pylint: disable=eval-used
67 iot.publish("lamp", str(not lamp))
68 # funhouse.set_text(f"Lamp: {not lamp}", 0)
69 time.sleep(0.3)
70
71
72iot = Hub(display=display, io=io, nav=(up, select, down, back, submit))
73
74iot.add_device(
75 feed_key="lamp",
76 default_text="Lamp: ",
77 formatted_text="Lamp: {}",
78 pub_method=pub_lamp,
79)
80iot.add_device(
81 feed_key="temperature",
82 default_text="Temperature: ",
83 formatted_text="Temperature: {:.1f} C",
84)
85iot.add_device(
86 feed_key="humidity", default_text="Humidity: ", formatted_text="Humidity: {:.2f}%"
87)
88
89iot.get()
90
91while True:
92 iot.loop()
93 time.sleep(0.01)
Advanced example¶
This illustrates more features of the library
1# SPDX-FileCopyrightText: 2021 Dylan Herrada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5import ssl
6import displayio
7import board
8from digitalio import DigitalInOut, Direction, Pull
9from adafruit_display_text.label import Label
10import terminalio
11import touchio
12import socketpool
13import wifi
14import adafruit_minimqtt.adafruit_minimqtt as MQTT
15from adafruit_io.adafruit_io import IO_MQTT
16from adafruit_dash_display import Hub
17
18up = DigitalInOut(board.BUTTON_UP)
19up.direction = Direction.INPUT
20up.pull = Pull.DOWN
21
22select = DigitalInOut(board.BUTTON_SELECT)
23select.direction = Direction.INPUT
24select.pull = Pull.DOWN
25
26down = DigitalInOut(board.BUTTON_DOWN)
27down.direction = Direction.INPUT
28down.pull = Pull.DOWN
29
30back = touchio.TouchIn(board.CAP7)
31submit = touchio.TouchIn(board.CAP8)
32
33try:
34 from secrets import secrets
35except ImportError:
36 print("WiFi secrets are kept in secrets.py, please add them there!")
37 raise
38
39rgb_group = displayio.Group()
40R_label = Label(
41 terminalio.FONT,
42 text=" +\nR:\n -",
43 color=0xFFFFFF,
44 anchor_point=((0, 0.5)),
45 anchored_position=((5, 120)),
46 scale=2,
47)
48G_label = Label(
49 terminalio.FONT,
50 text=" +\nG:\n -",
51 color=0xFFFFFF,
52 anchor_point=((0, 0.5)),
53 anchored_position=((90, 120)),
54 scale=2,
55)
56B_label = Label(
57 terminalio.FONT,
58 text=" +\nB:\n -",
59 color=0xFFFFFF,
60 anchor_point=((0, 0.5)),
61 anchored_position=((175, 120)),
62 scale=2,
63)
64rgb_group.append(R_label)
65rgb_group.append(G_label)
66rgb_group.append(B_label)
67R = Label(
68 terminalio.FONT,
69 text="00",
70 color=0xFFFFFF,
71 anchor_point=((0, 0.5)),
72 anchored_position=((35, 120)),
73 scale=2,
74)
75G = Label(
76 terminalio.FONT,
77 text="00",
78 color=0xFFFFFF,
79 anchor_point=((0, 0.5)),
80 anchored_position=((120, 120)),
81 scale=2,
82)
83B = Label(
84 terminalio.FONT,
85 text="00",
86 color=0xFFFFFF,
87 anchor_point=((0, 0.5)),
88 anchored_position=((205, 120)),
89 scale=2,
90)
91rgb_group.append(R)
92rgb_group.append(G)
93rgb_group.append(B)
94
95# pylint: disable=unused-argument
96
97
98def rgb(last):
99 display.show(None)
100 rgb_group[3].text = "00"
101 rgb_group[4].text = "00"
102 rgb_group[5].text = "00"
103 display.show(rgb_group)
104 time.sleep(0.2)
105 index = 0
106 colors = [00, 00, 00]
107
108 while True:
109 if select.value:
110 index += 1
111 if index == 3:
112 index = 0
113 time.sleep(0.3)
114 continue
115
116 if up.value:
117 colors[index] += 1
118 if colors[index] == 256:
119 colors[index] = 0
120 rgb_group[index + 3].text = hex(colors[index])[2:]
121 time.sleep(0.01)
122 continue
123
124 if down.value:
125 colors[index] -= 1
126 if colors[index] == -1:
127 colors[index] = 255
128 rgb_group[index + 3].text = hex(colors[index])[2:]
129 time.sleep(0.01)
130 continue
131
132 if submit.value:
133 color = ["{:02x}".format(colors[i]) for i in range(len(colors))]
134 color = "#" + "".join(color)
135 iot.publish("neopixel", color)
136 break
137
138 if back.value:
139 break
140 time.sleep(0.1)
141
142 display.show(None)
143 time.sleep(0.1)
144
145
146def rgb_set_color(message): # pylint: disable=unused-argument
147 return int(message[1:], 16)
148
149
150def door_color(message):
151 door = bool(int(message))
152 if door:
153 return int(0x00FF00)
154 return int(0xFF0000)
155
156
157def on_door(client, feed_id, message):
158 door = bool(int(message))
159 if door:
160 return "Door: Closed"
161 return "Door: Open"
162
163
164def pub_lamp(lamp):
165 if isinstance(lamp, str):
166 lamp = eval(lamp) # pylint: disable=eval-used
167 iot.publish("lamp", str(not lamp))
168 # funhouse.set_text(f"Lamp: {not lamp}", 0)
169 time.sleep(0.3)
170
171
172display = board.DISPLAY
173
174# Set your Adafruit IO Username and Key in secrets.py
175# (visit io.adafruit.com if you need to create an account,
176# or if you need your Adafruit IO key.)
177aio_username = secrets["aio_username"]
178aio_key = secrets["aio_key"]
179
180print("Connecting to %s" % secrets["ssid"])
181wifi.radio.connect(secrets["ssid"], secrets["password"])
182print("Connected to %s!" % secrets["ssid"])
183
184# Create a socket pool
185pool = socketpool.SocketPool(wifi.radio)
186
187# Initialize a new MQTT Client object
188mqtt_client = MQTT.MQTT(
189 broker="io.adafruit.com",
190 username=secrets["aio_username"],
191 password=secrets["aio_key"],
192 socket_pool=pool,
193 ssl_context=ssl.create_default_context(),
194)
195
196# Initialize an Adafruit IO MQTT Client
197io = IO_MQTT(mqtt_client)
198
199iot = Hub(display=display, io=io, nav=(up, select, down, back, submit))
200
201iot.add_device(
202 feed_key="lamp",
203 default_text="Lamp: ",
204 formatted_text="Lamp: {}",
205 pub_method=pub_lamp,
206)
207iot.add_device(
208 feed_key="temperature",
209 default_text="Temperature: ",
210 formatted_text="Temperature: {:.1f} C",
211)
212iot.add_device(
213 feed_key="humidity", default_text="Humidity: ", formatted_text="Humidity: {:.2f}%"
214)
215iot.add_device(
216 feed_key="neopixel",
217 default_text="LED: ",
218 formatted_text="LED: {}",
219 color_callback=rgb_set_color,
220 pub_method=rgb,
221)
222iot.add_device(
223 feed_key="battery",
224 default_text="Battery: ",
225 formatted_text="Battery: {}%",
226)
227iot.add_device(
228 feed_key="door",
229 default_text="Door: ",
230 formatted_text="Door: {}",
231 color_callback=door_color,
232 callback=on_door,
233)
234
235iot.get()
236
237while True:
238 iot.loop()
239 time.sleep(0.01)