Add BTHome thermometer

master
Gordon Williams 2022-12-14 10:23:36 +00:00
parent 51304530f6
commit 69660b69aa
6 changed files with 83 additions and 0 deletions

View File

@ -0,0 +1 @@
0.01: New App!

View File

@ -0,0 +1,9 @@
# BTHome Temperature and Pressure
This app displays temperature and pressure and advertises them over bluetooth using BTHome.io standard (along with battery level)
This can be used to integrate with [Home Assistant](https://www.home-assistant.io/), so you can use your Bangle as a wireless temperature/pressure sensor.
More info on the standard at https://bthome.io
And the data format used is https://bthome.io/format/

View File

@ -0,0 +1 @@
require("heatshrink").decompress(atob("mEw4kA///1N6BIPf//1gMIwdE8sG2me+9Y/8C/2snXsoUNpdnzdt/xj/AH4AYgMRAAUQCyoYSCQNXs1muoFBFyHm1X//+qtwwPiMX1+YmczxP6uIwNFwN6yeDnGDmc504wNFwOpnGYC4OJweaGBsR9WTmYtBmc4GAOuC5ZGBt4SBAAQEBwf2JBcBiupnIuCmedxGTzVRC5cX1AuDnPZF4OKuIXLi3zIoedMgMzn9hC5uICQON5IDBxAXSznYC6RdDPQYXNO4JcB7pdCO56nBnGZ7p6DU5zXBXgSqDa5sAiPqIgOZd4c510RCxQXBi+pRQIXBxODzVxC5hIBvR1DnE505GMGAevzAvC/QuNGAfm1X//+qtwuOGAURq9ms11AoIWOGAQAEFw1EDBwWFggBCkUgAQMigUAAIIAJoABDCgIXQFwYXBCYYBDHAMCEAIkCFgcEAIIKCCoQFCkAhBAQIlCkAsBOoIXCBoIvEAwQTCAYI2BIwgXIF4YXDQwIVCC4YIBMIwfCAQRfGYBSPNC6TBFACgwBACouWAH4AiA="))

58
apps/bthometemp/app.js Normal file
View File

@ -0,0 +1,58 @@
// history of temperature/pressure readings
var history = [];
// When we get temperature...
function onTemperature(p) {
// Average the last 5 temperature readings
while (history.length>4) history.shift();
history.push(p);
var avrTemp = history.reduce((i,h)=>h.temperature+i,0) / history.length;
var avrPressure = history.reduce((i,h)=>h.pressure+i,0) / history.length;
var t = require('locale').temp(avrTemp).replace("'","°");
// Draw
var rect = Bangle.appRect;
g.reset(1).clearRect(rect.x, rect.y, rect.x2, rect.y2);
var x = (rect.x+rect.x2)/2;
var y = (rect.y+rect.y2)/2 + 10;
g.setFont("6x15").setFontAlign(0,0).drawString("Temperature:", x, y - 65);
g.setFontVector(50).setFontAlign(0,0).drawString(t, x, y-25);
g.setFont("6x15").setFontAlign(0,0).drawString("Pressure:", x, y+15 );
g.setFont("12x20").setFontAlign(0,0).drawString(Math.round(avrPressure)+" hPa", x, y+40);
// Set Bluetooth Advertising
// https://bthome.io/format/
var temp100 = Math.round(avrTemp*100);
var pressure100 = Math.round(avrPressure*100);
Bangle.bleAdvert[0xFCD2] = [ 0x40, /* BTHome Device Information
bit 0: "Encryption flag"
bit 1-4: "Reserved for future use"
bit 5-7: "BTHome Version" */
0x01, // Battery, 8 bit
E.getBattery(),
0x02, // Temperature, 16 bit
temp100&255,temp100>>8,
0x04, // Pressure, 16 bit
pressure100&255,(pressure100>>8)&255,pressure100>>16
];
NRF.setAdvertising(Bangle.bleAdvert);
}
// Gets the temperature in the most accurate way with pressure sensor
function drawTemperature() {
Bangle.getPressure().then(p =>{if (p) onTemperature(p);});
}
if (!Bangle.bleAdvert) Bangle.bleAdvert = {};
setInterval(function() {
drawTemperature();
}, 10000); // update every 10s
Bangle.loadWidgets();
Bangle.setUI({
mode : "custom",
back : function() {load();}
});
E.showMessage("Reading temperature...");
drawTemperature();

BIN
apps/bthometemp/app.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

View File

@ -0,0 +1,14 @@
{ "id": "bthometemp",
"name": "BTHome Temperature and Pressure",
"shortName":"BTHome T",
"version":"0.01",
"description": "Displays temperature and pressure, and advertises them over bluetooth using BTHome.io standard",
"icon": "app.png",
"tags": "bthome,bluetooth,temperature",
"supports" : ["BANGLEJS2"],
"readme": "README.md",
"storage": [
{"name":"bthometemp.app.js","url":"app.js"},
{"name":"bthometemp.img","url":"app-icon.js","evaluate":true}
]
}