Skip to content

Commit 64c42de

Browse files
author
brentru
committedJan 8, 2019
update example to show 1, 2, 5 day forecasts
1 parent 7ada763 commit 64c42de

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed
 

Diff for: ‎Adafruit_IO/mqtt_client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def _mqtt_message(self, client, userdata, msg):
117117
elif self.on_message is not None and parsed_topic[1] == 'groups':
118118
topic = parsed_topic[3]
119119
payload = msg.payload.decode('utf-8')
120-
else:
120+
else: # default topic
121121
topic = parsed_topic[1]
122122
payload = '' if msg.payload is None else msg.payload.decode('utf-8')
123123
self.on_message(self, topic, payload)

Diff for: ‎examples/mqtt/mqtt_weather.py

+32-10
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
ADAFRUIT_IO_USERNAME = 'USER'
2323

2424
# Set to ID of the forecast to subscribe to for updates
25-
forecast_id = 1234
25+
forecast_id = 2153
2626

2727
# Set to the ID of the feed to subscribe to for updates.
2828
"""
@@ -38,7 +38,12 @@
3838
forecast_days_2
3939
forecast_days_5
4040
"""
41-
forecast_type = 'current'
41+
# Subscribe to the current forecast
42+
forecast_today = 'current'
43+
# Subscribe to tomorrow's forecast
44+
forecast_tomorrow = 'forecast_days_2'
45+
# Subscribe to forecast in 5 days
46+
forecast_in_5_days = 'forecast_days_5'
4247

4348
# Define callback functions which will be called when certain events happen.
4449
def connected(client):
@@ -47,20 +52,37 @@ def connected(client):
4752
# passed to this function is the Adafruit IO MQTT client so you can make
4853
# calls against it easily.
4954
print('Connected to Adafruit IO! Listening to forecast: {0}...'.format(forecast_id))
50-
# Subscribe to changes on a feed named DemoFeed.
51-
client.subscribe_weather(forecast_id, forecast_type)
55+
# Subscribe to changes on the current forecast.
56+
client.subscribe_weather(forecast_id, forecast_today)
57+
58+
# Subscribe to changes on tomorrow's forecast.
59+
client.subscribe_weather(forecast_id, forecast_tomorrow)
60+
61+
# Subscribe to changes on forecast in 5 days.
62+
client.subscribe_weather(forecast_id, forecast_in_5_days)
5263

5364
def disconnected(client):
5465
# Disconnected function will be called when the client disconnects.
5566
print('Disconnected from Adafruit IO!')
5667
sys.exit(1)
5768

58-
def message(client, feed_id, payload):
59-
# Message function will be called when a subscribed feed has a new value.
60-
# The feed_id parameter identifies the feed, and the payload parameter has
61-
# the new value.
62-
print('Weather Subscription {0} received new value: {1}'.format(forecast_id, payload))
63-
69+
def message(client, topic, payload):
70+
"""Message function will be called when any subscribed forecast has an update.
71+
Weather data is updated at most once every 20 minutes.
72+
"""
73+
# Raw data from feed
74+
print(topic)
75+
print(payload)
76+
# parse based on topic
77+
if topic is 'current':
78+
# Print out today's forecast
79+
print('Current Forecast: {0}'.format(payload))
80+
elif topic is 'forecast_days_2':
81+
# print out tomorrow's forecast
82+
print('Forecast Tomorrow: {0}'.format(payload))
83+
elif topic is 'forecast_days_5':
84+
# print out forecast in 5 days
85+
print('Forecast in 5 Days: {0}'.format(payload))
6486

6587
# Create an MQTT client instance.
6688
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

0 commit comments

Comments
 (0)
Please sign in to comment.