Skip to content

Commit 38c439b

Browse files
author
brentru
committed
remove subscription call, add on_message required for io.loop() iterations
1 parent 1bf64e6 commit 38c439b

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

Adafruit_IO_Air_Quality/code.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ def connected(client):
5050
# calls against it easily.
5151
print("Connected to Adafruit IO!")
5252

53-
# Subscribe to Group
54-
io.subscribe(group_key=group_name)
55-
5653
# pylint: disable=unused-argument
5754
def disconnected(client):
5855
# Disconnected function will be called when the client disconnects.
5956
print("Disconnected from Adafruit IO!")
6057

58+
def message(client, topic, message):
59+
pass
60+
6161
### Sensor Functions ###
6262
def calculate_aqi(pm_sensor_reading):
6363
"""Returns a calculated air quality index (AQI)
@@ -137,6 +137,7 @@ def read_bme280(is_celsius=False):
137137
temperature = temperature * 1.8 + 32
138138
return temperature, humidity
139139

140+
140141
### CODE ###
141142

142143
# Connect to WiFi
@@ -152,13 +153,13 @@ def read_bme280(is_celsius=False):
152153
broker="io.adafruit.com", username=secrets["aio_user"], password=secrets["aio_key"],
153154
)
154155

155-
156156
# Initialize an Adafruit IO MQTT Client
157157
io = IO_MQTT(mqtt_client)
158158

159159
# Connect the callback methods defined above to Adafruit IO
160160
io.on_connect = connected
161161
io.on_disconnect = disconnected
162+
io.on_message = message
162163

163164
# Connect to Adafruit IO
164165
print("Connecting to Adafruit IO...")
@@ -189,13 +190,25 @@ def read_bme280(is_celsius=False):
189190
aqi_reading = sample_aq_sensor()
190191
aqi, aqi_category = calculate_aqi(aqi_reading)
191192
print("AQI: %d"%aqi)
193+
192194
print("category: %s"%aqi_category)
195+
193196
# temp and humidity
194197
temperature, humidity = read_bme280()
195198
print("Temperature: %0.1f F" % temperature)
196199
print("Humidity: %0.1f %%" % humidity)
200+
201+
# Publish to IO
202+
print("Publishing to Adafruit IO...")
203+
# TODO: sleep a bit after these calls
204+
io.publish(feed_aqi, aqi)
205+
io.publish(feed_aqi_category, aqi_category)
206+
io.publish(feed_humid, humidity)
207+
io.publish(feed_temp, temperature)
208+
print("Published!")
197209
except (ValueError, RuntimeError) as e:
198210
print("Failed to get data, retrying\n", e)
199211
wifi.reset()
200212
io.reconnect()
201213
continue
214+
time.sleep(10)

0 commit comments

Comments
 (0)