This week I posted an update on twitter and facebook of my latest project where I took all the wired window and door sensors from my old security system and integrated them into SmartThings. Many people said they want to do the same thing and I know that my usual step-by-step spoon-fed tutorial was in order.
$50 and a few hours later, all the contact sensors from the old pre-wired security system hooked into @smartthings! pic.twitter.com/syGulurrEb
— kendrick coleman (@KendrickColeman) February 20, 2016
When I purchased my home last year, I had a tough decision to make. We were going to do a lot of renovations and I had to decide whether to keep the existing alarm system or just rip it all out. The alarm system was installed in the early 90's and the plastic keypads and motion sensors that were installed in the walls had turned yellow from sun damage. I spent a week researching how to replace everything because it didn't make sense to rip out a perfectly good security system, but finding replacement parts proved impossible. It was an old GE Caddx Security System that is now obsolete. I ended up disconnecting the power and back up battery, removing the keypads and motion sensors, and letting the contractor cover all the holes with drywall. However, all the contact sensors within the doors and windows were still there and in-place. I should note, that my smoke alarms were never integrated into the security system so I didn't have to worry about those. I also ended up ripping out the alarm speaker/siren thinking I would never need it again. In hindsight, I should have kept it.
A few months went by and I got SmartThings and started getting into home automation. The first thing we have to do is lay down an initial requirement. SmartThings is the only platform that can be used for this tutorial. I really like SmartThings because it's a plug-n-play home automation set up that has a ton of integrations as well as an Open API & Developer Program that allows lots anyone to create custom apps and devices. It's pretty cheap to get started with home automation, but it can get expensive quick. I finally found myself with a few hours of spare time and decided to look into integrating the door and window sensors into SmartThings. I could have spent $25-$45 per door and window on Z-Wave Contact Sensors, but it would be ugly, expensive, and a hassle replacing batteries every year. After a few hours of research, spending a long time reading comment threads, buying equipment and waiting for delivery, I was able to have it fully integrated. It's awesome to give this old technology new life. So lets do this!
Materials:
- SmartThings Hub v2.0 - $99
- SmartThings Shield for Arduino - $35 (no longer available. Use one of the WiFi/LAN methods now)
- SainSmart Mega 2560 (cheap knock off of the Arduino Mega 2560) - $15 (also on eBay for $14 shipped)
- Male-to-Male Jumper Wires - $4 (super cheap on eBay ($1.50). May as well get some Male to Female as well for future use)
- Old Apple iPhone/iPad Charger (big brick ones work best since they are 2A) or a 5V USB Charger - $0
- A longer USB Printer cable laying around. The one that comes with the Mega 2560 is only 1' - $0
- Wire Nuts (lots really small ones, 1 really big one) or Solder gun, solder, and electric tape - $0 (I had it all laying around)
- Scissors or razor/box cutter - $0
Step 1. Examine Your Security System
Your security system may have been segmented out into zones for doors or windows to belong to a certain "zone". You can keep everything in zones if you want, but I opted to have every individual device work on its own. Of course, this is also going to depend on how many devices you have. I have 5 doors that act individually, but all my windows are wired together before they reach the panel so they all have to be on a single circuit. The example we are going to use for this tutorial will not have any motion sensors, sirens, etc. It's going to only be the contact sensors, which are very simple to use.
One thing you may notice on the old security system is that there are resistors that limit the amount of power making a loop on each circuit. This was necessary on the old system because the sensors may have been ruined if too much current was running through them. However, it's your choice to keep or remove them with this new setup. I opted to remove them because the Arduino will be responsible for sending a low voltage signal through the digital GPIO pins and doesn't need an Ohm resistor. It will keep it cleaner.
Step 2. Know Your Security System
The wires going into your panel may or may not be labeled to which device they lead to. Mine were not. For me to properly wire everything, I needed to test each circuit and see what the wires were leading to. To test it, we need to load a Sketch on the Arduino. Download and Install the Arduino Software on your computer. You can also go ahead and place the SmartThings board onto the Arduino. Look at the pin numbers to make sure they line up. Pin 1 on the Shield lines up with Pin 1 on the Arduino, etc. Grab 2 jumper wires. Plug one jumper wire into IO Pin 7, and plug the other jumper wire into a GRD (ground) IO input. Next, plug the Arduino into the computer. Go to tools and set the type of Arduino to Mega.
Create a new Sketch by copying and pasting this code (taken from parralax.com)
const int switchPin = 7; // // Reed switch to digital pin 7 void setup() { pinMode(switchPin, INPUT); // switchPin is an input digitalWrite(switchPin, HIGH); // Activate internal pullup resistor Serial.begin(9600); } void loop() { Serial.println(digitalRead(switchPin)); // Display current value delay(200); }
Hit the upload button. Click on the magnifying glass to bring up the Serial Monitor. Set the Baud Rate to 9600. At this point, you should see "1" scrolling on the screen every 200ms.
Here's the fun part. Unscrew the wires going to Zone 1. With your first pair of wires, connect one to the wire coming from Pin 7 and the other coming from GRD. You will not get shocked, the voltage is going to be really really low. If successful, there will now be "0"s scrolling on the Serial Monitor. Have someone go around the house and open doors or windows until you see "1" scroll on the screen. Label the pair of wires so you know what each wire is going to. Keep doing this for every pair of wires that are coming from Door or Window contacts.
Step 3. Build The Arduino Code (sketch)
Like I mentioned earlier, SmartThings has a Developer Friendly Open API and Daniel Orgochock created the ST_Anything Project. This is where all the magic happens. It's a project that has gained LOTS of attention. My goal of writing this blog was to create a noob-friendly version because the GitHub Project README goes deep pretty-fast and most people don't want to read 250+ comments. However, always pay close attention to the GitHub Project README as the source of truth because projects graduate and mature over time.
The key to learning all this was to look at the examples from my outdated/deprecated version which was used with ST_Anything_Doors and adapting to make sure the code made sense. Read through all the examples in the GitHub Project to learn how to integrate more things like PIR Motion Sensors, Sirens, Smoke Alarms, Furnaces and more. First part is to look at the Sketch that matches your hardware set up. In my instance, I use the ST_Anything_Multiples_Thingshield.ino sketch as my baseline but I only needed the contact sensors.
- Download/clone the repo using git git clone https://github.com/DanielOgorchock/ST_Anything
- Locate your Arduino directory
- Mac: ~Documents/Arduino
- Windows: C:\My Documents\Arduino
- Copy all the folders within the ST_Anything/Arduino/libraries folder from the cloned repo to Arduino/libraries. These libraries are necessary for the following code to work.
- Create a Sketches folder within your Arduino directory
- Create a folder inside of Sketches called ST_Anything_Doors.
- Create a new file called ST_Anything_Doors.ino and copy and paste the following code:
- Here's where you need to go through the code and edit lines 109-116, 158-1631 and 200-205 to fit your use case. Use my code as a starting block. Remember in part two where Pin 7 was used for our testing? This is where you specify pins 4-5, 7-13 and A0-A7 (analog only) for each of your devices. Do not use pin 0-3 or 6 as they are reserved. Add or remove lines as necessary. You must have your contact sensors listed as contact1, contact2, etc and use commenting for your own purposes. This will automatically create devices in SmartThings later. Remember to view the original sketch to add things like water sensors, smoke alarms, etc.
- Click the Verify button within the Arduino program and see if it compiles. If it compiles, then click Upload.
- Lastly, to facilitate communication between the shield and Arduino, grab 2 jumper wires. (If you are NOT using the Mega and using an Uno or something else you had laying around, you must read the Geeky Material on the ST_Anything GitHub Project README because serial communication is necessary.)
- Connect Pin 2 on the SmartThings Shield to Pin 14 on the Arduino Mega
- Connect Pin 3 on the SmartThings Shield to Pin 15 on the Arduino Mega
- Click on the Serial Monitor and you should see all the devices start to show up and some debug logging happening on the screen.
Step 4. Hook up the Arduino to your old security system
Now it's time to connect everything together. Match the GPIO Pin to one wire from the sensor. For instance within the code, I have the Front Door matched to GPIO Pin 5. Take a jumper cable and use your scissors or box cutter to clip one male end and strip it to expose part of the wire. Take one wire from the Front Door and use a small wire nut (or if you want to go full permanent, use a soldering iron) to tie the two wires together. Take the other male end of the jumper wire and plug it into GPIO Pin 5 on the SmartThings Shield. Continue doing that with every component.
We cannot forget to close the circuit with the ground. Take one jumper wire and all the second wires from every component and tie them together using a large wire nut. Take the other male end and plug it into one of the three GRD GPIO Pins.
Use your old Apple iPhone/iPad charger and the Printer USB cable to power up the Arduino
Step 5. Pair your SmartThings Shield to SmartThings
If you know your way around SmartThings, this step is easy. Start searching for new devices within the SmartThings App and then hit the Switch button that is physically on the SmartThings Shield. It will show up as Arduino Things. Click Done.
Step 6. Create a New Custom Device Type/Handler
- Go to the portal for the backend of your hub and login then go to My Device Types (https://graph.api.smartthings.com/ide/devices)
- Follow the directions on the README to install/create the custom device handlers within the Smartthings IDE
- Follow the directions for the Wifi/LAN or the ThingShield Device handler
- At this point, click on the Arduino ThingShield and you should see all your devices. Go around and test the sensors. Open and close some doors and you should see the device change state.
Step 7. Rename Child Devices
At this point, you should be all set up and have your devices working. Go into the SmartThings IDE and go to your devices and you can rename the individual devices per your setup. Refer to your code comments so you know what contact1 matches to vs contact2.
You done. pretty awesome, right!? Now each sensor can be added to the Smart Home Monitor or you can take a look at using Smart Alarm which is nice because it has delays built-in.