|
| 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 | +API Documentation: https://io.adafruit.com/services/weather |
| 7 | +
|
| 8 | +Author: Brent Rubell for Adafruit Industries |
| 9 | +""" |
| 10 | + |
| 11 | +# Import standard python modules. |
| 12 | +import sys |
| 13 | +import json |
| 14 | + |
| 15 | +# Import Adafruit IO MQTT client. |
| 16 | +from Adafruit_IO import MQTTClient |
| 17 | + |
| 18 | +# Set to your Adafruit IO key. |
| 19 | +# Remember, your key is a secret, |
| 20 | +# so make sure not to publish it when you publish this code! |
| 21 | +ADAFRUIT_IO_KEY = 'KEY' |
| 22 | + |
| 23 | +# Set to your Adafruit IO username. |
| 24 | +# (go to https://accounts.adafruit.com to find your username) |
| 25 | +ADAFRUIT_IO_USERNAME = 'USER' |
| 26 | + |
| 27 | +# Set to ID of the forecast to subscribe to for updates |
| 28 | +forecast_id = 2153 |
| 29 | + |
| 30 | +# Set to the ID of the feed to subscribe to for updates. |
| 31 | +""" |
| 32 | +Valid forecast types are: |
| 33 | +current |
| 34 | +forecast_minutes_5 |
| 35 | +forecast_minutes_30 |
| 36 | +forecast_hours_1 |
| 37 | +forecast_hours_2 |
| 38 | +forecast_hours_6 |
| 39 | +forecast_hours_24 |
| 40 | +forecast_days_1 |
| 41 | +forecast_days_2 |
| 42 | +forecast_days_5 |
| 43 | +""" |
| 44 | +# Subscribe to the current forecast |
| 45 | +forecast_today = 'current' |
| 46 | +# Subscribe to tomorrow's forecast |
| 47 | +forecast_two_days = 'forecast_days_2' |
| 48 | +# Subscribe to forecast in 5 days |
| 49 | +forecast_in_5_days = 'forecast_days_5' |
| 50 | + |
| 51 | +# Define callback functions which will be called when certain events happen. |
| 52 | +# pylint: disable=redefined-outer-name |
| 53 | +def connected(client): |
| 54 | + # Connected function will be called when the client is connected to Adafruit IO. |
| 55 | + # This is a good place to subscribe to feed changes. The client parameter |
| 56 | + # passed to this function is the Adafruit IO MQTT client so you can make |
| 57 | + # calls against it easily. |
| 58 | + print('Connected to Adafruit IO! Listening to forecast: {0}...'.format(forecast_id)) |
| 59 | + # Subscribe to changes on the current forecast. |
| 60 | + client.subscribe_weather(forecast_id, forecast_today) |
| 61 | + |
| 62 | + # Subscribe to changes on tomorrow's forecast. |
| 63 | + client.subscribe_weather(forecast_id, forecast_two_days) |
| 64 | + |
| 65 | + # Subscribe to changes on forecast in 5 days. |
| 66 | + client.subscribe_weather(forecast_id, forecast_in_5_days) |
| 67 | + |
| 68 | +# pylint: disable=unused-argument |
| 69 | +def disconnected(client): |
| 70 | + # Disconnected function will be called when the client disconnects. |
| 71 | + print('Disconnected from Adafruit IO!') |
| 72 | + sys.exit(1) |
| 73 | + |
| 74 | +# pylint: disable=unused-argument |
| 75 | +def message(client, topic, payload): |
| 76 | + """Message function will be called when any subscribed forecast has an update. |
| 77 | + Weather data is updated at most once every 20 minutes. |
| 78 | + """ |
| 79 | + # forecast based on mqtt topic |
| 80 | + if topic == 'current': |
| 81 | + # Print out today's forecast |
| 82 | + today_forecast = payload |
| 83 | + print('\nCurrent Forecast') |
| 84 | + parseForecast(today_forecast) |
| 85 | + elif topic == 'forecast_days_2': |
| 86 | + # Print out tomorrow's forecast |
| 87 | + two_day_forecast = payload |
| 88 | + print('\nWeather in Two Days') |
| 89 | + parseForecast(two_day_forecast) |
| 90 | + elif topic == 'forecast_days_5': |
| 91 | + # Print out forecast in 5 days |
| 92 | + five_day_forecast = payload |
| 93 | + print('\nWeather in 5 Days') |
| 94 | + parseForecast(five_day_forecast) |
| 95 | + |
| 96 | +def parseForecast(forecast_data): |
| 97 | + """Parses and prints incoming forecast data |
| 98 | + """ |
| 99 | + # incoming data is a utf-8 string, encode it as a json object |
| 100 | + forecast = json.loads(forecast_data) |
| 101 | + # Print out the forecast |
| 102 | + try: |
| 103 | + print('It is {0} and {1}F.'.format(forecast['summary'], forecast['temperature'])) |
| 104 | + except KeyError: |
| 105 | + # future weather forecasts return a high and low temperature, instead of 'temperature' |
| 106 | + print('It will be {0} with a high of {1}F and a low of {2}F.'.format( |
| 107 | + forecast['summary'], forecast['temperatureLow'], forecast['temperatureHigh'])) |
| 108 | + print('with humidity of {0}%, wind speed of {1}mph, and {2}% chance of precipitation.'.format( |
| 109 | + forecast['humidity'], forecast['windSpeed'], forecast['precipProbability'])) |
| 110 | + |
| 111 | +# Create an MQTT client instance. |
| 112 | +client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) |
| 113 | + |
| 114 | +# Setup the callback functions defined above. |
| 115 | +client.on_connect = connected |
| 116 | +client.on_disconnect = disconnected |
| 117 | +client.on_message = message |
| 118 | + |
| 119 | +# Connect to the Adafruit IO server. |
| 120 | +client.connect() |
| 121 | + |
| 122 | +# Start a message loop that blocks forever waiting for MQTT messages to be |
| 123 | +# received. Note there are other options for running the event loop like doing |
| 124 | +# so in a background thread--see the mqtt_client.py example to learn more. |
| 125 | +client.loop_blocking() |
0 commit comments