Microbit signal data out over bluetooth?

Posted on
of 2
/ 2
  • Hey, I'm trying to get into a bit of bluetooth LE. Whilst I've got a microbit with me I want to have it send out a value from an analog pin over bluetooth for anything to pick up. The part I'm confused about is I can't find anything the references using the microbits bluetooth at all.

    The espruino webIDE is the only thing I've seen use it. I can't find anything on the microbit.co.uk/app tools that reference bluetooth. I thought about looking at micropython but apparently they'll never do it;


    Perhaps I'm misunderstanding how BLE is supposed to work. Next I'm going to look at how the Web-IDE from NPM is talking to bluetooth for a bit of an example.

  • Luckily Espruino's a bit more memory efficient than MicroPython ;)

    I forgot to include the BLE API in the online reference. I've just updated it - the stuff you want is here.

    At the moment the support is limited to advertising data (ideally you'd be able to set proper GATT information), but it should be perfect for what you want.

    For example the following reports the current temperature:

    setInterval(function() {
        0x1809 : [0|E.getTemperature()]
    }, 30000);

    The UUID given should be one of the standard bluetooth ones at the moment.

    It's still a bit limited (it seems you can't send much advertising data at the moment before it runs out of memory, and I'm not sure why).

  • Just to add here, as I understand it you've got 2 main bits to Bluetooth Smart - Advertising, and GATT.

    Advertising is when a device automatically 'pushes' out data in an advertising packet every so often. So it says what it's name is, and what it supports - you don't need to transmit anything to it to get that information. That's what Espruino supports at the moment.

    For GATT, you have 'characteristics' - just a bunch of properties that can be read and set - and the other device has to be able to transmit data in order to use those. At the moment they're not exposed in Espruino (except the 2 that are needed to use Bluetooth as a serial port).

    They could be in the future (and will be) - it's just a matter of someone finding time to do it :)

  • That's an amazing amount of info Gordon thank you. I'm going to look around what I can and hopefully help.

    From reading the page I got the impression I still should be able to use the usb to program it. I tried the Should I start a new thread for this?

    I've been having trouble connecting to the microbit over USB once espruino is uploaded. I can see "espruino MICROBIT" listed in bluetooth on my phone. chrome on there can't connect to it though. I checked enable-web-bluetooth is enabled.

    Just tried the 1v85 espruino_1v85_microbit.hex from the downloads page zip and also espruino_1v85.63_microbit.hex from the master. Can see them on my phone but can't connect over usb. Chrome IDE normally and run as admin. unplugged/plugged in the bit too.

  • No problem... What OS are you using? I updated the docs recently: http://www.espruino.com/MicroBit

    Basically on Windows you need a driver for the serial port - but once that is installed, a port should appear just like with a normal Espruino board.

    But yeah, if that doesn't work, start a new thread - when we get it sorted the answer might help someone else (although if you can think of any additions to the Micro:bit page they'd really be appreciated!).

  • Yes it was the serial port driver. Wasnt expecting another driver after using arduinos and things. (Win 10 x64)

    I'm just working on trying to find the thing from my phone now the web Bluetooth api seems a bit restrictive.

    Doesn't your example need two pipes by the way?

  • Doesn't your example need two pipes by the way?

    No, it's a JS hack (I've changed it so it'll be more obvious in 1v86's documentation) - doing a binary OR (|) with 0 converts the number to an integer (it's like using Math.floor, but takes less space).

    I'm not 100% sure it's needed as it may happen automatically, but the number that is advertised via Bluetooth is an integer, not floating point.

    If you're working on other phone software for Bluetooth LE, you could take a look at using 'Nordic UART' - I think they have posted some example code. Basically then you can send JavaScript direct to Espruino - so if you want to show something on the LEDs you can literally just send show(0xFF)\n

  • Hi Jack,

    If you want to send JavaScript straight to the micro:bit from from your Android phone you can do that right now with the DroidScript plugin. I'm just making a few improvements to it right now actually (you can also read the console output from the micro:bit over BLE)

  • Hello guys.

    Im becoming crazy with this.
    I read your conversation about the GATT profile, where the BT is broadcasting data but on what way I can read it? I need to pair the computer? what software can I use to debug this broadcast? Cannot do a normal socket comunication in two ways?

    Upd: I started writing this one at lunch and now at night my microbit stop working (Had a issue since i bought it). Because I love JS and I want to start in this world, I though to buy a new board but im the worst person with electronic (Im front end dev). Your others board have the same API or are more flexible to do comunication with Bluetooth? Which one include BT?

  • The only BLE-capable board we do at the moment is Puck.js: https://www.kickstarter.com/projects/gfw­/puckjs-the-ground-breaking-bluetooth-be­acon

    It's not available right now, but will ship in December. Same API but it can do 'central' mode as well - so can initiate a connection to something else.

    In terms of BLE:

    Advertising you don't need to pair. The easiest I've found it to use noble and node.js. Example program for reading data here: https://github.com/sandeepmistry/noble/b­lob/master/examples/advertisement-discov­ery.js

    For GATT you have to pair (I think). Espruino implements a normal socket-style connection called 'Nordic UART' which it puts the JS REPL on (by default) - Nordic provide an Android app to use it, as do Adafruit, or you should find various different implementations of it around.

  • For anyone trying to follow this along:
    I ran this code on the microbit:

    setInterval(function() {
        0x180F : [88]
    }, 30000);

    And then installed nRF Connect for Mobile for Android. If I then scan for the microbit, it shows (in this case a fake) battery percentage of 88% (see included image). A lot of the other free BLE tools in the Play store failed to either find the microbit or to display the data.

    Note though that on iOS nRF Connect does not give the same result. In fact, it fails to show the data. I used Bluetooth Smart Scanner and LE Nearby to create the other two screenshots.

    p.s. the iOS screenshots were created today using different (still fake) battery percentage of 100% = 0x64

    Next challenge: have it broadcast Eddystone-URL, but I'll start a new topic for that.

    4 Attachments

    • 20160727_055435000_iOS.png
    • 20160727_055416000_iOS.png
    • 20160727_055354000_iOS.png
    • 20160725_185913919_iOS.jpg
  • Great - thanks for posting up!

    I think right now the fact that it also advertises the Nordic UART service means that there isn't room in the advertising packet for Eddystone, but if you post up the code I'll see about modifying the firmware to allow you to disable the UART service advertising (it just means that if you wanted to program it wirelessly, you'd have to use one of the proper Espruino tools that knew it was looking for an Espruino).

  • @Pierre, the following would appear to work:

      0xFEAA : [
      0x10, // frame_type (URL)
      0xEE, // the RSSI dBm we've measured for this beacon 1 meter away
      0x02, // 2 = http://
      112, 117, 99, 107, 45, 106, 115, 0 // puck-js.com (0 = .com)

    At least the Nordic tools pick it up as being an Eddystone beacon... However the Physical Web app doesn't seem to. I'm checking up on why, and I'll let you know what I find.

  • It just did on iOS though when I tested your code on my microbit....
    Checking Android..

    1 Attachment

    • 20160727_122414000_iOS.jpg
  • Strange. Not on Android in Physical Web.But it shows up in Beacon Scanner now on Android! :)

    1 Attachment

    • Screenshot_2016-07-27-14-29-05.png
  • I just heard it's because they recently changed to only allow HTTPS, so

      0xFEAA : [
      0x10, // frame_type (URL)
      0xEE, // the RSSI dBm we've measured for this beacon 1 meter away
      0x03, // 3 = https://
    }, {interval:100,discoverable:true,showName­:false}); 

    should work - but it still doesn't :(

  • p.s. I did create a new topic for this like you asked, but for now continuing here. Google Chrome on iOS is happy with it also and finds the beacon (see bottom right corner of the screen).

    1 Attachment

    • Foto 27-07-16 14 37 51.png
  • I know, sorry - I'd posted the first comment before you opened the new thread though :)

    So it looks like Android is very picky, and you have to specify the advertising data exactly. I've had to build a new version of Espruino that allows you to specify the entire advertising packet (hopefully a micro:bit build will be available in a half hour or so).

    Also, Android will silently refuse to recognise any non-HTTPS links, even if they're via goo.gl (which is HTTPS), so you're hit twice.

    This works though (with the new build)

    NRF.setAdvertising([0x03,  // Length of Service List
      0x03,  // Param: Service List
      0xAA, 0xFE,  // Eddystone ID
      0x13,  // Length of Service Data
      0x16,  // Service Data
      0xAA, 0xFE, // Eddystone ID
      0x10,  // Frame type: URL
      0xF8, // Power
      0x03, // https://
  • So that would be a build newer than 27-Jul-2016 14:45 ?
    (newer than espruino_1v86.148_efm32ggstk.hex)

  • Yes - so what's on http://www.espruino.com/binaries/travis/­master/ now should work.

  • And it does. Both on iOS and Android. Thank you for "fixing" this (making this feature available)!
    I'll try to do a screencast (in English) explaining how things need to be setup tonight/tomorrow.

    Buying custom beacons probably is cheaper than using a micro:bit, but if a school has them available, this is at least a fun project to doe.

    2 Attachments

    • Screenshot_2016-07-27-18-44-42-1.png
    • Screenshot_2016-07-27-18-37-31-1.png
  • Also, you can potentially do more fun things with it - for example it can use E.getTemperature() to read the temperature, and can then add #21 (for instance) to the end of the link.

    When you go to that website '#21' it could then report the temperature as 21 degrees C.

  • Also, if you do write it up, please could you mention the Puck.js KickStarter ? it's for this sort of thing, but much faster and with more memory.

  • Thanks! Looks great!

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

Microbit signal data out over bluetooth?

Posted by Avatar for JackBennett @JackBennett