22import board
33import busio
44from digitalio import DigitalInOut , Direction , Pull
5- import adafruit_dotstar as dotstar
5+ import neopixel
66from adafruit_esp32spi import adafruit_esp32spi , adafruit_esp32spi_wifimanager
77from adafruit_io .adafruit_io import IO_HTTP , AdafruitIO_RequestError
88from simpleio import map_range
2727
2828spi = busio .SPI (board .SCK , board .MOSI , board .MISO )
2929esp = 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 )
3131wifi = 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
135135elapsed_minutes = 0
136136prv_mins = 0
137- aqi_readings = 0
137+ aqi_readings = 0.0
138+ fetch_time_attempts = 0
139+
138140
139141while 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