You are reading a single comment by @AkosLukacs and its replies. Click here to read the full conversation.
  • Hi. I'm use esp32-wroom-32 and DHT22 sensor with DHT22.js script.

    I call the read function every 5 seconds and every time I get less and less memory, the log below as it was at the beginning of work was 1917, and after a while it was already 1887 and so on until there is an error.

    What my problem?

    My code:

      setInterval(function() {
        this.dht.read((response) => {
    
          console.log(response)
          console.log(process.memory())
          console.log('_____')
    
        });
      }, 5000);
        
    

    My logs:

     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v04 (c) 2019 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    >
    {
      "raw": "100000010001011010000000011100001000100­001",
      "rh": 111.4, "temp": 45 }
    { "free": 1917, "usage": 383, "total": 2300, "history": 179,
      "gc": 0, "gctime": 1.989 }
    _____
    {
      "raw": "010000001000101100000000001110000100001­111",
      "rh": 55.6, "temp": 22.5 }
    { "free": 1929, "usage": 371, "total": 2300, "history": 179,
      "gc": 0, "gctime": 1.954 }
    _____
    {
      "raw": "010000001000110010000000001110000100010­101",
      "rh": 56.2, "temp": 22.5 }
    { "free": 1926, "usage": 374, "total": 2300, "history": 179,
      "gc": 0, "gctime": 1.96 }
    _____
    {
      "raw": "010000001000110100000000001110000100010­111",
      "rh": 56.4, "temp": 22.5 }
    { "free": 1922, "usage": 378, "total": 2300, "history": 179,
      "gc": 0, "gctime": 1.956 }
    _____
    {
      "raw": "010000001000110001000000001110001000010­101",
      "rh": 56.1, "temp": 22.6 }
    { "free": 1919, "usage": 381, "total": 2300, "history": 179,
      "gc": 0, "gctime": 1.965 }
    _____
    {
      "raw": "010000001000101000000000001110001000001­100",
      "rh": 55.2, "temp": 22.6 }
    { "free": 1915, "usage": 385, "total": 2300, "history": 179,
      "gc": 0, "gctime": 1.958 }
    _____
    {
      "raw": "010000001000101000000000001110001000001­100",
      "rh": 55.2, "temp": 22.6 }
    { "free": 1912, "usage": 388, "total": 2300, "history": 179,
      "gc": 0, "gctime": 1.96 }
    _____
    {
      "raw": "010000001000110001000000001110001000010­101",
      "rh": 56.1, "temp": 22.6 }
    { "free": 1908, "usage": 392, "total": 2300, "history": 179,
      "gc": 0, "gctime": 1.964 }
    _____
    {
      "raw": "010000001001010011000000001110001000110­111",
      "rh": 59.5, "temp": 22.6 }
    { "free": 1905, "usage": 395, "total": 2300, "history": 179,
      "gc": 0, "gctime": 1.961 }
    _____
    {
      "raw": "010000001001010111000000001110001000111­011",
      "rh": 59.9, "temp": 22.6 }
    { "free": 1901, "usage": 399, "total": 2300, "history": 179,
      "gc": 0, "gctime": 1.967 }
    _____
    {
      "raw": "010000001001010011000000001110001000110­111",
      "rh": 59.5, "temp": 22.6 }
    { "free": 1898, "usage": 402, "total": 2300, "history": 179,
      "gc": 0, "gctime": 1.963 }
    _____
    {
      "raw": "010000001001001001000000001110001000101­101",
      "rh": 58.5, "temp": 22.6 }
    { "free": 1894, "usage": 406, "total": 2300, "history": 179,
      "gc": 0, "gctime": 1.969 }
    _____
    {
      "raw": "010000001000111011000000001110001000011­111",
      "rh": 57.1, "temp": 22.6 }
    { "free": 1887, "usage": 413, "total": 2300, "history": 179,
      "gc": 0, "gctime": 1.966 }
    _____
    
  • As always, check you wiring. Loose wire can lead to communication errors. And the DHT22 uses a single wire bit-banged protocol, that's not as robust as for example I2C or SPI.

    The DHT22 module does retry up to 10 times by default if can't communicate with the DHT22 or there is a CRC error. With 500ms delay between each retry, worst case that's over your 5000ms interval.
    If you have a logic analyzer (even a <10USD one), you can check the communication for retries.
    Or if not, you can log the time between the start and end:

      setInterval(function() {
        var t0 = new Date
        this.dht.read((response) => {
          var t1 = new Date()
          console.log(response)
          console.log(process.memory())
          console.log('start:', t0, 'end:', t1, 'elapsed:', t1-t0)
          console.log('_____')
        });
      }, 5000);
    

    And if my theory is right, you will see some corelation in over 5 second communication and memory leak. Then you can either increase the interval to ~10 seconds, or you can pass in a lower retry count.

About

Avatar for AkosLukacs @AkosLukacs started