IoT communication with the Adafruit IO API

Talk of the Town

Article from Issue 203/2017
Author(s):

The Adafruit IO API offers a convenient means for network-ready sensors and other components.

The development of the Arduino platform [1] a little more than 10 years ago opened the world of electronic sensors to a wider audience. Around that time, the US engineer Limor Fried, also known as Lady Ada, founded Adafruit Industries [2]. Her goal was to make it easier to handle electronic components.

Adafruit's compact circuit boards combine electronic components with easy-to-handle breakout boards, saving the user the time and effort of configuring the components separately. Even for complex circuits and sensors based on industry protocols, such as I2C or SPI, all you need is a program in C++ and the necessary libraries.

Another recent trend is services that manage sensors and control components over the Internet. Simple web services provide measurement data for display in a browser window. A user at the browser or an automation script can transmit configurations and send switch signals to the components.

Most electronic platforms today provide wireless extensions. Arduino boards can be equipped with Ethernet and WiFi shields. Devices like the Arduino Yún [3] have built-in network components. However, even if your hardware provides network functionality, you'll need to expend some time and effort on finding a way to manage the device over the network. Adafruit offers a very convenient API for network-enabled sensors and microcontrollers called Adafruit IO [4].

Adafruit IO is a programming interface that is available in two flavors: a REST API and an MQTT API (Figure 1). The REST API follows the well-known principles of the REST architecture, and the MQTT protocol, which comes straight from the industrial sector, is specifically designed for controlling sensors on networks.

Getting Access

Developers have two options for accessing the Adafruit IO API. One option is to register on the Adafruit IO website and then access the well-documented API [5]. The other way is to get the Adafruit IO Node.js Server [6] from GitHub and install it locally.

The Adafruit IO website shows registered users an overview of existing dashboards in the user account. A menu on the left lets you select additional views.

Users access API-related overviews by selecting the Feeds, Groups, Dashboards, Triggers, and Settings entries. Additionally, links to API Documentation and other information (Guide and Tips, Adafruit IO Forum, and Blog/Changelog) also appear.

The example of a temperature sensor demonstrates how a wireless-enabled microcontroller sends sensor values to the Adafruit IO API via the Internet. The Adafruit-Huzzah board [7], which is based on the ESP8266, serves as an IoT device.

IoT Client

A Python script on the Linux client reads the data from the API and – using the Conky system monitor – displays the data on the desktop. The circuit consists of just a few components (see Table 1); Figure 2 is a rough sketch of the structure.

Table 1

Components for the IoT Temperature Sensor

Component

Price (EUR)

Price (US$)

Adafruit Huzzah

13.00

9.95

FTDI cable

6.00

9.95

DHT22

6.00

9.95

10-kOhm resistor (25)

1.10

0.75

220-Ohm resistor (25)

1.10

0.75

LED

0.10

4.00

Breadboard

3.00

5.00

Jumpers

3.00

3.95

Figure 2: The structure of the IoT temperature sensor.

Once the hardware is ready, you can generate a data feed in the Adafruit IO user account. Feeds take data from a transmitter and make it available for retrieval by a client. A click on Actions | Create a New Feed generates a new feed. The IoT user selects an appropriate name and description for the feed, and it is then accessible for retrieval.

The easiest way for the user to program the IoT temperature sensor is with the Arduino IDE [8], which exists for both 32- and 64-bit Linux systems. The Adafruit-Huzzah board features a built-in USB interface and serial port. If just a serial UART port is available, you'll need an FTDI cable to provide a serial interface via USB.

If you integrate the appropriate Adafruit Huzzah board with the Arduino IDE using the instructions at the Adafruit website [9], you can use Sketch | Include libraries | Manage libraries to install five libraries: DHT, ArduinoHttpClient, Adafruit unified sensor, Adafruit MQTT Library, and Adafruit Arduino IO.

The simple sensor program based on the sample code from the Adafruit IO library is shown in Listing 1. The config.h file in Listing 2 shows the information necessary to access data for WiFi and the Adafruit IO API. You need to adjust the settings in the file before compiling the code and uploading it to the board.

Listing 1

AdalOClient.ino

 

Listing 2

config.h

 

In Sight

The Feed view reveals whether the sensor is delivering data successfully to the API. If it works, you can set up the client on the Linux desktop in the next step (see the Python scripts in Listings 3 and 4).

Listing 3

getTemp.py

 

Listing 4

getHum.py

 

The command

sudo pip install adafruit-io

installs the Python libraries for the Adafruit IO API using the Pip package manager. The scripts require the API key in the second line of Listing 3, which is stored in the user account under Settings | Manage AIO Keys | View AIO Key. When executed, the script retrieves the data from the Adafruit IO API feed and displays the current value.

You have several options for getting the script output to the desktop. In Gnome Panel, for example, the Argos plugin [10] shows the values or matching icons in the panel. Another option is provided by KDE's Plasma widgets, which the user places on the desktop. However, creating a widget that displays the feed data requires some training [11].

It is particularly easy to display the values using the Conky [12] system monitor and the execi function, which displays the script output. A configuration entry for the two scripts from Listings 3 and 4, showing an update every 600 seconds, looks like the following:

Temperature: ${execi 600 python getTemp.py}
Humidity: ${execi 600 python getHum.py}

Figure 3 shows the Simple Conky [13] configuration. The .conkyrc configuration file resides in the ~/.conky/ folder. To make sure the display program automatically starts the service when the system starts, you need to integrate it into the startup configuration for the desktop. Optionally, you can copy the appropriate font into the system font directory [14] and install with fc-cache -v.

Figure 3: The feed data appears in the Conky widget.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Digital Spirit Level

    The small MPU6050 sensor contains a gyroscope and an accelerometer, which means that you can build a digital spirit level with it.

  • Web Serial API

    Upgrade your computer with LEDs, buttons, or sensors to control a microcontroller board over USB from your web browser.

  • Adafruit PyPortal

    Unlike other displays for the Raspberry Pi, Adafruit's PyPortal touchscreen provides an autonomous environment, including a microprocessor, sound output, and a WiFi connection.

  • Monitoring Beehives

    Beekeepers can get to know their colonies better without continuously disturbing the industrious insects. Using a Raspberry Pi and various sensors, two hobby beekeepers monitor the temperature and humidity of their hives, with plans to monitor their weight.

  • Automated Irrigation

    An automated watering system comprising a Raspberry Pi Zero W, an analog-to-digital converter, and an inexpensive irrigation kit can help keep your potted plants from dying of thirst.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News