Skip to content

Commit ecb3fae

Browse files
author
brentru
committed
add location metadata
1 parent 096f3f0 commit ecb3fae

File tree

1 file changed

+20
-28
lines changed

1 file changed

+20
-28
lines changed

Adafruit_IO_Air_Quality/code.py

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -128,42 +128,35 @@ def read_bme280(is_celsius=False):
128128
feed_temperature = io.get_feed("air-quality-sensor.temperature")
129129

130130

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)
134135

135136
elapsed_minutes = 0
136137
prv_mins = 0
137138
aqi_readings = 0.0
138-
fetch_time_attempts = 0
139-
140139

141140
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)
156150
wifi.reset()
157-
fetch_time_attempts = 0
151+
wifi.connect()
158152
continue
159153

160-
if cur_time[4] > prv_mins:
154+
if cur_time.tm_min >= prv_mins:
161155
print("%d min elapsed.."%elapsed_minutes)
162-
prv_mins = cur_time[4]
156+
prv_mins = cur_time.tm_min
163157
elapsed_minutes += 1
164-
fetch_time_attempts+=1
165158

166-
if elapsed_minutes >= 2:
159+
if elapsed_minutes >= 1:
167160
print("Sampling AQI...")
168161
aqi_reading = sample_aq_sensor()
169162
aqi, aqi_category = calculate_aqi(aqi_reading)
@@ -177,19 +170,18 @@ def read_bme280(is_celsius=False):
177170
print("Humidity: %0.1f %%" % humidity)
178171

179172
# Publish all values to Adafruit IO
180-
# TODO: This should be within a retry loop...
181173
print("Publishing to Adafruit IO...")
182174
try:
183-
io.send_data(feed_aqi["key"], str(aqi))
175+
io.send_data(feed_aqi["key"], str(aqi), location_metadata)
184176
io.send_data(feed_aqi_category["key"], aqi_category)
185177
io.send_data(feed_temperature["key"], str(temperature))
186178
io.send_data(feed_humidity["key"], str(humidity))
187179
print("Published!")
188180
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)
190182
wifi.reset()
183+
wifi.connect()
191184
continue
192185
# Reset timer
193186
elapsed_minutes = 0
194-
fetch_time_attempts += 1
195187
time.sleep(30)

0 commit comments

Comments
 (0)