This project turns a BBC micro:bit into a versatile Bluetooth Low Energy (BLE) device that can be controlled from a web browser. It allows a web-based client to interact with the micro:bit's sensors and actuators.
Try it out with the companion web app: https://wongfei2009.github.io/microbit-webble-p5js/
- Read data from the accelerometer, magnetometer, and temperature sensor.
- Check the state of the A and B buttons.
- Control the 5x5 LED matrix.
- Communicate with the micro:bit over a UART (serial) connection.
- Continuously sends ambient light level readings over the Bluetooth UART connection.
- A music player that can play tunes sent from the connected web client.
The micro:bit runs a program that initializes a number of Bluetooth services. A connected device can then use these services to interact with the micro:bit.
A connected client can send music to the micro:bit in chunks over the UART connection. The protocol is as follows:
- A message with a
MUSIC:prefix is sent to set the tempo. - The music data is sent in one or more messages with a
CHUNK:prefix. - The last chunk must contain the string
|ENDto signify the end of the music data. - The micro:bit assembles the chunks and then uses a custom library to parse the music string and play it.
This repository can be added as an extension in MakeCode.
- open https://makecode.microbit.org/
- click on New Project
- click on Extensions under the gearwheel menu
- search for https://github.com/wongfei2009/ubitwebblejs and import
- Node.js (v14 or higher)
- npm (comes with Node.js)
- Clone the repository:
git clone https://github.com/wongfei2009/ubitwebblejs.git
cd ubitwebblejs- Install PXT command-line tools and set up the micro:bit target:
npm install -g pxt
pxt target microbit- Install project dependencies:
pxt installTo build the project and generate the .hex file:
pxt buildOr use the Makefile:
make buildThe compiled files will be generated in the built/ directory. The main file is built/binary.hex.
Option 1: Automatic deployment (if micro:bit is connected via USB):
pxt deployOr use the Makefile:
make deployOption 2: Manual deployment:
- Connect your micro:bit to your computer via USB
- The micro:bit will appear as a USB drive named "MICROBIT"
- Copy the
built/binary.hexfile to the MICROBIT drive - The micro:bit will automatically flash the new program
- This project uses Bluetooth services, which are incompatible with the Radio module in MakeCode
- Make sure only Bluetooth (not Radio) is listed in the dependencies in
pxt.json - After flashing, the micro:bit will display "BLE" on the LED matrix when ready