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
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.