Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A node script for listening to micro:bit serial messages and controlling spotify on a Windows
var SerialPort = require("serialport");
const Readline = SerialPort.parsers.Readline;
const SpotifyWebHelper = require('spotify-web-helper');
const spotify = SpotifyWebHelper({'port': 4381});
var port = new SerialPort('COM7', {
baudRate: 115200,
autoOpen: false
})
const parser = new Readline();
port.pipe(parser);
spotify.player.on('ready', () => {
console.log("Spotify ready")
port.open(() => {
console.log("Port open");
parser.on('data', (data) => {
console.log('Received Data: ' + data.toString());
processData(data);
});
})
function processData(data) {
if (data.indexOf('PLAY') == 0) {
// Handle PLAY received
spotify.player.pause(true);
} else if (data.indexOf('PAUSE') == 0) {
// Handle PAUSE received
spotify.player.pause(false);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment