I'm hosting a Halloween party this weekend and I figured I'd see what I hack together with all these micro:bits I got lying around.
My first thought was that it'd be great if guests at the party could control the music using a micro:bit. Button A / B for the next / previous song, and perhaps a shake for to shuffle to the next song.
I'll walk you through the steps I took to get this working. First, here's a video of the final product:
micro:bit serialThe micro:bit has serial support, which is a fairly universal way for devices to talk to each other over the wire. This means one micro:bit will have to be connected to my computer the whole time, and it will be communicating (as a proxy) with both the computer (which controls Spotify) and the other micro:bit.
micro:bit 1 (Proxy)Let's start of by going to makecode.microbit.org. The "proxy" micro:bit is going to simply take any messages it receives over the radio and send them through the serial.
Let's also set a radio group, so we don't interfere with any other micro:bits around :P
Here's the complete code for the proxy micro:bit. I've added a nice animation to play when we receive something over the radio: https://makecode.microbit.org/_HDu3bbMuR77k
micro:bit 2 (Remote control)The other micro:bit will be controlling the playlist remotely, it will send messages that will eventually make their way to the computer (through the proxy micro:bit)
The three messages I got started with were "Next" when a user hits button B, "Previous" when a user hits button A, and "Shuffle" when a user shakes the micro:bit.
This is the code I ended up with:
and here's the code share: https://makecode.microbit.org/_b5eU5aLgAAXs
I plugged the controller micro:bit to a couple of batteries and it was ready to go.
Node + serialThis is were things get interesting. In order for my micro:bit to communicate with my computer, I need some process on my computer that's listening to the right serial ports. I'm going to use Node for this. If you're not familiar with Node, you can get started here. I'll publish everything you need for this project at the end if you'd prefer to skip figuring this part out.
There's a really good Node library called node-serialport. First off, let's install it. Create a new directory wherever you want this project to live, and run the following:
npm install serialport
Using your favourite text editor, create a new file, let's call it microbit-spotify.js and edit that file.
We'll start off by importing the serialport library, and configuring it with the correct port to listen to:
var SerialPort = require("serialport");
const Readline = SerialPort.parsers.Readline;
var port = new SerialPort('/dev/tty.usbmodem1422', {
baudRate: 115200,
autoOpen: false
})
Setting the path and the baudRate (115200) is how we tell the serial library that we have a micro:bit connected to the computer, and where to find it. The path will most likely vary on your computer. To find the path of your micro:bit, unplug your micro:bit and run this command:
ls /dev/tty.*
Keep a note of the list of paths that show up on your screen. Your micro:bit is NOT one of those. Plug your the micro:bit in, and run that command again. There should be one extra item in the list of paths that show up. That is your micro:bit path, replace /dev/tty.usbmodel1422
above with your own micro:bit path. Keep in mind, if you change the USB port that you connect your micro:bit to, the path will change.
Let's setup the Readline parser so we make sure we read the serial data line by line, this way the messages aren't cut in parts when we're processing them.
const parser = new Readline();
port.pipe(parser);
Let's open the serial port, and handle messages that we receive:
port.open(() => {
console.log("Port open");
parser.on('data', (data) => {
console.log('Received Data: ' + data.toString());
processData(data);
});
})
function processData(data) {
}
I've added a few console logs throughout so we know when the port is open and when we are receiving data.
Whenever we receive data over the serial port, we will call the function processData
with the data received, so let's place all our code that handles what messages we receive in that function.
Here's how we can handle the different messages that we could receive:
function processData(data) {
if (data.indexOf('NEXT') == 0) {
// Handle NEXT received
} else if (data.indexOf('PREVIOUS') == 0) {
// Handle PREVIOUS received
} else if (data.indexOf('SHUFFLE') == 0) {
// Handle SHUFFLE received
}
}
It's now time for the last component, interacting with Spotify.
Spotify + NodeI began my search trying out a few Spotify node packages, and I finally settled for spotify-node-applescript. From the name you can probably guess this won't work on anything but a Mac. If there's enough interest, I'll consider doing a version of this article for Windows in the future :)
-- Edit: I have since created a similar project for Windows, see: https://www.hackster.io/samelhusseini/microbit-spotify-windows-8a8551
Just like before, let's install that package, run the following in your working directory:
npm install spotify-node-applescript
Let's import the spotify package at the top of our microbit-spotify.js file:
var SerialPort = require("serialport");
var spotify = require('spotify-node-applescript');
...
When we receive the "NEXT" message, we'll call the spotify package and tell it to play the next song, like so:
spotify.next(function () {
// Playing the next song
});
and for "PREVIOUS":
spotify.previous(function () {
// Playing the previous song
});
Shuffle is a little tricker, we'll have to set the shuffle mode on first, and then skip to the next song.
spotify.setShuffling(true);
spotify.next(function () {
// Playing the next song
});
In which case, we will probably want to setShuffling to false when we receive the "NEXT" or "PREVIOUS" messages, just in case we shake the microbit and then want to just skip to the next / previous song.
That's about all it takes, let's give it a go. In order to run our script, in a terminal window, run the following command:
node microbit-spotify.js
We should see the "Port open" message, and we should be ready to receive messages from the micro:bit.
Here's the complete code for the Node script (microbit-spotify.js):
https://gist.github.com/samelhusseini/71bf0f721e0f26a96c320b69f83ec9c6
Comments