• My first Espruino project.

    I just got a Puck.JS. I have been reading for the past few days on BLE, NRF.SetAdvertising, setWatch, tutorials, videos etc. Some excellent stuff out there, but very scattered and some old and some new, so I am a bit confused.

    What I want to do is pretty straight forward - I thought.

    I want to press the button on the Puck.js and turn my stereo on/off through Home Assistant.

    To do this (I think) I want to press the Puck.js button and send a BLE advertisement to a BLE-MQTT Gateway (ESPHome) which turns it into MQTT pub which I can subscribe to in Home Assistant and use to run an automation which will turn a media_player on and off.

    So press- stereo on
    Another press - stereo off.
    Just a plain ol' toggle.

    But I also want the other BLE advertising to go ahead, not just at the button press.

    So... right now I can get E.GetTemperature and E.Battery using NRF.SetAdvertising and can read it via the Gateway/MQTT fine into Home assistant just fine.

    And I can also toggle the state of the button on and off.
    But...
    What I can't do is all of that together. It is an either/ or. Here is my code

    var buttonState = 0;
    NRF.setAdvertising({
      0x1809 : E.getTemperature(),
      0x180F : E.getBattery(),
    }, {interval: 500});
    
    setWatch(function() {
        buttonState = !buttonState;
        digitalWrite(LED2, buttonState);
        NRF.setAdvertising({
            0xFFFF : [buttonState]
        });
    }, BTN1, { repeat:1, edge:"rising", debounce: 20 });
    

    With this code in Puck, (and viewing ble output in nrfconnect) I first see temp and batt, but as soon as I press the button, 0xFFFF gets set with buttonState but Temp and Batt are no longer visible.

    How do I get button state to be passed as and when it is toggled, AND keep temperature and battery going at some set interval as well?

    Thanks

About

Avatar for kab @kab started