Monday, May 25, 2015

Remote Control Your Mac With Node.js and Arduino

The combination of Arduinos and Node.js allows us to do a lot of unexpected things. In this article, I'll show how you can create a remote control for your Mac via Arduinos, Node.js and AppleScript.

If you are new to combining Arduinos and Node.js, I've previously covered turning on LED lights and displaying web API data on LCD text displays.

Our Arduino remote control will increase and decrease our Mac's volume, tell our Mac to play an iTunes playlist of our choosing and set it to stop whatever is playing on iTunes (which is likely to be that playlist!).

Setting Up Our Arduino

Ensure that you've got the StandardFirmata sketch installed on your Arduino board itself, as we'll be using the johnny-five library to send instructions to our Arduino. That will only work if you've got StandardFirmata on there first:

Installing Standard Firmata on an Arduino

Our Arduino breadboard set up for this demo looks like so:

Sketch for our Arduino Mac Remote Control

Our Server Code

Our Node.js server code is relatively short and sweet for this demo:

[code language="js"] var five = require('johnny-five'), board = new five.Board(), exec = require('child_process').exec, btn1, btn2, btn3, btn4, btn5, currentVolLevels = {}; board.on('ready', function() { console.log('Arduino board is ready!'); btn1 = new five.Button(7); btn2 = new five.Button(6); btn3 = new five.Button(5); btn4 = new five.Button(4); btn5 = new five.Button(3); btn1.on('down', function(value) { askiTunes('play playlist \"Top 25 Most Played\"'); }); btn2.on('down', function(value) { askiTunes('stop'); }); btn3.on('down', function(value) { setVolumeLevel(currentVolLevels['output volume'] + 5); }); btn4.on('down', function(value) { setVolumeLevel(currentVolLevels['output volume'] - 5); }); btn5.on('down', function(value) { toggleMute(); }); getVolumeLevels(); }); function getVolumeLevels() { exec("osascript -e 'get volume settings'", function(err, stdout, stderr) { if (!err) { var levels = stdout.split(', '); levels.forEach(function(val,ind) { var vals = val.split(':'); if (vals[1].indexOf('true') > -1) currentVolLevels[vals[0]] = true; else if (vals[1].indexOf('false') > -1) currentVolLevels[vals[0]] = false; else currentVolLevels[vals[0]] = parseInt(vals[1]); }); console.log(currentVolLevels); } }); } function setVolumeLevel(level) { console.log('Setting volume level to ' + level); exec("osascript -e 'set volume output volume " + level + "'", function() { getVolumeLevels(); }); } function toggleMute() { var muteRequest = currentVolLevels['output muted'] ? 'without' : 'with'; console.log('Toggling mute to ' + muteRequest + ' muted'); exec("osascript -e 'set volume " + muteRequest + " output muted'", function() { getVolumeLevels(); }); } function askiTunes(event, callback) { exec("osascript -e 'tell application \"iTunes\" to "+event+"'", function(err, stdout, stderr) { console.log('iTunes was just asked to ' + event + '.'); }); } [/code]

That Code Explained

Now the all important part of the article - what all of that code means! Lets go over how everything fits together.

In order to interface with our Arduino board, we are using johnny-five. We start by setting up our johnny-five module and our Arduino board through that. Then we define variables to store our five buttons.

[code language="js"] var five = require('johnny-five'), board = new five.Board(), btn1, btn2, btn3, btn4, btn5, [/code]

We also set up our exec() function which is what allows us to run AppleScript commands from Node.js.

[code language="js"] exec = require('child_process').exec, [/code]

When johnny-five lets us know our board is ready to use, we run a quick console.log and define our five buttons and the Arduino pins they are connected to (7, 6, 5, 4 and 3).

[code language="js"] board.on('ready', function() { console.log('Arduino board is ready!'); btn1 = new five.Button(7); btn2 = new five.Button(6); btn3 = new five.Button(5); btn4 = new five.Button(4); btn5 = new five.Button(3); [/code]

On each button's down event, we run a different function. On our first button, we run the askiTunes() function which sends iTunes a request. In our case, it is requesting our "Top 25 Most Played" playlist.

[code language="js"] btn1.on('down', function(value) { askiTunes('play playlist \"Top 25 Most Played\"'); }); [/code]

Continue reading %Remote Control Your Mac With Node.js and Arduino%


by Patrick Catanzariti via SitePoint

No comments:

Post a Comment