22
22
ADAFRUIT_IO_USERNAME = 'USER'
23
23
24
24
# Set to ID of the forecast to subscribe to for updates
25
- forecast_id = 1234
25
+ forecast_id = 2153
26
26
27
27
# Set to the ID of the feed to subscribe to for updates.
28
28
"""
38
38
forecast_days_2
39
39
forecast_days_5
40
40
"""
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'
42
47
43
48
# Define callback functions which will be called when certain events happen.
44
49
def connected (client ):
@@ -47,20 +52,37 @@ def connected(client):
47
52
# passed to this function is the Adafruit IO MQTT client so you can make
48
53
# calls against it easily.
49
54
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 )
52
63
53
64
def disconnected (client ):
54
65
# Disconnected function will be called when the client disconnects.
55
66
print ('Disconnected from Adafruit IO!' )
56
67
sys .exit (1 )
57
68
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 ))
64
86
65
87
# Create an MQTT client instance.
66
88
client = MQTTClient (ADAFRUIT_IO_USERNAME , ADAFRUIT_IO_KEY )
0 commit comments