@@ -50,14 +50,14 @@ def connected(client):
50
50
# calls against it easily.
51
51
print ("Connected to Adafruit IO!" )
52
52
53
- # Subscribe to Group
54
- io .subscribe (group_key = group_name )
55
-
56
53
# pylint: disable=unused-argument
57
54
def disconnected (client ):
58
55
# Disconnected function will be called when the client disconnects.
59
56
print ("Disconnected from Adafruit IO!" )
60
57
58
+ def message (client , topic , message ):
59
+ pass
60
+
61
61
### Sensor Functions ###
62
62
def calculate_aqi (pm_sensor_reading ):
63
63
"""Returns a calculated air quality index (AQI)
@@ -137,6 +137,7 @@ def read_bme280(is_celsius=False):
137
137
temperature = temperature * 1.8 + 32
138
138
return temperature , humidity
139
139
140
+
140
141
### CODE ###
141
142
142
143
# Connect to WiFi
@@ -152,13 +153,13 @@ def read_bme280(is_celsius=False):
152
153
broker = "io.adafruit.com" , username = secrets ["aio_user" ], password = secrets ["aio_key" ],
153
154
)
154
155
155
-
156
156
# Initialize an Adafruit IO MQTT Client
157
157
io = IO_MQTT (mqtt_client )
158
158
159
159
# Connect the callback methods defined above to Adafruit IO
160
160
io .on_connect = connected
161
161
io .on_disconnect = disconnected
162
+ io .on_message = message
162
163
163
164
# Connect to Adafruit IO
164
165
print ("Connecting to Adafruit IO..." )
@@ -189,13 +190,25 @@ def read_bme280(is_celsius=False):
189
190
aqi_reading = sample_aq_sensor ()
190
191
aqi , aqi_category = calculate_aqi (aqi_reading )
191
192
print ("AQI: %d" % aqi )
193
+
192
194
print ("category: %s" % aqi_category )
195
+
193
196
# temp and humidity
194
197
temperature , humidity = read_bme280 ()
195
198
print ("Temperature: %0.1f F" % temperature )
196
199
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!" )
197
209
except (ValueError , RuntimeError ) as e :
198
210
print ("Failed to get data, retrying\n " , e )
199
211
wifi .reset ()
200
212
io .reconnect ()
201
213
continue
214
+ time .sleep (10 )
0 commit comments