phrants.net
4Juin/200

Raspberry Pi Alarm Update : 3 years later

The Raspberry Pi home alarm conversion has been running non-stop for over three years. By my calculation, I have saved $1230 to date! I was paying $30/month for 2G radio monitoring, plus $50+ a year to the local police department for emergency response.

The hardware has not changed at all-- I've only added a small backup UPS for the system, which also powers the router and modem. I have made some minor changes to the code, which I am including below.

Error handling : I was (and still am) somewhat new to Python, so my original code did not gracefully handle errors. An inability to contact my personal server would often crash the script. I rewrote the "pingmysite" function as follows:

def pingmysite():
--try:
----response = requests.get('https://server.com/alarm.php?mypiekey=798793475ukjsghhdfghdfghwghtkjhkjsfrtg', timeout = 1)
----if response.status_code == requests.codes.ok:
-----print ("site pinged successfully")

--except: # catch * all exceptions
----e = sys.exc_info()[0]
----print ( "Error: %s " % e )
----no_server()
--time.sleep(25)

System health : It occurred to me that at some point the small boat light that has been dimly glowing for three years may eventually burn out, and I would be none the wiser. This led me to write the "emergency_dark" function to complement the original "emergency" function. This changed the checklight function as well:

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 ("New Visible Value :%d lux" %(ch0 - ch1))
--luxvar = ch0 - ch1
--if luxvar > 25:
----print ("too damn bright!")
----emergency()
--if luxvar < 2:
----print ("too dark! no power or burnt out light bulb!")
----emergency_dark()
--time.sleep(5)
--onthehour()

E-mail woes : My script connects to a mono-purpose Gmail account, which only works with the "less secure" features activated in the advanced settings. Google started to force these accounts into "more secure" mode if they weren't being used on a regular basis. This led me to add a "daily_report" function, that simply sends an e-mail to me once a day with the message "Your Pi server is online".

def daily_report():
--#for the e-mail server to stay active with Google
--server = smtplib.SMTP('smtp.gmail.com', 587)
--server.starttls()
--server.login("----------@gmail.com", "------------------------")
--msg = "Good news. Your Pi server is online."
--server.sendmail("----------@gmail.com", "----------@gmail.com", msg)
--server.quit()
--time.sleep(120)

Remote Server : It has happened a couple of times that my remote server has been unavailable to the script. It tries to reach it, but the site is down for some reason, and I wanted to know about this in a timely fashion. I wrote this function that is called from the earlier "pingmysite" function.

def no_server():
--#make sure my personal server is up and running
--server = smtplib.SMTP('smtp.gmail.com', 587)
--server.starttls()
--server.login("----------@gmail.com", "------------------------")
--msg = "Warning : Unable to connect to remote server. Make sure your website is online."
--server.sendmail("----------@gmail.com", "----------@gmail.com", msg)
--server.quit()
--time.sleep(120)

Concluding Thoughts : It has been over three years of completely reliable operation, and I regularly test the system to make sure everything is working. With the better error handling and system health reports, I am even more satisfied with this DIY project.