|
8 | 8 | import adafruit_esp32spi.adafruit_esp32spi_socket as socket
|
9 | 9 | import adafruit_minimqtt.adafruit_minimqtt as MQTT
|
10 | 10 | from adafruit_io.adafruit_io import IO_MQTT
|
11 |
| - |
| 11 | +from adafruit_io.adafruit_io_errors import AdafruitIO_MQTTError |
12 | 12 | from simpleio import map_range
|
13 | 13 |
|
14 | 14 | import adafruit_pm25
|
@@ -58,6 +58,23 @@ def disconnected(client):
|
58 | 58 | def message(client, topic, message):
|
59 | 59 | pass
|
60 | 60 |
|
| 61 | +def on_new_time(client, topic, message): |
| 62 | + """Obtains new time from Adafruit IO time service. |
| 63 | + """ |
| 64 | + current_time = message.split("-")[2].split("T")[1] |
| 65 | + current_time = current_time.split(".")[0] |
| 66 | + current_hour = current_time.split(":")[0] |
| 67 | + current_minute = current_time.split(":")[1] |
| 68 | + # Add new PM.25 reading to hourly average |
| 69 | + if (current_hour - prv_hour >= 1): |
| 70 | + print("new hour!") |
| 71 | + prv_hour = current_hour |
| 72 | + if (current_minute - prv_minute >= 10): |
| 73 | + print("new 10m increment!") |
| 74 | + prv_minute = current_minute |
| 75 | + |
| 76 | + |
| 77 | + |
61 | 78 | ### Sensor Functions ###
|
62 | 79 | def calculate_aqi(pm_sensor_reading):
|
63 | 80 | """Returns a calculated air quality index (AQI)
|
@@ -179,36 +196,51 @@ def read_bme280(is_celsius=False):
|
179 | 196 | # Air quality index category
|
180 | 197 | feed_aqi_category = group_air_quality + ".category"
|
181 | 198 |
|
| 199 | +# Set up location metadata |
| 200 | +# TODO: Use secrets.py |
| 201 | +location_metadata = "40.726190, -74.005334, -6" |
182 | 202 |
|
| 203 | + |
| 204 | +# Call on_new_time to update the time from Adafruit IO |
| 205 | +io._client.add_topic_callback("time/ISO-8601", on_new_time) |
| 206 | +# Subscribe to the Adafruit IO UTC time service |
| 207 | +io.subscribe_to_time("ISO-8601") |
| 208 | + |
| 209 | +prv_minute = 0 |
| 210 | +prv_hour = 0 |
| 211 | + |
| 212 | +initial_time = time.monotonic() |
183 | 213 | while True:
|
184 | 214 | try:
|
185 | 215 | # Keep device connected to io.adafruit.com
|
186 | 216 | # and process any incoming data.
|
187 | 217 | io.loop()
|
188 |
| - # TODO: read every 10min |
189 |
| - # air quality |
190 |
| - aqi_reading = sample_aq_sensor() |
191 |
| - aqi, aqi_category = calculate_aqi(aqi_reading) |
192 |
| - print("AQI: %d"%aqi) |
193 |
| - |
194 |
| - print("category: %s"%aqi_category) |
195 |
| - |
196 |
| - # temp and humidity |
197 |
| - temperature, humidity = read_bme280() |
198 |
| - print("Temperature: %0.1f F" % temperature) |
199 |
| - print("Humidity: %0.1f %%" % humidity) |
200 |
| - |
201 |
| - # Publish to IO |
202 |
| - print("Publishing to Adafruit IO...") |
203 |
| - # TODO: sleep a bit after these calls |
204 |
| - io.publish(feed_aqi, aqi) |
205 |
| - io.publish(feed_aqi_category, aqi_category) |
206 |
| - io.publish(feed_humid, humidity) |
207 |
| - io.publish(feed_temp, temperature) |
208 |
| - print("Published!") |
209 |
| - except (ValueError, RuntimeError) as e: |
| 218 | + now = time.monotonic() |
| 219 | + print(now-initial_time) |
| 220 | + while (now - initial_time >= 10): |
| 221 | + # air quality |
| 222 | + aqi_reading = sample_aq_sensor() |
| 223 | + aqi, aqi_category = calculate_aqi(aqi_reading) |
| 224 | + print("AQI: %d"%aqi) |
| 225 | + print("category: %s"%aqi_category) |
| 226 | + |
| 227 | + # temp and humidity |
| 228 | + temperature, humidity = read_bme280() |
| 229 | + print("Temperature: %0.1f F" % temperature) |
| 230 | + print("Humidity: %0.1f %%" % humidity) |
| 231 | + |
| 232 | + # Publish to IO |
| 233 | + print("Publishing to Adafruit IO...") |
| 234 | + io.publish(feed_aqi, int(aqi), location_metadata) |
| 235 | + io.publish(feed_aqi_category, aqi_category) |
| 236 | + io.publish(feed_humid, humidity) |
| 237 | + io.publish(feed_temp, temperature) |
| 238 | + print("Published!") |
| 239 | + |
| 240 | + initial_time = now |
| 241 | + except (ValueError, RuntimeError, AdafruitIO_MQTTError) as e: |
210 | 242 | print("Failed to get data, retrying\n", e)
|
211 | 243 | wifi.reset()
|
212 | 244 | io.reconnect()
|
213 | 245 | continue
|
214 |
| - time.sleep(10) |
| 246 | + time.sleep(0.5) |
0 commit comments