Building a home automation system – Power control (Part 3)

Just got myself a RFXtrx433 USB transceiver from RFXcom. It’s a tiny box which allows you the communicate with a wide range of home automation hardware that uses the 433 MHz band, check out the impressive list of supported hardware on their website. There is one store in Norway that sell these, and guess what, it’s in Strandgaten here in Bergen (walking distance) – http://www.smarthus.info/

There’s a guy named Kevin that have been working a nodejs library for it, available here: node-rfxcom. He’s made support for events and triggering of most of the protocols – it’s is under active development at the moment. It works quite well here for lighting2 equipment (Nexa / Home Easy based). I added support for lighting1 events (ARC – older protocol – uses code wheel), since it seems that Nexa still sell some equipment using this (the single wall button in my case).

So far I’m only using cheap Nexa plug-in modules and wall buttons, which seem to work well enough, but the plug-in modules aren’t exactly pretty. A lot of the lighting in my apartment is also hard-wired (halogen spots in the roof, no sockets), so sooner or later I’m going to have to get some real built in modules. This has to be done by a certified electrician and I want to be sure that I go for the right technology. I got a tip that insteon is planning on releasing european modules soon(tm), these are mesh-networked (bonus!). And since every receiver is also a sender/repeater I’m hoping they can be queried: “Hello lighting module A, what is your current dimming level?”

Anyway, I have the RFX unit up and running as a service on my home automation message bus now, so now it’s finally possible to subscribe for events from transmitters (ex: wall switch) and send orders to receivers (ex: plug in modules connected to lights). Subscribing to the coffee-machine and turning it off after 60 minutes, or turning off several lights/appliances if the last-man-out button near the exit door is pressed can now be done by:

Gist – 3923421

Now I need to find some suitable PIR (passive infra-red) motion sensors to use with the system, and also try to research which in wall technology to go for in the future – so that I can control more of the lighting here at home.

Flocking / Synchronization

There are a few behaviors in nature that seems really advanced, impressive and organic, that actually can be simulated on a computer using only a few simple rules. One of them is flocking – the “formation”-flying of birds or movement of schools of fish. There’s an old algorithm/simulation by Craig Reynolds called Boids (often used in screensavers and even used for modeling the flying bats in batman) that explains this pretty well. Make a few objects that react to their neighborhood according to the following three simple rules:

  • Separation – try to stay at least x units away from all neighbours.
  • Alignment – try to match the average direction of your neighbours.
  • Cohesion – steer towards the center location of your neighbours.

Flocking in paper.js

Click on the image above for a live demo 🙂

Implement the rules and depending on how you weight them you quickly get behavior that looks impressively organic. Was bored the other evening, and my coding fingers are itching since I’ve barely gotten to do any programming at work these last months – the meeting/planning vs programming ratio is through the roof. So I did a quick implementation for shits and giggles. Check it out here: http://goo.gl/eY8KY (or click the image above). I have no idea if it works with all browsers, but works with chrome at least.

There’s also a decent TED talk on the subject:

What’s it going to be used for? Notting really, but might be useful to know if I ever get to chance to program a large army of hunter killer robots with swarm capability.

The Shell – zsh, dotfiles, etc.

I decided to go for zsh again (instead of bash) as a default shell not that long ago. I’ve used it on and off before, it’s really powerful, but takes a lot of configuration to get access to all the nice features it offers. Whenever i reinstalled or changed computers I usually just returned to the de-facto bash shell.

These days configuration/plugin/themes collections like oh-my-zsh and the more recent fork prezto makes it really easy to get a a fully functional and good looking zsh up and running real quick. Git clone oh-my-zsh or prezto, activate the plugins you’re after and you got it 🙂

Zsh syntax highlighting

There are a few external plugins I really like as well – for example zsh-syntax-highlighting seen above. Clone and add to plugin list as describes. It offers similar shell syntax highlighting as the fish shell.

Oh, and the theme used above is from agnoster. Pretty minimal while giving a lot of information – using the powerline fonts (a series of patched fonts – a favorite among many vim users.) They’ve added pretty symbols to indicate things like branches, background jobs, root, etc. Looks a bit messy below, but that’s mostly because i wanted to try to show as many features as possible in the least amount of space.

Powerline

Works really well in iTerm2. The terminal to use if you’re on OS X.

I try to keep my dotfiles on github, that way it’s pretty easy to move between machines and keep them up to date. Nice to have access to all your aliases and settings on the machines you’re using. And whenever you’re on a new mac, to avoid going mental, just apply your collection of sane mac defaults. (“natural” scrolling my ass :p) 🙂

There are some pretty awesome collections of dotfiles on github – some worth checking out: holman’s, mathias’ and even whole projects like: dotfiles.github.com

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:

-gist-

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:

-gist-

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.

K7im9483-note
K7im9484

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: http://blagg.tadkom.net/2012/09/15/better-wr703n-antenna-mod/

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