IoT communication with the Adafruit IO API
Talk of the Town
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 |
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
.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
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.
News
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.
-
DebConf24 to be Held in South Korea
Busan will be the location of the latest DebConf running July 28 through August 4
-
Fedora Unleashes Atomic Desktops
Fedora has combined its solid distribution with rpm-ostree system to make it possible to deliver a new family of Fedora spins, called Fedora Atomic Desktops.
-
Bootloader Vulnerability Affects Nearly All Linux Distributions
The developers of shim have released a version to fix numerous security flaws, including one that could enable remote control execution of malicious code under certain circumstances.