Bangle & Arduino communication through Bluetooth?

Posted on
  • I'm expecting to receive a Nicla Sense ME within a couple of weeks, and thought that I'd try to send data from it to Bangle.js2 through Bluetooth, probably showing some dashboard or graphs on the watch. Here's the datasheet btw.
    Has anyone done something similar with any Espruino and Arduino device and be willing to share the code, especially the Arduino code?
    I'm aware of the tutorials, e.g. this one, and it seems quite easy to communicate between Espruino devices, might be easy on the Arduino side as well.

  • I haven't specifically done anything, but if you can configure the Arduino code to expose a Bluetooth characteristic then it should be trivial to handle on Espruino.

    Also worth adding that Espruino implements the Web Bluetooth API, so if you can find an example of using the Arduino code with Web Bluetooth then you can pretty much copy/paste the Web Bluetooth code onto Bangle.js and it should work

  • Thx @Gordon! Looking at this this tutorial I understand Web Bluetooth is supported. Until the device is delivered, I'll try communicating between a Wio Terminal (using Arduino) and Bangle.js2 to understand a bit more.

  • Ok, so now I've been able to transmit a value from Wio Terminal through Arduino with this code.
    Using nRF Connect I've verified that the value is updated frequently (see attached screenshot).

    I've tried to get my head around Bluetooth communication through this, this, and other BT-tutorials found at Espruino.com, but I'm still at loss how to connect from Bangle.js2 and read this simple value, so would appreciate any help.

    I purchased 3 Pucks around 6 years ago, and if I remember correctly it was very easy to connect them together through Bluetooth, but can't find that code now. Of course the current use case is not identical, so don't know how much it would help anyway.

    Edit: Can't delete an attachment (the huge picture), sorry for that


    2 Attachments

    • wio terminal ble.PNG
    • wio terminal ble 2.png
  • Which characteristic are you trying to read from? Battery level?

    I think what you need is startNotifications - there's a bit of code in the reference: http://www.espruino.com/Reference#l_Blue­toothRemoteGATTCharacteristic_startNotif­ications

    It looks like there might be a UART characteristic there, and to use that the code above can be used pretty much as-is.

    But for battery I think something like this might do it:

    var gatt;
    NRF.connect("pu:ck:js:ad:dr:es random").then(function(g) {
      gatt = g;
      return gatt.getPrimaryService(0x180F);
    }).then(function(service) {
      return service.getCharacteristic(0x2A19);
    }).then(function(characteristic) {
      characteristic.on('characteristicvaluech­anged', function(event) {
        console.log("battery: "+JSON.stringify(event.target.value.buff­er));
      });
      return characteristic.startNotifications();
    }).then(function() {
      console.log("Done!");
    });
    
  • Thx @Gordon!
    It was a pain in the... to find out the MAC address of Wio Terminal's Bluetooth chip, but I eventually found out that manufacturers often just increase the WiFi address with 1, which indeed worked here.
    Now at least I'm seeing the dummy value in the console, so at least have a starting point.

  • Great! You can use NRF.requestDevice to do it by name as well. Sorry - that might have been better ;)

  • 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(Graph­ics);
    
    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('characteristicvaluech­anged', function(event) {
        console.log("battery: "+JSON.stringify(event.target.value.buff­er));
        
        show_data(event.target.value.buffer);
        //graph(event.target.value.buffer);
      });
      return characteristic.startNotifications();
        
      }).then(function() {
      console.log("Done!");
    });
    
    
    
  • one thing that puzzles me is this line g.drawString(" Batt:" + data, 1, g.getWidth()/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).

    No problems when using the vector font though

  • Ahh - "7x11Numeric7Seg" is 'numeric' - so it only contains numbers and one or two other bits. The font map next to each font in https://www.espruino.com/Fonts#font-modu­les usually shows what's available

  • Well “numeric” in the font name should’ve told me something, glad to get it straightened out.

  • hi all, i am looking for a simple code to transmit the heart rate from the bangle to a shell via bluetooth (hs 05). can you help me?

  • Did you perhaps mean HC-05? That seems to be a classic Bluetooth device and not a BLE device. Bangle supports BLE, and I'm not sure if it's possible to communicate directly between classic and BLE devices.

  • ok thanks, need to find BLE device

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

Bangle & Arduino communication through Bluetooth?

Posted by Avatar for ThomasVikström @ThomasVikström

Actions