Tuesday, July 26, 2016

Connecting Amazon Dash buttons to IFTTT via your DD-WRT Router

Recently Amazon had their Dash buttons on sale for a dollar a piece and were still offering $5 back on the first purchase using Amazon Dash so I figured this would be a fun, no cost way to place an order and then have some fun toys to play around with. Amazon also sells unprogrammed devices for $20, so this was worth at least $20. In researching what fun projects I could try, I found very detailed instructions for integrating Amazon Dash but unfortunately, none of the setups I found matched something I could easily do. I don’t have a dedicated device to listen for the dash button presses, but I do have DD-WRT router which is extremely configurable and always on. So I figured I would try and make this work.

What you need:

  • Amazon Dash button(s)
  • Router with DD-WRT firmware installed
  •  IFTTT account

Overview:


Before we jump in, I’d like to describe the process at a high level, so we know what’s happening at each step and we know why each step is important. What we’ll be walking through is connecting your Amazon Dash to your Wifi Network so it can communicate. Then we are going to setup a trigger event and action in IFTTT. Lastly, we’ll use your DD-WRT router to link the pushing of the dash button to the triggering of the IFTTT event and action. This will allow you to perform any action IFTTT allows you to with a single press of the Amazon Dash button.

Step 1: Make sure you have everything


Make sure you have all of the above needed to start

Step 2: Setup the Amazon Dash Button


Once you have the Amazon Dash buttons in your possession, you’ll need to teach them how to connect to your wifi network. Luckily for us, David Kryzaniak over at davekz.com already put a nice tutorial on how to do this with the Amazon app. Simply follow the directions in section ‘Dash Button Setup’, you can stop before the ‘Finding The Mac Address’ section.


Step 3: Configure IFTTT


Log into your IFTTT account and connect to the Maker channel (https://ifttt.com/maker)


Get your Maker Channel Key


Go through the process of setting up the channel and you’ll eventually reach this screen, which will show you your Maker Channel Key and how to trigger it. Of particular interest is the last line with the curl statement. Copy that to clipboard or notepad for use later.



Set up an IFTTT recipe


In this example, I’m going to connect the Maker event to a adding a row into a Google Spreadsheet. This will help me log the buttons presses, but you can use anything else if so desired e.g. Turn on/off Light if you have a connect home channel, or send text message to someone. Too many possibilities to list.

IFTTT Step 1


The recipe should start with the Maker channel trigger, so search for Maker

IFTTT Step 2 – Click ‘Receive a web request’



IFTTT Step 3 – Create an event name


Not to be confused with key value from the Maker channel, the Event Name will help identify a single activity. This will allow us to create many different events all under the same key value obtain earlier. If you have multiple buttons like I do, you will have multiple Event names.

As an example, I entitled the first button I setup as ‘amazon-dash-bounty’


After clicking ‘Create Trigger’, you’ll get a new screen. This is where we connect one event to another.

Click the ‘that’ link.


IFTTT Step 4 – Select Action Channel


Search the activity you’d like to perform. As mentioned earlier, I’m going to show how to post to a Google Spreadsheet as that is a simple but useful example. I search for ‘google’ and then click ‘Google Drive’


IFTTT Step 5 – Configure Action


From Google Drive channel, we’ll select ‘Add row to spreadsheet’


IFTTT Step 6 – Complete Action setup


In this final IFTTT step, give the spreadsheet a name and path before clicking, ‘Create Action’. If you have multiple Amazon buttons, it might be advisable to remove the ‘EventName’ in the last box for ‘Drive Folder Path’. Otherwise you will have one spreadsheet for each Dash button vs one spreadsheet for all Dash buttons. In my example, I have multiple buttons adding rows to only 1 spreadsheet.

Click ‘Create Action’ when done with configuration.




IFTTT Step 7 – Create and connect Recipe


Go ahead and review the recipe, which tells us that if get a maker event called ‘amazon-dash-bounty’ then a row is going to be added to the a google spreadsheet.



IFTTT wrap up


Earlier we mentioned to copy down that curl statement to clipboard or notepad. Now would be a good time to pull that out and make the modifications to it.
Since the time we initially created the key, we’ve also created an event, or multiple events if you have more than 1 buttons. We need to update this string and keep it handy. Go ahead and replace the below string with your key and event name(s).

curl -X POST https://maker.ifttt.com/trigger/amazon-dash-bounty/with/key/<my key>

Step 4 – Configure your DD-WRT router


By now, we’ve step up the Dash button to connect to your wifi and we’ve created the activity for which the button will perform when pressed. The last part (and most difficult part) will be to make some modifications to your DD-WRT router to listen for the button press and then trigger the curl statement IFTTT.

Determine the MAC address of your button(s):


We’ll need to be able to specifically identify your Amazon Dash button and in order to do this, we’ll use its MAC address. There are probably a few ways you can do this, but the easiest for me was to goto ‘Status’ tab and then the ‘LAN’ tab.


At the bottom is a list of DHCP clients.

Before Dash Button Pressed, I have 6 DHCP clients.


After Dash Button Pressed, I now have 7 DHCP clients, a new entry appears at the top with hostname = *.



If you click on the MAC Address link, a pop-up window with confirm the device is an Amazon device.



Once you identify your dash button, copy the MAC Address to clipboard/notepad.

Note: if you pressed the dash button recently, it may already exist in the DHCP Client list. In which case, click on each MAC Address link to find it. If you have multiple buttons, you’ll need to take a more structured approach, but can use trashcan link to remove buttons previously identified.


Enable JFFS2 Support


We’re going to be storing a script and a log file on the router and to do so, we’ll need a persistent location that doesn’t get wiped when the router reboots.

I’m definitely not an expert on network devices, but if you want more information of JFFS and how it works on DD-WRT, you can read up at the following link:


To enable JFFS2 log back into your router and click on ‘Administration’ tab, and ‘Management’ sub tab.



Towards the bottom, you’ll find the JFFS2 section. Enable JFFS2 and disable ‘Clean JFFS2’.


Save DD-WRT the configuration, apply changes, reboot router.

Note: Depending on your router, you may not have enough space to enable JFFS. Also, when I first enabled JFFS2, the Total/Free Size kept showing zero. I believe it took one or two router reboots for this to properly show up.

Enable DNSMasq


Log into your router via your browser and click on the ‘Services’ tab, and the ‘Services’ sub tab.

About half way down the page you’ll find a DNSMasq section. Make sure DNSMasq is enabled on the first radio button. Under ‘Additional DNSMasq Options’, you’ll want to put something similar:

dhcp-script=/jffs/etc/config/amazon-dash-dnsmasq.sh


Now you don’t have to name the script ‘amazon-dash-dnsmasq.sh’ nor do you have to put in the /jffs/etc/config/ directory. You can specify either, but make sure that location is persistence storage and doesn’t get wiped upon rebooting your router.

For more info on the dnsmasq command and dhcp-script option, get educated here:


The script


Now the script I’m going to share is the one that works for me. It is not the cleanest script and there is plenty of room for improvement. If you only have 1 dash button, just delete all the lines and if statements that correspond to buttons 2 and higher (lines 11-14 and lines 33-71). If someone can provide something better, I’ll update this post. Save the file locally on your computer as ‘amazon-dash-dnsmasq.sh’ or whatever file name you decided to input into the ‘Additional DNSMasqOptions’.




After getting the MAC address properly in there, the next most important part is the curl statement. Earlier I asked you to keep those commands handy. Here you will want to paste them into the script file adding the switch -k. Don’t forget that, otherwise the curl command will want you to supply some certificate information and probably won’t run.

curl -k -X POST https://maker.ifttt.com/trigger/<YOUR_EVENT_#1>/with/key/<YOURKEY>

Connect to your router and upload the file


Use WINSCP to connect to your router. If you don’t know how to do this, you may need some additional DD-WRT configuration. Do a google search to help you with this part. Once connected, navigate to the /jffs/ directory.

If the /jffs/etc/ doesn’t exist, go ahead and create it with root ownership.

Once created, navigate to /jffs/etc/ and create the /jffs/etc/config folder if not already present with same rights and ownership.


Navigate into /jffs/etc/config/ and upload the script from your computer. I would recommend transferring as a ‘text’ file and not binary to avoid carriage returns being inserted into the script. This may cause the script to not run. When uploading in WINSCP, click on transfer setting and set the Transfer mode to ‘Text’


Once uploaded, you should see the file:



Set the permission on the file 0700 and root ownership. You can get this screen by right clicking on the file and going to properties.


As alternative to WINSCP, you can try to SSH in and use vi or some tool to post the file in the correct location and set the permissions.

Testing It:


Now we should be all set!

Press the Amazon Dash button and then refresh WINSCP. We should now see a log file:



In the contents I see the following. Please note Param 1 is old because my router had not expired the DHCP lease since my last dash press. Had it expired, it would say ‘add’ in Param1. Param2 is the MAC Address which is checked in the script.



Checking my Google Spreadsheet a new row was posted with the same time stamp!



Limitations:


These buttons are great, but since they rely on Wifi, the button needs to be in range of the device detecting its presence. So that means you cant keep these in your backpack or purse and trigger events wherever you choose. Similarly, just because you might bring the button into an area with wifi (your employer's office), it also will not work there either. Initially, I was thinking I could bust one open and wire my mailbox to alert me when it was opened, but since my mailbox is out of wifi range, this is a no-go for me. Long story short, these buttons will only work within range of the DD-WRT device you configure.

Other notes:


When it comes to get a script running on your router, there are many ways to skin the cat. I tried a few of them successfully and unsuccessfully. I found the DNSMasq method to be the best and easiest. If you have problems getting this working, you can also try putting a repeating script in the ‘Administration’ tab, ‘Commands’ sub tab and save it as a startup script. I couldn’t get this to work, but I also wasn’t thrilled with the idea of something constantly running on the router. For DD-WRT executing scripts and startup scripts, check out the following, which were very helpful to me.



7 comments:

  1. Great writeup. FYI my version of DD-WRT (8/24/2016) didn't include cURL, so I had to use a crappy combination of multiple echo statements and netcat.

    ReplyDelete
  2. Brief Gargoyle (openwrt based) notes:

    -there was storage available on the router file system, so no need for the jffs procedure

    - the "dhcp-script=/path/to/file" should go in the /etc/dnsmasq.conf file

    - curl not available, but i only needed to ping a url, so "wget http://destination -O /tmp/dash" worked great for me

    - in the shell script I put the MAC addresses in quotes, and used == instead of = in the expressions. Also changed first elif to if and added a second fi at the end.

    - set up firewall in the router so that traffic generated by the buttons doesn't leave the local network.

    Great job, thanks for sharing

    ReplyDelete
  3. Hello,

    I am trying to get this to work with my setup. However, I cannot get it working correctly. When I run the script in a PuTTy window I get the following error "./dash.sh: line 23: syntax error: unexpected "elif""

    Do you have any advice on how to make this work?

    Thanks!

    ReplyDelete
    Replies
    1. Actually, I finally got this to work by using Davide Ottaviani post. I also had to change the elif to if and added a second fi to the end of file.

      However, it is now working great! Thanks for the post!

      Delete
  4. Thanks for a great article. It's really amazing. I think it's helpful for everyone.I got my best idea.Thanks for sharing.
    Desh Cam Buying Guide

    ReplyDelete
  5. hi, hope someone can help me. im trying to use this script in AsusMerlin

    but getting

    [: del: unknown operand
    [: dashmac: unknown operand

    I believe there must be some settings i need to set on the router? but as a novice... can i have some help?

    thanks

    ReplyDelete
  6. Casino App Review & Download for Android (.apk) & iOS - KT Hub
    Read the Casino 울산광역 출장안마 App Review and 안양 출장안마 Download for Android (.apk) & iOS for the 인천광역 출장안마 2021 ⭐ Mobile Casino App for 경주 출장안마 android and 평택 출장마사지 iOS ⭐ Mobile Casino App

    ReplyDelete