New joint DHT11/DHT22 module

Posted on
Page
of 4
Prev
/ 4
Last Next
  • d[18] === d.charCodeAt(18) ? ...feel a bit... (I do not want to say it). ;-)

    NB: 1st is much shorter! ...lets go 'fix' RFID Reader...

  • d[18] === d.charCodeAt(18) ?

    d[18] is like d.substr(18,1) - so it still returns a string. But in JavaScript numeric strings are treated as numbers when involved in maths, so it's just a 1 or a 0.

    Not great, but it's buried deep in a module, and users will be more worried about it being small and fast :)

  • ...so my expression d[18] === d.charCodeAt(18) evaluates to false... with d being a string of 19 or more characters... and in other words: no fixing of the RFID Reader code, a type converted numeric character is not the same as the ascii code of that character ('0' is 48, '1' is 49,...), and I needed the ascii code... --- now I feel double... and not only a bit...

  • @Gordon: now it works correct - thanks

  • @Gordon this does not work for me. I'm using AM2302, ESP8266

    >reset()
    =undefined
     _____                 _
    |   __|___ ___ ___ _ _|_|___ ___
    |   __|_ -| . |  _| | | |   | . |
    |_____|___|  _|_| |___|_|_|_|___|
              |_| http://espruino.com
     1v85 Copyright 2016 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    Flash map 4MB:512/512, manuf 0xe0 chip 0x4016
    > 
    
    {
      "raw": "010000000110011011000000010011000011001­1011",
      "rh": 41.1, "temp": -30.4 }
    Temp is -30.4 and RH is 41.1
    { "err": true, "checksumError": false,
      "raw": "",
      "temp": -1, "rh": -1 }
    

    It is getting err: true, if I unplug/replug the data pin from the AM2302 then sometimes it reads like this negative value.

    This is the code what i'm using now:

    var pin=D2;
    var dht = require("https://raw.githubusercontent.c­om/espruino/EspruinoDocs/master/devices/­DHT22.js").connect(pin);
    
    setInterval(function() {
      dht.read(
        function(a){
          console.log(a);
          console.log("Temp is "+a.temp.toString()+
                      " and RH is "+a.rh.toString());
    });}, 1000);
    

    Using this is wrong to:

    var dht = require("DHT22").connect(pin);
    
  • So it works the first time, but not subsequent ones?

    What about the old version, which is:

    var dht = require("https://raw.githubusercontent.c­om/espruino/EspruinoDocs/f884681b9b1d76d­c8685249afac6f2b4ce2a1542/devices/DHT22.­js").connect(pin);
    
  • This is what I get from this old version:

    >reset();
    =undefined
     _____                 _
    |   __|___ ___ ___ _ _|_|___ ___
    |   __|_ -| . |  _| | | |   | . |
    |_____|___|  _|_| |___|_|_|_|___|
              |_| http://espruino.com
     1v85.tve_master_cee7141 Copyright 2016 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    Flash map 4MB:512/512, manuf 0xe0 chip 0x4016
    >echo(0);
    =undefined
    { "temp": 25.2, "rh": 46.1 }
    Temp is 25.2 and RH is 46.1
    { "temp": 25.2, "rh": 48.4 }
    Temp is 25.2 and RH is 48.4
    { "temp": 25.2, "rh": 48 }
    Temp is 25.2 and RH is 48
    { "temp": 25.2, "rh": 49.9 }
    Temp is 25.2 and RH is 49.9
    { "temp": 25.3, "rh": 49.9 }
    Temp is 25.3 and RH is 49.9
    { "temp": 25.3, "rh": 50 }
    Temp is 25.3 and RH is 50
    { "temp": 25.3, "rh": 49.8 }
    Temp is 25.3 and RH is 49.8
    { "temp": 25.3, "rh": 49 }
    Temp is 25.3 and RH is 49
    { "temp": 25.3, "rh": 48.5 }
    Temp is 25.3 and RH is 48.5
    >clearInterval()
    =undefined
    

    Dont know when it works with the new. If I plug and unplug about every second, some times, it write out some negativ values, but the other part is just no data.

  • @MaBe told me to set the cpufreq to 80, this is better now I have readings, but I have negativ values in the temperature what is wrong.

  • Ok, so it works fine at 80, but not 160? That sounds like it's not really the module's fault then.

    Does the temperature look correct (but just negative?)

    Maybe you could look at the raw field and decode it manually to see what's going on - or at least post it up? If you look at the posts above you'll see that I actually had to change the code in order to get non-negative temperature, and that @MaBe was able to test it and get it working fine (I guess on ESP8266) so I don't know what is going wrong.

    Do you have a 'normal' DHT22 that you can test with, to see if it's your AM2303 that is doing something strange?

  • The negative temps readings are due to an error in line 39 of the module.

            temp : parseInt(d.substr(19,15),2)*0.2*(0.5-d[1­6])
    

    should be

            temp : parseInt(d.substr(19,15),2)*0.2*(0.5-d[1­8])
    

    as the data is offset by 2 leading bits. As such the sign is picking off the wrong bit, which inadvertently makes the temperature sign dependent on the value of the RH reading.

    My experience is that these sensors almost always fail on the first read as if there is some sort of initialization that the datasheet doesn't document. After that they always seem to read OK as long as the read rate is limited to 2 Hz per data sheet. Earlier code did not include the 500 ms delay (line 42) between repeat reads, which may explain some of the speed dependencies and why lengthening the read delay in earlier code helps (noted in another thread).

  • Thanks - that's crazy, I was sure I'd fixed that, but it seems not. I'll get that put in now.

  • I've just got around to testing this with my DHT22 (uploaded to an ESP-01 10 minutes ago) and I'm also occasionally getting negative numbers. As well as that I'm getting a lot of checksum errors that I wasn't getting with the old library.

    Here are some results, in case they're useful:

    {
      "raw": "010000000110010101000000001110001001111­000",
      "rh": 40.5, "temp": 22.6 }
    {
      "raw": "010000000110010100000000001110000101110­110",
      "rh": 40.4, "temp": 22.5 }
    {
      "raw": "010000000110010101000000001110000101110­111",
      "rh": 40.5, "temp": 22.5 }
    {
      "raw": "010000000110010101000000001110000101110­111",
      "rh": 40.5, "temp": 22.5 }
    {
      "raw": "010000000110010101000000001110000101110­111",
      "rh": 40.5, "temp": 22.5 }
    {
      "raw": "010000000110010101000000001110000101110­111",
      "rh": 40.5, "temp": 22.5 }
    { "err": true, "checksumError": true,
      "raw": "0100000001100101110000000011100001",
      "temp": -1, "rh": -1 }
    {
      "raw": "010000000110010111000000001110000101111­001",
      "rh": 40.7, "temp": -22.5 }
    { "err": true, "checksumError": true,
      "raw": "010000000110010111000000001110",
      "temp": -1, "rh": -1 }
    {
      "raw": "010000000110010111000000001110000101111­001",
      "rh": 40.7, "temp": -22.5 }
    {
      "raw": "010000000110010111000000001110000101111­001",
      "rh": 40.7, "temp": -22.5 }
    {
      "raw": "010000000110010111000000001110000101111­001",
      "rh": 40.7, "temp": -22.5 }
    {
      "raw": "010000000110010111000000001110000001111­000",
      "rh": 40.7, "temp": -22.4 }
    { "err": true, "checksumError": true,
      "raw": "010000000110010111000000001110000",
      "temp": -1, "rh": -1 }
    { "err": true, "checksumError": true,
      "raw": "010000000110011001000000001110000",
      "temp": -1, "rh": -1 }
    {
      "raw": "010000000110011000000000001110000001111­001",
      "rh": 40.8, "temp": 22.4 }
    { "err": true, "checksumError": true,
      "raw": "01000000011001100000000000111000",
      "temp": -1, "rh": -1 }
    {
      "raw": "010000000110011000000000001110000101111­010",
      "rh": 40.8, "temp": 22.5 }
    { "err": true, "checksumError": true,
      "raw": "010000000110011001000000001110000",
      "temp": -1, "rh": -1 }
    {
      "raw": "010000000110011001000000001110000001111­010",
      "rh": 40.9, "temp": 22.4 }
    
  • for me it works with 80 and 160 on ESP8266.

    and yes this was recommended by Gordon.

    section in the locale stored module:

            //temp : parseInt(d.substr(19,15),2)*0.2*(0.5-d[1­6])
            temp : parseInt(d.substr(19,15),2)*0.2*(0.5-d[1­8])
    
  • Is it corrected now? I still get negative values.

  • Try now - I just updated it.

  • Thanks, I'm running a test with it. (it's started now)

  • @Gordon it is much better. It is sometimes reads -1 -1 but mostly it is good. (I'm running the tests on an ESP8266 with the new tve build.)

    getting DHT sensor values
    {
      "raw": "010000000110000110000000001100101101010­010",
      "rh": 39, "temp": 20.3 }
    Temp is 20.3 and RH is 39
    getting DHT sensor values
    { "err": true, "checksumError": true,
      "raw": "01000000011000100000000000110010",
      "temp": -1, "rh": -1 }
    Temp is -1 and RH is -1
    getting DHT sensor values
    { "err": true, "checksumError": true,
      "raw": "0100000001100001110000000011001",
      "temp": -1, "rh": -1 }
    Temp is -1 and RH is -1
    getting DHT sensor values
    {
      "raw": "010000000110000111000000001100101101010­011",
      "rh": 39.1, "temp": 20.3 }
    Temp is 20.3 and RH is 39.1
    

    Here is the build version:

    >reset()
    =undefined
     _____                 _
    |   __|___ ___ ___ _ _|_|___ ___
    |   __|_ -| . |  _| | | |   | . |
    |_____|___|  _|_| |___|_|_|_|___|
              |_| http://espruino.com
     1v85.tve_master_cee7141 Copyright 2016 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    Flash map 4MB:512/512, manuf 0xe0 chip 0x4016
    
  • I can confirm, there is no negativ values til now started to run code yesterday evening. So I think it is better.

  • Great - thanks!

  • @Gordon some times I still get -1 because of incomplete raw data. Are we losing speed with all stuff in flash, is the used module broken or is some thing wrong with the used code ?

    // simple.js
    
    // 1v85.MaBe_master_e740e65  latest pull and unmodified  
    
    E.on("init",function(){
      dht = require("DHT22").connect(D0);
      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+
                      "\nerr:"+a.err+
                      ", cse:"+a.checksumError);},
          1);
      },3000);
    });
    
    save();
    
  • Ok, so this is on ESP8266? Maybe check E.getErrorFlags() to see if there was a buffer overflow.

    It might be because the pin changes state during some action in internal ESP8266 code where IRQs are disabled, and so the response gets delayed.

    I think at this point your option is either to use a proper Espruino or to just put up with it not responding correctly every time. As I understand it, the original one didn't work properly at all on ESP8266, so this is definitely an improvement.

  • Yes ESP8266 12E and glad that DHT22 is now working up to 99% !

    Added E.getErrorFlags() and require("ESP8266").printLog() - hope to catch a -1

  • Some times I have checksum errors on ESP.

    getting DHT sensor values
    { "err": true, "checksumError": true,
      "raw": "010000000110111110000000001101",
      "temp": -1, "rh": -1 }
    Temp is -1 and RH is -1
    E-Flags: []
    { "free": 967, "usage": 633, "total": 1600, "history": 398 }
    {
      "sdkVersion": "1.5.0",
      "cpuFrequency": 160, "freeHeap": 9816, "maxCon": 10,
      "flashMap": "4MB:512/512",
      "flashKB": 4096,
      "flashChip": "0xe0 0x4016"
     }
    183> < jshCanWatch = true
    190708> > jshCanWatch: pin=0
    190709> < jshCanWatch = true
    191235> > jshCanWatch: pin=0
    191236> < jshCanWatch = true
    191761> > jshCanWatch: pin=0
    191762> < jshCanWatch = true
    192287> > jshCanWatch: pin=0
    192287> < jshCanWatch = true
    getting DHT sensor values
    {
      "raw": "010000000110111110000000001101001010010­001",
      "rh": 44.6, "temp": 21 }
    Temp is 21 and RH is 44.6
    

    Added the debug things what @MaBe said:

            setTimeout(function(){
              console.log("E-Flags: ["+E.getErrorFlags()+"]\n\n");
              console.log(process.memory());
              console.log(ESP8266.getState());
            },
            1000);
    
  • When I saw you have a lot of trouble with DHT22 and the like. I decided to look into it.
    So I took the datablad, and made a simulator in my propeller from Parallax.
    I don't have this sensor, so I don't know if it will work.
    Then I took Gordon's proposals and twisted a bit. See post #1

    My connection between esp8266-01 and propeller are via a resistor of 330ohm, just to be safe.
    I chanced the time from 6ms to 20ms. On my system, if I am under 13 ms I begin to lose pulses.

    Try it and see if it can be used.

    // DHTxx_01 - esp8266-01
    
    // Sensor object constructor
    function HT(device, pin) {
      this.device = device;
      this.pin = pin;
    }
    // sensor query method...
    /** read sensor as either...
          read(callback);
          read(callback,number_of_tries); - default=3
          */
    HT.prototype.read = function (cb,n) {
      if (!n) n=3;
      var d = ""; 
      var ht = this;
      pinMode(ht.pin); // set pin state to automatic
      digitalWrite(ht.pin,0);
      this.watch = setWatch(function(t) {
        d+=0|(t.time-t.lastTime>0.00005); // 030, 070 usec (time in sec) // PB
      }, ht.pin, {edge:'falling',repeat:true} );
      setTimeout(function() {pinMode(ht.pin,'input_pullup');},1); // (time in msec) // PB
      setTimeout(function() {
        clearWatch(ht.watch);
        delete ht.watch;
    	//00+0000 0010 1000 1100 0000 0001 0101 1111 1110 1110
    	//00+0000 0010 1000 1100 1000 0000 0110 0101 0111 0011
    	//   r                   t                   c 
    	//d='00'+'000000101000110000000001010111­1111101110'; // rh=65.2 t= 35.1
    	//d='00'+'000000101000110010000000011001­0101110011'; // rh=65.2 t=-10.1
    	//d='00'+'000000101000110000000000011001­0111110011'; // rh=65.2 t= 10.1
        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))) {
          var o = { raw:d };
          if (ht.device=="DHT11") {
            o.rh = parseInt(d.substr(2,8),2);
            o.t = parseInt(d.substr(18,8),2);
          } else {
            o.rh = parseInt(d.substr(2,16),2)*0.1;
            //o.t = parseInt(d.substr(19,15),2)*0.2*(d[18]-0­.5);
            o.t = parseInt(d.substr(19,15),2)*0.2*(0.5-d[1­8]); // PB
          }
          //cb(o);
          cb({o:o, n:n}); // PB
    
        } else {
          if (n>1) setTimeout(function() {ht.read(cb,--n);},500);
          //else cb({err:true, raw:d});
          else cb({err:true, checksumError:cks>0, raw:d, temp:-1, rh:-1, n:n});
        }
      //},6);
      },20); // time in msec (min 15 virker) 6 20 // PB
    };
    
    
    //*************** start here ******************
    // esp8266
    var ESP8266 = require('ESP8266');
    //ESP8266.setCPUFreq(80);
    console.log(ESP8266.getState());
    
    
    // Test with
    var dht = new HT("DHT22",D0);
    setInterval(function() {
      dht.read(print);
    },3000);
    
    

    TVE's firmware downloaded today and used in the test.

     _____                 _
    |   __|___ ___ ___ _ _|_|___ ___
    |   __|_ -| . |  _| | | |   | . |
    |_____|___|  _|_| |___|_|_|_|___|
              |_| http://espruino.com
     1v85.1127 Copyright 2016 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    Flash map 512KB:256/256, manuf 0xc8 chip 0x4013
    >echo(0);
    {
      "sdkVersion": "1.5.3(aec24ac9)",
      "cpuFrequency": 160, "freeHeap": 10296, "maxCon": 10,
      "flashMap": "512KB:256/256",
      "flashKB": 512,
      "flashChip": "0xc8 0x4013"
     }
    =undefined
    {
      "o": {
        "raw": "010000000001001101000000010101111110101­101",
        "rh": 7.7, "t": 35.1 },
      "n": 3 }
    {
      "o": {
        "raw": "010000000001001110000000010101111110101­110",
        "rh": 7.8, "t": 35.1 },
      "n": 3 }
    {
      "o": {
        "raw": "010000000001001111000000010101111110101­111",
        "rh": 7.9, "t": 35.1 },
      "n": 3 }
    {
      "o": {
        "raw": "010000000001010000000000010101111110110­000",
        "rh": 8, "t": 35.1 },
      "n": 3 }
    {
      "o": {
        "raw": "010000000001010001000000010101111110110­001",
        "rh": 8.1, "t": 35.1 },
      "n": 3 }
    {
      "o": {
        "raw": "010000000001010010000000010101111110110­010",
        "rh": 8.2, "t": 35.1 },
      "n": 3 }
    {
      "o": {
        "raw": "010000000001010011000000010101111110110­011",
        "rh": 8.3, "t": 35.1 },
      "n": 3 }
    
    
  • I'ts not good for me:

     _____                 _
    |   __|___ ___ ___ _ _|_|___ ___
    |   __|_ -| . |  _| | | |   | . |
    |_____|___|  _|_| |___|_|_|_|___|
              |_| http://espruino.com
     1v85 Copyright 2016 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    Flash map 4MB:512/512, manuf 0xe0 chip 0x4016
    >echo(0);
    {
      "sdkVersion": "1.5.0",
      "cpuFrequency": 160, "freeHeap": 8768, "maxCon": 10,
      "flashMap": "4MB:512/512",
      "flashKB": 4096,
      "flashChip": "0xe0 0x4016"
     }
    =undefined
    { "err": true, "checksumError": true,
      "raw": "0100000001101000110000000011110001100",­
      "temp": -1, "rh": -1, "n": 1 }
    { "err": true, "checksumError": true,
      "raw": "0100000001101001000000000011110001",
      "temp": -1, "rh": -1, "n": 1 }
    { "err": true, "checksumError": true,
      "raw": "0100000001101000110000000011110001100",­
      "temp": -1, "rh": -1, "n": 1 }
    { "err": true, "checksumError": true,
      "raw": "010000000110100011000000001111000110",
      "temp": -1, "rh": -1, "n": 1 }
    { "err": true, "checksumError": true,
      "raw": "010000000110100011000000001111000110",
      "temp": -1, "rh": -1, "n": 1 }
    { "err": true, "checksumError": true,
      "raw": "010000000110100011000000001111000110",
      "temp": -1, "rh": -1, "n": 1 }
    { "err": true, "checksumError": true,
      "raw": "0100000001101000110000000011110001",
      "temp": -1, "rh": -1, "n": 1 }
    { "err": true, "checksumError": true,
      "raw": "0100000001101001000000000011110001100",­
      "temp": -1, "rh": -1, "n": 1 }
    { "err": true, "checksumError": true,
      "raw": "010000000110100011000000001111000110",
      "temp": -1, "rh": -1, "n": 1 }
    

    and some times this (but 95% is the above):

    {
      "o": {
        "raw": "010000000110100011000000001111000110010­101",
        "rh": 41.9, "t": 24.1 },
      "n": 1 }
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

New joint DHT11/DHT22 module

Posted by Avatar for Gordon @Gordon

Actions