HLW8012 energy monitor chip / module request

Posted on
  • Hi.

    I own SONOFF POW device where I successfully installed Espruino.
    I see there is still no module for HLW8012. Does anyone have a sketch script to handle it.

    I see on Arduino side there is already a library, we can take it over I guess.
    See here: http://tinkerman.cat/hlw8012-ic-new-sono­ff-pow/

  • Quite interesting device, this HLW8012...

    If you know how HLW8012 is connected to ESP8266, you should be able to whip something up in JS.

    As long as the frequencies emitted on pins CF (7) and CF1 (6) are not too high, you can use setWatch() to measure them. Place a setWatch() that increments a 0-initialized variable by 1 and measure the time it takes to reach a particular value. (The code example counts down / decrements).

    var SEL = ...   // 0(A)/1(V) // set pin SEL (8) is connected to
      , CF1 = ...   // A/V       // set pin CF1 (7) is connected to
      , CF  = ...   // power W   // set pin CF  (6) is connected to
      , vId = 0     // value ID, 0 for current [A], 1 for voltage [V]
      , v0  = 0.5   // V RMS / 1 Hertz
      , a0  = 0.015 // A     / 1 Hertz
      , c0  = 100;  // count for which the time is measured 
      , tC  = 0     // time Correction value to offset 'setup and tear down'
      , val = 0     // value measured [A/V]
      ;
      
    function m() { // measure
      digitalWrite(SEL,vId);
      var n = c0
        , t = getTime()
        , watchId = setWatch(function() {
            if (!--n) {
              t = getTime() - t - tC;
              clearWatch(watchId);
              val = (vId)
                ? c0 / t * a0
                : c0 / t * v0;
              console.log(val + ((vId) ? " V RMS" : " A" ));
            }
          }, { repeat:true, edge:rising, debounce:0 }); 
    
    function onInit() {
      m();
    }
    
    setTimeout(onInit,1000);
    

    Write to console may not be what you are looking for.

    To increase accuracy and keep the time kind 0f constant for a measurement, you can make a first measurement with a given count to get an idea of the frequency and an adjusted count for the second measurement. You may repeat the second measurement and take the average to increase accuracy... (assuming current stays stable during the measurement time).

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

HLW8012 energy monitor chip / module request

Posted by Avatar for Vasily @Vasily

Actions