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

About

Avatar for allObjects @allObjects started