Interfacing
raspberry read temperature from DHT11
38
26082
|
[Interfacing]
raspberry read temperature from DHT11
[Copy link]
|
|
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:- root@bpi:/home/bananapi# LD_PRELOAD=/home/bananapi/raspbian-3.1-libwiringPi.so ./dht11
- Use GPIO1 to read data!
- Enter OS-------
- Congratulations ! Sensor data read ok!
- RH:34.0
- TMP:24.0
- Congratulations ! Sensor data read ok!
- RH:34.0
- TMP:24.0
Copy the Code Filed bug for WiringBPI: https://github.com/LeMaker/WiringBPi/issues/1 |
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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:- while(!digitalRead(pinNumber)); //wait to high
Copy the Code changed to:- #define SENSOR_TIMEOUT_LOOP 5000
- int wait;
- for (wait = 0; wait < SENSOR_TIMEOUT_LOOP && !digitalRead (pinNumber); wait++);
Copy the Code |
|
|
|
|
|
|
|
如果没有接上拉电阻去掉这个函数 pullUpDnControl(pinNumber,PUD_UP);就正常工作了
|
|
|
|
|
|
|
|
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
- #include <wiringPi.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #define MAX_TIME 85
- #define DHT22PIN 5
- int dht22_val[5]={0,0,0,0,0};
-
- void dht22_read_val()
- {
- uint8_t lststate=HIGH;
- uint8_t counter=0;
- uint8_t j=0,i;
- for(i=0;i<5;i++)
- dht11_val[i]=0;
- pinMode(DHT22PIN,OUTPUT);
- digitalWrite(DHT22PIN,LOW);
- delay(18);
- digitalWrite(DHT22PIN,HIGH);
- delayMicroseconds(40);
- pinMode(DHT22PIN,INPUT);
- for(i=0;i<MAX_TIME;i++)
- {
- counter=0;
- while(digitalRead(DHT22PIN)==lststate){
- counter++;
- delayMicroseconds(1);
- if(counter==255)
- break;
- }
- lststate=digitalRead(DHT22PIN);
- if(counter==255)
- break;
- // top 3 transistions are ignored
- if((i>=4)&&(i%2==0)){
- dht22_val[j/8]<<=1;
- if(counter>16)
- dht22_val[j/8]|=1;
- j++;
- }
- }
- // verify cheksum and print the verified data
- if((j>=40)&&(dht22_val[4]==((dht22_val[0]+dht22_val[1]+dht22_val[2]+dht22_val[3])& 0xFF)))
- {
- printf("Humidity = %d.%d %% Temperature = %d.%d °C\n",dht22_val[0],dht22_val[1],dht22_val[2],dht22_val[3]);
- }
- else
- printf("Invalid Data!!\n");
- printf("Humidity = %d.%d %% Temperature = %d.%d °C\n",dht22_val[0],dht22_val[1],dht22_val[2],dht22_val[3]);
- }
-
- int main(void)
- {
- printf("Interfacing Temperature and Humidity Sensor (DHT22) With Banana Pi\n");
- if(wiringPiSetup()==-1)
- exit(1);
- while(1)
- {
- dht22_read_val();
- delay(3000);
- }
- return 0;
- }
Copy the Code
|
|
|
|
|
|
|
|
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?
|
|
|
|
|
|
|
|
With thanks. Plenty of stuff.
|
|
|
|
|
|
|