RaspHousy part 1

RaspHousy: house automation Part 1




There's some talk nowadays about the "connected home" or "smart home". I've seen some offerings in the local hardware store that attempt to improve upon tradicional HVAC (heating, ventilation & A/C) controls by learning your habits, or being able to connect to the internet. Same thing for sprinkler control but apparently much less advancement in that front.

I've been attracted to the idea of a smarter way to monitor and control those things, but those offerings in the market are too limited and most of the time they're quite pricey. On the other hand, since the Raspberry Pi came out I've been trying to come up with an excuse to get one and do something. Hm, a project was essentially begging me to take it. So I did.

I bought a Raspberry Pi, after deciding that I'd prefer that over an Arduino. Arduino is a nice programmable microcontroller with lots of I/O possibilities. But I decided I wanted to run some software in addition to the control/program functions themselves. I wanted a web interface, and nice monitoring graphs, and optionally alerts and historic metric collection. I needed something with a full OS.

At $35, the RPi is a pretty safe bet even if your particular project goes nowhere. You can still transform it into a nice DLNA media client and tape it to the back of your TV. Sort of like a Chromecast but open for tinkering. I had also recently learned about 1-wire and how it can be used to chain together temperature sensors and other devices. It's very practical and simple, but I found general consumer-level hardware for it a bit expensive. For instance, 8-channel relay will set you back $75. So I decided to use 1-wire only for the sensors ($5 a piece with no connecting sockets) and buy a general purpose 8-channel relay board from SainSmart for $13. I would drive the relays with the general purpose I/O pins in the RPi, and the 1-wire network would be connected via a USB adapter (although I did order two free engineering samples from dallas semiconductor for a controller chip but that one needs other electronic components to make it work).

So anyway, here's the howto for phase 1: the Raspberry Pi connected to the relay board, driving it safely with active-on-high (more on that later).

Phase I Howto

What we'll build is this: A RPi connected to a relay board, controlling it manually via commands given on the shell (optionally using the Python modules linked at the end which are work in progress).

What you'll need:

  1. Raspberry Pi Model B Rev. 2 (here for $41.16)
  2. SainSmart 8x Relay Module (here for $18.10)
  3. Breadboard and jumper wires (here for $8.86)
  4. 8x 2N222 NPN transistors (about $4 a dozen in RadioShack)
  5. 8x 2.2k resistors (less than $3 on RadioShack)
  6. 8x 10k resistors (less than $3 on RadioShack)

Total cost: $78.12

It is a lot of money for a "toy" that just controls some relays. But this is the basis for all the processing you'll need to have remote programming, monitoring, alerts and historic metrics collection.

The relay module

For different reasons apparently regarding manufacturing costs and simplicity, the relays are inactive on high voltage, and activate when signal voltage goes close to zero. The relay is not a "on-off" thing, it's really a "NO-NC" or normally open-normally closed circuit. This means each relay has three contacts, let's call them A, B, and C. When 'inactive', contacts A and B are connected, and when active, B and C are connected. With that in mind, if you want to drive this relay with the 3.3v digital I/O pins in the RPi, you have roughly 3 options:

  1. Drive it directly and have your GPIO pins be normally low. The relays be always active but your load is connected to contacts A and B so that when you inactivate the relay, your load receives voltage. That's very simple but has a few drawbacks. First, your relay module will be consuming energy constantly when it has all the relays in the active mode all the time. Second, and more importantly, when the relay module is not energized its normal resting mode will be to enable all your loads. That's not very convenient. 
  2. Drive it directly but keep the GPIO pins normally high, bringing them low when you want to activate a relay. That means your loads are connected to B and C and the resting mode for each relay means your loads are off. Nice. But the problem is, as soon as the relay module is energized, if your RPi isn't keeping those pins high, the relays will activate. So every time you boot up there could be flickering, even provided you energize the relay in sync with the RPi (more on that later, because the RPi may not be able to energize all the relays).
  3. Put some simple circuitry in between to ensure the relay always gets a high signal whenever it's energized and the GPIO pins are low, even if the pins are floating. This means you energize the relays with an external power supply and the RPi only provides the signal voltage, and it's rock solid without spurious activations and able to drive all 8 relays with the single RPi.
I went with option 3 since it's clearly the best and the additional work was easy enough that even I could do it. It's been some 25 years since I first played with transistors and resistors, and maybe 12 since I last did it (digital systems lab course at the University).

What you need to do is have a transistor pull down your voltage by connecting the collector and emitter whenever you supply the 3.3v at the base. A couple resistors round it up, avoiding stray floating voltages and ensuring the transistor receives the input needs for operation. Many other transistor types will do, but you'll need different resistors.

The diagram above was taken from this nice writeup I found. It's basically all you need for phase 1 so I'm not adding much value here. Anyway, once you connect everything, it will look like this:







For driving the relay using the GPIO pins on the RPi, I chose WiringPi, a library that was written to mimic what the original Wiring library does for Arduino. It provides standalone programs for controlling the GPIO pins ("gpio write 1 0") and a library you can link against for programming. I chose Python, so I used WiringPi2-Python.

Here are some demo videos of me playing with the relays and some christmas lights ('tis the season after all, you know):

Unfortunately my relay module is defective; 2 out of the 8 relays are "stuck" on the off position. If you tap on them slightly, they'll come on but you have to do it every time. So of course I'm returning it in exchange for a new one. Here's a video of me giving them a little tap.



Finally, here's a link to the project on github where I'm developing an API to control and monitor the relays using a RESTful interface in Python, with Protocol Buffers and web.py.

https://github.com/larojas/rh

It's a work in progress and at the time of this writing I only have the extensible web server with one plugin for WiringPi. The first release will be the full RESTful API that monitors and controls the relays, then I plan to add monitoring for 1-wire sensors. Later releases will include a web-based UI that connects to that server, and if time permits, an Android app that connects to that server as well.

In the following blog posts I'll talk about setting up the hardware for 1-wire sensors, hooking up the sprinkler valves, and setting up nagios and cacti for monitoring, alerts, and metrics gathering.

UPDATE: I also have some videos but sharing them directly in blogger has proved to be impossible. So it's probably gonna be YouTube and I'm too lazy for that right now. I'm just going to publish this post since it has been sitting in draft for a month (wrote it while on vacation).

UPDATE2: After returning the second Sainsmart relay board because of stuck relays, I bought a different brand which sells the same thing, and I mean the same freakin' board, for cheaper, and worked right the first time. Here it is.


Comments

Popular posts from this blog

Project 300ZX [11] - 'Tis the season for reupholstery

Project 300ZX [5] - Shift knob / e-brake handle