You are reading a single comment by @dklinkman and its replies. Click here to read the full conversation.
  • This is an updated function. Added 2 lines to the 2nd catch. It handles the disconnected case better. You'll still get a red blink because the write fails. But on next press it will try and reconnect and you'll be back in business.

    // Function to call 'toggle' on the other Puck
    function sendToggle() {
      if (!busy) {
        busy = true;
        if (!connected) {
          NRF.requestDevice({ filters: [{ name: 'Puck.js d958' }] }).then(function(device) {
            return device.gatt.connect();
          }).then(function(d) {
            connected = d;
            return d.getPrimaryService("6e400001-b5a3-f393-e0a9-e50e24dcca9e");
          }).then(function(s) {
            return s.getCharacteristic("6e400002-b5a3-f393-e0a9-e50e24dcca9e");
          }).then(function(c) {
            txCharacteristic = c;
            busy = false;
            // Now actually send the toggle command
            sendToggle();
          }).catch(function() {
            if (connected) connected.disconnect();
            connected=false;
            digitalPulse(LED1, 1, 500); // light red if we had a problem
            busy = false;
          });
        } else {
          txCharacteristic.writeValue("toggle()\n").then(function() {
            digitalPulse(LED2, 1, 500); // light green to show it worked
            busy = false;
          }).catch(function() {
            if (connected) connected.disconnect();
            connected=false;
            digitalPulse(LED1, 1, 500); // light red if we had a problem
            busy = false;
          });
        }
      }
    }
    
    
About

Avatar for dklinkman @dklinkman started