Skip to content

Commit 65ee0c6

Browse files
author
brentru
committed
add code, WIP, hangs on minimqtt > 10min rcv?
1 parent 9a40b75 commit 65ee0c6

File tree

1 file changed

+70
-39
lines changed

1 file changed

+70
-39
lines changed

Adafruit_IO_Air_Quality/code.py

Lines changed: 70 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import adafruit_pm25
1515
import adafruit_bme280
1616

17+
1718
### WiFi ###
1819

1920
# Get wifi details and more from a secrets.py file
@@ -59,21 +60,16 @@ def message(client, topic, message):
5960
pass
6061

6162
def on_new_time(client, topic, message):
62-
"""Obtains new time from Adafruit IO time service.
63+
"""Obtains new time from Adafruit IO time service
64+
and parses the current_hour/current_minute
6365
"""
66+
global current_hour, current_minute
6467
current_time = message.split("-")[2].split("T")[1]
6568
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-
69+
current_hour = int(current_time.split(":")[0])
70+
current_minute = int(current_time.split(":")[1])
71+
72+
7773

7874
### Sensor Functions ###
7975
def calculate_aqi(pm_sensor_reading):
@@ -200,47 +196,82 @@ def read_bme280(is_celsius=False):
200196
# TODO: Use secrets.py
201197
location_metadata = "40.726190, -74.005334, -6"
202198

203-
204199
# Call on_new_time to update the time from Adafruit IO
205200
io._client.add_topic_callback("time/ISO-8601", on_new_time)
206201
# Subscribe to the Adafruit IO UTC time service
207202
io.subscribe_to_time("ISO-8601")
208203

209-
prv_minute = 0
204+
initial_time = time.monotonic()
205+
206+
current_hour = 0
207+
current_minute = 0
210208
prv_hour = 0
209+
prv_minute = 0
210+
time_elapsed = 0
211+
212+
average_aqi = 0
211213

212-
initial_time = time.monotonic()
213214
while True:
214215
try:
215216
# Keep device connected to io.adafruit.com
216217
# and process any incoming data.
217218
io.loop()
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
241219
except (ValueError, RuntimeError, AdafruitIO_MQTTError) as e:
242220
print("Failed to get data, retrying\n", e)
243221
wifi.reset()
222+
wifi.connect()
244223
io.reconnect()
245224
continue
246-
time.sleep(0.5)
225+
226+
# Check current time against io.adafruit time service
227+
if (current_hour - prv_hour >= 1):
228+
print("new hour!")
229+
# Reset prv_hour
230+
prv_hour = current_hour
231+
io.publish(feed_temp, current_minute)
232+
time_elapsed = True
233+
elif (current_minute - prv_minute >= 1):
234+
print("new 1m increment!")
235+
print(current_minute)
236+
# Reset prv_minute
237+
prv_minute = int(current_minute)
238+
io.publish(feed_temp, current_minute)
239+
time_elapsed = True
240+
elif current_minute % 10 == 0:
241+
print("new 10m increment!")
242+
# Reset prv_minute
243+
prv_minute = int(current_minute)
244+
io.publish(feed_temp, current_minute)
245+
time_elapsed = True
246+
else:
247+
# no difference in current time
248+
continue
249+
250+
if time_elapsed:
251+
aqi_reading = sample_aq_sensor()
252+
aqi, aqi_category = calculate_aqi(aqi_reading)
253+
# Average AQI readings over amount of readings
254+
if current_minute > 0:
255+
average_aqi += aqi
256+
average_aqi /= current_minute
257+
258+
print("AQI: %d"%aqi)
259+
print("Category: %s"%aqi_category)
260+
261+
# temp and humidity
262+
temperature, humidity = read_bme280()
263+
print("Temperature: %0.1f F" % temperature)
264+
print("Humidity: %0.1f %%" % humidity)
265+
266+
# Publish to IO
267+
print("Publishing to Adafruit IO...")
268+
# TODO: These need to be within a try/except block
269+
io.publish(feed_aqi, int(aqi), location_metadata)
270+
io.publish(feed_aqi_category, aqi_category)
271+
io.publish(feed_humid, humidity)
272+
io.publish(feed_temp, temperature)
273+
print("Published!")
274+
# Reset time_elapsed
275+
time_elapsed = False
276+
277+
time.sleep(60)

0 commit comments

Comments
 (0)