Adventures with microbits

It’s the Raspberry Pi Competition with PA Consulting again and this year’s topic is about healthcare and well-being.

The students who want to do the competition have been talking about sensors to read heart rate, step count, crash detection. We are constrained by budget (there are great components specifically for the Pi we can use – like the senseHat) but we have lots of microbits at school – I wondered if we could use them to record something like step count and then send that data over radio?

Idea sketched out on envelope!

The Microbit 1 code was easy enough:

 

The icons aren’t strictly necessary but give the user a bit of feedback that the code is working. This would be running on the users microbit that would be powered by a battery pack.

Microbit 2 provided to be a trickier problem. I know Micropython can be run directly on the Microbit. However I came to the conclusion that it would be impossible/tricky to get data out of it and into the Pi. Yes there are posts on the web about people who do it via the serial interface, but I felt this would have been too tricky to explain to my Y7-11 students.

Then I discovered bitio (https://github.com/whaleygeek/bitio ) a library that allows you to connect to your attached microbit using Python, thus allowing you to run the full set of Python commands (who knew MicroPython has different implementations of how you access and write to a file?)

Steps:

  • Download the code from git hub and Unzip the file locally
  • Drag bitio.hex to your microbit – wait till it updates and you should see the bitio logo on it. (I discovered a problem with dragging things to my microbit from Windows pcs – and ended up having to use this cmd to ensure it does it properly :
robocopy /z . D:\ bitio.hex

  • Try running some of the example programs in the included src folder to connect your microbit first

I then updated the version of radio.py after much faffing about to this:

This is running on Thonny on my computer (haven’t even started thinking about putting it on my Pi yet!)

As you can see it is saving all button presses/shakes on the remote microbit to a text file – “received.txt”

The easiest way I could see of people seeing these inputs was to make a little Flask webpage on my computer (will be Raspberry PI) that just reads in this text file and puts it on the screen. The code is below and it’s running in Pycharm (so yes I have two Python programs running at once)

With the index.html here:

As you may have spotted the page refreshes every 2 secs to get any updated data – this could probably be done better, maybe using Ajax?

Next steps?

Make it more useful I suppose!

  • For example – use a basic step counter program on microbit 1 to record all your steps for a day.
  • Then on pressing of a button it is broadcast to microbit 2.
  • Microbit 2 could be listening out for each step count for the member of each person in the family for example
  • And then the website could display the stats for each, so you could have a race between each member of the family?
  • Not sure that having a local text file is the best way to do this, perhaps I should be posting it to a public website using the requests module.

Again not sure how much this is using a Raspberry pi as this is mostly leveraging the power of microbits! But it’s a start….