Skip to content

Commit 096f3f0

Browse files
author
brentru
committed
update for Feather M4 instead of itsy!
1 parent fa10867 commit 096f3f0

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

Adafruit_IO_Air_Quality/code.py

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import board
33
import busio
44
from digitalio import DigitalInOut, Direction, Pull
5-
import adafruit_dotstar as dotstar
5+
import neopixel
66
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
77
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
88
from simpleio import map_range
@@ -27,7 +27,7 @@
2727

2828
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
2929
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
30-
status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
30+
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
3131
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
3232

3333
# Connect to a PM2.5 sensor over UART
@@ -91,7 +91,7 @@ def sample_aq_sensor():
9191
while (time.monotonic() - time_start <= 2.3):
9292
try:
9393
aqdata = pm25.read()
94-
aq_samples.append(aqdata["particles 25um"])
94+
aq_samples.append(aqdata["pm25 env"])
9595
except RuntimeError:
9696
print("Unable to read from sensor, retrying...")
9797
continue
@@ -130,34 +130,43 @@ def read_bme280(is_celsius=False):
130130

131131
# Set up location metadata
132132
# TODO: Use secrets.py instead!
133-
location_metadata = "40.726190, -74.005334, -6"
133+
location_metadata = "41.823990, -71.412834, 19"
134134

135135
elapsed_minutes = 0
136136
prv_mins = 0
137-
aqi_readings = 0
137+
aqi_readings = 0.0
138+
fetch_time_attempts = 0
139+
138140

139141
while True:
140-
print("Obtaining time")
141-
try:
142-
cur_time = io.receive_time()
143-
# print(cur_time)
144-
except (ValueError, RuntimeError) as e:
142+
if fetch_time_attempts <= 3:
143+
try:
144+
print("Fetching time...")
145+
print("attempt ", fetch_time_attempts)
146+
cur_time = io.receive_time()
147+
except (ValueError, RuntimeError) as e:
148+
print("attempt ", fetch_time_attempts)
145149
print("Failed to get data, retrying\n", e)
146150
wifi.reset()
151+
fetch_time_attempts+=1
147152
continue
153+
else:
154+
print("attempt ", fetch_time_attempts)
155+
print("failed to fetch time, resetting wifi")
156+
wifi.reset()
157+
fetch_time_attempts = 0
158+
continue
148159

149160
if cur_time[4] > prv_mins:
150161
print("%d min elapsed.."%elapsed_minutes)
151-
# Sample the AQI every minute
152-
aqi_readings += sample_aq_sensor()
153162
prv_mins = cur_time[4]
154163
elapsed_minutes += 1
164+
fetch_time_attempts+=1
155165

156-
if elapsed_minutes >= 10:
166+
if elapsed_minutes >= 2:
157167
print("Sampling AQI...")
158-
# Average AQI over 10 individual readings
159-
aqi_readings /= 10
160-
aqi, aqi_category = calculate_aqi(aqi_readings)
168+
aqi_reading = sample_aq_sensor()
169+
aqi, aqi_category = calculate_aqi(aqi_reading)
161170
print("AQI: %d"%aqi)
162171
print("Category: %s"%aqi_category)
163172

@@ -166,20 +175,21 @@ def read_bme280(is_celsius=False):
166175
temperature, humidity = read_bme280()
167176
print("Temperature: %0.1f F" % temperature)
168177
print("Humidity: %0.1f %%" % humidity)
169-
# TODO: Publish all values to Adafruit IO
170178

179+
# Publish all values to Adafruit IO
180+
# TODO: This should be within a retry loop...
181+
print("Publishing to Adafruit IO...")
171182
try:
172183
io.send_data(feed_aqi["key"], str(aqi))
173184
io.send_data(feed_aqi_category["key"], aqi_category)
174185
io.send_data(feed_temperature["key"], str(temperature))
175186
io.send_data(feed_humidity["key"], str(humidity))
187+
print("Published!")
176188
except (ValueError, RuntimeError) as e:
177-
print("Failed to get data, retrying\n", e)
189+
print("Failed to send data, retrying\n", e)
178190
wifi.reset()
179191
continue
180-
181-
182192
# Reset timer
183193
elapsed_minutes = 0
194+
fetch_time_attempts += 1
184195
time.sleep(30)
185-

0 commit comments

Comments
 (0)