Skip to content

Commit c6b9dfc

Browse files
author
brentru
committed
adding example of MQTT Weather API
1 parent f00bdcd commit c6b9dfc

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

examples/mqtt/mqtt_weather.py

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""
2+
Example of using the Adafruit IO MQTT Client
3+
for subscribing to the Adafruit IO Weather Service
4+
Note: This feature is avaliable for IO Plus Subscribers ONLY
5+
6+
Author: Brent Rubell for Adafruit Industries
7+
"""
8+
9+
# Import standard python modules.
10+
import sys
11+
12+
# Import Adafruit IO MQTT client.
13+
from Adafruit_IO import MQTTClient
14+
15+
# Set to your Adafruit IO key.
16+
# Remember, your key is a secret,
17+
# so make sure not to publish it when you publish this code!
18+
ADAFRUIT_IO_KEY = 'KEY'
19+
20+
# Set to your Adafruit IO username.
21+
# (go to https://accounts.adafruit.com to find your username)
22+
ADAFRUIT_IO_USERNAME = 'USER'
23+
24+
# Set to ID of the forecast to subscribe to for updates.
25+
forecast_id = 1234
26+
27+
# Set to the ID of the feed to subscribe to for updates.
28+
"""
29+
Valid forecast types are:
30+
current
31+
forecast_minutes_5
32+
forecast_minutes_30
33+
forecast_hours_1
34+
forecast_hours_2
35+
forecast_hours_6
36+
forecast_hours_24
37+
forecast_days_1
38+
forecast_days_2
39+
forecast_days_5
40+
"""
41+
forecast_type = 'current'
42+
43+
# Define callback functions which will be called when certain events happen.
44+
def connected(client):
45+
# Connected function will be called when the client is connected to Adafruit IO.
46+
# This is a good place to subscribe to feed changes. The client parameter
47+
# passed to this function is the Adafruit IO MQTT client so you can make
48+
# calls against it easily.
49+
print('Connected to Adafruit IO! Listening for {0} changes...'.format(FEED_ID))
50+
# Subscribe to changes on a feed named DemoFeed.
51+
client.subscribe_weather(forecast_id, forecast_type)
52+
53+
def disconnected(client):
54+
# Disconnected function will be called when the client disconnects.
55+
print('Disconnected from Adafruit IO!')
56+
sys.exit(1)
57+
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+
64+
65+
# Create an MQTT client instance.
66+
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
67+
68+
# Setup the callback functions defined above.
69+
client.on_connect = connected
70+
client.on_disconnect = disconnected
71+
client.on_message = message
72+
73+
# Connect to the Adafruit IO server.
74+
client.connect()
75+
76+
# Start a message loop that blocks forever waiting for MQTT messages to be
77+
# received. Note there are other options for running the event loop like doing
78+
# so in a background thread--see the mqtt_client.py example to learn more.
79+
client.loop_blocking()

0 commit comments

Comments
 (0)