You are reading a single comment by @user156756 and its replies. Click here to read the full conversation.
  • 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.c­om/muet/EspruinoDocs/master/modules/SWBu­tton.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);
    
    }
    
About

Avatar for user156756 @user156756 started