It's been just over a month since I created the original micro:bit + Spotify project for my Halloween party. The project got a lot of interest, and I've had a couple of people request I do a Windows version. As promised, here it is.
As much as possible, I'll try to keep the same format as the other project. Note, given the Node Spotify libraries have varying support on Windows vs Mac, I'm having to vary the project on Windows to fit the library a little.
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.
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 two messages I got started with were "Play" and "Pause" when the user hits any of the buttons (A or B). The remote micro:bit will basically act as a toggle switch to pause and resume our music.
This is the code I ended up with:
And here's the code share: https://makecode.microbit.org/_LjUD9hMrsHYD
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 -g 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('COM7', {
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. For me, the path was "COM7", this will most likely vary on your computer. To find the path of your micro:bit, run the following command:
serialport-list
And use the first field in the list that it returns. For example, here's what I got:
COM7 USB\VID_0D28&PID_0204&MI_01\9900023432044E45005480180000004500000000CFCF28BD mbed
We're only interested in the first column, which is "COM7". If you have a number of micro:bits plugged into your computer, you'll see a number of these. Unplug them so you can narrow down the micro:bit port you're interested in.
Next, 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('PLAY') == 0) {
// Handle PLAY
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-web-helper. Here are a few others if you'd like to do some experimentation yourself:
Just like before, let's install that package, run the following in your working directory:
npm install spotify-web-helper
Let's import the spotify package at the top of our microbit-spotify.js file and create an instance of the helper:
var SerialPort = require("serialport");
var
const spotify = SpotifyWebHelper({'port': 4381});
...
Note I passed in an option to set the port of the helper to 4381. This is because the default port the library uses is 4370, and that is no longer the default port for the Spotify Web Helper process.
When we receive the "PAUSE" message, we'll call the spotify helper and tell it to pause the current song, like so:
spotify.player.pause(true);
And for "PLAY":
spotify.player.pause(false);
Notice we are calling the same function but with a boolean parameter whether to pause or not pause (ie: resume).
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/anonymous/e7da9b7e8036792a85734c6f9ef81bf9
Comments