-
• #27
Thanks for the tips - as always very helpful - and again thanks for the bthome example.
-
• #28
Getting back to this with all the Home Assistant updates and saw that BTHome now has button events. Works lovely
var SWBtn = require("https://raw.githubusercontent.com/muet/EspruinoDocs/master/modules/SWButton.js"); var buttonState = 0; var btnData = {}; var idleTimeout; if (!Puck.bleAdvert) Puck.bleAdvert = {}; // these get created with a puck button press var mySWBtn = new SWBtn(function(k){ if (k === "S" ) { // single button press buttonState = !buttonState; btnData.state = buttonState; btnData.btn = 0x01; btnData.press = + 0; } else if (k === "L" ) { // long button press btnData.press = 1; btnData.btn = 0x04; } else if (k === "SS") { // double short press btnData.press = 2; btnData.btn = 0x02; } updateBTHome(btnData); }); function initBTHome() { tempF = [(E.getTemperature()*9/5)+32]; Puck.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 tempF&255,tempF>>8, 0x3A, // button event none 0x00, ]; NRF.setAdvertising(Puck.bleAdvert); } setInterval(function() { initBTHome(); //}, 1*60*1000); // 1 min }, 10000); // 10 sec function updateBTHome(btnData) { Puck.bleAdvert[0xFCD2] = [ 0x40, /* BTHome Device Information bit 0: "Encryption flag" bit 1-4: "Reserved for future use" bit 5-7: "BTHome Version" */ 0x09, // count1 button press, 1 bit btnData.press, 0x0F, // binary button state, uint8 (1 byte) btnData.state, 0x3A, // button event btnData.btn, ]; NRF.setAdvertising(Puck.bleAdvert); if (idleTimeout) clearTimeout(idleTimeout); idleTimeout = setTimeout(function() { idleTimeout = undefined; Puck.bleAdvert[0xFCD2][2] = 0; NRF.setAdvertising(Puck.bleAdvert); },2500); }
-
• #29
That's great - thanks for letting us know! Are all those elements needed?
Looking at https://bthome.io/format/ I see the
Events
heading and it looks like you need 0x3A (event), and then something before to say what it was (0x0F for generic bool?). But is the 0x09 at the start just for backwards compatibility with older BTHomes?I should make a
bthome
library to make this easy to use. Do you have any thoughts on what should be in it? -
• #30
Just added a library for this: https://www.espruino.com/BTHome
I'd be interested to see if it all works ok for you.
I notice that button events on their own don't seem to appear though, and in your code you have defined a button - do you have any ideas if that is needed? I'm struggling to find info on it...
-
• #31
Actually I take that back - it works fine, I just didn't spot how the events come in (they're there when you look at the log for the device)
Looks good, although I guess you could keep advertising battery/temperature even when the button is pressed, and should probably advertise 0x0F/btnData.state all the time so home assistant knows the button is there all the time, just not pressed?
What exactly isn't it fast enough for?
It's actually a bit unfortunate that they chose to do buttons this way, because it feels like either you have a short timeout and you risk the button press getting never received, or you have a long one and potentially other devices think the button is pressed more than once. If the button had been advertised as a 'press count' that just incremented when the button was pressed things would have been much easier - no timeout would be needed.