zbx-sadman / zabbuino

Zabbix agent for Arduino
92 stars 14 forks source link

dht11 #1

Closed vcppov closed 8 years ago

vcppov commented 8 years ago

Hi! Did you check response for dht11 sensor?

Looks like it counts incorrect: $ zabbix_get -s 192.168.x.x -k 'dht.temperature[2,11]' 0.6 $ zabbix_get -s 192.168.x.x -k 'dht.humidity[2,11]' 5.1

I tried compare your part of code with working code: Your part of code:

// now pull it low for ~20 milliseconds
  pinMode(_pin, OUTPUT);
  digitalWrite(_pin, LOW);
  delay(20);
  noInterrupts();
  digitalWrite(_pin, HIGH);
  delayMicroseconds(40);
  pinMode(_pin, INPUT);

  // read in timings
  //  for ( i = 0; i < MAXTIMINGS; i++) {
  for ( i = 0; i < 85; i++) {
    counter = 0;
    while (digitalRead(_pin) == laststate) {
      counter++;
      delayMicroseconds(1);
      if (counter == 255) {
        break;
      }
    }
    laststate = digitalRead(_pin);

    if (counter == 255) break;

    // ignore first 3 transitions
    if ((i >= 4) && (i % 2 == 0)) {
      // shove each bit into the storage bytes
      data[j / 8] <<= 1;
      //      if (counter > _count)
      if (counter > 6)
        data[j / 8] |= 1;
      j++;
    }

  }

Working with dht11 (another scatch) part of code:

    pinMode(pin, OUTPUT);
    digitalWrite(pin, LOW);
    delay(18);
    digitalWrite(pin, HIGH);
    delayMicroseconds(40);
    pinMode(pin, INPUT);

    // ACKNOWLEDGE or TIMEOUT
    unsigned int loopCnt = 10000;
    while(digitalRead(pin) == LOW)
            if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

    loopCnt = 10000;
    while(digitalRead(pin) == HIGH)
            if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

    // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
    for (int i=0; i<40; i++)
    {
            loopCnt = 10000;
            while(digitalRead(pin) == LOW)
                    if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

            unsigned long t = micros();

            loopCnt = 10000;
            while(digitalRead(pin) == HIGH)
                    if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

            if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
            if (cnt == 0)   // next byte?
            {
                    cnt = 7;    // restart at MSB
                    idx++;      // next byte!
            }
            else cnt--;
    }

You have chance mix that parts or try to fix you part. With DS* it works, but in my situation i increased timeout from 850 to 1250 ms (looks like 850 not enough for DS18B20)

Thank you for you code!

zbx-sadman commented 8 years ago

Tanx for testing.

Unfortunately I don't have all the sensors to make tests. My set is: AM2302 (DHT22), BMP085, DS18S20.

You can ask me: why DHT11 processing included in my code? I have simple answer: i took some code from AdaFruit libs & check working with my hardware. Today I saw an updated Adafruit DHT library and found a modified code. I'm going to update Zabbuino .ino's soon.

But i have read that DHT11 anyway return wrong values on low humidity. And... i remember that when i connect my AM2302 to Raspberry Pi by long cable, i too got strange results. Then i measure voltage on remote end - its was on lowest operational limit. After sensor supply voltage rising all values became normal.

Also, i refer again to DS18B20 datasheet ( https://www.adafruit.com/datasheets/DS18B20.pdf ) and found max Tconv=750ms (for 12bit). I have no idea why yours DS18B20 have no time to perform the conversion operation. Example of OneWire lib contain 1000ms delay, but i decrease its for taking speed profit. Then i used that code with my hardware and haven't reached temp. read errors. However there are no obstacles to use long delays.

vcppov commented 8 years ago

I have 5 x DS in one line with parasite power and just switch to your sketch from another (hardware and scheme leave not changed). And you sketch works only with old DS (with serial 10xxxx), but new one (28xxx) wants more time to prepare temperature.

zbx-sadman commented 8 years ago

DHT11 must work in v1.0.0, Parts of DHTlib source is used.