Interfacing

raspberry read temperature from DHT11

38 26082
tosiara  
Edited by tosiara at Wed Nov 5, 2014 06:44

Confirmed, this is WiringBPI issue. I have transferred libwiringPi.so.2.0 from Raspbian image and built the program against that library - now all works on Lubuntu too:
  1. root@bpi:/home/bananapi# LD_PRELOAD=/home/bananapi/raspbian-3.1-libwiringPi.so ./dht11
  2. Use GPIO1 to read data!
  3. Enter OS-------
  4. Congratulations ! Sensor data read ok!
  5. RH:34.0
  6. TMP:24.0
  7. Congratulations ! Sensor data read ok!
  8. RH:34.0
  9. TMP:24.0
Copy the Code
Filed bug for WiringBPI: https://github.com/LeMaker/WiringBPi/issues/1

I've rebuild WiringPi from the sources with my Bananian (even if it wasn't necessary).

As for the sources : https://github.com/seblucas/lol_dht22

axelf  
Edited by axelf at Mon Dec 8, 2014 13:46

Hi,
I seem to have the same problem as tosiara using the program with the latest version of bananian.
The CPU-frequency is @ 600MHz. Is that OK? As tosiara mentioned to use libwiringPi.so.2.0 of Raspian: where can I get this file?
Thanks a lot!

Edit: Hmm, with another program I found for the Raspberry Pi everything is working fine. As it is also using wiringpi I am a bit confused that it is working.

tosiara  
Edit: Hmm, with another program I found for the Raspberry Pi everything is working fine. As it is also using wiringpi I am a bit confused that it is working.

For some reason that another example works without resistor, but fails with pullup resistor. While the example in this thread works fine no matter if resistor is present or not.

The only issue with the code - infinite "while" loops which may hang forever if sensor does not answer. I have changed those to limited "for", like this:
  1. while(!digitalRead(pinNumber)); //wait to high
Copy the Code
changed to:
  1. #define SENSOR_TIMEOUT_LOOP 5000
  2. int wait;
  3. for (wait = 0; wait < SENSOR_TIMEOUT_LOOP && !digitalRead (pinNumber); wait++);
Copy the Code

bdwk  
如果没有接上拉电阻去掉这个函数 pullUpDnControl(pinNumber,PUD_UP);就正常工作了

Oleg  
Edited by Oleg at Sun Oct 25, 2015 12:04

Hi,
Pls help me. I want to read data from dht22,BPI M1+Lubuntu , wiringPI was installed from https://github.com/LeMaker/WiringBP. GPIO works well ( gpio readall, tried LED test with command gpio write ).
Build some code:
-Adafruit_Python_DHT by python show me 'Failed to get reading. Try again!' https://github.com/adafruit/Adafruit_Python_DHT/blob/master/examples/AdafruitDHT.py
-This code https://github.com/seblucas/lol_dht22 show error  - checking for wiringPiSetup in -lwiringPi... no
configure: error: Fail to find wiringPi library...Where can i dowload   libwiringPi.so.2.0 from Raspbian image?

-code below provides incorrect data
  1. #include <wiringPi.h>  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <stdint.h>  
  5. #define MAX_TIME 85  
  6. #define DHT22PIN 5  
  7. int dht22_val[5]={0,0,0,0,0};  
  8.   
  9. void dht22_read_val()  
  10. {  
  11.   uint8_t lststate=HIGH;  
  12.   uint8_t counter=0;  
  13.   uint8_t j=0,i;  
  14.   for(i=0;i<5;i++)  
  15.      dht11_val[i]=0;  
  16.   pinMode(DHT22PIN,OUTPUT);  
  17.   digitalWrite(DHT22PIN,LOW);  
  18.   delay(18);  
  19.   digitalWrite(DHT22PIN,HIGH);  
  20.   delayMicroseconds(40);  
  21.   pinMode(DHT22PIN,INPUT);  
  22.   for(i=0;i<MAX_TIME;i++)  
  23.   {  
  24.     counter=0;  
  25.     while(digitalRead(DHT22PIN)==lststate){  
  26.       counter++;  
  27.       delayMicroseconds(1);  
  28.       if(counter==255)  
  29.         break;  
  30.     }  
  31.     lststate=digitalRead(DHT22PIN);  
  32.     if(counter==255)  
  33.        break;  
  34.     // top 3 transistions are ignored  
  35.     if((i>=4)&&(i%2==0)){  
  36.       dht22_val[j/8]<<=1;  
  37.       if(counter>16)  
  38.         dht22_val[j/8]|=1;  
  39.       j++;  
  40.     }  
  41.   }  
  42.   // verify cheksum and print the verified data  
  43.   if((j>=40)&&(dht22_val[4]==((dht22_val[0]+dht22_val[1]+dht22_val[2]+dht22_val[3])& 0xFF)))  
  44.   {   
  45.     printf("Humidity = %d.%d %% Temperature = %d.%d °C\n",dht22_val[0],dht22_val[1],dht22_val[2],dht22_val[3]);  
  46.   }  
  47.   else  
  48.        printf("Invalid Data!!\n");
  49.         printf("Humidity = %d.%d %% Temperature = %d.%d °C\n",dht22_val[0],dht22_val[1],dht22_val[2],dht22_val[3]);   
  50. }  
  51.   
  52. int main(void)  
  53. {  
  54.   printf("Interfacing Temperature and Humidity Sensor (DHT22) With Banana Pi\n");  
  55.   if(wiringPiSetup()==-1)  
  56.     exit(1);  
  57.   while(1)  
  58.   {  
  59.      dht22_read_val();  
  60.      delay(3000);  
  61.   }  
  62.   return 0;  
  63. }
Copy the Code




Oleg  
Any body visits this forum? For BPI+M1, python and RPi.GPIO python module:

import RPi.GPIO as GPIO
import time

usleep = lambda x: time.sleep(x/1000000.0)
pingpio = 24
maxcycles = 1000

class DHT():
        def read(self):
            if(self.expectPulse(0)==0):
                return "Timeout waiting for start signal low pulse."
            if(self.expectPulse(1)==0):
                return "Timeout waiting for start signal high pulse."
            i=0
            cycles = []
            while (i<80):
              cycles.append(self.expectPulse(0))
              cycles.append(self.expectPulse(1))
              i=i+2
            i=0
            st=''
            while (i<40):
                lowCycles  = cycles[2*i]
                highCycles = cycles[2*i+1]
                if(((lowCycles == 0) or (highCycles == 0)) and i != 39):
                    #print "error waiting for pulse "+ str(i)+ " L "+str(lowCycles)+ " H "+str(highCycles)
                    return '-'
                if(lowCycles>highCycles):
                    st=st+'0'
                else:
                    st=st+'1'
                i=i+1
              return st
                    

        def expectPulse(self,level):
            count=0;
            while(GPIO.input(pingpio)==level):
                if(count>=maxcycles):
                    return 0
                count=count+1;
                #usleep(1);
            return count

if __name__ == "__main__":
        dht = DHT()
        while(1):
            try:
                GPIO.setmode(GPIO.BCM)
                GPIO.setup(pingpio, GPIO.OUT)
                GPIO.output(pingpio, GPIO.HIGH)
                time.sleep(1)
                GPIO.output(pingpio, GPIO.LOW)
                usleep(1000)
                GPIO.output(pingpio, GPIO.HIGH)
                usleep(20)
                GPIO.output(pingpio, GPIO.LOW)
                GPIO.setup(pingpio, GPIO.IN)
                usleep(5)
                res =  dht.read()
                if(res!='-'):
                    hum = int(res[:16], 2)
                    t = int(res[17:32], 2)
                    crc = int(res[33:40], 2)
                    crc1 = int(res[:8], 2)
                    crc2 = int(res[9:16], 2)
                    crc3 = int(res[17:24], 2)
                    crc4 = int(res[25:32], 2)
                    hum = float(hum)
                    t = float(t)
                    if((crc1+crc2+crc3+crc4) ==crc ):
                        print "Humidity: "+str(hum/10)
                        print "Temp:"+str(t/10)
                time.sleep(2)
            except:
                time.sleep(2)

Hi, i have been trying for a week to make that thing work on banana pi M1. I wasnt able to get any reading ether with the python scripts or the c programs. I have tried to set up my dht11 sensor on an arduino uno and it worked right away.  I have bananian installed but i also have tried and with raspbian V4. No luck with anything. The gpio pins of my banana are working because i have my doorbell set up on them. Does anyone have any suggestion?

ElizaPt  
With thanks. Plenty of stuff.

You have to log in before you can reply Login | Sign Up

Points Rules