• I've been taking a look at reading data from an electricity monitor I have (An 'Owl' one) and that uses Manchester coding too.

    I'm not sure this is 100% correct, but here's my simple decoder which at least gives me something:

    var data = "";
    var last = 0;
    function onEdge(e) {  
      var d = 0|((e.time-e.lastTime)*1000);
      if (d==0) {
        if (e.state) data+=last;
      } else if (d==1) {
      } else {
        data = "";
        last = 1;
    setWatch(onEdge, B15, { edge:"both", repeat:true });

    It's designed to execute relatively quickly, so again it's not very easy to read. Basically:

    • This one expects the data length to vary between 0.66ms and 1.3ms, so it checks whether they are greater or less than 1ms long. It does this by multiplying by 1000, and then converting the number to an integer (using 0| - a JavaScript trick). You might have to change the multiplier in your case (depending on the protocol).
    • Then, d==0 for a small pulse, and d==1 for a long pulse
    • In manchester coding, because of the way the bits are output, you get two small pulses if the bit stays the same and one large pulse if it changes. So if the pulse is small, we ignore one of them (by checking e.state)
    • Then we just add the digit we think we got to the 'data' string, and when we get something we're not expecting we output what we had and then zero it.

    I soldered right onto the insides of my electricity monitor so I'm not getting any noisy signals. You'll probably have to do some extra checks before outputting the data, so the noise doesn't cause lots of stuff to be output. Even a simple if (data.length>16) ... might be enough


Avatar for Gordon @Gordon started