Setup a Raspberry Pi
Learn how to setup a Raspberry Pi 3 model B to work with docker, dbus, and bluetooth.
Learn how to setup a Raspberry Pi 3 model B to work with docker, dbus, and bluetooth.
Learn about how Bluetooth Low Energy (BTLE) works.
Build a "peripheral" application in Python 3.
Integrate all the pieces with the Linux D-Bus system.
We're going to build a device (hardware and software) that can accurately measure the temperature of human skin. It's 2021 and pointing a pistol-grip thermometer at your forehead is a pretty normal thing to do. Our device won't have a display attached, it's just going to broadcast a Bluetooth LE characteristic that can displayed on a phone or in a web browser.
We're going to install Docker on the Raspberry Pi, but why would we do that? Dependency management was the hardest part of this project, so Docker is going to keep us sane. In order for a Python application to send messages over D-Bus it needs access to the type definitions in the glib package.
To get our application working, it's helpful to understand how we're actually going to make the Bluetooth radio do things. The Bluetooth radio is controlled by a driver called BlueZ. BlueZ runs a daemon called bluetoothd If we want the radio to do something, we need to tell bluetoothd to make it happen. But, how does one running process(our app) tell another running process(bluetoothd
) to do something? This is where D-Bus comes in.
Our application is what Bluetooth calls a "peripheral". The peripheral advertises its presence for a "central" device that want's to connect to it. The central could be a cell phone, or web browser - it's usually where the human interaction happens. If you're familiar with the Tesla "phone key" this is a great example. The car is the peripheral, and the phone(key) acts as the central listening for the car's advertisement. When the phone(key) sees the car's advertisement, it connects and tells the car that it's OK to unlock.
Let's write the code to get the Bluetooth radio advertising our service, allowing centrals to connect, and notifying those centrals when the temperature value changes.
Probably the most important section of this guide, I'll show you want to do when your code isn't working as you expected. With BlueZ, D-Bus, and our own code in play, the first thing to figure out is which component is the problem...
You could write the central as a mobile application, but for our purposes we're going to use the Web Bluetooth API. This will let us connect to our peripheral through some Javascript code.