Skip to content

Commit 9a40b75

Browse files
author
brentru
committed
handle time service, mqtterrors
1 parent 38c439b commit 9a40b75

File tree

1 file changed

+56
-24
lines changed

1 file changed

+56
-24
lines changed

Adafruit_IO_Air_Quality/code.py

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
99
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1010
from adafruit_io.adafruit_io import IO_MQTT
11-
11+
from adafruit_io.adafruit_io_errors import AdafruitIO_MQTTError
1212
from simpleio import map_range
1313

1414
import adafruit_pm25
@@ -58,6 +58,23 @@ def disconnected(client):
5858
def message(client, topic, message):
5959
pass
6060

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+
6178
### Sensor Functions ###
6279
def calculate_aqi(pm_sensor_reading):
6380
"""Returns a calculated air quality index (AQI)
@@ -179,36 +196,51 @@ def read_bme280(is_celsius=False):
179196
# Air quality index category
180197
feed_aqi_category = group_air_quality + ".category"
181198

199+
# Set up location metadata
200+
# TODO: Use secrets.py
201+
location_metadata = "40.726190, -74.005334, -6"
182202

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()
183213
while True:
184214
try:
185215
# Keep device connected to io.adafruit.com
186216
# and process any incoming data.
187217
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:
210242
print("Failed to get data, retrying\n", e)
211243
wifi.reset()
212244
io.reconnect()
213245
continue
214-
time.sleep(10)
246+
time.sleep(0.5)

0 commit comments

Comments
 (0)