This instructable is all about monitoring the state of relays using a Raspberry Pi. In the particular case we will have a look at a sophisticated Telegärtner DoorLine Pro intercom which holds two voltage-free relays. Normally, those are used to trigger an electric door strike or a doorbell but together with a Raspberry Pi can be used to send out Twitter messages.
So, why would you possibly need a tweeting intercom? – Well, first of all you are not limited to sending tweets only. Theoretically, you could kick off any Raspberry Pi project so you are only limited by your creativity 😉 We chose to go for tweeting since the DoorLine Pro intercom comes with an integrated motion sensor which can be configured to trigger the two available relays. Imagine you are away from home and someone tries to break into your house: The intercom motion sensor would trigger the relay, the Raspberry Pi would detect the state change and take a picture tweeting it alongside an alert message and time-stamp.
If your intercom is equipped with a touchscreen featuring a code-lock function (like the DoorLine Pro does) you could also install it at the front door of your super secret server farm. Whenever someone types in the (correct) PIN triggering the door strike, the Raspberry Pi would take a picture/video and log the activity.
Of course, you could also do it just for fun and trigger fireworks / smoke machine / trap door etc. 🙂
This instructable will work with any intercom equipped with relays but to get the most out of it the intercom should contain enhanced features like a motion sensor, code lock function or an integrated telephone system.
Step 1: Needed Hardware
For the very basic setup (monitoring the state of a voltage-free relay without utilizing any doorbell/door strike at the same relay) the following hardware is required:
- Intercom (w/ voltage-free relay contacts)
- Raspberry Pi (w/ internet connection if you wish to send out Tweets)
- a couple of male-female jumper wires
For the advanced setup you additionally need:
- some more wires
- electric door strike or doorbell
- Raspberry Pi camera module (only in case you’d like to take pictures as well)
- soldering iron
- 12V AC or DC power supply
- 1 x rectifier (if AC PSU used)
- 1 x 12V to 3.3V step-down converter
Step 2: Wiring: Basic Setup
That’s the easy part considering you don’t want to switch a doorbell or a door strike via the same relay: Just connect the 3V3- and the GPIO 6 pin with the relay contacts as shown in the wiring diagram above.Note: In this experiment we didn’t utilize a door strike / doorbell at the same relay which would require a voltage being applied. Hence, a rectifier (if using an AC PSU) and a step down voltage converter would have to be interposed in order to not damage the Raspberry Pi. We will show you this setup in the next step 😉
Step 3: Wiring Cont’ed
In case you want to monitor the relay state AND use eg. an electric door strike at the same relay the setup gets a bit more complex. In this case we have to apply a voltage to the relay (12V DC in particular but you could also apply an alternating current – remember to install a rectifier right before the DC/DC step-down converter if you decide to go with AC).
- Connect the positive PSU pole with the first relay contact.
- Connect the second relay contact with the positive poles at the door strike and the DC/DC step-down converter (at the 12V side).
- Connect the negative PSU pole with the negative poles at the door strike and the DC/DC step-down converter (at the 12V side)
- Connect the converters’ positive pole (at the 3.3V side) with GPIO 6 pin at your Raspberry Pi and the negative pole with any GND pin.
Step 4: Preparing for the Magic
We use a Raspberry Pi Model B, Rev. 2 running Raspbian. Since we wrote the code in Java we need to install the JDK first:
sudo apt-get update && sudo apt-get install oracle-java7-jdk
We also need to install WiringPi:
sudo apt-get install git-core (in case you haven’t installed GIT yet)
git clone git://git.drogon.net/wiringPi
git pull origin
(Now that we got wiringPi let’s check whether the wires are connected to the correct GPIO pins. The position of the GPIO pins differs depending on which Raspberry Pi model and revision you use. Remember: For our basic setup we use the 3V3 and GPIO #6 pins, for the advanced setup the GPIO #6 and GND pins. Just type “gpio readall” into the command line interface and you’ll get an overview about the GPIO pin locations on your Raspberry Pi model.)
Last but not least, Pi4J:
curl -s get.pi4j.com | sudo bash
Since our code snippet is based on the Pi4J GPIO listener example we will use those directories for the sake of simplicity. In order to send out some tweets via the Java program, download and open the latest twitter4j.zip, navigate to /lib/ and extract the twitter4j-core-x.x.x.jar to /opt/pi4j/lib/.
The tweet function requires (of course) a twitter account and you also have to create a Twitter App so that you can eventually access the API. Once you finished that, visit the “Keys and Access Tokens” section within the App management. Make sure your app has read and write permission. As indicated in the screenshot above, you need to implement several keys into your Java file.
Step 5: The Coding
As noted above we built our code snippet based on the GPIO State Listener Exampleprovided by the Pi4J team. This example program is capable of listening to GPIO pin state changes and it returns those in the command line interface.
The program is located in /opt/pi4j/examples
You can open the program with any editor of your choice and modify it yourself or just copy our version into the folder. We added a simple if-statement which triggers a tweet (don’t forget to implement your API tokens) as soon as the state changes to “HIGH” (door strike relay activated). If you’d like to take picture with the R-Pi camera as well and save it with a timestamp, you may use the following code:
java.lang.Runtime runtime = java.lang.Runtime.getRuntime();
java.lang.Process pic = runtime.exec(“raspistill -o /home/pi/” + pic_time.format(cal.getTime()) + “.jpg”);
Make sure the timestamp got a proper format. The timestamp “time” which we use for tagging the CLI messages is defined as:
SimpleDateFormat time = new SimpleDateFormat(“dd.MM.yyyy, HH:mm:ss“);
This format won’t work as a filename for the pictures since the process can’t be executed. Therefore introduce another format such as:
SimpleDateFormat pic_time = new SimpleDateFormat(“ddMMyyyy-HHmmss“);
When you are ready to rumble compile the Java program:
javac -classpath .:classes:/opt/pi4j/lib/’*’ -d . Listener_Tweet.java
and execute it:
sudo java -classpath .:classes:/opt/pi4j/lib/’*’ Listener_Tweet
Step 6: Results and Conclusion
Finally, after all the wiring, configuration and coding you can watch your intercom pumping out tweets 😉
The YouTube video above shows how it works: In this case we called the intercom from a DECT handset and activated the door strike via key sequence.
We hope we could contribute a little bit to the instructable universe. Please don’t be too harsh – it’s the first time we ever did something like that and over the years we got used to our quick-and-dirty MacGyver approaches 😀
Credits to: The Pi4J Project, Wiring Pi Project, Twitter4J Project