Monthly Archives: April 2017
Fun with Home Automation and Python

I was adding some new devices to my ISY home automation controller the other day and I was thinking it sure would be nice to get an image from one of my cameras to my phone when a sensor was triggered. Sure I could login to my home VPN connection ( because who would allow direct camera or any other access to your home from the internet) then connect to the camera and take a look. But that’s a lot of effort for something so small. Top that with I have been trying to do as much as possible with Python to get more experience with it.  I set out on a mission. It was actually pretty quick a couple hours and I was good to go. Here is what I did.

Create a python script that would act as a web server and accept post/put commands. ( the base of this code was found on the interwebs)

Python code used (sanitized of course)

#!/usr/bin/env python

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import SocketServer
import urllib
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP
import smtplib
import sys

class S(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header(‘Content-type’, ‘text/html’)
self.end_headers()

def do_GET(self):
self._set_headers()
self.wfile.write(“<html><body><h1>hi!</h1></body></html>”)

def do_HEAD(self):
self._set_headers()

def do_POST(self):
content_length = int(self.headers[‘Content-Length’])
post_data = self.rfile.read(content_length)
self._set_headers()
if post_data == “cam=garage”:
URl = “http://1.1.1.1/snapshot.cgi?user=username&pwd=secret”
urllib.urlretrieve(URl, “garage.jpg”)
self.wfile.write(“<html><body><h1>POST!</h1><pre>” + post_data + “</pre></body></html>”)
recipients = [‘h1tch@email.com’]
emaillist = [elem.strip().split(‘,’) for elem in recipients]
msg = MIMEMultipart()
msg[‘Subject’] = ‘Garage Image’
msg[‘From’] = ‘camera@email.com’
msg[‘Reply-to’] = ‘camera@email.com’

msg.preamble = ‘Multipart massage.\n’

part = MIMEText(“Garage Image”)
msg.attach(part)

part = MIMEApplication(open(‘garage.jpg’,”rb”).read())
part.add_header(‘Content-Disposition’, ‘attachment’, filename=’garage.jpg’)
msg.attach(part)

server = smtplib.SMTP(“mail.email.com:25”)
server.ehlo()
server.starttls()
server.login(“camera@email.com”, “secret-password”)

server.sendmail(msg[‘From’], emaillist , msg.as_string())

elif post_data == “cam=outbuilding”:
URl = “http://1.1.1.2:25111/snapshot.cgi?user=user&pwd=secret-passcode”
urllib.urlretrieve(URl, “outbuilding.jpg”)
self.wfile.write(“<html><body><h1>POST!</h1><pre>” + post_data + “</pre></body></html>”)
recipients = [‘h1tch@email.com’]
emaillist = [elem.strip().split(‘,’) for elem in recipients]
msg = MIMEMultipart()
msg[‘Subject’] = ‘Outbuilding Image’
msg[‘From’] = ‘camera@email.com’
msg[‘Reply-to’] = ‘camera@email.com’

msg.preamble = ‘Multipart massage.\n’

part = MIMEText(“Outbuilding Image”)
msg.attach(part)

part = MIMEApplication(open(‘outbuilding.jpg’,”rb”).read())
part.add_header(‘Content-Disposition’, ‘attachment’, filename=’outbuilding.jpg’)
msg.attach(part)

server = smtplib.SMTP(“mail.email.com:25”)
server.ehlo()
server.starttls()
server.login(“camera@secureitsupport.us”, “secret-pass”)

else:
self.wfile.write(“<html><body><h1>POST!</h1><pre>” + “NONE” + “</pre></body></html>”)

def run(server_class=HTTPServer, handler_class=S, port=80):
server_address = (”, port)
httpd = server_class(server_address, handler_class)
print ‘Starting httpd…’
httpd.serve_forever()

if __name__ == “__main__”:
from sys import argv

if len(argv) == 2:
run(port=int(argv[1]))
else:
run()

 

I just run this on a small linux VM on my home network that is also running homebridge using nohup.

 

This allows me to use a network resource on my ISY home automation controller to send a POST request telling me what camera I want to get a snapshot of. Of course there is a lot of room for improvement. Could make it smaller and not use the same code twice for the SMTP sections and the like. But works well for its purpose and I got to have a little python fun.