I was lying in bed sick over the weekend, contentedly watching old Firefly episodes, when something horrible happened. I remembered that I had a load of clothes in the dryer and I was going to have to get up, walk across my apartment like a caveman and use my own eyeballs to check if the dryer was done. After that ordeal I vowed to come up with a way to make sure I never had to go through that again. Fortunately Kookye recently sent us their Raspberry Pi IoT Starter Kit which has all sorts of easy-to-use sensors along with example code to get started. If you are interested in smart home projects make sure to check out the review we did of this kit a while back.
I was sure that something in there would let me check on the dryer without getting off the couch. I noticed that the kit included a vibration sensor and, because my dryer is old and shaky, I decided that would do nicely. My plan was to attach the vibration sensor to the side of the dryer and have the Pi host a website that I could look at on my phone to check whether the dryer was done. Before we get started here is a screenshot of the final result:
The first step is to hook up the vibration sensor to your Pi. The vibration sensor is really easy to use with just 3 pins: ground, Vcc and a digital out. The digital out will go high when vibration is detected and low when it is sitting still. You can adjust the sensitivity using a screwdriver.
If you don’t need the whole kit you can buy just this sensor, but if you want to try out more than a few of the sensors then buying the kit will probably end up being cheaper. I will be using GPIO 21 on the Raspberry Pi 2 B+ as shown below. You can use any pin you want but you will need to change a constant in the Python script. Also the pin numbering scheme on your Pi may vary slightly depending on which version you have.
Now all we need to do is poll GPIO 21 to check whether vibration is going on. I ran some tests to see how sensitive the sensor is and the answer is: not very. I set it up to measure 100 times a second for 10 seconds while the dryer was running. I repeated that test a few times and got the results below.
Even while the dryer is vibrating it won’t register most of the time. That said, it is still easily sensitive enough to work for what we are trying to do. We don’t need to check whether the dryer is on every millisecond, we can measure the vibrations over a period of seconds and if it detects any vibrations during that period we can be pretty confident that the dryer is running. This is because while the vibration detector will sometimes tell us there are no vibrations while it is in fact vibrating, it will never tell us (at least it didn’t in my tests) that it is vibrating while it is actually sitting still.
Using the Python GPIO library is pretty simple. Just a few functions will get us up and running to put it in input mode and read the value. First the
init_gpio method is used to set a pin as input:
1 2 3
def init_gpio_input(bcm_pin): GPIO.setmode(GPIO.BCM) GPIO.setup(bcm_pin, GPIO.IN)
Let’s define another function to poll the GPIO over the course of a few seconds and return the number of times it was high and low:
1 2 3 4 5 6 7 8 9 10 11 12
def poll_gpio(duration_secs, frequency_hz, input_pin): start_time = time.time() end_time = start_time + duration_secs count_high = 0 count_low = 0 while time.time() < end_time: if GPIO.input(input_pin) == 0: count_low += 1 else: count_high += 1 time.sleep(1.0 / frequency_hz) return count_low, count_high
And finally a function that will call our polling function in a loop and write the results to a file. Our website will pull data from that file and display it. For the sake of simplicity, if the vibration detector saw a vibration on even 1 sample during our 10 second measurement period we will say that the dryer is running.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
def main_loop(): vib_pin = 21 init_gpio_input(vib_pin) status_file = os.path.join(os.path.dirname(__file__), 'dryer_status.txt') while True: low, high = poll_gpio(10, 100, vib_pin) status = 'Dryer Status: ' if high > 1: status += 'On\n' else: status += 'Off\n' with open(status_file, 'w') as f: status += 'Updated: ' + str(datetime.now()) f.write(status)
We will write to the file in the format:
Dryer Status: (on/off)
Updated: (date and time stamp)
The file is called dryer_status.txt and it lives in the same directory as the Python script. You can find a link to the complete code at the bottom of the article.
The next step is to set the Pi up so it can host a website. If you already have Apache and PHP installed you can skip this section. If you don’t know what that means then don’t worry, it’s really simple. You will need to install an application called Apache to allow your Pi to act as a server for websites. Apache’s job is to send our website to any computer that asks for it using Hypertext Transfer Protocol (HTTP). You will also need to install PHP which is a language that lets you dynamically generate content for a webpage. To install Apache and PHP in one step just run
sudo apt-get install apache2 php5 libapache2-mod-php5
And that’s all you need. If you’re on a device on the same network as your Pi you can test if Apache is working by navigating to your Pi’s IP address with “http://” in front. For instance if you Pi is at 192.168.1.12, go to http://192.168.1.12. A default page will load up telling you Apache is working.
When you navigate to your Pi’s IP address, Apache will send you the HTML file located at
We want Apache to send us our own PHP website instead, so delete index.html and replace it with
You can paste the code below into index.php to display our dryer status as a web page.
<!DOCTYPE html> <html> <body> <a href="http://www.breakoutbros.com"> <img src="logo.png" alt="BreakoutBros" style="width:640px;height:225px;"> </a> <p> <h2> <?php $status_file_name = "/home/pi/dev/vibration_kookye/python/dryer_status.txt"; echo nl2br(file_get_contents($status_file_name)); ?> </h2> </body> </html>
You will need to change the
status_file_name variable to point to the location where your Python script is located. This PHP script is really simple, it will load the
dryer_status.txt file your Python script created and display it under the BreakoutBros logo. The PHP code is also on our Github.
I attached the vibration sensor to the side of my dryer using electrical tape. Make sure you don’t attach it anywhere where it could touch bare metal. Don’t attach it anywhere on your dryer that gets hot and keep it away from the vent.
DISCLAIMER: Dryers are fire hazards. Use only flame retardant materials. Consult your owners manual or the manufacturer before attaching anything to your dryer.
I connected my Pi to Wifi and can navigate to my Pi’s IP address in a browser to check if it is running or not without interrupting my weekend movie, gaming or coding. Unfortunately this will only work if you are connected to the same local network as the Pi. Exposing your Pi to the internet is possible but it is also a security risk so we will cover that another day. Using this basic methodology you can make almost any kind of home IoT sensor web accessible. If there is another smart home device you have had success with or something you want to see us cover leave a comment below.
You can find all of the code for this tutorial (both Python and PHP) on our Github. The code in PHP folder will go in
Remember to delete index.html from that folder first. If you don’t want to change the path in the PHP script then save the files in the Python folder to
FOLLOWUP: Part 2 of this project tells you how to send notifications from the Pi directly to your phone.
Hope you enjoyed! Be sure to subscribe to our email list so you don’t miss future tutorials and reviews!