Shutdown your Raspberry Pi from the touch of a « Nerf » Button

Pierre-yves Baloche
4 min readApr 8, 2019

--

When you are working with a RaspberryPi, you are often facing the problem of being able to shut it down properly and ideally from the touch of a button. You can always add on one of the GPIO a simple push button to do the job, but then comes the question : « How can I integrate it nicely ? » . Let’s investigate a possible re-use of the Amazon (Discontinued) « Dash » button to do so.

Upgrading an existing Pi-Project

The crew from Pimoroni (https://shop.pimoroni.com/) developed over the years a set of add-ons to be able to add such functionality to existing Pi projects.

One of their projects I enjoy the most, is their « Pirate-Radio » (https://shop.pimoroni.com/products/pirate-radio-pi-zero-w-project-kit) to easily listen to Internet radio stations. All functionalities such as skip tracks/stations and volume controls are available from the « tip » of the finger through simple, short buttons, as well as for the shutdown one.

However, to find these little button can sometime be a problem, especially when you are suffering from the “Big Finger” illness ;) .

Therefore, I thought of using one of Amazon’s « Dash » button to control the remote shut down the radio.

I just “Nerf’ed” the Pi

This replenishment service button was launched in 2015 by Amazon, in order to be able to command « from a single press », the product that it was associated with, such as washing-machine product. Unfortunately it got discontinued in March 2019, as Alexa was supposed to be a better alternative for easy ordering. However, these buttons are great for makers ! Mine being tagged as « Nerf » , I though it would be perfect to « terminate » any Pi.

In order to do so, I decided to use the « Python Amazon Dash » (https://pypi.org/project/amazon-dash/) (PAD)as all the functions I would need would be integrated. However, I ran into some little troubles while installing the project, that I wanted:

  • PAD is scanning network activities with tcpdump related commands, hence you need to have it installed on your Pi, which might not be the case if you are using a « lite » edition of Raspbian. Just run the appropriate command to have it installed on your system beforehand:

sudo apt-get install tcpdump

  • PAD also depends on “scapy” (https://scapy.net/) to run, but the currently released version seems to have a problem while installing as the « LICENSE » file is missing. The Python package has been fixed but it is not yet released. You then need to install it with the following command to avoid the problem:

sudo pip install — pre scapy

  • Finally, PAD can be installed with no problem with:

sudo pip install amazon-dash

Now that all the necessary packages are installed, we need to find out our button’s identity

Where’s my button ?

To be able to use your button, you need first of all to have it associated with your Amazon account, but not fully configured. Indeed, the final stage of its configuration is to associate it with a product available on Amazon : just do not select anything! And don’t worry about the warning notifications & e-mails on your phone about finishing the process, we’ll cover that up soon.

PAD includes all the functions to identify your button on your WI-FI network. Use the « discovery » command to scan the active devices on your network. Once you pressed your button, make note of the MAC address that came up on the screen.

sudo amazon-dash discovery

Finally we need to register our button in the configuration file of PAD to assign our shutdown command to the Raspberry Pi.

sudo vim /etc/amazon-dash.yml

All we need to do is to add another device, identified by the MAC address, and to use the « sudo poweroff » command to shutdown our Raspberry Pi.

# amazon-dash.yml# ---------------settings:  # On seconds. Minimum time that must pass between pulsations.  delay: 10devices:  XX:YY:ZZ:AA:BB:CC:    name: PiRadio Nerf Button    user: pi    cmd: sudo poweroff

Once configured, we activate the PAD service to have it running once the Raspberry Pi is powered on.

sudo systemctl enable amazon-dash.service

sudo systemctl start amazon-dash.service

Now, If I press the button, my Pi shuts down right away… Victory !

But here comes the SPAM…

As soon as you pressed the button, I bet you got a popup notification on your phone, inviting you to register a product with your button and I’m sure you could definitely do without it.

In order to get rid of any messages from Amazon about your button, your ISP box or local network router is your best friend.

You can reuse the MAC address of the button in the “Parental Control” section of the network controller to block any Internet access, always, for your button. That way, the button will still emit the message on the local network, but will not go back to Amazon anymore.

And this is the end of the SPAM from your button and the start of an easier way to remotely shutdown any Pi you’d like.

--

--