Control a BBC micro:bit from Node.js using BLE and Haxe
These are the Haxe node.js externs from https://github.com/sandeepmistry/node-bbc-microbit and follows the api
The Micro Bit (also referred to as BBC Micro Bit, stylised as micro:bit) is an ARM-based embedded system designed by the BBC for use in computer education in the UK.
The board is 4 cm × 5 cm and has an ARM Cortex-M0 processor, accelerometer and magnetometer sensors, Bluetooth and USB connectivity, a display consisting of 25 LEDs, two programmable buttons, and can be powered by either USB or an external battery pack.[2] The device inputs and outputs are through five ring connectors that are part of the 23-pin edge connector.
Read more about Haxe
Make sure to follow the instructions for
noble
too.
npm install
haxelib git bbc-microbit https://github.com/MatthijsKamstra/hx-node-bbc-microbit
use this repo locally
haxelib dev bbc-microbit path/to/folder/src
or this git repo
haxelib git bbc-microbit https://github.com/MatthijsKamstra/hx-node-bbc-microbit
don't forget to add it to your build file
-lib bbc-microbit
I would suggest to us Visual Studio Code, it's a very nice editor and lets you set breakpoints in your js
file
But other code-editors may work as well!
create a (LED)pattern from an array
var X = [
1, 0, 0, 0, 1,
0, 1, 0, 1, 0,
0, 0, 1, 0, 0,
0, 1, 0, 1, 0,
1, 0, 0, 0, 1,
];
var buffer = new Buffer(new Pattern().createLedMatrixBuffer(X));
// follow example "LedPatternHx"
// or use a default Pattern
var buffer = new Buffer(new Pattern().createLedMatrixBuffer(Pattern.HEART));
Use string to listen to or EventName
import BBCMicrobit;
// strings will work
microbit.on('buttonAChange', function(value) {
console.log('\ton -> button A change');
});
// prevent spelling mistakes with EventName
microbit.on(EventName.BUTTON_A_CHANGE, function(value) {
console.log('\ton -> button A change');
});
Use the fixed values for AccelerometerPeriod Period
import BBCMicrobit;
// numbers will work
microbit.writeAccelerometerPeriod(160, function(err) {
console.log('\taccelerometer period set');
});
// prevent using wrong value use Period
microbit.writeAccelerometerPeriod(Period.NR_160, function(err) {
console.log('\taccelerometer period set');
});
- UART doesn't work: check api
- Light sensor doesn't work: it works with a trick read more but with this node.js code unable to access.
- The code is not written on the micro:bit, remove power (switch off) and the code is gone
- EventListeners examples doesn't work: the code is not uploaded to NPM yet (but if you want to use it read this)
After installing the dependencies via npm install
.
You need to update the node_modules/bbc-microbit
folder with two files:
And that will make that work.
I have created an issue "microbit.subscribeEvents" but it's not fixed yet.
most examples from https://github.com/sandeepmistry/node-bbc-microbit are "redone" in Haxe
- AccelerometerListener.hx
- ButtonListener.hx
- Compass.hx
- Discoverer.hx
- EventListener.hx // doesn't work out of the box (check this)
- LedPattern.hx
- LedPatternHx.hx
- LedPatternTest.hx
- LedText.hx
- MagnetometerBearingListener.hx
- MagnetometerListener.hx
- PinBlink.hx
- PinListener.hx
- TemperatureListener.hx
- https://microbit-playground.co.uk/howto/microbit-blink-led-example
- http://www.microbitlearning.com/page/5
- http://www.makerspace-uk.co.uk/a-selection-of-microbit-projects/
- https://learn.adafruit.com/adafruit-arduino-lesson-2-leds/resistors
- https://www.kitronik.co.uk/blog/bbc-microbit-kitronik-university/