- Go 100%
btmond: SwitchBot Monitor Pro (CO2) BLE to Riemann Daemon #
btmond is a lightweight, low-resource daemon written in Go to gather metrics broadcast via Bluetooth Low Energy (BLE) from a SwitchBot Monitor Pro (CO2) device and publish them to a Riemann monitoring instance.
It is optimized to run on the onboard Bluetooth of a Raspberry Pi Zero W Rev 1.1.
Features #
- Decodes SwitchBot Monitor Pro (CO2) BLE advertisements:
- Temperature (°C)
- Humidity (%)
- Carbon dioxide levels (CO2 ppm)
- Battery level (%)
- Hardware Watchdog: Automatically resets the onboard Bluetooth interface using a customizable shell command if no advertisements are detected within a configurable window (e.g. 5 minutes).
- Throttling: Prevents Riemann metric flooding by only publishing metrics at a rate-limited interval (e.g. once every 60 seconds).
- Automatic Reconnection: Automatically reconnects to the Riemann server (supports TCP or UDP protocols) if the connection drops.
- Graceful Shutdown: Implements OS signal trapping (
SIGINT,SIGTERM) for a clean exit.
1. Cross-Compilation for Raspberry Pi Zero W #
The Raspberry Pi Zero W Rev 1.1 uses an ARMv6 architecture. Standard Go ARM builds default to ARMv7, which will crash on the Pi Zero W with Illegal instruction.
You must target GOARM=6 when cross-compiling:
# Cross-compile for Raspberry Pi Zero W (ARMv6)
GOOS=linux GOARCH=arm GOARM=6 go build -trimpath -ldflags "-s -w" -o btmond ./cmd/btmond
Transfer the resulting btmond binary to your Raspberry Pi.
2. Configuration #
Create a configuration file at /etc/btmond.json (or any other path):
{
"riemann_addr": "riemann-server.local:5555",
"riemann_proto": "tcp",
"riemann_timeout_seconds": 5,
"bluetooth_interface": "hci0",
"switchbot_mac": "XX:XX:XX:XX:XX:XX",
"watchdog_timeout_minutes": 5,
"reset_command": "hciconfig hci0 down && sleep 2 && hciconfig hci0 up",
"publish_interval_seconds": 60,
"log_level": "info",
"co2_warn_threshold": 1000,
"co2_crit_threshold": 1400,
"batt_warn_threshold": 20,
"batt_crit_threshold": 10
}
Configuration Options #
| Option | Type | Default | Description |
|---|---|---|---|
riemann_addr |
string | "localhost:5555" |
Riemann host and port. |
riemann_proto |
string | "tcp" |
Protocol to communicate with Riemann (tcp or udp). |
riemann_timeout_seconds |
int | 5 |
Connection/write timeout to Riemann. |
bluetooth_interface |
string | "hci0" |
The local Bluetooth device interface name. |
switchbot_mac |
string | "" |
Optional MAC address of the SwitchBot to target. If empty, processes any detected SwitchBot Monitor Pro (CO2). |
watchdog_timeout_minutes |
int | 5 |
Period of inactivity before triggering the Bluetooth reset command. |
reset_command |
string | "sudo hciconfig hci0 down && sleep 2 && sudo hciconfig hci0 up" |
Command executed to reset the adapter if the watchdog triggers. |
publish_interval_seconds |
int | 60 |
Minimum interval (in seconds) between publishing metrics. |
log_level |
string | "info" |
Log level: info, debug, warn, or error. |
co2_warn_threshold |
int | 1000 |
Warning threshold for CO2 level (ppm) above which Riemann state is set to warning. |
co2_crit_threshold |
int | 1400 |
Critical threshold for CO2 level (ppm) above which Riemann state is set to critical. |
batt_warn_threshold |
int | 20 |
Warning threshold for battery level (%) below which Riemann state is set to warning. |
batt_crit_threshold |
int | 10 |
Critical threshold for battery level (%) below which Riemann state is set to critical. |
How to Find Your SwitchBot MAC Address #
You can discover the Bluetooth MAC address of your SwitchBot Monitor Pro (CO2) in a few ways:
-
Using
btmonditself: If you runbtmondwithout a config or MAC address filter, it will print decoded metric reports for any matching SwitchBot Monitor Pro (CO2) in range. Simply run it and look at the logs:sudo ./btmondYou will see lines like:
[INFO] Decoded metrics from AA:BB:CC:DD:EE:FF: Temp=22.5°C, Hum=45%, CO2=650ppm, Battery=90%Copy theAA:BB:CC:DD:EE:FFaddress and set it in yourswitchbot_macconfiguration. -
Using standard Linux Bluetooth tools (
bluetoothctl): Run the interactive bluetooth control tool:sudo bluetoothctlInside the prompt, type:
scan onLook for a device with the name containing
WoSensorTH(oftenWoSensorTHor similar, e.g.WoSensorTH_C). The MAC address will be listed next to it. Once found, typeexitto quit. -
Using mobile apps: You can also download free tools like nRF Connect on your iOS or Android phone, start scanning, and look for
WoSensorTHor filter by SwitchBot's UUIDFD3Dto reveal the MAC address.
3. Installation #
1. Install the Binary #
Move the compiled binary to /usr/local/bin/:
sudo mv btmond /usr/local/bin/
sudo chmod +x /usr/local/bin/btmond
2. Configure Permissions (if running as non-root) #
To allow scanning without running as root, grant cap_net_admin capabilities to the binary:
sudo setcap 'cap_net_raw,cap_net_admin+eip' /usr/local/bin/btmond
Note: If the reset_command uses hciconfig or triggers system commands, the daemon will still require appropriate permissions (e.g. running as root or having sudo rights without password for the reset script).
3. Create the Systemd Service #
Create the service unit file /etc/systemd/system/btmond.service:
[Unit]
Description=SwitchBot Monitor Pro (CO2) BLE Riemann Daemon
After=network.target bluetooth.target
Requires=bluetooth.service
[Service]
Type=simple
ExecStart=/usr/local/bin/btmond -config /etc/btmond.json
Restart=always
RestartSec=10
User=root
Group=root
[Install]
WantedBy=multi-user.target
4. Enable and Start the Service #
sudo systemctl daemon-reload
sudo systemctl enable btmond.service
sudo systemctl start btmond.service
5. Check Service Logs #
journalctl -u btmond.service -f