I am so glad you got back to this! I looked into your bangle app as an example to work from and get started. I am trying to take my puck which I use as a simple on/off/volume up/volume down button in Home Assistant based on button presses. I use the SwButton module to do the button click heavy lifting.
1 short press is toggle on and off via generic boolean (0x0F)
1 long press is vol up count (0x09) goes from 0 to 1 vol goes up
2 short presses is vol down count (0x09)goes from 0 to 2 vol goes down
These numbers come in as count in bthome and get used in an automation.
I have it working w EspruinoHub and MQTT fine - but want to "upgrade" BTHome so I can use the PiZero with the EspruinoHub on it for something else, and I have a lot to BluetoothProxies around the house anyway.
I wrote the following which seems to work fine except one issue -- the advertised count reverting to 0 after the button is pressed doesn't seem fast enough. Is that in my idletimeout?
Anyway - any comments or ways to make this cleaner/faster appreciated
//https://raw.githubusercontent.com/muet/EspruinoDocs/master/modules/SWButton.js
var SWBtn = require("SWButton");
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.press = + 0;
}
else if (k === "L" ) { // long button press
btnData.press = 1;
}
else if (k === "SS") { // double short press
btnData.press = 2;
}
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,
];
NRF.setAdvertising(Puck.bleAdvert);
}
setInterval(function() {
initBTHome();
}, 1*60*1000); // 1 min
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, // count button press, 1 bit
btnData.press,
0x0F, // binary button state, uint8 (1 byte)
btnData.state,
];
NRF.setAdvertising(Puck.bleAdvert);
if (idleTimeout) clearTimeout(idleTimeout);
idleTimeout = setTimeout(function() {
idleTimeout = undefined;
Puck.bleAdvert[0xFCD2][2] = 0;
NRF.setAdvertising(Puck.bleAdvert);
},2000);
}
Thanks for getting to this despite it being "another standard"
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
I am so glad you got back to this! I looked into your bangle app as an example to work from and get started. I am trying to take my puck which I use as a simple on/off/volume up/volume down button in Home Assistant based on button presses. I use the SwButton module to do the button click heavy lifting.
1 short press is toggle on and off via generic boolean (0x0F)
1 long press is vol up count (0x09) goes from 0 to 1 vol goes up
2 short presses is vol down count (0x09)goes from 0 to 2 vol goes down
These numbers come in as count in bthome and get used in an automation.
I have it working w EspruinoHub and MQTT fine - but want to "upgrade" BTHome so I can use the PiZero with the EspruinoHub on it for something else, and I have a lot to BluetoothProxies around the house anyway.
I wrote the following which seems to work fine except one issue -- the advertised count reverting to 0 after the button is pressed doesn't seem fast enough. Is that in my idletimeout?
Anyway - any comments or ways to make this cleaner/faster appreciated
Thanks for getting to this despite it being "another standard"