79 lines
2.5 KiB
Python
79 lines
2.5 KiB
Python
"""
|
|
Example of using the Adafruit IO MQTT Client
|
|
for subscribing to the Adafruit IO Weather Service
|
|
Note: This feature is avaliable for IO Plus Subscribers ONLY
|
|
|
|
Author: Brent Rubell for Adafruit Industries
|
|
"""
|
|
|
|
# Import standard python modules.
|
|
import sys
|
|
|
|
# Import Adafruit IO MQTT client.
|
|
from Adafruit_IO import MQTTClient
|
|
|
|
# Set to your Adafruit IO key.
|
|
# Remember, your key is a secret,
|
|
# so make sure not to publish it when you publish this code!
|
|
ADAFRUIT_IO_KEY = 'KEY'
|
|
|
|
# Set to your Adafruit IO username.
|
|
# (go to https://accounts.adafruit.com to find your username)
|
|
ADAFRUIT_IO_USERNAME = 'USER'
|
|
|
|
# Set to ID of the forecast to subscribe to for updates
|
|
forecast_id = 1234
|
|
|
|
# Set to the ID of the feed to subscribe to for updates.
|
|
"""
|
|
Valid forecast types are:
|
|
current
|
|
forecast_minutes_5
|
|
forecast_minutes_30
|
|
forecast_hours_1
|
|
forecast_hours_2
|
|
forecast_hours_6
|
|
forecast_hours_24
|
|
forecast_days_1
|
|
forecast_days_2
|
|
forecast_days_5
|
|
"""
|
|
forecast_type = 'current'
|
|
|
|
# Define callback functions which will be called when certain events happen.
|
|
def connected(client):
|
|
# Connected function will be called when the client is connected to Adafruit IO.
|
|
# This is a good place to subscribe to feed changes. The client parameter
|
|
# passed to this function is the Adafruit IO MQTT client so you can make
|
|
# calls against it easily.
|
|
print('Connected to Adafruit IO! Listening to forecast: {0}...'.format(forecast_id))
|
|
# Subscribe to changes on a feed named DemoFeed.
|
|
client.subscribe_weather(forecast_id, forecast_type)
|
|
|
|
def disconnected(client):
|
|
# Disconnected function will be called when the client disconnects.
|
|
print('Disconnected from Adafruit IO!')
|
|
sys.exit(1)
|
|
|
|
def message(client, feed_id, payload):
|
|
# Message function will be called when a subscribed feed has a new value.
|
|
# The feed_id parameter identifies the feed, and the payload parameter has
|
|
# the new value.
|
|
print('Weather Subscription {0} received new value: {1}'.format(forecast_id, payload))
|
|
|
|
|
|
# Create an MQTT client instance.
|
|
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
|
|
|
# Setup the callback functions defined above.
|
|
client.on_connect = connected
|
|
client.on_disconnect = disconnected
|
|
client.on_message = message
|
|
|
|
# Connect to the Adafruit IO server.
|
|
client.connect()
|
|
|
|
# Start a message loop that blocks forever waiting for MQTT messages to be
|
|
# received. Note there are other options for running the event loop like doing
|
|
# so in a background thread--see the mqtt_client.py example to learn more.
|
|
client.loop_blocking()
|