A while ago my door bell broke. So I did what any sensible person would; I decided that instead of buying a new one I would hook up an old bell I had to a Raspberry Pi and have the bell ring when someone pushed a button on my door. But then I though, “well, that’s a bit boring. My doorbell should tweet me, too!”
The long and the short of it is that I couldn’t get the mechanics of the bell ringer to work but had a lot of fun with the tweeting side of things because it involved my first shot at doing some Python code. And the really good news is that it’s ridiculously easy to accomplish.
This was roughly my process…
This first thing I did was to install a couple libraries for Python.
They will allow you to connect to Twitter using the API and read from/write to the GPIO (General Purpose Input/Output) pins on the Pi (respectively).
I hooked up a simple motor controller I bought for a few quid with a button and a few resistors – it probably wasn’t the best circuit in the world but it had been many years since I did anything with electronics and I had pretty much forgotten anything I may have learned! The circuit looked something like:
I did find some useful resources about using GPIO over at https://projects.drogon.net/raspberry-pi/gpio-examples/ – so you should check out that site if you, like me, are a beginner at playing with the hardware of the Pi.
Once I had everything hooked up I had to set up a Twitter developer account (I created a new account for purpose as I didn’t want to have my personal account tweet about someone ringing my door bell!). Logging in to https://dev.twitter.com/apps with the account I wanted to use, I created a new application. You’ll have to enter your application’s name (which has to be unique), description and url (I set mine up on a specific port on my network). You don’t need a callback url, so ignore that part of the form, and then create the app. When the app gets added, go to the application’s settings tab and select the access type of Read and write and save the settings. Then in the details tab click on the create my access token button.
Having followed those steps you should have your consumer key and consumer secret key. These need to be plugged in to the code at the relevant place.
And speaking of software, by using the two libraries mentioned above it was extremely easy to have my Pi tweet me when the button was pushed. The complete code is:
loading gist file...
I set it up so that it would randomly pick a tweet from a list. The list is literally a tweet per line and if I wanted I could use the date/time placeholders:
loading gist file...
Run it, push the button and enjoy a tweet and having your motor ramp up and down. Obviously, if you don’t want a motor you can pull out lots of stuff from the code and just leave in the twitter-relevant bits.
Now all I need to do is get a camera so I can have it tweet me a pic of the person that rang the door bell, too!