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