Bluetooth Low Energy for the Raspberry Pi
UART
The serial interface is one of the oldest interfaces in the IT world and has managed to survive for many generations on continually evolving carrier technologies. The UART service for BLE is the Nordic UART Service (NUS). For many applications, UART plays the role of an intermediate protocol. This is not exactly what the inventors of Bluetooth had in mind, but it is simple. In particular, developers can easily port existing applications.
Listing 3 shows an example of transferring sensor data with UART. A microcontroller reads a connected BME280 sensor, bundles the results into a string, and writes the string to the serial interface. An XIAO nRF52840 microcontroller is a great choice for entry-level applications because it is inexpensive and supports all common languages.
Listing 3
Transfer Sensor Data with UART
The code turns out to be very compact thanks to the CircuitPython libraries, but even with other programming languages the whole thing is unlikely to be much more complicated. The logic is similar on the client side. Instead of advertisements, the control panel scans for UART services, connects to the sensor, and reads from the serial port. The complete code for the server and client can be found in my project repository [6].
You may encounter some disadvantages of the UART procedure if other devices on the network offer NUS. The client program gets around this by querying the advertisement name BME280_Sensor
, but this is not an elegant approach. It would make more sense to have a separate service that defines the data structure.
Figure 5 shows the advantage of using UART, on the other hand. Adafruit's app can read UART data in CSV format without needing to understand the details of the data; you have a choice between text output or a plot for display purposes. A shared plot is not really suitable for the BME280 sensor data. The values for temperature, humidity (two-digit range), and pressure (four-digit range) are so far apart that the details are lost. But if you have the right kind of data, fast visualization of the measured values via a UART plot proves to be very useful.
Broadcasts
Broadcasts are a special type of advertisement. The peripheral device sends user data along with the advertisement. This means that other devices do not even have to establish a connection. Broadcasts are the only way to send data from one device to many others.
Broadcasts are popular in indoor navigation using beacons or in museums. In a museum, the advertisement typically contains a URL to a website that explains an exhibit in more detail. If the transmitter is close to the picture and transmits with low power, only suitable apps will receive the information.
For my example, the setup with the microcontroller and the BME280 sensor again provides a sample use case; Listing 4 shows the matching implementation. This program, including the appropriate client that reads the data, can also be found in the project repository [6].
Listing 4
Advertisement with Data
The AdafruitSensorMeasurement
class, a subclass of Advertisement
, has a set of predefined fields that the program populates with the measurements from the BME280 (lines 11 to 14). The command in line 16 ultimately sends the advertisement.
Any other devices can receive the advertisement by scanning – and even extract the data, if they know the internal structure. If the fields of the AdafruitSensorMeasurement
class don't match your projects, just copy the class and adapt it for your needs. This works even if you don't understand all the details.
Conclusions
BLE is ideal for networking battery-based sensors. The data sheet for the combined CYW43439 Bluetooth and WiFi chip on the new Raspberry Pi Pico W, for example, specifies energy consumption for BLE that is a factor of 1,000 lower than for WLAN (TX: 234 to 351 µA versus 270 to 320 mA).
Until BLE is enabled on the chip, you will have to resort to other microcontrollers. You won't get away with this quite as cheaply as with the Pico W. However, for around $10, the above-mentioned XIAO nRF52840 BLE is pretty affordable. Smartphones also support the wireless standard, which opens up other ideas for cool projects. For example, the Raspberry Pi could insist on a specific smartphone being nearby to release an application.
Infos
- "Connect Pi Devices and a Smartphone with Bluetooth" by Bernhard Bablok, Linux Magazine, issue 264, November 2022, pp. 62-27
- BLE Primer: https://www.bluetooth.com/bluetooth-resources/the-bluetooth-low-energy-primer/
- Official UUIDs for standardized profiles and services: https://btprodspecificationrefs.blob.core.windows.net/assigned-values/16-bit%20UUID%20Numbers%20Document.pdf
- Bleak: https://github.com/hbldh/bleak
- Nordic Semiconductor Developer Academy: https://devzone.nordicsemi.com/
- Git repository for this article: https://github.com/bablokb/ble-playground
« Previous 1 2
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
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.