workshop-nodeconfeu2018

Bluetooth Workshop

In this workshop we’ll figure out how to use Web Bluetooth to interact with other devices - but also how to use Espruino on embedded devices to develop our own Bluetooth-enabled hardware.

Step 1 - Connecting

The first step is to make sure you can connect to your Badge via Web Bluetooth. We’ll do this with the Espruino IDE - once connected you’ll be able to fully reprogram your badge if you want to.

Backlight Hacking

Now you’re connected, you could try customising your Badge’s backlight…

Badge.patterns.green=()=>{
  var n=0;
  return [()=>{
    n+=50;
    if (n>1536)n=0;
    NC.ledTop([0,Math.max(255-Math.abs(n-1024),0),0]);
    NC.ledBottom([0,Math.max(255-Math.abs(n-1384),0),0]);
    NC.backlight([0,Math.max(255-Math.abs(n-640),0),0,
                  0,Math.max(255-Math.abs(n-512),0),0,
                  0,Math.max(255-Math.abs(n-384),0),0,
                  0,Math.max(255-Math.abs(n-256),0),0]);
  },50];
};
// Run the pattern right now
Badge.pattern("green");

So what’s going on? The badge’s lights are controlled by arrays of blue, green, red values, all of which are between 0 and 255.

ledTop and ledBottom are just one colour, so take 3 elements.

However the LCD backlight has 4 separate LEDs, so it has 12 elements, [B1,G1,R1, B2,G2,R2, B3,G3,R3, B4,G4,R4] for the 4 LEDs.

require("Storage").write(".boot0",`
Badge=global.Badge||{};
Badge.patterns=Badge.patterns||{};
// --------------------------------- Your code
Badge.patterns.green=()=>{
  var n=0;
  return [()=>{
    n+=50;
    if (n>1536)n=0;
    NC.ledTop([0,Math.max(255-Math.abs(n-1024),0),0]);
    NC.ledBottom([0,Math.max(255-Math.abs(n-1384),0),0]);
    NC.backlight([0,Math.max(255-Math.abs(n-640),0),0,
                  0,Math.max(255-Math.abs(n-512),0),0,
                  0,Math.max(255-Math.abs(n-384),0),0,
                  0,Math.max(255-Math.abs(n-256),0),0]);
  },50];
};
Badge.defaultPattern = "green";  // <--------- pattern name here
`);

For making badge apps, and other stuff, check out the Nodeconf EU Badge’s documentation and examples.

Other stuff…

If you’re interested in playing with Espruino further…

Web Bluetooth Steps…

If the IDE at http://www.espruino.com/ide worked for you then great, you’re sorted!

If it didn’t and it couldn’t be made to work, but you can get Node.js and Noble working then you can use the Web Bluetooth wrapper for Node to follow along with more or less the same code (you can also use the Espruino command-line tools to communicate with your badge.

Step 2 - Advertising