This project allows you to configure your micro:bit to work as a Bluetooth Low Energy (BLE) remote to control an instance of Spotify running on macOS with Node.js and AppleScript. Users can use micro:bit to perform several actions like play or pause tracks, turn volume up or down and go to next or previous track. To carry out this actions, a custom event with a specific value for each action is raised over BLE when the micro:bit buttons are pressed by the user. The events raised by the micro:bit are catched by a Node.js script that is running on a computer with macOS.
The project is composed by two pieces of software:
- The remote control, which is the software that runs on the micro:bit.
- The event listener, which is the software that runs on your computer.
- In this diagram, the micro:bit is ready in the Play state, this is known because the micro:bit is showing the letter
P
on the LED screen. An user press theA
button and then a custom event is raised over BLE, with an specific value to indicate that we want to play or pause a track on Spotify.
- The event listener is a Node.js script that is running on the computer and is waiting for the events raised over BLE by the micro:bit device.
- When a custom event is catched by the Node.js script, it uses the API of the spotify-node-applescript package in order to control the instance of Spotify running on the computer.
The micro:bit device can be in one of a finite number of states at any given time. The list of available states are the following:
- State: Disconnected. The micro:bit device is not connected with the computer via BLE.
- State: Connected. The micro:bit device is connected with the computer via BLE.
- State: Play. The micro:bit device is ready to play or pause a track.
- State: Volume. The micro:bit device is ready to turn volume up or down.
- State: Next. The micro:bit device is ready to go to next or previous track.
The state machine can change from one state to another in response to external inputs. In our project there are two possible inputs:
- Input: AB. When the micro:bit buttons
A
andB
are pressed together.
The change from one state to another is called a transition.
State Machine DiagramThe state diagram for the project can be modeled as follows:
Where each state is represented by a node (circle) and the edges (arrows) show the transitions from one state to another. Each arrow is labeled with the input that triggers that transition.
ActionsThe actions that users can perform to control Spotify via BLE are:
- Play or pause tracks.
- Turn volume up or down.
- Go to next or previous track.
To run these actions the micro:bit device needs to be in one of these states:
- P (Play).
- V (Volume).
- N (Next).
When the micro:bit is in one of these states the user can press the A
or B
buttons in order to raise a custom event to control Spotify. The below code shows what are the different possibilities that an user can perform when the A
button is pressed depending of the current state.
input.onButtonPressed(Button.A, () => {
switch (current_state) {
case state.PLAY_READY:
console.log("play/pause");
control.raiseEvent(EVENT_CONTROL_SPOTIFY, EventValue.PLAY_PAUSE);
break;
case state.VOLUME_READY:
console.log("volume -");
control.raiseEvent(EVENT_CONTROL_SPOTIFY, EventValue.VOLUME_DOWN);
break;
case state.NEXT_READY:
console.log("prev");
control.raiseEvent(EVENT_CONTROL_SPOTIFY, EventValue.PREV);
break;
}
})
The below code shows what are the different the actions that will be performed when the B
button is pressed depending of the current state.
input.onButtonPressed(Button.B, () => {
switch (current_state) {
case state.VOLUME_READY:
console.log("volume +");
control.raiseEvent(EVENT_CONTROL_SPOTIFY, EventValue.VOLUME_UP);
break;
case state.NEXT_READY:
console.log("next");
control.raiseEvent(EVENT_CONTROL_SPOTIFY, EventValue.NEXT);
break;
}
})
No Pairing SecurityAlthough this script is using BLE there is no need to pair your micro:bit with your computer. In order to get this, the pxt.json
file has been configured as follows. The property of the bluetooth json object called open
has been configured with a value of 1
which means no security.
"yotta": {
"config": {
"microbit-dal": {
"bluetooth": {
"open": 1
}
}
}
}
How to Build and Deploy the Remote Control- Install node.js.
- Install the PXT (Microsoft Programming Experience Toolkit) command line tool with
npm
.
npm install -g pxt
- Clone this repository to your computer.
git clone https://github.com/josejuansanchez/microbit-ble-spotify.git
cd microbit-ble-spotify
cd ble-remote-control
pxt target microbit
- Install the required PXT packages for this project (
core
andbluetooth
). The dependencies of the project are defined inpxt.json
.
pxt install
- Connect the micro:bit to your computer via USB.
- To build and deploy the project, execute
pxt
with no arguments or runmake
. Doing this, PXT will compile and link the code into an.hex
file and will deploy it into your connected micro:bit.
pxt
When you run the pxt
command, you will see a similar message to this:
Using target PXT/microbit with build engine yotta
Target dir: /microbit-ble-rc-spotify/node_modules/pxt-microbit
PXT Core dir: /microbit-ble-rc-spotify/node_modules/pxt-core
running 'pxt deploy' (run 'pxt help' for usage)
Package built; written to binary.hex; size: 605847
copying binary.hex to /Volumes/MICROBIT/
wrote hex file to /Volumes/MICROBIT/
Now, your micro:bit device is ready to work as a remote control for Spotify on macOS with Node.js and AppleScript. The next step is to run the ble-event-listener
script to listen to the custom events raised by the BBC micro:bit over BLE.
The compiled firmware is available in the resources/firmware
folder of the project. If your want you can avoid the build process and copy the .hex
file to micro:bit disk drive.
- Go to
ble-event-listener
directory.
cd ble-event-listener
- Run the Node.js script:
node index.js
When you run the script, you will see a similar message to this:
Scanning for micro:bit...
Now, your event listener is ready to accept BLE connections from a micro:bit device.
Packages Used in This ProjectReferences- Finite-state machine. Wikipedia.
Comments