Hello. Sign in to get personalized recommendations. New customer? Start here.

Hi!   Sign in    Register

Home > life > Wireless Expert > Developing Applications for the BBC micro:bit

Developing Applications for the BBC micro:bit

2016/7/18 11:40:34     Source: www.bluetooth.com     Views:1913     Comments:0

Summary:bit

In Part 1 of my BBC micro:bit series, I explained the background of the BBC micro:bit, a remarkable programmable device which will inspire school children to get creative with coding and making things. I also shared a quick look at its architecture and main features.  In Part 2, I gave a guided tour of the micro:bit’s default Bluetooth® profile. In this final blog post, I’ll be examining ways in which you can create code for the micro:bit, and how you can write code for other devices which allow them to interact with the micro:bit over Bluetooth.

Take Your Pick

One of the things I think sets the micro:bit apart from other comparable devices for the education sector is the fact there’s a significant collection of tools and languages available to the budding programmer. The fact that this has been accomplished in such a short time is testimony to the level of support there is out there from all sorts of organisations and technical communities.

There are several development tools for the complete beginner and I’ll discuss them next. But there are also approaches to developing for micro:bit which, coupled with the fact that its runtime is open source, mean you can get in very deep and learn anything and everything about what makes this device tick. There’s a joke about timers in there somewhere. I’ll leave that one to you.

This means the barrier to entry for becoming a micro:bit developer is very low but the ceiling governing how much you can learn is very high. Want to know how multiplexing and persistance of vision is used to make the micro:bit 5 x 5 LED display work? It’s all there in the runtime code and it’s described in Lancaster University’s excellent documentation.

Development Tools for Beginners

The BBC provided a collection of online development tools for beginners. I’ll discuss them shortly. Since the BBC’s site was launched, Microsoft has been busy creating their own micro:bit development site and I’ve been working with them to provide comprehensive capabilities for developers who want to make use of Bluetooth on the micro:bit.

codethemicrobit.com

The new Microsoft site is www.codethemicrobit.com and, in my opinion, it is by far the best of the online micro:bit development tools for beginners and the best tool if you want full access to the micro:bit’s Bluetooth profile.

It provides a visual programming, drag-and-drop tool involving building application logic by arranging graphical programming blocks representing predefined, parameterised functions. And it’s very easy to use.

To use Bluetooth in your micro:bit application you first need to add the Bluetooth package to your project using the menu More/Add Package.

Figure 1 – Adding the Bluetooth Package to a codethemicrobit Project

Once this is done, you have a complete collection of Bluetooth related ‘blocks’ with which to build the Bluetooth capabilities of your application. Many are very simple and merely result in a given Bluetooth service being available at run-time. But there are also blocks which provide access to Bluetooth connect and disconnect events and the Bluetooth UART service can be used to transmit arbitrary data to a connected peer or to receive data from a peer device.

Figure 2 – The codethemicrobit Bluetooth Package

If you read part 2 of my micro:bit blog series you’ll probably spot that there are a few services missing from the list of blocks above. For historical reasons relating to the BBC requirements, the Device Information, DFU and Event services are currently included automatically as long as you use at least one of the other services from the Bluetooth blocks shown above. We still have some work to do and the aim is for all Bluetooth services to have their own blocks in the codethemicrobit.com Bluetooth package so that you have complete control over exactly what Bluetooth capabilities your micro:bit code has.

To illustrate the power of codethemicrobit.com, here’s an application which implements the micro:bit side of my Animal Vegetable Mineral guessing game which you can see on our micro:bit page or inYouTube. It took only 10 minutes to create.

\

Figure 3 – micro:bit Animal, Vegetable, Mineral Guessing Game

I think Microsoft’s codethemicrobit.com is just fantastic and the team behind it has done an amazing job. It’s easy and it’s flexible, most importantly, for Bluetooth developers there are only a few constraints regarding what you can or cannot do.

microbit.co.uk

The BBC’s online tools for the beginner are all available at www.microbit.co.uk . Two are from Microsoft and are called “Blocks” and “Touch Develop” (or “TD” for short). The third is from a company called Code Kingdom and I’m informed it sits on top of Microsoft’s TD APIs.

All three of these tools are suitable for beginners. Blocks is similar to Microsoft’s tool at codethemicrobit.com but does have a number of limitations, including with respect to Bluetooth.

Figure 4 – The Microsoft Block Editor

TD and the Code Kingdom editors use scripting but within what I can only describe as an intelligent user interface which guides and assists. They best way to get a feel for this is to give it a try.

Figure 5 – Touch Develop and the Code Kingdoms Editors Side by Side

Even with these three tools for beginners you can create Bluetooth applications. You are limited to a range of predefined use cases designed to be used in conjunction with the official micro:bit Android application from Samsung but you can still do some very cool things. These limitations were implemented to ensure things stay as simple as possible for the novice developer whilst introducing them to the world of wireless communications. There are other tools for those who want to do more and I’ll cover those shortly.

The Samsung Android application allows developers to write code and flash it over Bluetooth to their micro:bit all without touching a PC of any sort. You can code on your phone or tablet which is a fantastic capability. Microsoft’s Blocks’ editor seems particularly well suited for this. Here’s a video of me programming my micro:bit to be a music player Bluetooth remote control. Watch just how easy and gratifying this is.

Taking It to the Next Level

For more ambitious developers wishing to take complete control of their micro:bit, including its extensive Bluetooth capabilities, programming in C/C++ using either the web-based ARM mbed editor or it’s offline cousin, Yotta is the way to go. The idea of using C/C++ may be a little intimidating for some relatively new developers but I have to say, the micro:bit C/C++ APIs are amongst the nicest and easiest to use C/C++ APIs I’ve ever seen.

For example, indicating you want your application to include the Bluetooth magnetometer service so you can use your micro:bit as a digital compass, requires only one line of code.

new MicroBitMagnetometerService(*uBit.ble, uBit.compass);

Scrolling some text across the LED display is similarly simple:

uBit.display.scroll(“Bluetooth!”);

There are lots of code examples for the C/C++ micro:bit developer here. I recommend looking at how I programmed my micro:bit to let me remote control a two-wheeled buggy.

Honourable Mention

The range of tools and programming languages is growing all the time. Here are a few others I’ve found that you might find it useful to know about.

  • Python developers will be delighted to know there’s a micropython interpreter which lets you run Python code on the micro:bit. Unfortunately, there’s no access to Bluetooth from Python due to memory constraints but for Python fans it’s great that it’s an option
  • Espruino lets you program your micro:bit using JavaScript and supports Bluetooth. I’ve not played with it yet but I hear good things
  • Tickle will support BBC micro:bit soon

Client Applications

To use Bluetooth with a micro:bit you need something else which will connect to the micro:bit and act as a client. Pretty much every smartphone platform on the planet now has native APIs for using Bluetooth low energy technology from an application, so take your pick. If developing native applications in Java, C/C++ or Swift doesn’t sound like your idea of fun, then maybe you’d prefer to use web technologies. There are a variety of SDKs that let you create cross-platform mobile applications using HTML5, JavaScript and CSS out there such as Apache Cordova.

Evothings

I recently started to use a development tool from Evothings which lets me code using HTML5 and JavaScript, has a nice Bluetooth JavaScript API appropriately named “easyble” and includes a series of complete applications specifically for micro:bit which use the micro:bit default Bluetooth profile. It’s easy to learn and lets you create standalone applications for iOS, Android and Windows all from one set of code.

What I really like about Evothings is how quick and easy it is to edit, deploy and test your application as you progress, with the simple act of saving a source file resulting in the latest code being automatically uploaded to your smartphone for testing in the Evothings Viewer application.

app.startScan = function()
{
evothings.easyble.startScan(
function(device)
{
// Connect if we have found an Microbit.
if (app.deviceIsMicrobit(device) && app.notDiscovered(device.address))
{
devices.push(device)
app.showDiscoveredDevice(device.address, device.name)
}
},
function(errorCode)
{
console.log(‘Error: startScan: ‘ + errorCode + ‘.’);
});};

Figure 5 – Scanning for micro:bits Using the Evothings Easyble API from JavaScript

Onwards and Upwards!

One million BBC micro:bits were given to 11-and 12-year old schoolchildren in the UK to inspire them to get creative with coding. The micro:bit is now available for purchase in the UK and, with a bit of luck, we’ll see it spread its wings to the rest of the world before long. As I hope this series has illustrated, it’s a great device to learn about coding and making and with an extensive choice of tools and Bluetooth on-board, I’m expecting micro:bit to be everywhere in no time at all.

(Credit: Martin Woolley)


About Product
An Intro to Blu

Bluetooth® technology is one...[More]

Bluetooth SIG M

The introduction of Bluetoot...[More]