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+Light
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
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.
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:
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:So for instance if you want to show battery as well as button presses, you could do:
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:
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+Light
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.Then on MQTT you should see the following:
Hope that helps!