Skip to content

Instantly share code, notes, and snippets.

Created November 27, 2017 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/e7da9b7e8036792a85734c6f9ef81bf9 to your computer and use it in GitHub Desktop.
Save anonymous/e7da9b7e8036792a85734c6f9ef81bf9 to your computer and use it in GitHub Desktop.
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