phrants.net
28Jan/170

Standard House Alarm Wifi Conversion with Raspberry Pi

The local company that monitors the alarm system for my home recently contacted me to let me know that the 2G cellular radio band was being discontinued by the FCC and that I would have to pony up an additional $250 for a technician to come by and install a new 4G radio-- this being in addition to my $30/month service bill. It was a perfect reason for me to ditch the monitoring company and find a DIY solution.

9-16V LED (1.25 inch)

9-16V LED (1.25 inch)

The plan:

  • Piggyback onto my existing system with a wifi-enabled Raspberry Pi
  • Receive text and e-mail alerts when the alarm system is triggered
  • Receive an alert if the Pi stops running or loses internet access

Here's what I purchased to get this hack done:

  1. Raspberry Pi with Pixel pre-loaded (had this already)
  2. Black project box ($7 - link)
  3. Adafruit Digital Light Sensor ($6 - link)
  4. LED Boat Light ($14 - link)
TSL2561

TSL2561

My existing system consists of a DSC control panel and a PC1616 circuit board. The idea is straightforward: connect the variable voltage LED boat light to the Bell Circuit. Normally the Bell Circuit is used for an external audible siren, though many PC1616 boards just have a resistor between the two connections. When the alarm detects an intrusion, (additional) power is sent through this circuit.

I learned that power is always flowing through this circuit, so when I first connected the LED, the light was dimly lit. I found that when the alarm is activated, the light becomes twice as bright.

So?

  • Dim light : all is good
  • Bright light : something is bad

My Python code on the Pi just runs in a loop waiting for the Lux sensor to report that brightness has increased beyond the set threshold. It was straightforward enough to have the Python program send an e-mail to my phone. My cell provider (Verizon) also provides an e-mail address associated with my text messages, so I can receive a text alert at the same time.

The last piece of my Python program sends a ping request to my web server. If that server isn't pinged every 15 minutes, a running cron job on the server will e-mail me that my alarm system no longer has access to the internet. The added benefit is that I get e-mailed about any lapse in my broadband service from my ISP.

I decided to use a variable voltage light because I wasn't sure exactly what voltage was needed to flow through the PC1616 circuit as to not trigger a voltage alert from the unit. This light solution will take and use whatever it receives, up to 16 volts.

I've been impressed with the stability of the Pi-- it has yet to crash, despite running nonstop for several months. Again, if it does crash, I'll be alerted by my cron job that the server hasn't been pinged. Another great thing about the Pi, which runs headless, is that you can pull up the desktop environment in VNC viewer on your phone or laptop. The interface is great.

Here are some selections from the Raspberry Pi Code:

import smbus
import time
import smtplib
import requests

# Get I2C bus
bus = smbus.SMBus(1)

# TSL2561 address, 0x39(57)
bus.write_byte_data(0x39, 0x00 | 0x80, 0x03)
bus.write_byte_data(0x39, 0x01 | 0x80, 0x02)
time.sleep(0.5)
data = bus.read_i2c_block_data(0x39, 0x0C | 0x80, 2)
data1 = bus.read_i2c_block_data(0x39, 0x0E | 0x80, 2)

# Convert the data
ch0 = data[1] * 256 + data[0]
ch1 = data1[1] * 256 + data1[0]

def pingmysite():
response = requests.get('http://yoursite.com/pi_ping.php?id=your_session_key')
if response.status_code == requests.codes.ok:
print ("site pinged successfully")
time.sleep(15)

def onthehour():
print (time.strftime("%H:%M"))
rightnow = time.strftime("%H:%M")
#new code checks every 15 mintues, same idea...
if ":00" in rightnow:
print ("on the hour")
pingmysite()

def emergency():
server = smtplib.SMTP('smtp.xxxx.com', 587)
server.starttls()
server.login("user@xxxx.com", "password_here")
msg = "Raspberry Pi Alarm activated!"
server.sendmail("user@xxxx.com", "your-email@xxxx.com", msg)
server.sendmail("user@xxxx.com", "2025554444@vtext.com", msg)
server.sendmail("user@xxxx.com", "2025553333@vtext.com", msg)
server.quit()
time.sleep(60)
#checklight()

 

def checklight():
data = bus.read_i2c_block_data(0x39, 0x0C | 0x80, 2)
data1 = bus.read_i2c_block_data(0x39, 0x0E | 0x80, 2)
ch0 = data[1] * 256 + data[0]
ch1 = data1[1] * 256 + data1[0]
print ("Full Spectrum(IR + Visible) :%d lux" %ch0)
print ("Infrared Value :%d lux" %ch1)
print ("Visible Value :%d lux" %(ch0 - ch1))
luxvar = ch0 - ch1
if luxvar > 10:
print ("ALARM ACTIVATED!")
emergency()
time.sleep(5)
onthehour()

rightnow = time.strftime("%H:%M")

while rightnow != "26:00":
rightnow = time.strftime("%H:%M")
checklight()
checklight()