Introduction: Amazon Dash Button Hack (Raspberry Pi IOT Button)
So I have a Raspberry Pi running a lot of home automation and I wanted a standalone button that could trigger an event. There are a lot of expensive IOT buttons out there but I had heard that you could re-purpose an Amazon Dash Button (£5) to do the job. I tried a lot of the online tutorials out there and couldn't get them to work so I went my own route...
My method requires:
- An Amazon Dash Button
- A Raspberry Pi
- Limited knowledge of Python
- A router that allows you to send its logs to a SysLog server (I have a Sky Hub)
And works as follows:
- The Dash Button is pressed
- It connects to the router and tries to establish a connection to the internet but this is blocked by a firewall rule
- The connection attempt is logged in the routers logs and sent to the Raspberry Pi which is acting as a SysLog server
- The Raspberry Pi receives the log entry (which contains the MAC address of the Dash Button) and triggers an event
Step 1: Partially / Fully Setup the Amazon Dash Button
As with most other Amazon Dash Button hacks you first need to register your Amazon Dash Button but stop the process just before you select a product. This sets up the Dash Button to talk to your router but stops it going so far as to order you loads of Finish Dishwasher Tablets :-)
Optional: The hack method I am using will eventually stop the Dash Button from talking to Amazon at all and so you could (in theory) fully setup the Dash Button at this stage and perhaps even put in a quick order to get your £5 off.
Step 2: Send Router SysLog to Raspberry Pi
The method of doing this will vary from router to router but it was very easy on my Sky Hub. I simply logged into the admin page of the router and provided the IP address of my Raspberry Pi in the SysLog Server address.
Note: My Raspberry Pi has a static IP address and so it will not change.
Step 3: Setup Raspberry Pi As SysLog Server
To get the Raspberry Pi to receive the SysLog now being sent by my router I found a neat little script on GitHub:
I made some minor alterations to the code for my purposes. Note the MAC address in the script will need to be swapped out for the MAC address of your Dash Button.
<p>#!/usr/bin/env python</p><p>import SocketServer import time import datetime</p><p>HOST, PORT = "0.0.0.0", 514 lastRun = datetime.datetime.now() - datetime.timedelta(seconds=60) #This initiates the lastRun variable with an arbitrary time</p><p>class SyslogUDPHandler(SocketServer.BaseRequestHandler): def handle(self): data = bytes.decode(self.request.strip()) socket = self.request global lastRun if '<strong>ac:63:be:b9:e2:f1</strong>' in str(data)\ and datetime.datetime.now() > lastRun + datetime.timedelta(seconds=35): #You need to replace the MAC address with the MAC address of your Dash Button print "Button Pushed!" lastRun = datetime.datetime.now()</p><p>if __name__ == "__main__": try: server = SocketServer.UDPServer((HOST,PORT), SyslogUDPHandler) server.serve_forever(poll_interval=0.5) except (IOError, SystemExit): raise except KeyboardInterrupt: print ("Crtl+C Pressed. Shutting down.")</p>
Step 4: SysLog the Button Press
So the SysLog is being sent to the Raspberry Pi and the Raspberry Pi is set up to receive the log.
Now we need to get the MAC address of the Dash Button to appear in the SysLog when pressed, this way the Python script will see it and will register a button press.
The way I was able to do this on my Sky Hub was as follows:
- First give the Dash Button a static IP address
- Next setup a firewall rule to prevent that local IP from accessing the web at all
- You need to enable logging for this firewall rule so that it appears in the SysLog
- This has the added benefit of preventing the Dash Button from talking to Amazon (and placing orders!)
Step 5: Final Test
Now for the final test:
- Run the python script on the Raspberry Pi
- Press the Amazon Dash Button
- The router should catch the attempt to connect to the internet and block it (firewall rule)
- The attempt should be logged in the SysLog
- The Raspberry Pi should receive the SysLog entry and detect the specified MAC address in the log text
- "Button Pushed!" should be printed on the Pi terminal
- You will see that the python script prevents "Button Pushed!" from being printed if it has already done so in the last 35 seconds. This is because the Sky Hub would log a number of SysLog entries whilst the Dash Button made repeated attempts to connect. After 30 seconds the Dash Button gives up.
- You can change the "print "Button Pushed!" line for any event you want to trigger. I've set my one up to turn all my downstairs lighting on when pressed.