• Hi!

    It's great that you've got EspruinoHub set up - that should make things way easier.

    There's the example of button presses on https://www.espruino.com/BLE+Node-RED:

    var pressCount = 0;
    setWatch(function() {
      pressCount++;
      NRF.setAdvertising({
        0xFFFF : [pressCount]
      });
    }, BTN, { edge:"rising", repeat:true, debounce:50 });
    

    That uses 0xFFFF which we just use as a general UUID for testing, but there are a few others listed on https://github.com/espruino/EspruinoHub#­advertising that you can add too:

    • 1809 decodes to temp (Temperature in C)
    • 180f decodes to battery
    • feaa decodes to url (Eddystone)
    • 2a6d decodes to pressure (Pressure in pa)
    • 2a6e decodes to temp (Temperature in C)
    • 2a6f decodes to humidity (Humidity in %)

    So for instance if you want to show battery as well as button presses, you could do:

    setInterval(function() {
      NRF.setAdvertising({
        0xFFFF : [pressCount],
        0x180f : [E.getBattery()]
      });
    }, 60000); // update once a minute
    

    You can also make your own by modifying EspruinoHub at https://github.com/espruino/EspruinoHub/­blob/master/lib/attributes.js#L38

    There's another option mentioned in https://github.com/espruino/EspruinoHub#­advertising too, which is actually much nicer and tidier - you advertise JSON:

    var data = {a:1,b:2};
    NRF.setAdvertising({},{
      showName:false,
      manufacturer:0x0590,
      manufacturerData:JSON.stringify(data)
    });
    

    The only thing to watch out for there is you don't want your JSON string to get too long, since there's a ~20 byte limit on the size of the data you can advertise.

    As an example though, if you want to broadcast door openings then you can look at the code to measure when the door is open at http://www.espruino.com/Puck.js+Door+Lig­ht

    var zero = Puck.mag();
    var doorOpen = false;
    function onMag(p) {
      p.x -= zero.x;
      p.y -= zero.y;
      p.z -= zero.z;
      var s = Math.sqrt(p.x*p.x + p.y*p.y + p.z*p.z);
      var open = s<1000;
      if (open!=doorOpen) {
        doorOpen = open;
        digitalPulse(open ? LED1 : LED2, 1,1000);
      }
    }
    Puck.on('mag', onMag);
    Puck.magOn();
    

    And where you know the door has changed state then you just update the information you advertise with setAdvertising - so for example the below updates battery level every minute AND also updates with the door open state.

    var advertise = {dr:0,bt:E.getBattery()};
    function updateAdvertising() {
      NRF.setAdvertising({},{
        showName:false,
        manufacturer:0x0590,
        manufacturerData:JSON.stringify(data)
      });
    }
    
    // update battery
    setInterval(function() {
      advertise.bt = E.getBattery();
      updateAdvertising();
    }, 60000); // update once a minute
    
    // update door
    var zero = Puck.mag();
    var doorOpen = false;
    function onMag(p) {
      p.x -= zero.x;
      p.y -= zero.y;
      p.z -= zero.z;
      var s = Math.sqrt(p.x*p.x + p.y*p.y + p.z*p.z);
      var open = s<1000;
      if (open!=doorOpen) {
        doorOpen = open;
        digitalPulse(open ? LED1 : LED2, 1,1000);
        advertise.door = open?1:0; // use numbers rather than true/false to save data length
        updateAdvertising();
      }
    }
    Puck.on('mag', onMag);
    Puck.magOn();
    

    Then on MQTT you should see the following:

    /ble/advertise/ma:c_:_a:dd:re:ss/dr -> 1/0 -> door open status
    /ble/advertise/ma:c_:_a:dd:re:ss/bt -> 0..100 -> battery level
    

    Hope that helps!

About

Avatar for Gordon @Gordon started