Adafruit IO

Now that you have a cool physical dashboard built, how do you hook it up to Adafruit IO?  Check out the video below that walks through creating a simple Python script to display Adafruit IO feeds on a physical dashboard.  Below the video are the basic steps to follow and the code to use.

Setup

To hook up the Pi physical dashboard to Adafruit IO you'll first need to do a little bit of setup.  Before you start make sure you have the physical dashboard project assembled and working (using curl or other tools to test it).  In addition you'll want to have an account on Adafruit IO setup (visit http://io.adafruit.com to join the beta), and it will help to familiarize yourself with Adafruit IO feeds and dashboards by checking out these AIO basics guides.

Next you'll need to connect to the Pi and run the following commands to install a Python Adafruit IO client library:

Download: file
sudo pip install adafruit-io
sudo pip install requests

Now configure your physical dashboard with the gauges and widgets you'd like to display.  For this example I'm using the following dashbord config.ini:

Download: file
[slider]
type = SevenSegmentWidget
address = 0x74
decimal_digits = 0

[humidity]
type = SevenSegmentWidget
address = 0x76
decimal_digits = 2

[temp]
type = SevenSegmentWidget
address = 0x71
decimal_digits = 1

This configuration defines the following three 7-segment displays:

  • slider - A 7-segment display at I2C address 0x74 with no digits after the decimal point displayed.
  • humidity - A 7-segment display at I2C adress 0x76 with 2 digits after the decimal point displayed.
  • temp - A 7-segment display at I2C address 0x71 with 1 digit after the decimal point displayed.

Code

To hook up the dashboard to display Adafruit IO feeds you can create a simple Python script similar to the one shown below.  For example create a script called aio_dashboard.py on the Raspberry Pi and fill in:

Download: file
# Raspberry Pi Physical Dashboard & Adafruit IO Example
# Author: Tony DiCola
# License: Public domain
#
# This example will display three feeds from Adafruit IO on a dashboard.  The
# feeds are:
#  - pi-dashboard-slider: Will display a slider with values from 0-100 on a
#                         dashboard 7-segment display called 'slider'.
#  - pi-dashboard-humidity:  Humidity sensor feed displayed on a 7-segment display
#                            called 'humidity'.
#  - pi-dashboard-temp: Temperature sensor feed displayed on a 7-segment display
#                       called 'temp'.
#
# To modify this example to use your own feeds you'll want to change the following
# code:
#  - connected function: Add a client.subscribe() call for each feed you want to
#                        display on the dashboard.
#  - message function:  Add an if/elif check to check for each feed you want
#                       to display and have it make an HTTP POST against the
#                       dashboard using the feed payload as a value.
#  - DASHBOARD_URL variable: Change this to the URL of your Pi dashboard if you
#                            aren't running this script on the same Pi as the
#                            dashboard (otherwise keep it the same localhost:5000
#                            value).  Make sure NOT to end this with a slash!
#
# Be sure to modify the ADAFRUIT_IO_KEY and ADAFRUIT_IO_USERNAME variables to
# set your AIO key and username!

# Import standard python modules.
import random
import sys
import time

# Import Adafruit IO MQTT client.
from Adafruit_IO import MQTTClient

# Import requests library used for making HTTP calls to the dashboard server.
import requests


# Set to your Adafruit IO key & username below.
ADAFRUIT_IO_KEY      = 'YOUR ADAFRUIT IO KEY'       # Set to your Adafruit IO key.
ADAFRUIT_IO_USERNAME = 'YOUR ADAFRUIT IO USERNAME'  # See https://accounts.adafruit.com
                                                    # to find your username.
# Set the URL of the physical dashboard to use.  If running on the same Pi as
# the dashboard server then keep this the default localhost:5000 value.  If
# modified make sure not to end in a slash!
DASHBOARD_URL = 'http://localhost:5000'  # URL of the physical dashboard.
                                         # Don't end with a slash!


# 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 for feed changes...'
    # Subscribe to the three pi-dashboard feeds that will be displayed on the
    # dashboard.  Modify this to subscribe to all the feeds you want to display.
    client.subscribe('pi-dashboard-slider')
    client.subscribe('pi-dashboard-humidity')
    client.subscribe('pi-dashboard-temp')

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('Feed {0} received new value: {1}'.format(feed_id, payload))
    # Update physical dashboard depending on the changed feed.
    # Notice the feed_id is checked to find out which feed changed, then the
    # appropriate physical dashboard widget is changed.
    if feed_id == 'pi-dashboard-slider':
        # The requests.post function will make an HTTP request against the
        # dashboard.  See the requests documentation for more information:
        #   http://docs.python-requests.org/en/latest/
        requests.post('{0}/widgets/slider'.format(DASHBOARD_URL), data={'value': payload})
    elif feed_id == 'pi-dashboard-humidity':
        requests.post('{0}/widgets/humidity'.format(DASHBOARD_URL), data={'value': payload})
    elif feed_id == 'pi-dashboard-temp':
        requests.post('{0}/widgets/temp'.format(DASHBOARD_URL), data={'value': 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()

# Use the loop_blocking function to run the message loop for processing Adafruit
# IO events.  Since this script doesn't do any other processing this blocking
# version of the message loop is fine.  All the program logic will occur in the
# callback functions above when Adafruit IO feeds are changed.
client.loop_blocking()

This script is based on the mqtt_client.py example from the Adafruit IO Python library, but with a few small changes to make it send feed data to a dashboard.  You'll want to modify a few parts of the script as described below.

First set your Adafruit IO key and username in this section near the top of the script:

Download: file
# Set to your Adafruit IO key & username below.
ADAFRUIT_IO_KEY      = 'YOUR ADAFRUIT IO KEY'       # Set to your Adafruit IO key.
ADAFRUIT_IO_USERNAME = 'YOUR ADAFRUIT IO USERNAME'  # See https://accounts.adafruit.com
                                                    # to find your username.

(you might have noticed in the video above I load the AIO key from a local file, however it's not really necessary to load from a file unless you'd like to keep the key out of the script code)

Next if you're running this script outside of the Pi that's running your dashboard you'll need to change the DASHBOARD_URL variable below:

Download: file
# Set the URL of the physical dashboard to use.  If running on the same Pi as
# the dashboard server then keep this the default localhost:5000 value.  If
# modified make sure not to end in a slash!
DASHBOARD_URL = 'http://localhost:5000'  # URL of the physical dashboard.
                                         # Don't end with a slash!

Make sure the DASHBOARD_URL value is set to the IP address or hostname of your Pi that's running the dashboard.  If you're running this script on the same Pi as the dashboard then leave it as http://localhost:5000.  Make sure this URL does not end in a slash!

Now modify the connected function code to subscribe to all the feeds you wish to display on the dashboard.  This function will be called when a successful connection is made to the Adafruit IO service and it serves as a good point to subscribe to feeds or do other initialization.

Download: file
# 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 for feed changes...'
    # Subscribe to the three pi-dashboard feeds that will be displayed on the
    # dashboard.  Modify this to subscribe to all the feeds you want to display.
    client.subscribe('pi-dashboard-slider')
    client.subscribe('pi-dashboard-humidity')
    client.subscribe('pi-dashboard-temp')

Notice I'm subscribing to three feeds in the above code:

  • pi-dashboard-slider: This will be displayed on the slider dashboard widget.
  • pi-dashboard-humidity: This will be displayed on the humidity dashboard widget.
  • pi-dashboard-temp: This will be displayed on the temp dashboard widget.

Change the code to subscribe to each of the feeds you're using.

Finally modify the message function code to look for your feeds and push their payload to the dashboard:

Download: file
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('Feed {0} received new value: {1}'.format(feed_id, payload))
    # Update physical dashboard depending on the changed feed.
    # Notice the feed_id is checked to find out which feed changed, then the
    # appropriate physical dashboard widget is changed.
    if feed_id == 'pi-dashboard-slider':
        # The requests.post function will make an HTTP request against the
        # dashboard.  See the requests documentation for more information:
        #   http://docs.python-requests.org/en/latest/
        requests.post('{0}/widgets/slider'.format(DASHBOARD_URL), data={'value': payload})
    elif feed_id == 'pi-dashboard-humidity':
        requests.post('{0}/widgets/humidity'.format(DASHBOARD_URL), data={'value': payload})
    elif feed_id == 'pi-dashboard-temp':
        requests.post('{0}/widgets/temp'.format(DASHBOARD_URL), data={'value': payload})

Notice the message function checks the feed_id parameter for each of the feeds that were subscribed to earlier.  If one of those feeds changes then the requests library is used to make a HTTP POST request that updates the appropriate dashboard widget.  The payload parameter will hold the updated value of the feed and is used by the requests.post function to send the updated feed value to the dashboard.

For example in the code above changes to the pi-dashboard-humidity feed will update the dashboard widget called humidity (which is exposed at the http://raspberrypi:5000/widgets/humidity URL with the dashboard).

You'll want to modify the if/elif statements to check for your feeds and push the value to the appropriate dashboard widget.

That's all you need to change to use this script!  Make sure your physical dashboard is running on the Pi, then connect to the Pi and run the script:

Download: file
python aio_dashboard.py

The script should print out a message when it's connected to Adafruit IO, and when it receives a subscribed feed change.  Build a dashboard on Adafruit IO or use another script to update your subscribed feeds and you should see those feeds appear on your physical dashboard widgets--cool!

If you see an error that the script can't connect to Adafruit IO, make sure you have the AIO key and username correctly set for your account.

If you don't see your feed updated on the physical dashboard when it changes on Adafruit IO check a few things:

  • Make sure the script is successfully connecting to Adafruit IO (i.e. your username and key are correct).
  • Make sure you're subscribing to the feeds that you're changing.
  • Make sure the if/elif checks in your message function are looking for the feeds you've subscribed to and are changing.
  • Check the DASHBOARD_URL points to the URL of the Pi running your dashboard (or the default localhost:5000 if running the script on the same Pi as the dashboard).
This guide was first published on Jan 16, 2016. It was last updated on Jan 16, 2016. This page (Adafruit IO) was last updated on Feb 23, 2020.