· Monitor Spartan 650 security system
· Monitor home smoke alarms
· Monitor overhead garage
· Monitor temperature
· Monitor for changes to the network
· Send emails and text messages on alert conditions
· Remote activation of smoke alarms
· Remote activation of overhead garage door opener
· Logging of events
· Level of the alerts is only sent to email addresses that match that level (Info, Warning, Critical)
· Send SMS via email
Things I learnt:
· How to interface between GTK and an SQL database, under Python
· How to use a logic leveler
· Using multiple threads to monitor things concurrently
· Inter-program communication using sockets
· Interfacing between a C program and Python, using Cython
I am writing this some weeks after getting all of this working, so I might have not included all of the details of the steps I took to getting this to work. If you have questions, post a comment and I will get back to you.
Spartan 650 home security system
I got interested in home monitoring with the Raspberry PI when I started to look at home security systems for my son and saw that they all had interfaces that could be accessed over the internet. I wondered if I could do the same with my 30-year old Spartan 650 security system. What I decided to do is have the Raspberry Pi (RPI) act like a keypad. The keypad has 4 wires, which I determined to be power, ground, data and clock (based upon reading the manual and the labels on the panel). I read some on the internet on DSC alarm panels, so I expected some sort of pulse sequence to communicate with each other. The security system is a 12V and the RPI only wants a maximum voltage of 3.3V. I used a voltage divider to drop the voltage to what I needed. This was only going to be one way and I wanted bi-directional, but it would work for now (more on the bi-directional later). One thing I learnt about voltage dividers is that you need to connect the ground on the security system and the RPI together. I tried reading the change from low to high in Python by did not get any consistent reading. I expected that I should at least see something pretty regular on the clock line, but I did not. I needed to see these signals and started to research using oscilloscopes. I found a USB unit that runs on a Windows computer and ordered it and when on to build the program I was going to use to do the monitoring, alerting etc.
I decided on using Python, having some experience with it went I played with Maemo on a Nokia N900 some years ago. I decided I was going to break the functionality into two programs – one to do the monitoring and the other to user interface (GUI) and have the programs talk to each other. (This is before I understood about Xwindows and that the RPI could show multiple programs at the same time). I used SQLite databases to keep the data. I was familiar with GTK but decided I was going to use GLADE to build the interface. I was able to do almost all of the GUI on Windows and then copy the whole thing to the RPI. When eventually I did copy everything to the RPI, I had a number errors and found out that I had developed under Python3 and GTK2+. I decided that I was use the same versions on the RPI, but this caused a few problems that needed to be overcome. As I neared completion of the program, I still was some weeks away from the delivery of the USB oscilloscope. I started to research using the RPI as an oscilloscope and that is when I ran into PIGPIO.
This is a set of libraries and programs for interfacing with the RPI. They have a oscilloscope program too! With this tool I was able to determine that the clock signal was only 20 microseconds long. Oops, Python is way too slow to process signal changes that fast. I was going to have to use some other programming language to do that. A little more Googling around and I decided to use C. By this time I was pretty invested into Python, so I only wanted to implement this specific function in C and leave the rest in Python. Using the PIGPIO libraries I was able to get the C program to extract the data the security system. Yeah! Now all I had to do was interface the C program with my Python program.
This seemed to be the easiest method of interfacing C to Python, I ran into 2 problems. The first is that the installed version of Cython seemed to only work with Python2. I had to rebuild Cython under Python3 and the second problem I had was with my threads. My usage of PIGPIO used a call back program to get the readings from the security system. I also wanted to use a callback program to let the Python program know when I had some data. I ran into Signal 11 (segment fault) errors. I changed it so that the C program returned data on the main thread rather than using a call back. This worked fine but the next problem I had was that some of my threads seem to hang up and not process independently. More research told me that this is because Python has locks that prevent concurrency in some cases. I was able to overcome this problem by telling Cython that some programs should run as NOGIL and this got around the thread hang up problem. The logic levelers arrived, so I am going to try bi-directional, so that I can arm and disarm the alarm system remotely.
Logic levelers for bi-directional communication
So I tried to use the logic levelers to transfer the pulses on the 12V alarm system to the 3.3v of the RPI. No go. I turns out that the logic levelers can only respond to signals that are a minimum of 300 microseconds. I need something that will respond to something that is around 20 microseconds. I am still only uni-directional with my voltage divider. I am certainly not experienced in electronics, but from my googling around, it appears that there does not exist something that will transfer 12V pulses of 20 micro-seconds to 3.3 V.
I then tried to use the logic levels with my smoke detector (see below). Basically I wanted a bi-directional relay. It didn’t work. The problem is that when the power is removed from the low high, the circuit completes on the high side. Not good to have the smoke detectors going off every time power is removed from the RPI.
Temperature monitoring with the DS18B20
When I bought the RPI, buyapi.ca also had a 5 pack of the temperature sensors. This are pretty sophisticated devices that measure the temperature and transmit the temperature digitally on 1-wire. I had one working while I was waiting for the oscilloscope to arrive, so I made up 2 more, to monitor the temperature in my detached garage, inside the house and outside. Fortunately, when I ran an underground service to the garage some years ago, I ran an additional pipe filled with 5 pairs of telephone wires. My father is an electrician and things of these things. They sat unused for 30 years, but I am making good use of them now. The outside temp monitor is just stuck into the cold air intake on my furnace. Not totally accurate, but good enough for my purposes. I learnt a few things here. Firstly, I put a resistor in with each sensor, but as a read more, it is just a pull-down resistor and since all of the sensors are connected to the RPI in parallel, I probably only needed 1 resistor (seems to work fine with 1 resistor per sensor. Second thing I found out was that my sensors did not work. I went back to having only one sensor. Still no go. I thought maybe I screwed up the RPI, because I attached the sensors to the RPI when it was powered up (I know, not a good idea), so I started to google around to see if I could use a port other the GPIO4. That is when I found an entry that says the following change needs to be made:
sudo nano /boot/config.txt
add the following line to the bottom :
Got to love Unix. Making changes with no notification. Or at least none that I was able to find. The third thing I discovered about temperature sensors is that they sometimes give bad readings. This seems to happen on the first reading, so I assume that it is a timing issue (like starting the reading halfway through a sending). Anyway, I needed to add a test on the CRC field of the reading to make sure that I had good reading. On to monitoring the garage overhead door.
Overhead door monitoring
When I bought my new garage door opener, it came with the garage door monitor. It was a simple wireless system that showed a green light when the door was closed. I measured the voltage of the green LED and it was around 2.7V, perfect for the RPI. Besides logging with the door is opened and closed, I send an alert if the door is opened for more than 10 minutes. The only thing I am having problems with is that the two components of the garage door monitor seem to lose communicate with each other once in a while. I am still trying things, but probably will make a bigger antenna, or move the units closer to each other or scrap the whole thing and move to a magnetic switch.
Even though I have the system alerting me to significant events, I still wanted view the GUI on occasion. What I did was install OPENVPN and TightVNC. I am now able to securely connect to my home network and then remotely connect to the RPI. As an added benefit, I am able to access my NAS (although I was able to do the DLINK cloud service to access it before setting up a VPN. As a side benefit, I am able to use VPN back to my home network to access services like Songza when I am out of country.
Garage door opener
I took the garage door opener switch at the door and found that it was just a simple switch. I soldered two wires on to this switch and tested by touching the wires. The door opened! I then bought a relay that is for an RPI and used this to simulate the pressing of the garage door open button.
Network Intrusion detection
Since I have a computer on the network running on the network, I thought I could monitor my network. I now monitor for new devices on the network, which is like an intrusion detection. I installed a program called ARPSCAN to get the data. I have learnt a couple of things from looking at the data. One – I can tell when people come and go in the house, based upon their cell phones connecting and disconnecting from the home network. Two – I have multiple routers in the house – some as hubs for multiple wired devices in a room and some for wireless access points in places that the main wireless router does not get a good signal. Anyway, some of these routers seem to go missing once in a while.
I am pretty anal about backups, so I thought I would share with you how I do my backups on the RPI. I installed SAMBA on the RPI and from a windows computer, I use my syncing software (Allways Sync) and copy the PI directories to my NAS. The syncing software is able to manage versions, so I effectively have version management as well as backup. Quarterly, I take the SD card out of the RPI and put it into a card reader on my Windows computer and use True Image to take an exact copy of the card. I think I am covered for minor disasters (While a also copy the NAS Raspberry pi directory to the cloud (ASUS Webstorage) I am not backing up my SD card backup to the cloud).
Step 1: Smoke Detectors
I have wired smoke detectors in my house and they are wired in a way that if one alarms sounds, they all sound, the way that they do this is by sending a 9V signal on the 3rd wire. If I send 9V from the RPI I can turn on the smoke detectors. Not sure why I would want this, other than just because I can. I connected the smoke detectors to the RPI to get alerts when the smoke alarms go off and also to be able to set off the alarms on command. Interesting problem with this setup. When I shutdown my monitoring service, the smoke alarms sounded. I had a GPIO.cleanup() call in my program shutdown. Apparently cleanup turns all of the GPIO pins to input pins. Apparently this causes the voltage on the GPIO’s to go high. I removed the cleanup call and had to set up GPIO to ignore warnings of GPIO’s in use. I ended up creating my own bi-directional logic leveller. The circuit below is what I came up with after extensive googling other circuit. If anybody has a comment on my circuit, let me know. I would like to know if it did anything wrong with the circuit.
Step 2: Android App
I wanted to be able the garage door from my cell phone, so I wrote a small Android app to communicate with the RPI and send a command to click the garage button. I didn’t see any value in publishing to the play store so I didn’t parametrize my settings so I have hardcoded my router name in the program. The android device must be connected to the home network for this to work as I am not opening up any ports on my router.
· To help debug my disappearing routers, maybe I could setup a syslog server to get log messages from the routers.
· I would like to be able to get alerts when I am not connected to a cellular network. When I leave the country, the roaming charges are crazy, so I switch my phone into airplane mode and use an app called Fongo. Unfortunately, Fongo does not provide an email interface to its SMS service. I think I will try to programmatically interface with Hangouts or Facebook Messenger (which are the messaging apps I use when out of the country)
· Since the RPI is on all of the time, I am thinking of using it as a bit torrent client. Not sure exactly how to do this and how to have other programs make requests to the RPI to initiate a download. More investigation is required.