-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
executable file
·188 lines (161 loc) · 6.2 KB
/
code.py
File metadata and controls
executable file
·188 lines (161 loc) · 6.2 KB
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
# SPDX-FileCopyrightText: 2020 John Park for Adafruit Industries
#
# SPDX-License-Identifier: MIT
# Matrix Weather display
# For Metro M4 Airlift with RGB Matrix shield, 64 x 32 RGB LED Matrix display
"""
This example queries the Open Weather Maps site API to find out the current
weather for your location... and display it on a screen!
if you can find something that spits out JSON data, we can display it
"""
from os import getenv
import gc
import time
import json
import board
import busio
import displayio
import terminalio
import microcontroller
from digitalio import DigitalInOut, Direction, Pull
import adafruit_connection_manager
import adafruit_requests
from adafruit_display_text import label
#from adafruit_matrixportal.network import Network
from adafruit_matrixportal.matrix import Matrix
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_minimqtt.adafruit_minimqtt as MQTT
import custom_display
secrets = {
"ssid": getenv("CIRCUITPY_WIFI_SSID"),
"password": getenv("CIRCUITPY_WIFI_PASSWORD"),
"mqtt_host": getenv("MQTT_BROKER"),
"mqtt_user": getenv("MQTT_USER"),
"mqtt_pass": getenv("MQTT_PASS"),
"airgradient_id": getenv("AIRGRADIENT_ID")
}
gc.enable()
matrix = Matrix(width=32, height=32)
display = matrix.display
#display.root_group = None
#displayio.release_displays()
group = displayio.Group() # Create a Group
bitmap = displayio.Bitmap(32, 32, 2) # Create a bitmap object,width, height, bit depth
color = displayio.Palette(4) # Create a color palette
color[0] = 0x000000 # black background
color[1] = 0xFF0000 # red
color[2] = 0xCC4000 # amber
color[3] = 0x00FF00 # greenish
tile_grid = displayio.TileGrid(bitmap, pixel_shader=color)
group.append(tile_grid) # Add the TileGrid to the Group
display.root_group = group
#font = terminalio.FONT
display_text = 'init'
screen_text = label.Label(terminalio.FONT, text=display_text)
screen_text.x = 5
screen_text.y = 15
screen_text.color = 0x00FF00
group.append(screen_text)
if secrets == {"ssid": None, "password": None}:
try:
# Fallback on secrets.py until depreciation is over and option is removed
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in settings.toml, please add them there!")
raise
# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
# Secondary (SCK1) SPI used to connect to WiFi board on Arduino Nano Connect RP2040
if "SCK1" in dir(board):
spi = busio.SPI(board.SCK1, board.MOSI1, board.MISO1)
else:
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
pool = adafruit_connection_manager.get_radio_socketpool(esp)
ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp)
requests = adafruit_requests.Session(pool, ssl_context)
screen_text.text = 'WiFi'
print("Connecting to " + secrets["ssid"] + "...")
while not esp.is_connected:
try:
esp.connect_AP(secrets["ssid"], secrets["password"])
except OSError as e:
print("could not connect to AP, retrying: ", e)
continue
print("Connected to", esp.ap_info.ssid, "\tRSSI:", esp.ap_info.rssi)
print("IP address is", esp.ipv4_address)
# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
broker=secrets["mqtt_host"],
username=secrets["mqtt_user"],
password=secrets["mqtt_pass"],
socket_pool=pool,
ssl_context=ssl_context,
)
mqtt_topic = f"airgradient/readings/" + secrets["airgradient_id"]
mqtt_topic_aqi = f"homeassistant/sensor/ag_outdoors_calc_aqi/state"
mqtt_topic_night = f"homeassistant/binary_sensor/night_time/state"
main_display = custom_display.CustomDisplay(matrix.display)
# Define callback methods which are called when events occur
def connect(mqtt_client, userdata, flags, rc):
# This function will be called when the mqtt_client is connected
# successfully to the broker.
print("Connected to MQTT Broker!")
print(f"Flags: {flags}\n RC: {rc}")
def disconnect(mqtt_client, userdata, rc):
# This method is called when the mqtt_client disconnects
# from the broker.
print("Disconnected from MQTT Broker!")
def subscribe(mqtt_client, userdata, topic, granted_qos):
# This method is called when the mqtt_client subscribes to a new feed.
print(f"Subscribed to {topic} with QOS level {granted_qos}")
def unsubscribe(mqtt_client, userdata, topic, pid):
# This method is called when the mqtt_client unsubscribes from a feed.
print(f"Unsubscribed from {topic} with PID {pid}")
def publish(mqtt_client, userdata, topic, pid):
# This method is called when the mqtt_client publishes data to a feed.
print(f"Published to {topic} with PID {pid}")
def message(client, topic, message):
print(f"New message on topic {topic}: {message}")
if topic == mqtt_topic:
try:
parsed = json.loads(str(message))
main_display.set_temperature((float(parsed["atmpCompensated"]) * 9.0 / 5.0) + 32.0 )
main_display.set_humidity(float(parsed["rhumCompensated"]))
except ValueError:
main_display.set_temperature(float(888.8))
main_display.set_humidity(float(888))
if topic == mqtt_topic_aqi:
try:
main_display.set_aqi(float(message))
except ValueError:
main_display.set_aqi(float(888))
# Connect callback handlers to mqtt_client
mqtt_client.on_connect = connect
mqtt_client.on_disconnect = disconnect
mqtt_client.on_subscribe = subscribe
mqtt_client.on_unsubscribe = unsubscribe
mqtt_client.on_publish = publish
mqtt_client.on_message = message
print(f"Attempting to connect to {mqtt_client.broker}")
mqtt_client.connect()
print(f"Subscribing to {mqtt_topic}")
mqtt_client.subscribe(mqtt_topic)
print(f"Subscribing to {mqtt_topic_aqi}")
mqtt_client.subscribe(mqtt_topic_aqi)
print(f"Subscribing to {mqtt_topic_night}")
mqtt_client.subscribe(mqtt_topic_night)
reset_counter = 0
while True:
try:
main_display.update_display()
gc.collect()
mqtt_client.loop()
except Exception as e:
print("err: ", str(e))
reset_counter += 1
if reset_counter > 10:
microcontroller.reset()
time.sleep(5)