Home automation using simple Python, JS, Arduino and elementary circuits

This picture makes the following long story, short. The rest of this post will describe my ideas behind this schematic.

What is a good way to connect some home-made circuits? This is the essential question of any home automation: how to connect low-level, raw, electronics to high-level fancy front-end web or AI systems. It turns out there are many, many ways. Unfortunately, for me, I have something very specific in mind and I’m dealing with a lot of home-made electronics (because it saves money and increases power).

The Hardware
Since I have many sensors I can not use the GPIO-deficient Raspberry Pi, so I will instead use the Arduino. I’ll go into the description of the sensors later, but suffice to say there will be at least 11 necessary pins (after de-multiplexing) which is what Arduino does best. Arduino can then connect to the Raspberry Pi via USB and communicate via Serial. The Raspberry Pi then runs a program that listens to the Arduino about the states of the sensors and lights. Again, I’ll describe the sensors in great detail later.

The Software
The RPi has to handle messages between the Arduino and the front-end (either a web-page or a AI). I decided that websockets are the best way to do this (versus CGI or PHP) – their are plenty of libraries and they are easy to use and they perform great interrupts so you don’t have to constantly poll for data. I’m using Python with tornado for a server and websocket-client for a client. The server simply echoes all the messages and the messages are prefaced with the target recipient in the header and the light/motion sensor states are in succession. This way the total state of the system can get passed between everybody and then everybody can sync their information with it.

The Front End
I find it very simple to describe houses in terms of architectural layouts. Thus, I wanted the front end to essentially describe the layout of the housing area with clickable lamps and motion sensors that blink upon relieving input. This is what I came up with:
Motion sensor example
Everything in this GIF is built using SVG, using the svg.js library. SVG makes things quite nice because every drawing is also an element that can submit queries (i.e. the lamp shown can actually be clicked to be turned on and off). Also SVG scales well with webpage size and all that other good stuff. The client part of the webpage is very similar to the Python program – it uses javscript and the Websockets to communicate with the server.
This GIF actually comes from the working system. The blinking “red light” comes from a motion sensor going off which sends the signal to the Raspberry Pi which is sent to the server which broadcasts it to the clients (the Raspberry Pi and the Web page). Unfortunately I don’t trust the internet enough to post the url to this webpage because I don’t want you all turning on and off my lights. But let me know if your interested and I can share on a person-to-person basis.
Github code is forthcoming.



  1. Great work, i like how everything/different parts are working together in harmony and the challenge of how to connect low-level, raw, electronics to high-level fancy front-end web. I don’t know but i think you could replace Python with C language (CGI)

    1. Thanks. I probably could do CGI – I’m better with Python so I just used that :). I’m a fan of the Python websockets, they are pretty sweet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: