Skip to content

Instantly share code, notes, and snippets.

@ihewitt
Last active March 30, 2022 10:48
Show Gist options
  • Save ihewitt/10a81c4bc98346b98cf4db2770b6aa15 to your computer and use it in GitHub Desktop.
Save ihewitt/10a81c4bc98346b98cf4db2770b6aa15 to your computer and use it in GitHub Desktop.
var serial = "XX9999"; //<-- Put serial number to pair here
var device = "";
//now get advertised packets
var dt;
function scanForDevices() {
NRF.findDevices(function(devs) {
devs.forEach(function(dev) {
g.clear();
dt = dev.manufacturerData;
v = new DataView(new Uint8Array(dt,0,12).buffer);
epoch=946684800-28800; //off by 8hrs
timestamp = new Date((v.getUint32(2,true)+epoch)*1000); //Date [2-5]
age = parseInt(dt[1] /6); //Sample age [1]
sens_no = dt[8]; //Sensor count
phase = dt[9]; //State?
/* Doesn't seem to start at zero on a new sensor after all.
if (state == 0) {
console.log("Fresh sensor");
} else if (state == 1) {
console.log("Linking");
} */
// Running
life = 300 * (v.getUint16(6,true)); //Age of sensor
span = 15 - life / (60 * 60 * 24);
unk = dt[11]; //unknown?
glu = dt[10]/10; //gluc mmol
mg = glu*18;
if (phase == 4) { // countdown phase
time = dt[10];
console.log("Warming " + (59 - time) + " mins");
}
if (phase == 0xc) { // Stabilising
run = "stabilizing";
}
console.log(dt);
console.log("Date: " + timestamp.toISOString() +
" Days: " + parseInt(span) +
" Gluc: " + glu +
" Age: " + age);
g.setFont("6x8:4").drawString(age+" min", 0,50);
g.setFont("6x8:4").drawString(glu+" mmol",0,90);
g.setFont("6x8:4").drawString(mg+" mg",0,130);
});
}, {timeout: 2000, //Check for a couple of seconds
filters: [{id: device}]});
}
//Encode serial.
var ser = new Uint8Array([serial.charCodeAt(0)-55, //uppercase to 10 on
serial.charCodeAt(1)-55,
serial[2], //Numerals direct
serial[3],
serial[4],
serial[5]]);
NRF.setScan(function(d) {
if (d.name == 'AiDEX') //Is it a transmitter
{
var sens = new Uint8Array(d.manufacturerData, 0, 6);
if (sens == ser) // Find serial
{
device=d.id;
console.log("Device is: "+ device);
NRF.setScan(); //stop
setInterval(scanForDevices, 60000);
scanForDevices();
}
}
}, { active: true } );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment