Thursday, 20 April 2017

Making the micro:bit accelerometer available over Bluetooth

Here's a very simple C/C++ application, created using the mbed web IDE.

The project was created in the mbed IDE with the following parameters:

Platform="BBC micro:bit", Template="An example of how to use the micro:bit DAL's abstract....." and Project Name=-"microbit-ble-accelerometer"

All it does is establish event handlers for Bluetooth connect and disconnect events and adds the Bluetooth accelerometer service so that an application like micro:bit Blue or Bitty Data Logger can receive accelerometer data over Bluetooth.

#include "MicroBit.h"

MicroBit uBit;

void onConnected(MicroBitEvent)
{
    uBit.display.print("C");
}

void onDisconnected(MicroBitEvent)
{
    uBit.display.print("D");
}

int main()
{
    // Initialise the micro:bit runtime.
    uBit.init();

    // Insert your code here!
    uBit.display.scroll("X");
    
    uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_CONNECTED, onConnected);
    uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_DISCONNECTED, onDisconnected);

    new MicroBitAccelerometerService(*uBit.ble, uBit.accelerometer);

    // If main exits, there may still be other fibers running or registered event handlers etc.
    // Simply release this fiber, which will mean we enter the scheduler. Worse case, we then
    // sit in the idle task forever, in a power efficient sleep.
    release_fiber();
}


I also set MICROBIT_BLE_OPEN to 1 in microbit-dal/inc/core/MicroBitConfig.h so that pairing is not required. Much easier for testing purposes.

#define MICROBIT_BLE_OPEN                       1

After compiling to produce a hex file, copy the hex file onto your micro:bit. An application like nRF Connect on a smartphone or tablet should see a Bluetooth service with UUID which starts 0xe95d0753-. This is the accelerometer service. Enabling notifications on the first characteristic (Accelerometer Data) will result in its value updating when you move the micro:bit.

The project has been published here:

https://developer.mbed.org/users/bluetooth_mdw/code/microbit-dal-ble-accelerometer-example/



2 comments :

  1. Hello Martin,
    It is a great blog. Thank you very much. I am just a newby for both micro bit and bluetooth low energy. I want to register for free fall event and send message to the central device using callback method vis BLE. Is it possible?It would be great if you can guide me on this matter. Thank you very much.

    uBit.messageBus.listen(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_FREEFALL, onFreeFall);

    Kind Regards,
    Onder

    ReplyDelete
  2. What type of message do you want to send? If all you want to do is indicate to the Central device that the micro:bit is in free fall, then I recommend using the Event Service and the Microbit Event characteristic. See https://lancaster-university.github.io/microbit-docs/resources/bluetooth/bluetooth_profile.html

    I don't know how often FREEFALL events are generated. If once when free fall starts (as opposed to every time the accelerometer is sampled), then you can simply have the event sent over BLE to your Central mode application by subscribing to it. You need the Event Service in your hex file and your central mode app would need to subscribe and then handle these events.

    The event service is included in hex files by default. See config.json. Your central mode application would need to connect and then write to the Client Requirements characteristic to subscribe to your FREEFALL events. It then needs to handle receiving micro:bit events as Bluetooth notifications.

    See https://lancaster-university.github.io/microbit-docs/ble/event-service/ for an example of a central mode app subscribing to certain micro:bit events and then receiving them as notifications.


    ReplyDelete