2222ADAFRUIT_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"""
3838forecast_days_2
3939forecast_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.
4449def 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
5364def 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.
6688client = MQTTClient (ADAFRUIT_IO_USERNAME , ADAFRUIT_IO_KEY )
0 commit comments