Here's my variation on the Controlling Other Pucks tutorial code. It does some console logging, plus keeps track of attempts, retries, etc. There's a setInterval that simulates a button press every 5 seconds. That can be commented out. What it shows, at least in my case, is that the connection from one puck to another only lasts about 70 seconds, even when being frequently used. Another thing that I did was add some basic retry logic. For each button 'press' the logic will try up to a total of 3 times to connect and toggle the other puck before giving up and calling it a failure. Also attached is a log file that shows my results over a few minutes. Very consistent disconnect, retry pattern.
var busy = false;
var connected = false;
var txCharacteristic = false;
var attempts = 0;
var retries = 0;
var failures = 0;
function writeToggle() {
attempts++;
return new Promise(function(fulfill, reject) {
var emsg = "did not find device";
if(!busy) {
if (!connected) {
busy = true;
// be sure to change the filter to match your puck!
NRF.requestDevice({ filters: [{ name: 'Puck.js d958' }] }).then(function(device) {
console.log("got device, connecting...");
return device.gatt.connect();
}).then(function(d) {
console.log("connected!");
emsg = "did not get service";
connected = d;
return d.getPrimaryService("6e400001-b5a3-f393-e0a9-e50e24dcca9e");
}).then(function(s) {
console.log("got service");
emsg = "did not get characteristic";
return s.getCharacteristic("6e400002-b5a3-f393-e0a9-e50e24dcca9e");
}).then(function(c) {
console.log("got characteristic");
emsg = "unable to write value";
txCharacteristic = c;
return txCharacteristic.writeValue("toggle()\n");
}).then(function() {
console.log("wrote toggle");
emsg = "did not disconnect";
}).then(function() {
digitalPulse(LED2, 1, 500); // light green to show it worked
busy = false;
console.log("success!");
fulfill();
}).catch(function(error) {
console.log(emsg + ": " + error);
digitalPulse(LED1, 1, 500); // light red if we had a problem
if (connected) {
connected.disconnect();
connected = false;
}
busy = false;
reject();
});
}
else {
emsg = "unable to write value";
txCharacteristic.writeValue("toggle()\n").then(function() {
digitalPulse(LED2, 1, 500); // light green to show it worked
busy = false;
fulfill();
}).catch(function(error) {
digitalPulse(LED1, 1, 500); // light red if we had a problem
console.log(emsg + ": " + error);
if (connected) {
connected.disconnect();
connected = false;
}
busy = false;
reject();
});
}
}
});
}
function success() { /*console.log("ok");*/ }
function retry() {
console.log("error, retrying...");
retries++; console.log("attempts: " + attempts + ", retries: " + retries + ", failures: " + failures);
return writeToggle();
}
function start() {
writeToggle()
.then(success, retry)
.then(success, retry)
.catch(function(error) {
console.log("oops, failed! " + error !== undefined ? error : "");
failures++;
});
}
setInterval(function() { start(); }, 5000);
setWatch(start, BTN, { edge:"rising", debounce:50, repeat: true });
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.
Here's my variation on the Controlling Other Pucks tutorial code. It does some console logging, plus keeps track of attempts, retries, etc. There's a setInterval that simulates a button press every 5 seconds. That can be commented out. What it shows, at least in my case, is that the connection from one puck to another only lasts about 70 seconds, even when being frequently used. Another thing that I did was add some basic retry logic. For each button 'press' the logic will try up to a total of 3 times to connect and toggle the other puck before giving up and calling it a failure. Also attached is a log file that shows my results over a few minutes. Very consistent disconnect, retry pattern.
1 Attachment