Use an Amazon Dash button to run a bash command on the Raspberry Pi

In this tutorial you will use an Amazon Dash button to run a bash command. There are a few tutorials out there but for whatever reason, I could not get them to run a bash command. So here is how I got mine to work.

Prerequisites

    • A Raspberry Pi 2 or 3 with Raspbian installed.
    • An Amazon Dash button. Today, Amazon is selling the Dash buttons for $4.99.
    • An iOS or Android device with the Amazon app installed.

Install Node.js

Update repositories to include NodeSource Packages.
curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -
Now install node.js.
sudo apt install nodejs

Install node-dash-button

sudo apt-get install libpcap-dev
npm install node-dash-button

If npm isn’t installed install it with
sudo apt-get install npm

Setup your Dash Button on your App

Go to your Amazon app. Go to where I have highlighted in red if you have an iOS device. If you have an Android device, I apologize, I do not have screenshots yet. But navigate to Your account -> Dash Buttons & Devices -> Set up a new device. Do not finish setup the last step where you pick the product. As for iOS users, follow below.


Follow the instructions given on the app up until the step where you pick the product. Do not complete this step. Close the app. If you have fully paired your Dash button with Amazon, you will have to unpair it using the app and re-do these steps.

Find your Dash’s hardware address

We need to find the hardware address to add to our script later. To find the address we will use a script included with node-dash-button.
sudo node ~/node_modules/node-dash-button/bin/findbutton
Here is what mine outputs:

In my case, the Dash button did not appear as manufactured by ‘Amazon Technologies Inc.’ Notice that you will see other traffic not related to the Dash button. You may have to press the button several times to figure out which is the correct address. You will start receiving notifications on your phone each time you press a button. Unfortunately, unless you make a new account for this project, you’ll receive messages every time the button is pushed. I’ll update if I find a solution.

In my case, the hardware address is b4:7c:9c:ee:44:f3

We’ll need this information for the next step.

Create a node script to run your bash command.

sudo nano node_modules/node-dash-button/run-command.js
Copy and paste into the file.

[code lang=”js”]
var dash_button = require(‘node-dash-button’);
var dash = dash_button(‘HARDWARE ADDRESS HERE’, null, null, ‘all’); //Hardware Address
var exec = require(‘child_process’).exec;
var child;

dash.on(‘detected’, function() {
exec(‘YOUR COMMAND HERE’, function(error, stdout, stderr) {
console.log(‘stdout: ‘ + stdout);
console.log(‘stderr: ‘ + stderr);
if (error !== null) {
console.log(‘exec error: ‘ + error);
}
});
});

[/code]

Replace what I have capitalized with the hardware address found in the previous step and the command you wish to run.
Save and Exit.
Now run the script.
sudo node sudo ~/node_modules/node-dash-button/run-command.js
Press the button. Your command should now run. Keep in mind that the Dash buttons are not instant and cannot be pressed again for 10 seconds after pressing it once.

Start script on startup

Starting the script with cron will not work because the network will not be up in time for the script to work. So we’ll edit rc.local file.
sudo nano /etc/rc.local
Above the last line that reads exit 0, insert this line.
sleep 15 && node home/pi/node_modules/node-dash-button/run-command.js
Now reboot and test the button to see if it works!