|
14 | 14 | import adafruit_pm25 |
15 | 15 | import adafruit_bme280 |
16 | 16 |
|
| 17 | + |
17 | 18 | ### WiFi ### |
18 | 19 |
|
19 | 20 | # Get wifi details and more from a secrets.py file |
@@ -59,21 +60,16 @@ def message(client, topic, message): |
59 | 60 | pass |
60 | 61 |
|
61 | 62 | 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 |
63 | 65 | """ |
| 66 | + global current_hour, current_minute |
64 | 67 | current_time = message.split("-")[2].split("T")[1] |
65 | 68 | 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 | + |
77 | 73 |
|
78 | 74 | ### Sensor Functions ### |
79 | 75 | def calculate_aqi(pm_sensor_reading): |
@@ -200,47 +196,82 @@ def read_bme280(is_celsius=False): |
200 | 196 | # TODO: Use secrets.py |
201 | 197 | location_metadata = "40.726190, -74.005334, -6" |
202 | 198 |
|
203 | | - |
204 | 199 | # Call on_new_time to update the time from Adafruit IO |
205 | 200 | io._client.add_topic_callback("time/ISO-8601", on_new_time) |
206 | 201 | # Subscribe to the Adafruit IO UTC time service |
207 | 202 | io.subscribe_to_time("ISO-8601") |
208 | 203 |
|
209 | | -prv_minute = 0 |
| 204 | +initial_time = time.monotonic() |
| 205 | + |
| 206 | +current_hour = 0 |
| 207 | +current_minute = 0 |
210 | 208 | prv_hour = 0 |
| 209 | +prv_minute = 0 |
| 210 | +time_elapsed = 0 |
| 211 | + |
| 212 | +average_aqi = 0 |
211 | 213 |
|
212 | | -initial_time = time.monotonic() |
213 | 214 | while True: |
214 | 215 | try: |
215 | 216 | # Keep device connected to io.adafruit.com |
216 | 217 | # and process any incoming data. |
217 | 218 | 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 |
241 | 219 | except (ValueError, RuntimeError, AdafruitIO_MQTTError) as e: |
242 | 220 | print("Failed to get data, retrying\n", e) |
243 | 221 | wifi.reset() |
| 222 | + wifi.connect() |
244 | 223 | io.reconnect() |
245 | 224 | 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