Trying to get my device working...

Posted on
  • Hi @Gordon, I've picked this up again after another interruption. Here's the situation: I have a device with my own custom 128-bit service UUID and characteristics. It is a connectable iBeacon. I can connect easily with LightBlue (iOS) and view/change characteristics. I'm trying to connect to a service and change a characteristic that is an LED (00=off, ff=on). The code below has worked exactly twice, and fails a lot more. Failures are IDE disconnect, and "Uncaught Error: Unhandled promise rejection: No Services found". I can't reproduce when it passes, but it seems to be right after I've loaded a new revision of espruino (the first time). Very frustrating... Any ideas on how to debug what's happening? Code below (you helped me get this fixed last week or two), along with LightBlue screenshots.

    var devices;
    NRF.findDevices(function(devices) {
      if (devices.length < 1) throw new Error("Nothing found!");
      devices[0].gatt.connect().then(function(­g) {
        gatt = g;
        return gatt.getPrimaryService("bbc21523-008e-fe­6b-28ee-d6a44f9cb5da");
      }).then(function(service) {
        return service.getCharacteristic("bbc21525-008e­-fe6b-28ee-d6a44f9cb5da");
      }).then(function(characteristic) {
        characteristic.writeValue(0xff);
      }).then(function() {
        gatt.disconnect();
        console.log("Done!");
      });
    }, 3000);
    

    2 Attachments

    • IMG_6205.PNG
    • IMG_6204.PNG
  • Oh @Gordon, forgot to add that I'm running 1v92.54 on the puck-js.

  • @Gordon trying another method, by mac address. Also disconnects from IDE without changing the characteristic:

    var gatt;
    NRF.connect("d4:df:0a:04:44:03").then(fu­nction(g) {
      gatt = g;
      return gatt.getPrimaryService("bbc21523-008e-fe­6b-28ee-d6a44f9cb5da");
    }).then(function(service) {
      return service.getCharacteristic("bbc21525-008e­-fe6b-28ee-d6a44f9cb5da");
    }).then(function(characteristic) {
      characteristic.writeValue(0xff);
    }).then(function() {
      gatt.disconnect();
      console.log("Done!");
    });
    
  • That's odd - do you see the LEDs blink on for a fraction of a second - which would be the sign of an error? Please could you also try the standard 1v92 release and see if that behaves any better?

    Also, please could you check Puck.getBatteryPercentage() just to make sure it's not a low battery issue?

  • Battery is 100%. I reloaded with 1v92 release and similar behavior: not finding the service sometimes, and sometimes hanging and dropping the IDE. I haven't caught the LED blinking, though sometimes I have to pull the battery to reset to get the IDE connected again. Is there a serial debugger operating that I can catch? Any other ideas?

  • @Gordon one thought: could there be an issue with the number of devices returning information (buffer overflows)? I have at least a dozen BLE devices in my environment. I'll try to find an isolated coffee shop (though lots of devices show up now) to test this theory, but thought I'd pass this on in case there is a way to increase resources.

  • Yes, there's a serial connection available that might help? info here: espruino.com/Puck.js#serial-c­onsole

    There shouldn't really be a buffer overflow - because data is stored in Espruino's static jsvars, it's pretty difficult to get it to properly overflow. I also have quite a few BLE devices here and have never encountered problems - but roughly how many devices do you think you see in range?

    And are you using just the code you posted, or is there more? I guess if you're using up most of the RAM there is a small chance that the code doesn't handle running out of memory while searching for devices properly?

    If programming/debugging by serial you should at least see any error messages if they appear though.

    Having said that, having lots of BLE devices within range could probably make your connection more unstable. It's basically just luck whether their transmissions overlap or not, so once you get to a certain amount of devices all together it can make communications very unreliable.

  • @Gordon I'll look into the serial console connection. I have 14-18 devices within range, but went to a coffee shop where I was able to reduce that to 6 (too many fitness devices) and had similar results. The connection works just fine with an iOS, MacOS using LightBlue and my custom apps, so this is an issue I'm having solely with these espruino-based devices. I've had noble-based javascript application work using raspberry pi as well. Bottom line, it isn't a technology issue IMHO.

    Any additional debugging thoughts from you (or the community) would be greatly appreciated. There are several reasons I'd like to get espruino working for this. THANKS!

  • Around 20 devices in range should really be ok, and 6 definitely - I have pretty much that many in range all the time. The IDE is running on Mac OS?

    Are you using literally just the code that you posted above, or is that part of a much larger project that you're uploading?

    Also, can you use that code to change a characteristic on a different BLE device? Could it be something specific about your custom device that is causing Puck.js problems?

    Final thing - you could try characteristic.writeValue([0xff]); instead of characteristic.writeValue(0xff);. It shouldn't make a difference but could be worth a try.

  • The IDE is running on MacOS, correct. The code I posted is exactly what I'm running on a completely reset and restored device (both with released and experimental Espruino). I'll try a different (128-bit) characteristic on another device, good suggestion, although LightBlue on both iOS and MacOS work just fine with it. I'll also try the more explicit representation you suggest.

    The truly maddening thing is that I've actually seen it work twice, but can't repeat it with any predictability! Thanks @Gordon!

  • Hi @Gordon,
    Tried your suggestions above to no avail. I have a nice test bench using two puck-js devices as follows: set up one using the LBS (LightButtonService) I published several months ago. Set up the other to find and set the LED. Here's the code; I've verified that the LBS is still working as advertised (pun intended).

    I get either no service found or an IDE disconnect, sometimes requiring I pull the battery to reset the (finder) device.

    Let me know if you get the same/different results, THANKS!

    LBS code:

    NRF.setAdvertising({}, {name: "Nordic_Blinky"});
    
    pinMode(D5, "output");
    pinMode(D6, "output");
    pinMode(D7, "output");
    pinMode(D31, "output");
    pinMode(D16, "input");
    
    NRF.setServices({
      "00001523-1212-EFDE-1523-785FEABCD123" : {
        "00001524-1212-EFDE-1523-785FEABCD123" : { // button
          value : 0x00, // optional
          broadcast : false, // optional, default is false
          readable : true,   // optional, default is false
          writable : false,   // optional, default is false
          notify : true,   // optional, default is false
          indicate : false,   // optional, default is false
        },
        
        "00001525-1212-EFDE-1523-785FEABCD123" : { // LED
          value : 0x00, // optional
          broadcast : false, // optional, default is false
          readable : true,   // optional, default is false
          writable : true,   // optional, default is false
          notify : false,   // optional, default is false
          indicate : false,   // optional, default is false
                onWrite : function(evt) {
            digitalWrite([D5,D6,D31,D7], evt.data[0]);
                }
        }
        
      }
    });
    
    setWatch(function() {
      NRF.updateServices({
      "00001523-1212-EFDE-1523-785FEABCD123" : {
        "00001524-1212-EFDE-1523-785FEABCD123" : {
          value : 0x01,
          notify: true
        }
      }
    });
    }, D16, {edge:"rising", debounce:50, repeat:true});
    
    
    setWatch(function() {
      NRF.updateServices({
      "00001523-1212-EFDE-1523-785FEABCD123" : {
        "00001524-1212-EFDE-1523-785FEABCD123" : {
          value : 0x00,
          notify: true
        }
      }
    });
    }, D16, {edge:"falling", debounce:50, repeat:true});
    

    Finder code:

    var devices;
    
    NRF.findDevices(function(devices) {
      if (devices.length < 1) throw new Error("Nothing found!");
      devices[0].gatt.connect().then(function(­g) {
        gatt = g;
        return gatt.getPrimaryService("00001523-1212-EF­DE-1523-785FEABCD123");
      }).then(function(service) {
        return service.getCharacteristic("00001525-1212­-EFDE-1523-785FEABCD123");
      }).then(function(characteristic) {
        characteristic.writeValue( [0xff] );
      }).then(function() {
        gatt.disconnect();
        console.log("Done!");
      });
    }, 4000);
    
  • Hi @Gordon- did you have a chance to try the example above? I'm curious if you (or others) repeat my issues and especially if there is a solution. Thanks!
    Bill

  • Not yet, it's on my list. After the week off it took me literally all day yesterday to catch up on emails/forum posts.

  • Just tried with 1v92 and looks ok to me - just gives me Uncaught Error: Unhandled promise rejection: No Services found or Uncaught Error: Unhandled promise rejection: undefined at line 7 col 29

    The undefined rejection is a bit weird, but I've done it a bunch of times now and I don't get a disconnect.

    The error is because I have a bunch of Bluetooth LE devices here and your code is trying to connect to the first one that's found, which is probably never the correct one.

    I changed your code to the following so that it works the same but searches for the correct device, re-uploaded, and the red LED comes on on the Nordic_Blinky device as expected.

    var devices;
    NRF.findDevices(function(devices) {
      var idx = -1;
      for (var i=0;i<devices.length;i++) {
        if (devices[i].name == "Nordic_Blinky")
          idx = i;
      }
      if (idx < 0) throw new Error("Nothing found!");
      devices[idx].gatt.connect().then(functio­n(g) {
        gatt = g;
        return gatt.getPrimaryService("00001523-1212-EF­DE-1523-785FEABCD123");
      }).then(function(service) {
        return service.getCharacteristic("00001525-1212­-EFDE-1523-785FEABCD123");
      }).then(function(characteristic) {
        characteristic.writeValue( [0xff] );
      }).then(function() {
        gatt.disconnect();
        console.log("Done!");
      });
    }, 4000);
    

    Are you really using all those other pins though? Is it actually a Puck.js with a whole bunch of wires attached, or some other device?

    So yeah, it works fine for me as long as it's connecting to the correct device. If you want to connect by name look at NRF.requestDevice where you can just specify a name rather than manually searching: espruino.com/Reference#l_NRF_­requestDevice

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Trying to get my device working...

Posted by Avatar for billsalt @billsalt

Actions