Building a home automation system – The broker and sensors (Part 2)

Every now and then (when I have time..) I do a bit of work on what’s going to become my home automation system. At the moment it’s pretty basic, it’s simply events going over a message broker that small stubs of code react to. Most of it runs on my raspberry pi and a few jeenodes (atmega328 + hoperf rfm12b radio) as wireless sensors.

Everything connects to a simple message broker built on ZeroMQ – and I love it! ZeroMQ is so great to work with, it takes most of the hassle of network socket programming away leaving you to concentrate on what really matters. There is no daemons to deal with (as with most other message buses), it’s lightning fast, but doesn’t really handle persistant messaging.

At the core of the system I have this running on my raspberry pi:


It’s very simple – just two ZeroMQ sockets – one running in a push/pull configuration, another running in a pub/sub configuration. A relay for my sensor nodes will connect to it and push it JSON formated data from the different sensors located in my apartment.

"nodeid": 2,
"temperature": 2.19,
"voltage": 335,
"counter": 1215,
"event": "sensor",
"timestamp": "2012-09-24T15:49:28.369Z",
"location": "refrigerator"
"nodeid": 3,
"temperature": 23.5,
"voltage": 369,
"counter": 20,
"event": "sensor",
"timestamp": "2012-09-24T15:52:08.450Z",
"location": "livingroom-bookshelf"

The broker will relay events such as these, the two examples are wireless sensor nodes located in my refrigerator (currently holding 2.19C and battery voltage of 3.35v) and the other one in my livingroom bookshelf (2.19C and 3.69v). The broker will receive these (and others), and send them out to subscribers of sensor events. It makes it really easy to make something that reacts to the temperature, you can connect to the broker and subscribe to sensor events in 2-3 lines of code in most languages. Example:


As you can see, it would be very simple to expand this and create something that let me know if a battery of a sensor was running low. Or something that would PID adjust a heating source in the living room. Or something that would warn me if temperature in the fridge was running high.. or.. or.. or.. Beauty of it is that you don’t have to think about network stuff like disconnects..

I’ve made a small program in C# running on my HTPC that registers a few global hotkeys. If I press ctrl-alt-o then a small event will be sent out from the C# program, to the message bus, to everyone that subscribes to “receiver” events. There is a small program that does this and relays the message to a microcontroller, which in turn talks to my Pioneer VSX-2016av surround receiver via the SR bus I talked about in an earlier post. For example, sending events like these to the broker will toggle the power of the surround receiver and turn the volume up 5 steps.

"event": "receiver",
"action": "power"
"event": "receiver",
"action": "volumeup",
"count": 5

Quick example above, sorry for the camera shake.

It’s getting there, now I have sensor data and control over the surround receiver. The next steps will be to expand the available sensor data – I wish to add a DHT22 Humidity sensor and also a light sensor (LDR?) to the sensor nodes soon. And also maybe PIR (passive ir). I also need to add control over power soon (lighting and heating).

At the moment they are only using a DS18B20 temp sensor. When I have all the components in place I’ll make a small PCB with room for all the components, the radio and a smaller atmel microcontroller. Currently they take up a bit more space than needed.


Better WR703N Antenna mod

I blogged about a WR703N antenna mod earlier, I found some hints about a better way of doing it on the openwrt forum. There’s a (0 ohm) shunt resistor in the antenna path – labeled J1. I undid the changes I did earlier, unsoldered the J1 resistor.

With the board oriented with the wired lan to the left, I then soldered the core of the antenna wire to the RIGHT pad of the now removed J1 resistor. There is a big empty pad “north” of J1, where the antenna shield can be soldered.


EDIT: See Diarmaid Ó Cualain comment below, he has been getting better luck leaving J1 bridged (and instead cutting the internal antenna) – while still soldering to the same location. There are two capacitor on the antenna track on the other side of J1, which could be the reason for this.

Pioneer SR-Link control

I have a decent (but aging) surround receiver connected to my HTPC – a Pioneer VSX-2016av. Because of it’s age it really doesn’t have any good ways to control it from a PC (USB or even serial).

As part of my home automation setup it would be great to be able to control some of the features on the receiver. Volume up, volume down, power off, power on, etc.. Most of the time I use a wireless keyboard as an interface to my HTPC (running XBMC) – and the remote control for the receiver is only really used for volume control. Would love to be able to control these directly from the keyboard.

I’ve tried to control this receiver via IR, and actually set up a microcontroller to record the IR signal from the remote today. After decoding the modulated (38 khz) pulses I was able to play back the same signal and do stuff on the receiver from a microcontroller via IR. This could work.. but you actually have to point the IR diode more or less directly at the front of the receiver – reflections doesn’t work (maybe I could try a more powerful LED?). Was thinking about mounting the LED directly on the receiver, but it looks ugly 😦

Then I noticed the SR-link (in and out) ports on the back of the receiver. It’s a system Pioneer (and some other companies?) use for linking hifi/video equipment. You can use it to relay IR control and even transfer configuration options (in SR+). So I hooked up a microcontroller to the SR input port – you can use a mono 3.5″ jack – signal is on the middle pin. Ground can be taken directly from the chassis (shield on RCA-port for example).

Spent a while experimenting with this, by listening to the SR-output port while using the remote control. The signal is high (5v) as default, and pulled low on every IR pulse. Tried to replicate this using a microcontroller connected to SR Input on the receiver – wasted a lot of time beliving the signal should be 38 khz modulated – just like IR – but alas – no need for modulation – it won’t actually work if you modulate. Finally got it working – awesome! Now i can control my receiver from a microcontroller.

My goal is to get this directly connected to a Raspberry PI (which is going to be the HUB of my home automation system). If I can I really want to skip the microcontroller – and do it directly from the rasp-pi. I ported my microcontroller code to the PI, but it seems that I can’t get the timing accurate enough (running in userland).

Microsecond timing accuracy is a bitch in linux, I guess I could do this in kernel space – but that’s a lot of effort. Guessing I’ll probably have to go the microcontroller route. Also wish I had a decent scope, so that I could see how much jitter I’m dealing with here.

WR-703N External Antenna Mod (DIY)

UPDATE: Don’t do this mod, there are better techniques. Example: Solder directly to PCB antenna, or trying the J4 bridge mod as described here:

Maybe you’ve heard about the TP-Link WR-703N router? It’s a small “pocket”-router with wlan, 1x lan, 1x usb, Atheros chipset, 32 mb ram, 4 mb flash. It can run OpenWRT, and it’s really really cheap ($23 on ebay currently). They require very little power to run (100ma ish) and are powered via a micro-usb connector. And they have great hack potential (come on.. $23 for a linux thingy with lan, wlan, usb, gpio, etc..). You can upgrade the flash memory to at least 8 Mb, and even upgrade RAM to 64 mb if you have access to a hot air rework setup.

I’ve received two, and have just ordered a third, and have quite a few projects I want to use them for. Battery powered mesh network is one, great for getting networking in weird locations, they can run for many hours on a single 18650 battery. Also thinking about using one for a “umbrella warning sign” near the front door (I live in Bergen (Norway), a city with over twice the annual precipitation of the “Rain City” Seattle). A simple laser cut acrylic umbrella logo with the router’s pcb behind – using GPIO to drive a LED if rain is expected the next 8-10 hours.

Ebay link (be sure to buy the blue version!)

The routers have a tiny internal PCB-antenna, so on one of them I’ve added an external SMA connector. There are several ways of doing this, one is to solder the new antenna connector to the top of the existing PCB-antenna. A chinese website (google translate..) suggested desoldering a capacitor “upstream” and connect to the pads it used. I tried this, but will try both to see which works the best later.

Buy a SMA connector on Ebay with pigtail. Pry the blue cover off the router. Extract the PCB. Locate C114 on the backside. Desolder it (easiest is to place the soldering iron parallel to the capacitor so that you’re heating both pads). Solder the antenna core to the pad closest to the flash memory IC. Solder the antenna shield to the other pad (see image above). Cut the existing PCB antenna track.

Use a 6mm drill bit to make a suitable hole for the antenna connector (make sure there is room for the PCB under the connector).