It's pretty obvious that the Raspberry Pi is incredibly powerful. I use it for so many things, and honestly haven't even scratched the surface of what it can do. From what I have seen, many people believe that the SD card is the weak point for the Pi, because it is relatively easy to corrupt. This might happen when you do something that most people are used to doing with their Pi, which is unplugging it. It seems completely harmless, probably because it's the first thing we do it to most of our electronic devices when they're not working, but if the Pi is writing to the card when you unplug it, you run the risk of damaging the SD card. To prevent this, you should really be running sudo halt in the terminal instead of unplugging the Pi to stop all system processes, and rebooting from the terminal by typing sudo reboot. The reason that we unplug it in the first place is that to open a computer to run these commands can be a pain, and it's just too easy to pull the plug. So, to keep this from happening, we have to make it just as simple to reboot the Pi, if not easier, something that is (relatively) easily achieved with an Amazon Dash Button. If you don't know what a Dash button is, it is essentially a small internet connected button that is sold by Amazon and you normally are able to place orders for Amazon products at the push of a button. They cost $5, and if you use it to buy an item once, Amazon will credit your account with the $5, making the button free. You can buy them at this link, just make sure not to buy the AWS IOT one, it's 20 dollars and only designed for Amazon Web Services (plus you don't get your money back).
A couple notes: All of these pictures are either screenshots from my computer, or photos that I have taken, while I did not develop any of this software, every piece of media in this Instructable is my own.
Step 1: Button Setup
Now you will need to download the Amazon app on your phone or tablet to set it up. I've heard that there is a way to set up a button without a the Amazon App, but I have no idea how to do so. Once you have the App downloaded, navigate to the "My Account" tab. In there, you should see another tab titled "Dash Devices", and once you click on that you should see a button that reads "Set Up a New Device", click on that and then click on "Dash Button". Follow the onscreen instructions until you reach the "Select a Product" step. (If you want to try out the button you can always continue through the instructions, and then deactivate it later). At this point close the App. You may be asked if you want to cancel setup, which is exactly what you want to do. Essentially all you did is save your WiFi details to the button. The program that we are going to be setting up will be "sniffing" for ARP requests, which the button sends as it tries to connect to the network. Of course once it does there is nothing for it to do, and so it displays an error by flashing a red light. If you see a green light...well you might want to check to make sure you didn't just order something!
Step 2: Install NodeJS on Your Pi
The first thing you will want to do with your Pi is to set it up, a process I explained in detail in my NextCloud Instructable (Steps 1-4). If you don't know how to do that, go through those steps, and return here afterwards.
Now just to make sure that you have all the package lists downloaded and that your Pi is up to date you will want to run a few commands in the terminal:
- sudo apt-get update
- sudo apt-get upgrade
Once that finishes, you will want to install NPM, which is a package manager for Node projects. This can be installed by running
- sudo apt-get install npm
Now we need to install pcap, which will allow your Pi to intercept network requests by running
- sudo apt-get install libpcap-dev
Finally we get to installing NodeJS. There is no package available to the Pi, so you will have to add the source. This can be done by running
curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
- sudo apt-get install nodejs
To install NodeJS onto your machine.
Step 3: Install Dasher
At this point the only thing we need to install is Dasher, a program that detects the press of a Dash Button and then carries out a HTTP request or local command.
This program is cloned from GitHub, so you will need to install Git onto your Pi by running
- sudo apt-get install git
The install is extremely simple, and can be done by running these commands in sequence:
- git clone https://github.com/maddox/dasher.git
- cd dasher
- npm install
Unless you are feeling like digging through your router logs to find the MAC address of your button, it is much easier to run the following script
This script will, believe it or not, find the MAC address of your button, all you have to do is run the program and press your button. It should appear as "Amazon Technologies Inc." When you find the address, copy it, and either write it down or put it in an empty notepad file or something, because you'll need it in just a second.
Now you need to edit the config file. This file is located in a subdirectory, so navigate to it by typing
- cd ./config
To edit the config file, type
- sudo nano config.json
And paste in the contents of dashconfig.txt, which is attached below.
Right below the name you should see a parameter titled "address". In between the quotation marks directly after, place the MAC address that you found earlier, and close and save the file by typing Control + X and Y, hitting enter after each.
Step 4: Setup Your Script
Essentially at this point you need to decide what you want your button to do. The two main things that you could have it to is reboot or shutdown. You could even write your own shell script and have that execute, but really, you probably wouldn't be reading this if that was what you are trying to do. Because the buttons are free, I'm planning on ordering another one to allow me to reboot AND shutdown my Pi at the push of a button, and honestly, it makes sense to have both options. Once you've decided, run this in the terminal:
- cd /usr/local/bin
If you're going to reboot your Pi, download reboot.txt, and shutdown.txt if you want to shut it down, and type
- sudo nano dash.sh
Now paste in the contents of the text document, and take a look at what is says. Essentailly you have a shebang at the top that identifies the script as a bash script. Then you have a line that will output some text telling you what is going on, and a line that initiates the delay stated in the text, followed by the actual command.
Once you close and save that file, we need to make is executable. This is easily done by running
- sudo chmod +x dash.sh
To test that everything worked, you should be able to move back to your home directory and run the script by typing
- sudo dash.sh
Your pi should reboot or shutdown, and you should see the output from the echo command in the terminal.
If you're feeling up to it, take a look at the picture for this step and try to replicate the countdown in your script!
Step 5: Run Dasher
The only thing you need to do now is set up an init script that will be run at startup, so that you will always be able to reboot or shutdown your Pi without having to start Dasher every time. To do this, download the blank template below and run
- sudo nano /etc/init.d/dasher
To open the file. Now paste in the contents of init_template.txt, changing /home/pi/dasher to the correct username. (If you have been logging in as pi, you can leave this as-is, otherwise you will need to change it.) To wrap up, simply run
- sudo chmod 775 /etc/init.d/dasher
- sudo update-rc.d dasher defaults
You're nearly done! Check to see if it works my manually starting the script
sudo /etc/init.d/dasher start
And then watching the logs
tail -f /var/log/dasher.log
If all goes right, you should see the button press register, and immediately after that, the Pi should reboot, shutdown, or do whatever else you told it to do.
Step 6: Wrapping Up
You've got everything working, but this whole process left some extras on your Pi that you don't want. Remove the NodeJS source by running this command:
- sudo rm /etc/apt/sources.list.d/nodesource.list
That's it! This is a pretty simple and useful project that I hope encourages you to do a little more exploration as to what these buttons can do! Mess with the script, and see what you can make the button do. You can even add new buttons by duplicating the relevant lines in config.json so that you can have almost endless functions! (JSON files like this can be a little finicky, so if you need help, don't hesitate to ask!). The Dasher repository has a lot of great information, so check that out for some more tips and tricks!
Below I have attached the script that I use to reboot my Pi. It outputs the time pressed, and some other information, and then sends the output to me in an email before rebooting. I set up email based on this link, and if you decide to follow it (Use EXIM4!!) all you have to do is replace "email@example.com" with the email address you want to receive at.
Lastly, I am entering this Instructable in the IOT contest, and I think that this is a perfect example of the power of the Internet of Things, and hopefully you do too. I feel like this is a great stepping stone for those looking to get into IOT development, which is something that you can learn more about in my Alexa Skills Kit Instructable. If you like this project, consider giving it a vote, I'd really appreciate it.
I've created some other similar Instructables, so if you liked this, you'd probably like those too, check them out!