Tuesday, 7 February 2017

Pairing a BBC micro:bit with a Raspberry Pi using BlueZ

A Raspberry Pi 3 has Bluetooth low energy built in and a Raspberry Pi 2 can have a Bluetooth USB dongle plugged into it to give it Bluetooth capabilities. BlueZ, the Bluetooth stack for Linux needs then to be installed. Once done, you have a Bluetooth enabled Raspberry Pi.

But what about your BBC micro:bit? Well, the easiest way to use your Pi with your micro:bit is to install a hex file whose settings do not require it to be paired to another device before it can be used. PXT lets you pick whether or not pairing is required and if so, whether you want to use "passkey pairing", where you enter a 6 digit number displayed by the micro:bit or "just works" pairing, where all you need to do is initiate the pairing process, and the rest happens by magic.

If you opt to use passkey pairing, it's possible to have BlueZ pair your micro:bit with your raspberry Pi. After that, any communication with the micro:bit from the Pi will use that pairing information and where necessary, data will be encrypted.

Here's an example of me pairing my Raspberry Pi with my micro:bit using BlueZ from a Linux terminal session:


pi@raspberrypi:~ $ sudo hciconfig hci0 down
pi@raspberrypi:~ $ sudo hciconfig hci0 up
pi@raspberrypi:~ $ sudo bluetoothctl
[NEW] Controller B8:27:EB:C5:E9:31 raspberrypi [default]
[NEW] Device D0:F5:DF:C0:AE:95 BBC micro:bit
[NEW] Device 90:03:B7:C9:9C:D8 Flower power 9CD8
[bluetooth]# scan on
Discovery started
[CHG] Controller B8:27:EB:C5:E9:31 Discovering: yes
[CHG] Device D0:F5:DF:C0:AE:95 RSSI: -61
[CHG] Device D0:F5:DF:C0:AE:95 Name: BBC micro:bit [vuzig]
[CHG] Device D0:F5:DF:C0:AE:95 Alias: BBC micro:bit [vuzig]
[CHG] Device 90:03:B7:C9:9C:D8 RSSI: -80
[bluetooth]# paired-devices
Device D0:F5:DF:C0:AE:95 BBC micro:bit [vuzig]
[bluetooth]# remove D0:F5:DF:C0:AE:95
[DEL] Device D0:F5:DF:C0:AE:95 BBC micro:bit [vuzig]
Device has been removed
[bluetooth]# agent KeyboardDisplay
Agent registered
[NEW] Device D0:F5:DF:C0:AE:95 BBC micro:bit [vuzig]
[CHG] Device 90:03:B7:C9:9C:D8 RSSI: -89
[bluetooth]# pair D0:F5:DF:C0:AE:95
Attempting to pair with D0:F5:DF:C0:AE:95
[CHG] Device D0:F5:DF:C0:AE:95 Connected: yes
Request passkey
[CHG] Device D0:F5:DF:C0:AE:95 UUIDs: 00001800-0000-1000-8000-00805f9b34fb
[CHG] Device D0:F5:DF:C0:AE:95 UUIDs: 00001801-0000-1000-8000-00805f9b34fb
[CHG] Device D0:F5:DF:C0:AE:95 UUIDs: 0000180a-0000-1000-8000-00805f9b34fb
[CHG] Device D0:F5:DF:C0:AE:95 UUIDs: e95d93af-251d-470a-a062-fa1922dfa9a8
[CHG] Device D0:F5:DF:C0:AE:95 UUIDs: e95d93b0-251d-470a-a062-fa1922dfa9a8
[CHG] Device D0:F5:DF:C0:AE:95 Appearance: 0x0200
[agent] Enter passkey (number in 0-999999): 959145
[CHG] Device D0:F5:DF:C0:AE:95 Paired: yes
Pairing successful
[CHG] Device D0:F5:DF:C0:AE:95 Connected: no

I've highlighted the text I entered in red. Initially, I brought the Bluetooth adapter's HCI (Host Controller Interface) down then up. This is a handy way to reset it and in my experience, this is sometimes necessary.

I then put my micro:bit into pairing mode.

Having done so, I launched "bluetoothctl", a utility that lets me enter various commands.

Once started, I ran "scan on" to start BlueZ scanning for other devices, and as you can see, it lists the Bluetooth devices that it finds, including a micro:bit.

I then ran "paired-devices" to see what devices my Pi was already paired with. It listed my micro:bit so to start with I removed that pairing. You should always do this if you've installed a new hex file on the micro:bit since this will have caused any previous pairing information to be lost from the micro:bit and the micro:bit and the device it is paired with must both have the same pairing information.

Then I specified "agent KeyboardDisplay". This tell the BlueZ to inform other devices that it is a device with both a keyboard and a display, during the pairing process. The point here is that during pairing, the two devices exchange information about each other's IO capabilities and this is used to determine what the best way to go about pairing might be. If neither device had a keyboard, for example, there would be no point trying to perform passkey pairing.

After that, I initiate pairing with the MAC address of my micro:bit as an argument. Pairing proceeds and the micro:bit display responds with an arrow on its display, pointing at button A. I pressed button A and was prompted on the Pi to enter the passkey displayed on the micro:bit.

And that's all there is to it. There's even less to do if you load a hex file configured for "just works" pairing.


Thursday, 2 February 2017

BETT 2017

Bett is a large conference and exhibition focused on technology and education. It has a very international attendance. I was there this year for the second time and spoke to people from various European nations as well as from the USA, Singapore and Hong Kong.

micro:bit featured all over the place as well as of course in the micro:bit Education Foundation's area.

Microsoft were everywhere as well! Rightly so. I love what Microsoft have done for developers with PXT, in particular.

Perhaps best of all, I got to see the Bloodhound project for schools going through its paces. They had an awesome set up, allowing two rocket cars to be raced inside a perspex safety "tunnel" with accelerometer data streamed from their embedded micro:bits over Bluetooth to tablets running my Bitty Data Logger application. Wonderful!



I gave a presentation, both on Wednesday and Thursday, about the opportunity afforded by the emerging Internet of Things and how micro:bit and Bluetooth are providing a great vehicle for educators to prepare youngsters for the Age of IoT when they leave the education system. You can see highlights of the presentation in this short video:


Thursday's presentation was very well attended, with standing room only, which is always very gratifying. And I got a "good job!" from none other than a teacher. That definitely made my day :-)

Wednesday, 21 December 2016

Bitty Software and Preparing Youngsters for the Age of the IoT

Bitty Software is the name I use for my micro:bit smartphone/tablet applications project. At the time of writing, there are 5 Bitty Software applications for each of iOS and Android in their respective app stores. In each case, micro:bit coding tutorials are available, the idea being that to use the smartphone applications, you have to program your micro:bit. For those who don't want to, or who are simply too impatient to get started, pre-built hex files are also available.

The idea behind Bitty Software was to create resources for education and fun which shine a light on communications technology and the micro:bit rather than just on coding. Bluetooth is of course my communications technology of choice.

Why emphasise communications? Well, given one of the primary goals of the micro:bit project is to educate youngsters in coding and making things and to prepare them for potentially becoming the next generation of technology entrepreneurs and leaders (or simply to make a living from their technical skills), communication between devices is an essential thing to know about, and here's why.

Goldman Sachs released a report in 2014 which discussed the Internet of Things (IoT). In that report, they characterised the internet as evolving through three major phases.

1. The 1990s. 1 billion devices are connected to the internet. These are largely desktop computers and servers. People use modems that make a quaint bleeping sound.

2. The 2000s. 2 billion devices are connected to the internet. This is the age of the smartphone.

3. The year 2020. It's forecast that by 2020 there will be <drum roll> 28 billion devices connected to the internet. This is a phenomenal figure. And those devices? Computers, smartphone and tablets. Obviously. But also light bulbs, manufacturing equipment, sensors of every conceivable type and much more besides.

The IoT is why youngsters need to be educated in communications as well as coding. At the "edge tier" of any IoT architecture, you'll generally find smaller devices, often equipped with microcontroller units (MCUs). The BBC micro:bit is an MCU. And they will very typically communicate wirelessly with each other and with the internet via entities known as "gateways".

There are various wireless technologies. Some of them are categorised as "low power wireless technologies" meaning that they use relatively small amounts of power to communicate data. Bluetooth low energy is one example and this is the Bluetooth technology on the micro:bit. Low power use is critical in many IoT scenarios. Consider a smart building. It can only be "smart" in any meaningful sense of the word if we have lots of data about its various aspects and from every nook and cranny of the building, let alone every room; temperature, light levels, water levels, security status of windows and doors, occupancy of rooms and so on. Data can be accrued, aggregated and analysed. It can form the basis of intelligent automation and reveal hitherto unknown things about the building (is this building energy efficient compared to other, similar buildings?) but this can only be achieved with the right type and quantity of data, acquired at the right time.

To obtain this magical data requires sensors. Lots of sensors. Everywhere. Placing sensors in wall spaces, attics and under the ground becomes untenable if they need their batteries changing every few weeks. Devices have to run for many years on a single battery or better still, require so little power that energy harvesting techniques which generate power from ambient light, temperature changes, mechanical vibration or naturally occurring radio in the environment, can be used instead of batteries.

Its ubiquity (over 3 billion Bluetooth devices shipped in 2015 alone), wide platform support, developer friendliness and very low power requirements have made Bluetooth the low power communications technology of choice for the IoT.

So those youngsters we have such high hopes for, they need to be immersed in communications as well as coding and learn about creating systems of devices, not just creating the code for one device.

Bitty Software and Bluetooth. Doing their bit. OK, I nearly finished on a pun there!


Wednesday, 16 November 2016

micro:bit support

The 'micro:bit foundation' has arrived and is charged with looking after the future of the micro:bit. It's also a great place to go for technical support. We've lacked a central place to find answers to questions or to get assistance until now, so this is a big step forwards.

Here are some handy support resources for you:

1. If you need help with a Bitty Software application or tutorial

Send a message to @bittysoftware on Twitter. If you can, post more extensive details on a blog or similar and include a link to these details in your tweet.

2. Anything else micro:bit related

Email help@microbit.org or go to http://support.microbit.org and open a support ticket.

That's it!

Tuesday, 15 November 2016

micro:bit Blue - open source at last!

Most of the micro:bit demos involving a smartphone shown on my micro:bit page involve an Android application I wrote and which I eventually decided to call "micro:bit Blue". It was initially put together to help me to test the Bluetooth profile I'd designed for the micro:bit. Then, as I started to have ideas, it transformed into a series of demos.

But I always had the aim that it be released as open source, so I also did my best to provide educational documentation in the helps screens so that people who are new to Bluetooth could get the hang of the primary concepts.

A few months ago I loaded the application into Google Play so that other people could find and install it easily.

 It's right here:

https://play.google.com/store/apps/details?id=com.bluetooth.mwoolley.microbitbledemo&hl=en_GB

Meanwhile, releasing the source code was delayed by a variety of rather uninteresting issues. And then some more. To cut a long story short and at the same time apologise for having taken so long over this, today I'm delighted to say that the source code is now available!

The new microbit foundation have kindly offered a home for the code and their home being more palatial than my own github home, I gratefully accepted their offer.

I hope those people who have been waiting for this are pleased and that everyone and anyone with an interest in writing smartphone apps for the micro:bit finds this useful.

Here it is:

https://github.com/microbit-foundation/microbit-blue

 Enjoy :-)

Thursday, 10 November 2016

Bitty Data Logger V1.2.0

Yesterday, Bitty Software released a new version of the Bitty Data Logger application. If you're unfamiliar with the app, check it out at the Bitty Software web site.

The new release makes the following improvements:

  1. A new "Results" screen lists up to 30 data files.
  2. Data can now be uploaded at any time.
  3. New "Extra Data" screen provides the opportunity to add a time and distance value after a test run has completed. A speed value is automatically calculated.
  4. The Activate / Deactivate button is now named Start / Stop and is coloured green and red.
  5. The Settings screen now allows a Project Name and a Team Name to be entered.
Bitty Data Logger is currently being evaluated for use in the Bloodhound model rocket car competition for schools, Race for the Line. This is of course, *very* exciting!
Bitty Data Logger is free and available for both iOS and Android. The code needed for the micro:bit is available as a hex file from the Bitty Software web site or you can create it yourself, with guidance available in the form of a couple of coding tutorials, one for C/C++ and one for the PXT tool.

The application is great for school or personal projects:
Spice up your science lessons with the @bittysoftware #microbit data logging app for Android & iOS: #edchathttps://t.co/zBFzku9G95 pic.twitter.com/w7Kgg5VHTE

Here are some screenshots of the new release. 

Ready to start data logging!

And we're off....

After capturing data we can record extra, optional data

Up to 30 data files are kept. NEW files have not yet been uploaded
After uploading, you can download to your computer from the URL provided

Data file details include the URL which was allocated when uploading

Tuesday, 1 November 2016

micro:bit proximity beacons

One the big growth areas for Bluetooth is in "beacons". Bluetooth beacons broadcast data which can be used by another device to deduce roughly where it is. This can be in terms of a particular location or perhaps in terms of proximity to an object of interest.

ABI predict that by 2020, there will be 400 million Bluetooth beacons shipping each year.

Beacons work by using Bluetooth "advertising" to broadcast a small amount of data, which can be received and acted upon by anyone in range with a suitable device and software, typically a smartphone and application.

There are various beacon message formats, which define the way Bluetooth advertising packets are used as containers for beacon data. iBeacon is Apple's beacon message format. Eddystone comes from Google.

Google have a very interesting project called the Physical Web and it in large part, is all about how Bluetooth beacon technology, coupled with software on smartphones and tablets, coupled with the web, can enable people to more easily discover and interact with physical things in the environment.

Believe me. This is going to be BIG.

I just helped add Eddystone beacon capabilities to the BBC micro:bit. Kudos to Thomas Beverly for having kicked things off by implementing support for Eddystone URL messages in the micro:bit's "runtime". I tagged along for the ride and added support for UID messages over the weekend and it's now pretty easy to use a micro:bit for a wide range of beacon scenarios.

I have a couple of videos on my Bitty Software web site. The first shows the UID Eddystone message type in use and the second uses the URL message type. Take a look at them now to get the idea, especially if you're not already familiar with beacons.

Don't forget, you don't need to make a Bluetooth connection to a beacon to use it... you just have to be near enough to it to receive its broadcast data and as I've noted elsewhere in this blog, micro:bits can have a range of hundreds of metres when using Bluetooth at full power. 

So you could place micro:bit beacons all over (say) your school, and with a suitable application on phones and tablets, provide information pertinent to each location or some other response, automatically on someone simply coming into range of the micro:bit's beacon broadcasts.

Right now, turning your micro:bit into a beacon requires you to do some C/C++ programming. I'm hoping though that Microsoft's awesome PXT tool will end up with a "Bluetooth Beacon Block" in the future so that making micro:bit beacons is as easy as can be.

The microbit-samples repo will soon have Eddystone beacon code examples in it and I've also submitted updates to the Lancaster University micro:bit documentation on the subject.

Meanwhile, to whet your appetite, here's some code:

#include "MicroBit.h"

MicroBit uBit;

char URL[] = "https://goo.gl/TlUTF7";
const int8_t CALIBRATED_POWERS[] = {-49, -37, -33, -28, -25, -20, -15, -10};

uint8_t advertising = 0;
uint8_t tx_power_level = 6;

void startAdvertising() {
    uBit.bleManager.advertiseEddystoneUrl(URL, CALIBRATED_POWERS[tx_power_level-1], false);
    uBit.bleManager.setTransmitPower(tx_power_level);
    uBit.display.scroll("ADV");
    advertising = 1;
}