Not a problem, I learned something while searching for info about MAC-addresses.
Especially for other Bluetooth- and JS-newbies like me, below skeleton JS-code can either show the value received, or a simple graph. Just remove/add comments at the show_data- or graph-lines. And here's the Arduino-code I'm using.
On the JS-side there's for sure room for improvement, one thing that puzzles me is this line g.drawString(" Batt:" + data, 1, g.getHeigth()/2); which should display Batt:nnn where nnn is the value without leading zeros. On the Bangle 2 display is though instead shown b:nnn, so instead of " Batt:" is " b:" shown (lowercase b). The value itself is shown correctly.
var gatt;
var history = new Float32Array(64);
g.clear();
// Load fonts
require("Font7x11Numeric7Seg").add(Graphics);
function graph(data) {
// quickly move all elements of history back one
history.set(new Float32Array(history.buffer,4));
history[history.length-1] = data;
//
g.clear();
// Draw Graph
var r = require("graph").drawLine(g, history, {
miny: 26,
axes : true,
gridy : 10,
title: "Battery"
});
// Label last reading
g.setFontAlign(1,-1);
g.drawString(Math.round(data), r.x+r.w, r.gety(data)+2);
// Update the screen
g.flip();
}
function show_data(data) {
var on = true;
g.clear();
g.setFont("7x11Numeric7Seg",3);
g.drawString(" Batt:" + data, 1, g.getHeigth()/2);
}
NRF.connect("MA:C-:AD:DR:ES:S! public").then(function(g) {
gatt = g;
return gatt.getPrimaryService(0x180F);
}).then(function(service) {
return service.getCharacteristic(0x2A19);
}).then(function(characteristic) {
characteristic.on('characteristicvaluechanged', function(event) {
console.log("battery: "+JSON.stringify(event.target.value.buffer));
show_data(event.target.value.buffer);
//graph(event.target.value.buffer);
});
return characteristic.startNotifications();
}).then(function() {
console.log("Done!");
});
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.
Not a problem, I learned something while searching for info about MAC-addresses.
Especially for other Bluetooth- and JS-newbies like me, below skeleton JS-code can either show the value received, or a simple graph. Just remove/add comments at the
show_data
- orgraph
-lines. And here's the Arduino-code I'm using.On the JS-side there's for sure room for improvement, one thing that puzzles me is this line
g.drawString(" Batt:" + data, 1, g.getHeigth()/2);
which should displayBatt:nnn
where nnn is the value without leading zeros. On the Bangle 2 display is though instead shownb:nnn
, so instead of " Batt:" is " b:" shown (lowercase b). The value itself is shown correctly.