You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • Hi,

    Thought you might find this interesting. I'll try and post it into a module/tutorial soon, but this code decodes the time signal from the DCF77 time transmissions which cover europe on 77.5kHz.

    Someone's selling receivers on eBay here or you can almost certainly rip one out of an old radio clock.

    var minuteData = "";
    var lastData = getTime();
    
    // Decode 4 bits into a number
    function decode4(s) {
      return (0|s[0])*1 + (0|s[1])*2 + (0|s[2])*4 + (0|s[3])*8;
    } 
    
    // xor all items in s and return the result (for parity checks)
    function xor(s) {
      var r = 0;
      for (var i=0;i<s.length;i++) r^=s[i];
      return r;
    }
    
    // decode the DCF77 time transmission
    function decodeMinute(d) {
      console.log(d, d.length);
      
      if (xor(d.substr(21,7))!=d[28]) { 
        console.log("Bad minutes");
        return;
      }
      var minute = decode4(d.substr(21,4)) + decode4(d.substr(25,3))*10;
      if (xor(d.substr(29,6))!=d[35]) { 
        console.log("Bad hours");
        return;
      }
      var hour = decode4(d.substr(29,4)) + decode4(d.substr(33,2))*10;
      if (xor(d.substr(36,22))!=d[58]) { 
        console.log("Bad date");
        return;
      }
      var day = decode4(d.substr(36,4)) + decode4(d.substr(40,2))*10;
      var doy = decode4(d.substr(42,3));
      var month = decode4(d.substr(45,4)) + decode4(d.substr(49,1))*10;
      var year = decode4(d.substr(50,4)) + decode4(d.substr(54,4))*10;
      console.log(hour+":"+minute+", "+day+"/"+month+"/"+year);
      
      var date = new Date(2000+year, month-1, day, hour, minute, 0, 0);
      console.log(date.toString());
    }
    
    function onSec(e) {  
      // Work out what bit we got
      var d = e.time-e.lastTime;
      var bit = (d<0.15)?0:1;
      // if we had a 2 sec gap then it's the beginning of a minute
      if (e.time-lastData > 1.5) {
        decodeMinute(minuteData);
        minuteData = "";
      }
      lastData = e.time;  
      // now add this bit of data
      minuteData += bit;
    }
    
    setWatch(onSec, dataPin, { edge:"falling", repeat:true, debounce:75 });
    
About

Avatar for Gordon @Gordon started