Nathan's Blog

Web development and useful information

Nathan's Blog

Category: Raspberry Pi

Dash Button with Raspberry Pi

I set up my Raspberry Pi to listen for Amazon Dash Button presses recently.
Raspberry Pis are excellent devices for monitoring Dash button presses because they use very little electricity, so can be left running.

I spent some time getting it working so I’m adding the steps I took here.

1. The easiest way to get a Dash button’s MAC address:
Follow the steps in this post to find your Dash button’s MAC address.

View story at Medium.com

2. Install the required dependencies:

# Update your software
sudo apt-get update && sudo apt-get upgrade

# Install pydhcplib
sudo apt-get install python-pip
sudo pip install pydhcplib

3. Copy this script to your Raspberry Pi
The easiest way is to use wget:

wget https://gist.githubusercontent.com/mr-pj/75297864abef5c8f2d5c134be2656023/raw/adc4de046c8972243eb322a6137d508da9c80372/dashbutton.py

Here’s the script:

4. Replace the script’s MAC address with yours

# Replace "50:f4:de:f1:3b:a0" with the MAC address you found in step 1
# It needs to be lowercase, with a colon after every two characters
dashbuttons.register("50:f4:de:f1:3b:a0", do_something)

5. Make your Python script executable

sudo chmod +x /home/pi/dashbutton.py

7. Test the script
Your Python script should now be able to detect your Amazon Dash button presses. Test the Python script by running:

sudo python /home/pi/dashbutton.py

Press your button and you should see the message, “button has been pressed”.

8. Update the do_something function to make it do something useful
I’m using mine to track toilet roll changes. When I get down to two or less I send an email reminder.
I’m using another Dash button as a “digital habit tracker”.
I’m logging morning Meditation to a habits.txt text file on my Raspberry Pi. I’ve ordered four more to track other habits.


Analogue is so trendy.

Other uses for your Dash button:
Order pizza
Wireless doorbell
Control smart lights
Send a text
Track baby data
Track practice sessions

8. Set the Python script to run on startup

Edit /etc/rc.local

sudo vim /etc/rc.local

Add the following to the end of /etc/rc.local (before exit 0

# Wait for an Internet connection
# If you connect using an Ethernet cable, change 'wlan0' to 'eth0'
while ! /sbin/ifconfig wlan0 | grep -q 'inet addr:[0-9]'; do
    sleep 3
done

# Network connection now exists: run Dash listener
# Change /home/pi/dashbutton.py to the path you copied the script to
# Send errors to rclocal.log <-- Useful for debugging sudo /usr/bin/python /home/pi/dashbutton.py 2>&1 /home/pi/rclocal.log &

# Make sure exit 0 is the last line of rclocal
exit 0

9. Reboot and test your script

sudo shutdown -r now

You should have the dashbutton.py process running in the background, listening for presses.
To verify your Python script is running type:

ps wafux | grep dash

You should see something like:

root      2490  0.0  0.6   3756  2296 ?        S    May21   0:00 sudo /usr/bin/python /home/pi/dashbutton.py /home/pi/rclocal.log
root      2499  0.0  1.8   8904  6924 ?        S    May21   0:00  \_ /usr/bin/python /home/pi/dashbutton.py /home/pi/rclocal.log

This means the Python script is running and listening for your presses.

Prepaid energy monitor with a Raspberry Pi

Instructions: https://github.com/n8kowald/powerpi

My flat has a prepayment energy meter.
Sometimes I forget to check how much money we have and the power goes out, forcing me to walk to the nearest open PayPoint location at inconvenient times.

I wanted a way to automate these checks.

The Raspberry Pi B+ enclosed emails me a photo of the money we have left

I had an unused Raspberry Pi B+, camera module (REV 1.3), and a USB Wi-Fi dongle. With these I could take a photo of the amount left and email myself a photo every day at a specified time.

It turned out to be easy.
The most time consuming bit was experimenting with raspistill options to produce the best photo for the dark cabinet it’s enclosed in.

Amount of money left on our prepaid electricity meter

Amount of money left on our prepaid electricity meter

As you can see the photo is not amazing. However, considering it was taken in a dark cabinet I’m happy with the result.

Full instructions here: https://github.com/n8kowald/powerpi

Next, I want to use Optical Character Recognition (OCR) to detect the amount in the photo.
I could then take readings every 5 minutes, save that to a database and produce a graph of the times when we use the most electricity.

Easy DNS switching on OS X

I’m currently using my Raspberry Pi B+ as an ad-blocker. It works great.

Install Ad-blocker on your Raspberry Pi

If you have an up-to-date Raspberry Pi running Raspbian, you can install this by running a single command, here: http://jacobsalmela.com/instant-raspberry-pi-ad-blocker/

Make sure you assign a static IP to your Raspberry Pi: http://www.modmypi.com/blog/tutorial-how-to-give-your-raspberry-pi-a-static-ip-address

By default your Pi will be assigned a dynamic IP by your router. Assigning a static IP means you can connect using the same IP address every time, and setup easy DNS switching below.

Point your DNS server to your Raspberry Pi

To use your new Ad-blocker, set your DNS server to the static IP of your Raspberry Pi.
In this example I’m using OS X.
Changing your DNS in OS X is done by opening System Preferences > Network > Advanced > DNS and setting the static IP here.

Set DNS server on OS X

Try it out

Your Raspberry Pi ad-blocker should now be working. You can test if it’s working by visiting this URL: http://ads-blocker.com/testing.
If you don’t see ads on the page, it’s working.

Disabling Ad-blocking

I ran into an issue using my Raspberry Pi ad-blocker where clicking sponsored Google Ads in search results does not redirect to the link.
I wondered if there was an easier way to switch back to my default DNS server in OS X, than manually changing it in System Preferences.

Enter OS X’s Automator. It comes with OS X.

Easy DNS switching on OS X using Automator

1. Open Automator

2. Choose the ‘Application’ type

Automator - Application Type

3. Type ‘Shell’ into the filter to find the ‘Run Shell Script’ action. Double-click it to open the action in the right hand pane.

Screenshot 2015-03-29 19.07.23

4. Copy and paste this shell script, replacing 192.168.0.69 with YOUR Raspberry Pi’s static IP:

#!/bin/bash
# Use the DNS server hosted on my Raspberry Pi
networksetup -setdnsservers Wi-Fi 192.168.0.69

Automator - Run Shell Script

Note: If you use an Ethernet cable instead, change Wi-Fi above to Built-in Ethernet. I can’t bring myself to buy the £25 Thunderbolt to Ethernet adapter so I use Wi-Fi.

If you connect another way, you can find a list of all the network services on the server’s hardware ports by typing:

networksetup -listallnetworkservices

Change Wi-Fi to the network service you use to connect to the Internet.

5. Save and choose a name for your app, something like ‘Block-ads.app’

6. By default, your app will have the Automator icon. If you’d like to change the icon that’s easily done. All you need is to find a 128×128 PNG.

Default automator icon

7. Using the excellent www.iconfinder.com makes this simple. I found a nice free Raspberry Pi 128×128 PNG icon – https://www.iconfinder.com/icons/386490/raspberry_icon#size=128

8. Download the PNG to your computer, open it in ‘Preview’ and copy the icon using ⌘C.

9. Right click your app’s icon and select ‘Get Info’. Click on the current Automator icon in the top left.

Change icon

10. Press ⌘V to change the icon to the icon in your clipboard

Screenshot 2015-03-29 18.33.38

You can now use this app to easily turn ad-blocking on.
It will prompt for your password when you run it.

11. To create your ‘Show ads’ app, repeat the above steps and change the DNS server to your default DNS server address.

For my ‘Show ads’ app I used this icon: https://www.iconfinder.com/icons/386450/apple_ios_icon#size=128

Icon Downloads

Download Raspberry Pi icon (block ads) – 128×128 PNG
Download Apple icon – (show ads) 128×128 PNG

Powered by WordPress & Theme by Anders Norén