Created
November 27, 2017 18:00
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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