Use Raspberry Pi to Fix Everyday Problems

Welcome to DIY Tryin. Attach sensors toall the things! I'm Patrick Norton. I'm Michael Hand. This man is the heroof our office today. That's what we'regoing to do today. We're going to make youinto the office hero. Sensors, Raspberry Pi, website. Yeah, hundreds ofpeople hours saved. So here at DVN, we havea big bathroom problem where there's two bathrooms.

Well, yes. [LAUGHING] And like 54 people. Yeah. So, there's a lot of waitingin line for the bathroom. I made a website using aRaspberry Pi to update and tell you whether or not thebathroom is actually open. So, you know, you could if youwant to, use the Raspberry Pi B+, the recent update tothe Raspberry Pi model. We did a review ofthat on Tekzilla, guess last week, two weeks ago when it came out. Like that. It's very similar,and watch the video to find out howsimilar it actually is. But right now, you'reactually, basically using a door sensor, ahomemade door sensor, feeding in informationto Raspberry Pi, which sends a signal outto a web server. 

Using Dropbox for thatbecause I'm cheap. I like being cheap. And the final piece,de resistance, which is disconnectedat the moment so I don't electrocute myself. A Signal light. [LAUGHING] Yeah, so we havesignal lights so that if people don't wantto go to the website, they can just check that out. Should we start with the sensor,the code, or the website? Let's just do it all, rightafter, one after another. Sensors. Sensors it is. Here are my ghettoaluminum foil and duct tape with some wires sensors. So, it's pretty simple. There's one sensor onthe inner door jamb, and then one the door over here. 

So when the door closes,it will close the contact, and then the Raspberry Pi knowsthat the button is closed. All right., ourdoor sensor wires are running to thisbreadboard, which we have it wired upwith some resistors so that the Raspberry Pi knowswhen the connection is closed, and which door theconnection is closed on. So from there, we cansee that, and then we can modify some HTMLin our update page, and then send that up toDropbox because that's what we're using as our hosting. So also connected here, iswe have an 8-channel relay, but we're only usingtwo channels of it. This is used so that when thedoor connection is closed, it will turn on a light sothat if people don't want to go to the website, they canjust check to see if the lights on. Because if it's on,someone's in the back.

 Michael and I got back from thelocal building recycling center with a couple of 8-1/2inch stoplight lenses, and a big ole' length of heavyduty extension cord that I basically connected all ofthe neutral lines from the two lamps, and from the pigtail weneeded for the power supply. And since we're ina pretty simple kind of scroungy moodon this project. We got out theGorilla tape and used that to tape ourstoplight lenses to the front of our lamps. On to the actual code. First, we have all of ourlibraries that we're importing. The important oneshere are BeautifulSoup, which makes it reallyeasy to modify HTML, and RPi.GPIO that makes itso you can access the GPIO pins on the Raspberry Pi. Then we set up our GPIO pins. I'll put a linkin the show notes of what you need forthe Raspberry Pi. It's a little bitdifferent than Arduino you just need aresistor, pretty much. We have our input pinsthat are connected to the doors and our outputpins, which trigger the lights. We got some genericvariables here.

 And then the HTMLfile that we'll be actually updating, andonto our first function. Some cool things, thethreading.Timer function, will let you just keepon running a function, so that's how it willrun indefinitely. Getting access toour count variables so that we can check howlong someone's been in there. Not as creepy as it sounds, it'sjust so that after 10 minutes, if the door has been closed,I'll post a status saying, "Hey, the door might just beclosed and no one is in there, so check it out." Then, onto actuallychecking to see if our connections are closed. So if it is closed, we'll sendthe status one and the bathroom number to ourupdateHTML function. Show that in a minute. We'll update the timer, andthen we'll turn on the light. Otherwise, we'llsend status zero, we'll reset the count timer, andthen we'll turn the light off. And then, if the counthas been over 600 seconds, we will send statusthree in the bathroom. Same idea for bathroom two. So our updateHTLMfunction, it'll accept the bathroomnumber that's being sent and the status number.

 First we open up our HTML file. If the bathroom is closed,then it will modify the DIVs and all that. And BeautifulSoupmakes things so easy. If the bathroom is open,then we'll say it's open. And if the bathroom hasbeen closed for a long time, we'll say, "Jiggle the handle." Then we need toupdate the website. Right now, I'm usinga script called Dropbox uploader that justuploads files to Dropbox, and I'm sending itto the public folder, so that I can do a generic URLforwarding from the website that I bought. And then the end ofthe function is just to set off the initial functionso that the timers can kick in. And that's about it. And from all of that, we cango to our and refresh. I should probablyput an auto refresh, but you can see thatbathroom one is now closed as of this time. And bathroom two isopen at this point. And of course, our lightsalso signal that information. And if you want to learn moreabout Python or the Raspberry Pi, you can check out oursponsor at They've got tons of Pythontutorials up and running with the Raspberry Pi. Lots of good information. You'll learn something, andyou'll help out the show Hey Trace, what do youthink of I think it's awesome. I even put it on my dashboard. Currently, bathroomone is closed. Sadly. Hey Trace, do younotice the new addition? What new addition? Look over there. Oh my God! You got some lights. Wow. That's a really massivelight. [LAUGHING] OK. Bathroom one is open. Bathroom two is open.  I'm going to pee. Hey relax. It's empty. [LAUGHING] See you around. People are so into this project.

 I've had like 10 peoplecome up to me and be like, this is the bestthing I've ever seen. That's because people hatewaiting for the bathroom because they can befinishing a script, or calling somebody toset up an interview, or ordering something onAmazon, or ordering cannoli, or rearranging theirpencils or doing anything, but sitting in line andwaiting for the bathroom. The thing is though,while we used, you know, a sensor, plusRaspberry Pi, plus Python to create a device that tellspeople when the bathroom is open or not open,you could use this to say with a thermalsensor to send you a message when your freezeris getting above 32 degrees. Uh-huh. Or if there's water in thebasement of your house. You can useBeagleBoards, Arduinos, anything that you want. It just depends on how muchmoney you want to spend, whether you need networking,and what you like to code in. What do you like to code in? Do us a favor, tell or @DIYTryin And do usa favor, share the show with your friends. Tell them to subscribe. If you haven't subscribed,you should subscribe. Find out all about it on ournew website, It's slick. It's awesome. It's shiny. And there's buttons thathelp you share and get the RSS feeds, or howeveryou want to watch us.

 We're on the Roku. We're on iTunes. We're everywhere, man. And of course, whereon But go to the new websitebecause it's nice. It's nice. I'm Patrick Norton. I'm Michael Hand. We'll see you nextweek on DIYTryin. Making lives great. That's it for thisepisode of DIYTryin. We're going to quitwhile we're ahead. This is our last episode. Sorry. Yeah. It was-- [LAUGHTER] We need a shot of that face. We were joking, but weneed a shot of that face.  That was awesome.