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).
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 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
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:
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.