This project captures the broadcasts from my Bluetooth Low Energy Environmental Sensor and stores them in a SQLite database. I've also provided a CGI so you can RESTfully query the data in JSON and inspect it with interactive data graphics powered by Plotly.
This is a CMake project that requires Bluez and Boost. I built it on Ubuntu 20.04 for the Raspberry Pi model 4b. I found that both the Pi's internal Bluetooth radio and an external dongle I tried produced very limited reception. The best I could get from the Pi was reception from the next room. By contrast when I used a typical Dell laptop from 2015 reception worked from anywhere in the house and even across the street.
On linux the service must run with elevated capabilities to listen to Bluetooth. You can grant these capabilities like so:
sudo setcap 'cap_net_raw,cap_net_admin+eip' build_dir_path/project_binary_name
-1. Make a directory named SQLite3
in your home directory. Set the group ownership to www-data so that the CGI can
read it.
mkdir ~/SQLite3
chown your_user:www-data ~/SQLite3
-2. Copy the project binary and the contents of the run
directory from this project into it.
-3. Edit ble_sensor.service
to reflect your username, group, and the path to your home directory. Hint: read
the comments.
-4. As root, copy ble_sensor.service
to /etc/systemd/system
and enable the
service:
sudo systemctl enable ble_sensor.service
Created symlink /etc/systemd/system/multi-user.target.wants/ble_sensor.service → /etc/systemd/system/ble_sensor.service.
-5. Start the new service and check it's status.
sudo systemctl start ble_sensor
sudo systemctl status ble_sensor
● ble_sensor.service - Bluetooth Low Energy SQLite3 Service
Loaded: loaded (/etc/systemd/system/ble_sensor.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2021-01-13 16:58:33 EST; 1 day 16h ago
Main PID: 107224 (ble_sensor_serv)
Tasks: 1 (limit: 4200)
CGroup: /system.slice/ble_sensor.service
└─107224 /home/patrick/SQLite3/ble_sensor_service
Jan 13 16:58:33 raspi4 systemd[1]: Started Bluetooth Low Energy SQLite3 Service.
- Thank you to edrosten for his Modern clean C++ Bluetooth Low Energy on Linux without the Bluez DBUS API.
- Thank you to obiltschnig and the Poco Project Team for providing the example and library code from which the WebSocket server in this project is built.
- Thank you to hjiang for the jsonxx project. I tested several C++ JSON libraries including jsoncpp before deciding to use jsonxx for its simplicity and speed.
If you find a bug please create an issue. If you'd like to contribute please send a pull request.
The following were helpful references in the development of this project.