2
2
import board
3
3
import busio
4
4
from digitalio import DigitalInOut , Direction , Pull
5
- import adafruit_dotstar as dotstar
5
+ import neopixel
6
6
from adafruit_esp32spi import adafruit_esp32spi , adafruit_esp32spi_wifimanager
7
7
from adafruit_io .adafruit_io import IO_HTTP , AdafruitIO_RequestError
8
8
from simpleio import map_range
27
27
28
28
spi = busio .SPI (board .SCK , board .MOSI , board .MISO )
29
29
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 )
31
31
wifi = adafruit_esp32spi_wifimanager .ESPSPI_WiFiManager (esp , secrets , status_light )
32
32
33
33
# Connect to a PM2.5 sensor over UART
@@ -91,7 +91,7 @@ def sample_aq_sensor():
91
91
while (time .monotonic () - time_start <= 2.3 ):
92
92
try :
93
93
aqdata = pm25 .read ()
94
- aq_samples .append (aqdata ["particles 25um " ])
94
+ aq_samples .append (aqdata ["pm25 env " ])
95
95
except RuntimeError :
96
96
print ("Unable to read from sensor, retrying..." )
97
97
continue
@@ -130,34 +130,43 @@ def read_bme280(is_celsius=False):
130
130
131
131
# Set up location metadata
132
132
# TODO: Use secrets.py instead!
133
- location_metadata = "40.726190 , -74.005334, -6 "
133
+ location_metadata = "41.823990 , -71.412834, 19 "
134
134
135
135
elapsed_minutes = 0
136
136
prv_mins = 0
137
- aqi_readings = 0
137
+ aqi_readings = 0.0
138
+ fetch_time_attempts = 0
139
+
138
140
139
141
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 )
145
149
print ("Failed to get data, retrying\n " , e )
146
150
wifi .reset ()
151
+ fetch_time_attempts += 1
147
152
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
148
159
149
160
if cur_time [4 ] > prv_mins :
150
161
print ("%d min elapsed.." % elapsed_minutes )
151
- # Sample the AQI every minute
152
- aqi_readings += sample_aq_sensor ()
153
162
prv_mins = cur_time [4 ]
154
163
elapsed_minutes += 1
164
+ fetch_time_attempts += 1
155
165
156
- if elapsed_minutes >= 10 :
166
+ if elapsed_minutes >= 2 :
157
167
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 )
161
170
print ("AQI: %d" % aqi )
162
171
print ("Category: %s" % aqi_category )
163
172
@@ -166,20 +175,21 @@ def read_bme280(is_celsius=False):
166
175
temperature , humidity = read_bme280 ()
167
176
print ("Temperature: %0.1f F" % temperature )
168
177
print ("Humidity: %0.1f %%" % humidity )
169
- # TODO: Publish all values to Adafruit IO
170
178
179
+ # Publish all values to Adafruit IO
180
+ # TODO: This should be within a retry loop...
181
+ print ("Publishing to Adafruit IO..." )
171
182
try :
172
183
io .send_data (feed_aqi ["key" ], str (aqi ))
173
184
io .send_data (feed_aqi_category ["key" ], aqi_category )
174
185
io .send_data (feed_temperature ["key" ], str (temperature ))
175
186
io .send_data (feed_humidity ["key" ], str (humidity ))
187
+ print ("Published!" )
176
188
except (ValueError , RuntimeError ) as e :
177
- print ("Failed to get data, retrying\n " , e )
189
+ print ("Failed to send data, retrying\n " , e )
178
190
wifi .reset ()
179
191
continue
180
-
181
-
182
192
# Reset timer
183
193
elapsed_minutes = 0
194
+ fetch_time_attempts += 1
184
195
time .sleep (30 )
185
-
0 commit comments