Thursday, June 29, 2017

A simpler Amazon Dash trigger implementation for Google AIY

As I mentioned in my previous post there's an easier way to get the Amazon Dash trigger working with your Google AIY kit.
On the Raspberry Pi forums poster sheridat was kind enough to direct me to another implementation of Dash Trigger controls which doesn't require any modification to the Google AIY kit code.

Using this trick you should be able to create a trigger based on anything using any programming language that supports Raspberry Pi GPIO. It comes down to this:
The default trigger (on button press) can be tricked by toggling the GPIO pin it's attached to

So in Python the code to trigger the AIY kit can be done with either the following long-form script or it's shorter brother:
With this code in hand it becomes more straightforward to get the dash button trigger working. The Raspberry Pi forum post doesn't quite detail the process so I'll do so here. Much of this is copied over from my previous post.

First you'll need to ensure you have scapy and tcpdump available to root. To do so open a terminal and run the following commands: You'll also need to configure your dash button to connect to your wifi but not make any purchases. There's good writeup in the MagPi post, but I'll also provide a short breakdown here:
  1. Configure your Amazon Dash button completely with the Amazon app
  2. Deactivate the button
  3. Start configuring the button again, but when you reach the "choose an item" page don't select an item and exit out of the app.
This will leave your device capable of connecting to wifi but not making a purchase when you press it. When you press the button you should see white blinking lights followed by red lights (indicating a purchase was not completed). If you see a green light you should check your Amazon account in case you need to cancel the order.

The next thing you need is the MAC address(es) of your dash button(s). Once again the instructions exist in other examples, but I'll provide the short summary here. You'll need the MAC address of your Dash button(s).

If you don't know what a MAC address is you might have some trouble. The only pointer I can give is that a MAC address is 12 hexadecimal digits grouped into pairs separated by colons e.g. 1A:2B:3C:4D:5E:6F.

You should be able to log in to your router and get the IP and MAC address of your dash button(s). On my router it showed up only by the MAC address, however I've read that it may show up as "Internet Device". It's important to know that the Dash button usually connects about 5 seconds after it is pressed, and that it may not show up on your router except immediately after it connects.

Note: it's been reported that you can shorten the response time to 1 second by configuring a static IP address for the dash button with your router.

Warning: I've found that the phone I used to configure the Dash button is constantly pushing notifications whenever the Dash button is pressed. For me this is acceptable because I don't need notifications from the Amazon app and can block them. If you normally use the Amazon app this could be a problem I don't currently have a solution for.

The next big step is to create a script that will trigger whenever the dash button is pressed.

For reference you should probably try a debugging script. Here's the python script that helped me understand what was wrong with my configuration (it's based on a StackExchange query with some good sample code): Note you'll need to invoke the above script with sudo.
Note I've found that you need to have the Raspberry Pi plugged into ethernet. Wi-Fi may not work to detect ARP packets. You should see a listing of ARP requests until you exit using ctrl+c. If you've hit the Dash button in this time its MAC address should show in the listings.
 
Now here's the script to use your dash button(s) as a trigger:
  • You'll need to replace ##:##:##:##:##:## with your MAC address(es).
  • If you changed the trigger file in the previous examples you must configure it here by setting the variable "trigger_file" (default is "/tmp/voice_recognizer/trigger" same as our trigger script above).
  • Each dash button MAC address needs to be added to the "buttons" dictionary. The keys (i.e. "Bounty" or "Charmin") need to be unique for each dictionary entry, but you can add as many unique dictionary entries as you like.
  • It has to be run as root to work.

Getting the script to run whenever you want On my system I saved the above script as "/root/src/detect_dash.py" (you'll probably need to "sudo mkdir /root/src" before you can save there) I'll use this location in the following examples. You'll need to make this script executable with "sudo chmod +x /root/src/detect_dash.py".

Finally let's get the script running whenever you want the button to be detected. You have a few options:
  • By far the easiest is if you can open a terminal and leave it running. All you need to do is run and you should see that the "/etc/voice_recognizer/trigger" file is touched after each button press. 
  • If you are running from ssh or otherwise running without a desktop environment you can familiarize yourself with screen or tmux in order to get it running in the background. Unfortunately a full explanation of how to use those programs is outside the scope of this post.
  • Another way to run it from ssh is to force it to the background (you won't get any debugging messages if there's a problem!). To run the script in the background type into a terminal This complicated line will run the script in the background, hide the outputs and prevent the script from stopping when you disconnect.
  • If you want it to run every time your system starts up you can use the root crontab to schedule it. To edit the root crontab enter "sudo crontab -e" in the command prompt and add a new line to the crontab with the text "@reboot sudo python3 /root/src/detect_dash.py". Reboot your system and see if it worked!
Thanks for reading! I hope this helped someone.
Feel free to leave questions in the comments section or on the Raspberry Pi forums

No comments:

Post a Comment