• Here is my code for the puck v2. The idea is that when the button is pressed, it sends info (the button count, the battery level and the z-axis value) to Node-Red so that Node-Red can react to the event.

        var pressCount = 0;
        var batt = 0;
        setWatch(function() {
          batt = Puck.getBatteryPercentage();
          var acc_gyro = Puck.accel();
          pressCount++;
          NRF.setAdvertising({
            0x1812 : [pressCount],
            0x180f : batt,
            0x1821 : acc_gyro.gyro.z
          });
        }, BTN, { edge:"rising", repeat:true, debounce:50 });
    

    So, kind of works, but then it keeps sending

    Here is the first batch of three data points (button count, power and z-axis):

    9/17/2020, 8:01:08 PMnode: b0afda13.2a2f68
    /ble/advertise/e9:1d:0b:37:51:d3/1812 : msg : Object
    object
    topic: "/ble/advertise/e9:1d:0b:37:51:d3/1812"
    payload: "[2]"
    qos: 0
    retain: false
    _topic: "/ble/advertise/e9:1d:0b:37:51:d3/1812"
    _msgid: "6c9a4099.d4036"
    
    9/17/2020, 8:01:32 PMnode: b0afda13.2a2f68
    /ble/advertise/e9:1d:0b:37:51:d3/180f : msg : Object
    object
    topic: "/ble/advertise/e9:1d:0b:37:51:d3/180f"
    payload: "[100]"
    qos: 0
    retain: false
    _topic: "/ble/advertise/e9:1d:0b:37:51:d3/180f"
    _msgid: "78d1d1f2.a0fc7"
    
    9/17/2020, 8:01:32 PMnode: b0afda13.2a2f68
    /ble/advertise/e9:1d:0b:37:51:d3/1821 : msg : Object
    object
    topic: "/ble/advertise/e9:1d:0b:37:51:d3/1821"
    payload: array[1]
    qos: 0
    retain: false
    _topic: "/ble/advertise/e9:1d:0b:37:51:d3/1821"
    _msgid: "99cb3105.e5c77"
    

    Here is the next batch exactly one minute later.

    9/17/2020, 8:02:08 PMnode: b0afda13.2a2f68
    /ble/advertise/e9:1d:0b:37:51:d3/1812 : msg : Object
    object
    topic: "/ble/advertise/e9:1d:0b:37:51:d3/1812"
    payload: "[2]"
    qos: 0
    retain: false
    _topic: "/ble/advertise/e9:1d:0b:37:51:d3/1812"
    _msgid: "ab42bd04.966bc"
    
    9/17/2020, 8:02:32 PMnode: b0afda13.2a2f68
    /ble/advertise/e9:1d:0b:37:51:d3/180f : msg : Object
    object
    topic: "/ble/advertise/e9:1d:0b:37:51:d3/180f"
    payload: "[100]"
    qos: 0
    retain: false
    _topic: "/ble/advertise/e9:1d:0b:37:51:d3/180f"
    _msgid: "bf8ee39f.9ff23"
    
    9/17/2020, 8:02:32 PMnode: b0afda13.2a2f68
    /ble/advertise/e9:1d:0b:37:51:d3/1821 : msg : Object
    object
    topic: "/ble/advertise/e9:1d:0b:37:51:d3/1821"
    payload: array[1]
    qos: 0
    retain: false
    _topic: "/ble/advertise/e9:1d:0b:37:51:d3/1821"
    _msgid: "f303b855.f82eb8"
    

    Am I missing something with this? I thought the puck would sleep until the next press.

    On a side note... it seems like there are RSSI messages very frequently. Do they use just a small bit of power?

  • Here is my node red that is just logging the three metrics for now.

  • Maybe I am also missing something but these are not 'messages' and you are not 'sending' it. it is advertising which is set once but is advertised all the time every advertising interval even if puck sleeps. you could however set it to something else when button is released, but again it would be broadcasted all the time, that's what advertising is for, so that other devices can find it anytime. And rssi are not messages either, it is just current info/number about connection quality, nothing is sent.

  • Hi,

    It's as @fanoush says - the advertising messages are sent all the time regardless. There's no two-way communications unless you actively connect to the Puck, so to ensure delivery it just sends the advertising all the time (usually a few times a second).

    The power consumption of sending the data is very minimal so it's nothing to worry about, and the Puck is only executing JS when you press the button.

    EspruinoHub is seeing the advertising messages and does a few things:

    • Reports RSSI for every message - which is why you see a lot of them
    • Notices if a message hasn't changed, and only sends "/ble/advertise/e9:1d:0b:37:51:d3/180f" if it has or if a minute has passed (the code for that is here)

    Nodered has a rbe - report by exception - block that you can drag in to filter out the extra advertising messages, and only report when something has changed: http://www.espruino.com/BLE+Node-RED#detecting-button-presses

    Hope that helps!

  • @fanoush @Gordon , Thank you for being patient with a noob.

    This was excellent explanation!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Why does this code keep sending messages without button presses?

Posted by Avatar for HeneryH_(Henery_Hawk) @HeneryH_(Henery_Hawk)

Actions