You are reading a single comment by @Frida and its replies. Click here to read the full conversation.
  • Ok, time to bring the toys back.
    With this test I have set a LED to pin, active low.

    First test.

    function tt () {
      ht = {};
      //ht.pin = D0; // esp8266
      ht.pin = D12; // esp32 pico4
    
      setInterval (function () {
        var d = "";
        ta = getTime ();
        tb = 0;
    
        pinMode (ht.pin, 'opendrain');
    
        ht.watch = setWatch (function (t) {
          d = 0 + | (t.time-t.lastTime> 0.000050); // 50 usec (time in sec)
        }, ht.pin, {edge: 'falling', repeat: true});
    
        //pinMode(ht.pin, 'opendrain');
    
        setTimeout (function () {
          pinMode (ht.pin, 'input_pullup');
          tb = getTime ();
          console.log (tb-ta);
        }, 1000);
    
        setTimeout (function () {
          Clear watch (ht.watch);
          console.log ('d =' + d);
          console.log (process.memory ());
        }, 200);
    
    
      }, 3000);
    }
    
    TT ();
    
    

     
    It only gives me short pulses, and not for 1 second, as expected.
    If I remove clearWatch (ht.watch), I get a short pulse, then full pulse length and it uses all the memory.

    On esp8266 there are no problems.

    Next test.

    function tt () {
      ht = {};
      //ht.pin = D0; // esp8266
      ht.pin = D12; // esp32 pico4
    
      setInterval (function () {
        var d = "";
        ta = getTime ();
        tb = 0;
    
        //pinMode(ht.pin, 'opendrain');
    
        ht.watch = setWatch (function (t) {
          d = 0 + | (t.time-t.lastTime> 0.000050); // 50 usec (time in sec)
        }, ht.pin, {edge: 'falling', repeat: true});
    
        pinMode (ht.pin, 'opendrain');
    
        setTimeout (function () {
          pinMode (ht.pin, 'input_pullup');
          tb = getTime ();
          console.log (tb-ta);
        }, 1000);
    
        setTimeout (function () {
          Clear watch (ht.watch);
          console.log ('d =' + d);
          console.log (process.memory ());
        }, 200);
    
    
      }, 3000);
    }
    
    TT ();
    
    

    Here pinMode has been moved to after set_Watch and I can determine pulse length on LED.
    Also works on esp8266.

    And now for a workaround, so it works on both esp8266 and esp32 pico4 as I have.

    Hope this is something you can use?

    // testWatch02
    
    function DHT22 (pin) {
      this.pin = pin;
    }
    
    DHT22.prototype.read = function (cb, n) {
      if (! n) n = 10;
      var d = "";
      var ht = this;
    
      // start watching for state change
      ht.watch = setWatch (function (t) {
        d = 0 + | (t.time-t.lastTime> 0.00005);
      }, ht.pin, {edge: 'falling', repeat: true});
    
      pinMode (ht.pin, 'opendrain'); // PB obs It sets the output low immediately
    
      // raise pulse after 10ms
      setTimeout (function () {pinMode (ht.pin, 'input_pullup'); pinMode (ht.pin);}, 10);
    
      // stop looking after 100ms
      setTimeout (function () {
        if (ht.watch) {ht.watch = clearWatch (ht.watch); }
    
    d = d.substr (1); // PB you are a genius
        var cks =
            parseInt (d.substr (2.8) 2) +
            parseInt (d.substr (10.8), 2) +
            parseInt (d.substr (18.8), 2) +
            parseInt (d.substr (26.8), 2);
        if (cks && (((cks & 0xFF) == parseInt (d.substr (34.8), 2))) {
          cb ({
            raw: d,
            rh: parseInt (d.substr (2.16), 2) * 0.1,
            temp: parseInt (d.substr. (19.15), 2) * 0.2 * (0.5-d [18])
          });
        } else {
          if (n> 1) setTimeout (function () {ht.read (cb, - n);}, 500);
          // else cb ({err: true, checksumError: cks> 0, raw: d, temp: -1, rh: -1});
          else cb ({err: true, checksumError: cks> 0, raw: d, temp: -1, rh: -1, n: n});
        }
      }, 100);
    };
    
    DHT22.prototype.getName = function () {
    return 'Frida';
    };
    / *
    exports.connect = function (pin) {
        return new DHT22 (pin);
    };
    * /
    
    
    function dd () {
      esp = 0;
      if (esp) {
        pin = D0; // esp8266
        // esp8266 version 2v00.68 (c) 2018 G.Williams
        var ESP8266 = require ('ESP8266');
        ESP8266.setCPUFreq (80);
        console.log (ESP8266.getState ());
      } else {
        pin = D12; // esp32 pico4
        // esp32 pico4 version 2v02 (c) 2018 G.Williams
        console.log (ESP32.getState ());
      }
    
    // var dht = require ("DHT22a"). connect (pin);
      var dht = new DHT22 (pin);
    
      console.log (dht.getName ());
    
      setInterval (function () {
    
        dht.read (function (a) {
          console.log ("Temp is" + a.temp.toFixed (1) .toString () +
                      ", RH is" + a.rh.toFixed (1) .toString () +
                      "\ Nraw:" + a.raw +
                      "\ nn:" + a.n +
                      "\ Nerr:" + a.err +
                      ", cse:" + a.checksumError);},
                 1);
    
        setTimeout (function () {
          console.log ("E-Flags: [" + E.getErrorFlags () + "] \ n \ n");},
                   1000);
    
      }, 3000);
    
    }
    
    
    dd ();
    
    // save ();
    

    And some outputs.

    Temp is 35.1, RH is 92.3
    raw:010000001110011011000000010101111111111110
    n: undefined
    err:undefined, cse:undefined
    E-Flags: []
    Temp is 35.1, RH is 92.4
    raw:010000001110011100000000010101111111111111
    n: undefined
    err:undefined, cse:undefined
    E-Flags: []
    Temp is 35.1, RH is 92.5
    raw:010000001110011101000000010101111100000000
    n: undefined
    err:undefined, cse:undefined
    E-Flags: []
    Temp is 35.1, RH is 92.6
    raw:010000001110011110000000010101111100000001
    n: undefined
    err:undefined, cse:undefined
    E-Flags: []
    Temp is 35.1, RH is 92.7
    raw:010000001110011111000000010101111100000010
    n: undefined
    err:undefined, cse:undefined
    E-Flags: []
    Temp is 35.1, RH is 92.8
    raw:010000001110100000000000010101111100000011
    n: undefined
    err:undefined, cse:undefined
    E-Flags: []
    Temp is 35.1, RH is 92.9
    raw:010000001110100001000000010101111100000100
    n: undefined
    err:undefined, cse:undefined
    E-Flags: []
    Temp is 35.1, RH is 93.0
    raw:010000001110100010000000010101111100000101
    n: undefined
    err:undefined, cse:undefined
    E-Flags: []
    Temp is 35.1, RH is 93.1
    raw:010000001110100011000000010101111100000110
    n: undefined
    err:undefined, cse:undefined
    E-Flags: []
    Temp is 35.1, RH is 93.2
    raw:010000001110100100000000010101111100000111
    n: undefined
    err:undefined, cse:undefined
    E-Flags: []
    
    

    Happy coding.

About

Avatar for Frida @Frida started