I started with some of the examples that came with the software and just went from there. Many thanks to the developers for the source code and the forum. Feel free to use/modify/make suggestions. I pasted this code using the "code" tag but it might look funky. Does anyone have a better way to attach an in-line file?
- Code: Select all
[size=85]
import time, logging, smtplib
from email.mime.text import MIMEText
from alarmdecoder import AlarmDecoder
from alarmdecoder.devices import SerialDevice
SERIAL_DEVICE = '/dev/ttyAMA0' # Location of your AD2device
BAUDRATE = 115200 # Default Baud Rate
LOG_FILE = '/var/log/alarm.log' # Make sure you have permission to write this file
EMAIL_USERNAME = 'user@server.com' # Username to login to your SMTP server
EMAIL_PASSWORD = 'password' # Password for your SMTP account
EMAIL_HOST = 'mail.pop.com' # SMTP server
EMAIL_FROM = 'user@server.com' # Your email address
EMAIL_TO = 'user@server.com' # Destination email
SMS_TO = 'XXXXXXXXXX@txt.att.net' # Email to SMS gateway address
EMAIL_SUBJECT = 'Alert from House Alarm!'
def main():
try:
# Set up logging of all INFO (or more) messages using specified format for timestamp
logging.basicConfig(filename=LOG_FILE, level=logging.INFO, format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %H:%M:%S')
logging.info('---------- Starting log ----------')
# Set up an event handler and open the device
# I believe that AD2serial & AD2pi use a serial interface
# and the AD2USB uses a USB interface
device = AlarmDecoder(SerialDevice(interface=SERIAL_DEVICE))
device.on_zone_fault += handle_zone_fault
device.on_zone_restore += handle_zone_restore
device.on_arm += handle_arm
device.on_disarm += handle_disarm
device.on_power_changed += handle_power_changed
device.on_alarm += handle_alarm
device.on_low_battery += handle_low_battery
with device.open(baudrate=BAUDRATE):
while True:
time.sleep(1)
except Exception, ex:
logging.error('Exception:'+str(ex))
def handle_zone_fault(sender, zone):
logging.info('Zone Fault:'+str(zone))
def handle_zone_restore(sender, zone):
logging.info('Zone Restore:'+str(zone))
def handle_arm(sender):
logging.info('Armed')
Send_Email(EMAIL_FROM, SMS_TO, EMAIL_SUBJECT, 'Alarm-Armed')
def handle_disarm(sender):
logging.info('Disarmed')
Send_Email(EMAIL_FROM, SMS_TO, EMAIL_SUBJECT, 'Alarm-Disarmed')
def handle_power_changed(sender, status):
if status: #if panel on AC power then
pStatus = 'Panel on AC power'
else: #if panel on Battery power then
pStatus = 'Panel on Battery Power'
logging.warning(pStatus)
Send_Email(EMAIL_FROM, SMS_TO, EMAIL_SUBJECT, pStatus)
def handle_alarm(sender, status):
if status: #if alarm was just triggered
aStatus = 'Alarm Triggered'
else: #if alarm was just cancelled
aStatus = 'Alarm Cancelled'
logging.warning(aStatus)
Send_Email(EMAIL_FROM, SMS_TO, EMAIL_SUBJECT, aStatus)
def handle_low_battery(sender, status):
logging.warning('Low Battery:'+str(status))
Send_Email(EMAIL_FROM, EMAIL_TO, EMAIL_SUBJECT, 'Alarm-Low Battery')
def Send_Email(FromAddr, ToAddr, MsgSub, MsgText):
msg = MIMEText(MsgText)
msg['To'] = ToAddr
msg['From'] = FromAddr
msg['Subject'] = MsgSub
server = smtplib.SMTP(EMAIL_HOST)
server.login(EMAIL_USERNAME, EMAIL_PASSWORD)
server.sendmail(FromAddr, ToAddr, msg.as_string())
server.quit()
if __name__ == '__main__':
main()