Maker.io main logo

Sending and Receiving data with Adafruit IO and Python

2018-12-19 | By Maker.io Staff

Adafruit IO is a great platform for hobbyists who want to make IoT projects. We have previously looked at setting up Adafruit IO, we’ve looked at the dashboard, and we’ve made a simple water level project. In this how-to article, we will learn a bit more about sending and receiving data to/from Adafruit IO using Python.

Python and Adafruit IO

Adafruit IO is an easy-to-use IoT platform that is useful for storing data, viewing data, and controlling devices. Adafruit IO uses several different protocols, including REST and MQTT, but using these natively in any language requires somewhat complex programming as well as a working understanding of sockets and the protocols. This is where libraries come in. Luckily for us, there is an easy-to-use library called Adafruit IO. First, we need to install this library with the command below (this will work in a command prompt or terminal).

pip install adafruit-io

The Adafruit IO library is available for devices and other languages such as the Arduino but making an IoT-enabled Arduino is somewhat complex and requires the use of Wi-Fi modules. Low-end microcontrollers are not really designed to access the internet, so it is best to make these devices communicate with a nearby computer, which can then route that data to the internet (this communication could be something as simple as a serial port). The program that runs on the main computer can be written in Python.

Initializing the Adafruit IO Library

With the library installed and Python3 IDLE running, it’s time to code! The first step in using Adafruit IO is to get your private key. To find this key, log in to your Adafruit IO account and from the main page click the “View your AIO key”. This key is essentially your password SO KEEP IT SECRET!

Sending and Receiving data with Adafruit IO and Python

With our key obtained, we need to create a new Python file and add the following two lines.

Copy Code
from Adafruit_IO import Client, Feed, Data aio = Client('YOUR ADAFRUIT IO KEY')

The first line imports the libraries that we are going to use, while the second line creates an Adafruit IO object called “aio”, and this is where we put our AIO key. My old key can be seen above, so my first two lines of code would be the following:

Copy Code
from Adafruit_IO import Client, Feed, Data aio = Client(‘ab9aed7a1ef94ee696ff35735ec5d3ed’)

From this point on, we can now use functions associated with the different classes above to send and receive data. This is a prime example of how amazing languages like Python are and how brilliant Python libraries can be! Whoever they are, the individuals who created this library are, in my opinion, classic examples of good coders.

Choose a Feed

As you have already learned in previous articles, feeds can be thought of as rooms, where data can be sent to, read from, and stored. Each feed is unique, so whatever you do to one feed will not affect any other feed. Examples of feeds that you could create for your projects include:

  • Current wind speed: Weather station
  • Current temperature: Fridge
  • Current humidity: Electronics storage unit
  • Current air pressure: Wearable altitude meter for hiking
  • For our example, we will create a simple feed that will measure the temperature of my workshop, so we will create a feed on Adafruit IO called “Workshop temperature”. If you have created the feed properly, then you should see it appear on your list as shown below.

    Sending and Receiving data with Adafruit IO and Python

    Sending data to a feed

    Now that we have a feed to send data to and receive from, it’s time to implement these features into a Python program. Now, you would be forgiven to think that this would take a good number of lines of code, followed by some clever algorithm. But the truth is that sending data to a feed requires only a single line of code! The code below will send the value of 21 to our “Workshop temperature” feed.

    Copy Code
    aio.send('Workshop temperature', 21)

    Receiving data from a feed

    Just like in sending data, receiving the latest value from a feed is also one line of code, and the example below shows the received number being passed to a variable called “currentTemperature”.

    Copy Code
    currentTemperature = aoi.receive(‘Workshop temperature’).value

    You may have noticed the “.value” on the end of the receive function. This is needed because the aoi.receive returns an object with multiple values, which include a lot of metadata (which may be useful). If you do not use the .value on the end, you will get something similar to this when you print the value of currentTemperature:

    Copy Code
    Data(created_epoch=None, created_at='2018-05-10T15:14:54Z', updated_at='2018-05-10T15:14:54Z', value='21', completed_at=None, feed_id=804604, expiration='1528557294.0', position=None, id='0DW3N32ZBDCKHTG4T9KX9Q5YVH')

    If all goes to plan, at this point, you can open your feed on Adafruit IO, and you should see the following:

    Sending and Receiving data with Adafruit IO and Python

    Complete Python Example

    Copy Code
    from Adafruit_IO import Client, Feed, Data aio = Client("YOUR AIO KEY HERE") aio.send('Workshop temperature', 21) currentTemperature = aio.receive("Workshop temperature").value print (currentTemperature) print("done")
    TechForum

    Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

    Visit TechForum