@@ -128,42 +128,35 @@ def read_bme280(is_celsius=False):
128
128
feed_temperature = io .get_feed ("air-quality-sensor.temperature" )
129
129
130
130
131
- # Set up location metadata
132
- # TODO: Use secrets.py instead!
133
- location_metadata = "41.823990, -71.412834, 19"
131
+ # Set up location metadata from secrets.py file
132
+ location_metadata = (secrets ['latitude' ], secrets ['longitude' ], secrets ['elevation' ])
133
+
134
+ io .send_data (feed_aqi ["key" ], 'test' , location_metadata )
134
135
135
136
elapsed_minutes = 0
136
137
prv_mins = 0
137
138
aqi_readings = 0.0
138
- fetch_time_attempts = 0
139
-
140
139
141
140
while True :
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 )
149
- print ("Failed to get data, retrying\n " , e )
150
- wifi .reset ()
151
- fetch_time_attempts += 1
152
- continue
153
- else :
154
- print ("attempt " , fetch_time_attempts )
155
- print ("failed to fetch time, resetting wifi" )
141
+ try :
142
+ print ("Fetching time..." )
143
+ cur_time = io .receive_time ()
144
+ print ("Time fetched OK!" )
145
+ # Hourly reset
146
+ if cur_time .tm_min == 0 :
147
+ prv_mins = 0
148
+ except (ValueError , RuntimeError ) as e :
149
+ print ("Failed to fetch time, retrying\n " , e )
156
150
wifi .reset ()
157
- fetch_time_attempts = 0
151
+ wifi . connect ()
158
152
continue
159
153
160
- if cur_time [ 4 ] > prv_mins :
154
+ if cur_time . tm_min >= prv_mins :
161
155
print ("%d min elapsed.." % elapsed_minutes )
162
- prv_mins = cur_time [ 4 ]
156
+ prv_mins = cur_time . tm_min
163
157
elapsed_minutes += 1
164
- fetch_time_attempts += 1
165
158
166
- if elapsed_minutes >= 2 :
159
+ if elapsed_minutes >= 1 :
167
160
print ("Sampling AQI..." )
168
161
aqi_reading = sample_aq_sensor ()
169
162
aqi , aqi_category = calculate_aqi (aqi_reading )
@@ -177,19 +170,18 @@ def read_bme280(is_celsius=False):
177
170
print ("Humidity: %0.1f %%" % humidity )
178
171
179
172
# Publish all values to Adafruit IO
180
- # TODO: This should be within a retry loop...
181
173
print ("Publishing to Adafruit IO..." )
182
174
try :
183
- io .send_data (feed_aqi ["key" ], str (aqi ))
175
+ io .send_data (feed_aqi ["key" ], str (aqi ), location_metadata )
184
176
io .send_data (feed_aqi_category ["key" ], aqi_category )
185
177
io .send_data (feed_temperature ["key" ], str (temperature ))
186
178
io .send_data (feed_humidity ["key" ], str (humidity ))
187
179
print ("Published!" )
188
180
except (ValueError , RuntimeError ) as e :
189
- print ("Failed to send data, retrying\n " , e )
181
+ print ("Failed to send data to IO , retrying\n " , e )
190
182
wifi .reset ()
183
+ wifi .connect ()
191
184
continue
192
185
# Reset timer
193
186
elapsed_minutes = 0
194
- fetch_time_attempts += 1
195
187
time .sleep (30 )
0 commit comments