BT Sensors Plugin for Signalk is a lightweight BLE (Bluetooth Low Energy) framework for connecting to Bluetooth sensors on your boat and sending deltas to Signalk paths with the values the sensors reports.
A typical use case is a Bluetooth thermometer like the Xiaomi LYWSD03MMC, an inexpensive Bluetooth thermometer that runs on a 3V watch battery that can report the current temperature and humidity in your refrigerator or cabin or wherever you want to stick it (no judgement.)
The reported temperature can then be displayed on a Signalk app like Kip or, with appropiate mapping to NMEA-2000, a NMEA 2000 Multi-function display.
The Plugin currently supports the Xiaomi LYWSD03MMC, ATC flashed LYWSD03MMCs, Victron SmartShunt and the Inkbird IBS-TH2 thermometer.
Sounds like meager offerings but it's pretty easy to write and deploy your own sensor class for any currently unsupported sensor. More on that in the development section.
Signalk users with a Linux boat-puter (Windows and MacOS are NOT supported) and Bluetooth sensors they'd like to integrate into their Signalk datastream.
An MQTT server with an appropriate SK client plugin. There are several MQTT plugin clients in the Signalk appstore.
Advantages of this plugin over an MQTT server and client plugin are:
The key advantages of an MQTT setup is comprehensive support for BT devices and non-Linux platforms.
This will be the recommended installation when the code is ready for wider sharing. In the meantime, use the platform-specific developer install instructions below.
From a command prompt:
cd ~/[some_dir] git clone https://github.com/naugehyde/bt-sensors-plugin-sk cd bt-sensors-plugin-sk npm i [sudo] npm link cd [signalk_home] npm link bt-sensors-plugin-sk
Finally, restart SK. Plugin should appear in your server plugins list.
NOTE: "~/.signalk" is the default signalk home on Linux. If you're getting permissions errors executing npm link, try executing "npm link" under sudo.
After installing and restarting Signalk you should see a "BT Sensors Plugin" option in the Signalk->Server->Plugin Config page.
On initial configuration, wait 45 seconds (by default) for the Bluetooth device to complete its scan of nearby devices. Until the scan is complete, the Sensors section will be disabled. When the scan is complete your screen should look something like this:
TIP: If after 45 seconds (or whatever your Initial Scan Timeout is set to) you don't see "Scan completed. Found x Bluetooth devices." atop the config screen and the Sensors section is still disabled, close and re-open the config screen to refresh the screen. The config screen isn't as reactive as it oughtta be.
Then press the + button to add a sensor. Your screen should look like this:
Then select the sensor you want to connect to from the drop down.
TIP: If you need to rescan, disable and re-enable the plugin or restart Signalk.
Then select the class of bluetooth device. The class should have a similar name to the device. If you don't see the class for your device, you can develop your own (check out the development section.).
Then it's a simple matter of associating the data emitted by the sensor with the Signalk path you want it to update. In the example pictured here there are three data points (temperature, humidity and sensor voltage). Other classes will expose different data.
Remember to hit the submit button.
The plugin doesn't need for Signalk to restart but restart if that makes you more comfortable.
You should see data appear in your data browser. Here's a screenshot of Signalk on my boat displaying battery data from a Victron SmartShunt.
You can now take the data and display it using Kip, or route it to NMEA-2K and display it on a N2K MFD, or use it to create and respond to alerts in Node-Red. Life is good. So good.
The goal of this project is to support as many mariner-useful sensors as possible. If there's anything we can do to make sensor class development easier, please let us know.
You'll first need to know what data your sensor produces and the means by which it provides data (GATT Server or advertisement) and the details thereof.
The first approach is to see if the device manufacturer has documented this. Not all do. Don't worry if you can't find OEM docs, it's likely someone on the internet has figured out your device's whys and wherefores for you already. Google that thang.
If you're still coming up empty, there are any number of tools you can use to examine the Bluetooth data stream of your device.
With these tools you can see what data your device advertises, and what data it provides via a connection to its GATT Server.
To get the code you'll first need to clone this repository then create a branch. That's git talk. Google it if you don't know what that means.
Once you've done that you're ready for...
Below is a simple Device class for the Xiaomi thermometer with stock firmware. The code demonstrates the core responsibilities of a Bluetooth sensor device class in the BT-Sensor-plugin's framework:
const BTSensor = require("../BTSensor"); class LYWSD03MMC extends BTSensor{ static needsScannerOn(){ return false } static metadata = new Map() .set('temp',{unit:'K', description: 'temperature'}) .set('humidity',{unit:'ratio', description: 'humidity'}) .set('voltage',{unit:'V', description: 'sensor battery voltage'}) constructor(device){ super(device) } emitValues(buffer){ this.emit("temp",((buffer.readInt16LE(0))/100) + 273.1); this.emit("humidity",buffer.readUInt8(2)/100); this.emit("voltage",buffer.readUInt16LE(3)/1000); } async connect() { await this.device.connect() var gattServer = await this.device.gatt() var gattService = await gattServer.getPrimaryService("ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6") var gattCharacteristic = await gattService.getCharacteristic("ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6") this.emitValues(await gattCharacteristic.readValue()) await gattCharacteristic.startNotifications(); gattCharacteristic.on('valuechanged', buffer => { this.emitValues(buffer) }) } async disconnect(){ super.disconnect() await this.device.disconnect() } } module.exports=LYWSD03MMC
Most of the work is done in the connect() method. In this case, the connect() method creates a connection to the device:
await this.device.connect()
Then it gets the device's gattServer and primary service:
var gattServer = await this.device.gatt() var gattService = await gattServer.getPrimaryService("ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6")
Then, it requests the device's "characteristic" that will send us data.
var gattCharacteristic = await gattService.getCharacteristic("ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6")
Then it asks the characteristic for notifications when its value changes.
await gattCharacteristic.startNotifications();
Then most importantly it emits the values when the data has changed:
gattCharacteristic.on('valuechanged', buffer => { this.emitValues(buffer) })
In this implementation, the emitValues member function does the work of parsing the buffer and emitting the values.
emitValues(buffer){ this.emit("temp",((buffer.readInt16LE(0))/100) + 273.1); this.emit("humidity",buffer.readUInt8(2)/100); this.emit("voltage",buffer.readUInt16LE(3)/1000); }
NOTE: If you guessed that the plugin listens to changes to device objects and then publishes the deltas, you guessed right.
The problem with Gatt Server devices is they stay connected and eat up a lot of energy, draining your device's batteries. You can deactivate the device from the config screen when it's not in use or in this case you can flash the device with custom firmware that changes the device to a broadcast device that advertises its data obviating the need for a battery-draining connection. In the case of Xiaomi LYWSD03MMC you can flash it with some very useful software called ATC
Below is an example of a BTSensor subclass that uses the advertising protocol to get the data from a flashed Xiaomi thermometer.
const BTSensor = require("../BTSensor"); const LYWSD03MMC = require('./LYWSD03MMC.js') class ATC extends BTSensor{ constructor(device){ super(device) } static metadata = LYWSD03MMC.metadata connect() { const cb = async (propertiesChanged) => { try{ this.device.getServiceData().then((data)=>{ //TBD Check if the service ID is universal across ATC variants const buff=data['0000181a-0000-1000-8000-00805f9b34fb']; this.emit("temp",((buff.readInt16LE(6))/100) + 273.1); this.emit("humidity",buff.readUInt16LE(8)/10000); this.emit("voltage",buff.readUInt16LE(10)/1000); }) } catch (error) { throw new Error(`Unable to read data from ${util.inspect(device)}: ${error}` ) } } cb(); this.device.helper.on('PropertiesChanged', cb) } } module.exports=ATC
The big difference here is in the connect() method. All it does is wait on propertiesChanged and when that event occurs, the device object parses the buffer and emits the data. NOTE: Both classes have the same metadata, so the ATC class "borrows" the metadata from the LYWSD03MMC class.
When you're done working on your class and satisified that it's functioning properly, commit and request a merge (more git talk).
We love to see new sensor classes!