Martin's corner on the web

Controlling 433Mhz RF power sockets with a RFM12B module

One of the goodies of home automation is that you can actually turn appliances on and off based on some pre-defined conditions or via the Internet. Typically that task will require setting up a relay or multiple relays and hooking them up to a Internet-connected micro controller. The task isn’t that trivial if you would like to control several appliances scattered all over the house. One possible approach is to use radio controlled power sockets instead for the obvious advantage of wireless control. I purchased a set that is running on 433.92Mhz quite some time ago and have used it in manual mode since, the model I have is a “Duwi EMW200R” and can switch appliance up to 1000W. There are other models by the same manufacturer that can handle 3500W and I have seen a model that fits a DIN rail.

On the other side, while toying with the sensing shield, I was reviewing the RF12 sample code in the JeeLib and noticed this particular example of a RFM12B module being used to send OOK signals: https://github.com/jcw/jeelib/blob/master/examples/RF12/kaku_demo/kaku_demo.ino

As I already own the RF controlled sockets, I decided to capture their their protocol and be able to control them. Using a dedicated hardware 433Mhz OOK sender for the purpose is piece of cake using the Remote Switch library, I got the sockets switching on and off in less that 5 minutes, but my goal is to use the RFM12B instead. The reason for that is because there is a number of hardware platforms like the JeeNode, NanodeRF, SensingShield and so forth that do already have a RFM12B module installed and there is no need for extra hardware to do the controlling.

For the project I needed the following:

Hardware:

  • A 433Mhz OOK receiver so I can capture the RF signals from the remote control unit
  • Audio capturing circuit that interfaces the OOK receiver to my microphone jack. I used that same circuit when I captured IR signals for my earlier projects.
  • One of my sensing shields with a 868Mhz RFM12B mounted (yes, 868Mhz)
  • Optional: An audio loop back cable; I use it to replay recorded RF signals for the IR protocol analyzer.

Software:

  • I use Audacity to record the RF signals to a .WAV file but also to compare timings of my synthetic signal vs the original signal
  • IR protocol analyzer, a nice program that also works for RF protocol analysis
So, as a first step, I recorded the RF signal being sent to a .WAV file using Audacity. I then analyzed the timings and found out that it is a typical OOK protocol with 1304us for a zero or one, zero being a 326us high + 978us low and a one being 978us high and 326us low signal. I then edited the protocols.xml for IR protocol analyzer with the new protocol, rounded a bit the values so that they read ok:
<protocol name="Duwi">
 <description url="www.duewi.de">
 DUWI
 </description>
 <bitset>
 <bit value="1"> #990 _330 </bit>
 <bit value="0"> #330 _990 </bit>
 </bitset>
 <packet>
 <data name="byte0" length="8" bitorder="MSB" />
 <data name="byte1" length="8" bitorder="MSB" />
 <data name="byte2" length="8" bitorder="MSB" />
 </packet>
 </protocol>
This is an optional step, as the code is only 24 bits + a stop bit and you can do the reading by eye, but if you want to decode all buttons + all house codes it may come handy. In my case I just wanted to turn on and off the RF socket at House A, Address 1 so I didn’t dig in too much, although the protocol seems trivial, with interleaved codes.
The biggest challenge was the code itself, setting the timings took longer than expected. Remember that the RFM12B is an SPI device that has its overhead and latency, while dedicated OOK hardware eliminates these concerns. With lots of adjustments in the values, I was finally able to control my RF sockets using the RFM12B. As I set the module in 433Mhz mode, while the antenna is optimal for 868Mhz, there is notably shorter range. For a test, I soldered a 16.5mm antenna and the result was much better – about 10m. Still you have to remember that this module is designed for 868Mhz operations and range will be lower compared to the original remote control unit in all cases.
Here is my code for those interested:
// This example is based in JCW's KAKU RF sockets control code
// 2009-02-21 jc@wippler.nl http://opensource.org/licenses/mit-license.php

// Note that 868 MHz RFM12B's can send 433 MHz just fine, even though the RF
// circuitry is presumably not optimized for that band. Maybe the range will
// be limited, or maybe it's just because 868 is nearly a multiple of 433 ?

#include <JeeLib.h>
#include <util/parity.h>

// Turn transmitter on or off, but also apply asymmetric correction and account
// for 25 us SPI overhead to end up with the proper on-the-air pulse widths.
// With thanks to JGJ Veken for his help in getting these values right.
static void ookPulse(int on, int off) {
    rf12_onOff(1);
    delayMicroseconds(on + 150);
    rf12_onOff(0);
    delayMicroseconds(off - 200);
}

static void OOKSend(unsigned long cmd) {
    for (byte i = 0; i < 10; ++i) {
        for (byte bit = 0; bit < 25; ++bit) {
            int on = bitRead(cmd, bit) ? 1056 : 395;
            ookPulse(on, 1313 - on);
        }
	    delay(10); // approximate
    }
}

void setup() {
    Serial.begin(9600);
    Serial.println("\n[OOK_RF sockets]");
    rf12_initialize(0, RF12_433MHZ);
}

void loop() {
    Serial.println("off");
    OOKSend(0b0001010101010100010101000);
    delay(1000);
    Serial.println("on");
//    OOKSend(0b0111010101010100010101000);
    OOKSend(0b0101010101010100010101000);
    delay(1000);
}

 

14 thoughts on “Controlling 433Mhz RF power sockets with a RFM12B module

  1. Bram Werbrouck

    Do I need some more hardware?? At this moment I have just the emonTX, emonGlcd en Nanode RF (EmonBase) (from openenergymonitor.org) all using 868Mhz to communicate . If I understand this article, I can use power sockets on 433Mhz…

  2. admin Post author

    Well, that’s the beauty of it: you *can* use the 868Mhz RFM12B to control your 433Mhz power sockets with no additional hardware. Range is limited though, so pick the node that is closest to your appliance. Or alternatively you may issue a broadcast type of message and all the nodes (the emonTX, emonGlcd and the NanodeRF itself) can switch temporary in 433Mhz mode to issue the RF control command, then revert back to 868Mhz mode to continue their normal operation. That should be quite easy to achieve.

    1. Bram Werbrouck

      And what If I find power sockets on 868 MHZ, then I don’t have to switch between the MHZ’s I don’t know if they actually excist… I will google it up 🙂 Then I will try out your code

  3. Luis

    Hi Everyone,

    I’ve built a wireless data logging project with a nanodeRF (as a base) and a couple of remote RFM12B transceivers – everything working well.
    Something I would love to do is to hack/intercept the communications between my home power monitoring system wich is a CurrentCost (www.currentcost.com).
    It seems that this device uses the RFM12 to talk with the mains power clamp sensor in 433 Mhz band. I would like to at least try…

    Everythins is preatty self explanatory, even so, I would like to ask how did you recorded the RF audio signal?
    Best,

      1. Luís

        Hi again,

        Thank you very much for answering and the links.
        I will check and try that process as soon as I can.
        Thanks

    1. admin Post author

      This was just a proof of concept to be used in future projects, I only tested with direct code (the one shown in the post). There won’t be PHP, the web-connected NanodeRF or ArduinoEthernet+Sensing shield will serve normal HTML pages and the user will be able to issue a command. I don’t have immediate need to do it now, so that will be done sometime later.

  4. Bram Werbrouck

    Where can I find some PHP code that shows me an example to send a command to the NanodeRF ?? I have no id how to start…

  5. cybergibbons

    Thanks for the heads up with the IR protocol analyser… I’ve been using my own flakey decoder using CSV from a logic analyser for a long time.

  6. Pingback: Clapping to control RF power sockets | Martin's corner on the web

  7. Miguel

    Hello Martin,

    I’ve found that this sketch works really well with the recent Chacon (aka D.IO) power sockets.
    This is true for the first device of the A group.
    Can you please tell me how can I get the other devices to work? i.e.: what are the the bits that represent the device and what bits represent each group?
    Thank you for any help.