Interfacing

raspberry read temperature from DHT11

40 17512
Post Last Edited by julie20082004 at 2014-5-9 03:25

The DHT11 module vcc connect raspberry pi 5V Pin,Gnd connect GND,Vout connect GPIO1

.
1364619055_9472.jpg
  1. //
  2. //mydht11.c
  3. //
  4. #include <wiringPi.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>

  7. typedef unsigned char uint8;
  8. typedef unsigned int  uint16;
  9. typedef unsigned long uint32;

  10. #define HIGH_TIME 32

  11. int pinNumber =1;  //use gpio1 to read data
  12. uint32 databuf;



  13. uint8 readSensorData(void)
  14. {
  15.     uint8 crc;
  16.     uint8 i;

  17.     pinMode(pinNumber,OUTPUT); // set mode to output
  18.     digitalWrite(pinNumber, 0); // output a high level
  19.     delay(25);
  20.     digitalWrite(pinNumber, 1); // output a low level
  21.     pinMode(pinNumber, INPUT); // set mode to input
  22.     pullUpDnControl(pinNumber,PUD_UP);

  23.     delayMicroseconds(27);
  24.     if(digitalRead(pinNumber)==0) //SENSOR ANS
  25.        {
  26.          while(!digitalRead(pinNumber)); //wait to high

  27.           for(i=0;i<32;i++)
  28.            {
  29.            while(digitalRead(pinNumber)); //data clock start
  30.            while(!digitalRead(pinNumber)); //data start
  31.           delayMicroseconds(HIGH_TIME);
  32.           databuf*=2;
  33.            if(digitalRead(pinNumber)==1) //1
  34.                {
  35.                 databuf++;
  36.                }
  37.             }

  38.           for(i=0;i<8;i++)
  39.            {
  40.            while(digitalRead(pinNumber)); //data clock start
  41.            while(!digitalRead(pinNumber)); //data start
  42.           delayMicroseconds(HIGH_TIME);
  43.           crc*=2;  
  44.           if(digitalRead(pinNumber)==1) //1
  45.                {
  46.                 crc++;
  47.                }
  48.             }
  49.         return 1;
  50.        }
  51.    else
  52.         {
  53.         return 0;
  54.          }
  55. }

  56. int main (void)
  57. {

  58.   printf("Use GPIO1 to read data!\n");

  59.   if (-1 == wiringPiSetup()) {
  60.     printf("Setup wiringPi failed!");
  61.     return 1;
  62.   }

  63.   pinMode(pinNumber, OUTPUT); // set mode to output
  64.   digitalWrite(pinNumber, 1); // output a high level

  65.   printf("Enter OS-------\n");
  66.   while(1) {
  67.     pinMode(pinNumber,OUTPUT); // set mode to output
  68.     digitalWrite(pinNumber, 1); // output a high level
  69.     delay(3000);
  70.     if(readSensorData())
  71.     {
  72.        printf("Congratulations ! Sensor data read ok!\n");
  73.        printf("RH:%d.%d\n",(databuf>>24)&0xff,(databuf>>16)&0xff);
  74.        printf("TMP:%d.%d\n",(databuf>>8)&0xff,databuf&0xff);
  75.        databuf=0;
  76.      }
  77.     else
  78.      {
  79.         printf("Sorry! Sensor dosent ans!\n");
  80.        databuf=0;
  81.       }
  82.   }
  83.   return 0;
  84. }
Copy the Code
gcc -o mydht11 mydht11.c -L/usr/local/lib-lwiringPi
./mydht11
Result:
Use GPIO1 to read data!
Enter OS-------
Congratulations ! Sensor data read ok!
RH:35.0
TMP:25.0
djizik  
hot to fix???

Error

root@lemaker:~# gcc -o mydht11 mydht11.c -L/usr/local/lib-lwiringPi
/tmp/ccNikvym.o: In function `readSensorData':
mydht11.c.text+0x1c): undefined reference to `pinMode'
mydht11.c.text+0x30): undefined reference to `digitalWrite'
mydht11.c.text+0x38): undefined reference to `delay'
mydht11.c.text+0x4c): undefined reference to `digitalWrite'
mydht11.c.text+0x60): undefined reference to `pinMode'
mydht11.c.text+0x74): undefined reference to `pullUpDnControl'
mydht11.c.text+0x7c): undefined reference to `delayMicroseconds'
mydht11.c.text+0x8c): undefined reference to `digitalRead'
mydht11.c.text+0xac): undefined reference to `digitalRead'
mydht11.c.text+0xd8): undefined reference to `digitalRead'
mydht11.c:(.text+0xf8): undefined reference to `digitalRead'
mydht11.c:(.text+0x10c): undefined reference to `delayMicroseconds'
mydht11.c:(.text+0x130): undefined reference to `digitalRead'
mydht11.c:(.text+0x188): undefined reference to `digitalRead'
mydht11.c:(.text+0x1a8): undefined reference to `digitalRead'
mydht11.c:(.text+0x1bc): undefined reference to `delayMicroseconds'
mydht11.c:(.text+0x1d8): undefined reference to `digitalRead'
/tmp/ccNikvym.o: In function `main':
mydht11.c:(.text+0x23c): undefined reference to `wiringPiSetup'
mydht11.c:(.text+0x274): undefined reference to `pinMode'
mydht11.c:(.text+0x288): undefined reference to `digitalWrite'
mydht11.c:(.text+0x2a4): undefined reference to `pinMode'
mydht11.c:(.text+0x2b8): undefined reference to `digitalWrite'
mydht11.c:(.text+0x2c0): undefined reference to `delay'
collect2: ld returned 1 exit status
root@lemaker:~# gcc -o mydht11 mydht11.c
/tmp/ccEcEeRy.o: In function `readSensorData':
mydht11.c:(.text+0x1c): undefined reference to `pinMode'
mydht11.c:(.text+0x30): undefined reference to `digitalWrite'
mydht11.c:(.text+0x38): undefined reference to `delay'
mydht11.c:(.text+0x4c): undefined reference to `digitalWrite'
mydht11.c:(.text+0x60): undefined reference to `pinMode'
mydht11.c:(.text+0x74): undefined reference to `pullUpDnControl'
mydht11.c:(.text+0x7c): undefined reference to `delayMicroseconds'
mydht11.c:(.text+0x8c): undefined reference to `digitalRead'
mydht11.c:(.text+0xac): undefined reference to `digitalRead'
mydht11.c:(.text+0xd8): undefined reference to `digitalRead'
mydht11.c:(.text+0xf8): undefined reference to `digitalRead'
mydht11.c:(.text+0x10c): undefined reference to `delayMicroseconds'
mydht11.c:(.text+0x130): undefined reference to `digitalRead'
mydht11.c:(.text+0x188): undefined reference to `digitalRead'
mydht11.c:(.text+0x1a8): undefined reference to `digitalRead'
mydht11.c:(.text+0x1bc): undefined reference to `delayMicroseconds'
mydht11.c:(.text+0x1d8): undefined reference to `digitalRead'
/tmp/ccEcEeRy.o: In function `main':
mydht11.c:(.text+0x23c): undefined reference to `wiringPiSetup'
mydht11.c:(.text+0x274): undefined reference to `pinMode'
mydht11.c:(.text+0x288): undefined reference to `digitalWrite'
mydht11.c:(.text+0x2a4): undefined reference to `pinMode'
mydht11.c:(.text+0x2b8): undefined reference to `digitalWrite'
mydht11.c:(.text+0x2c0): undefined reference to `delay'
collect2: ld returned 1 exit status

djizik  
fix
gcc -o mydht11 mydht11.c -L/usr/local/lib -lwiringPi

agropi  
Post Last Edited by agropi at 2014-7-22 09:55

Hi,
also work BananaPi ?
I tried, but I always have a message by
"Sorry sensor dosent ans"
think is a problem of libraries wiringPI for banana?
I have set wrong or some port ?

sandwho  
How can I check the AM2302 to the banana pi with Adafruit-Raspberry-Pi-code?

agropi  
This code not working on banana. It return ever bit 1. Some ideas?

MonkeyG  
Same here...
I am using this loop to read data, and it seems after reading first couple of bits it returns only HIGH. No clue why is that happening
    /*--------------------------*/
    /*--- Start reading data ---*/
    /*--------------------------*/
        for (i=0; i<40; i++)
        {
                loopCnt = 1000;
                while(digitalRead(DHTPIN) == LOW)
        {
            if (loopCnt-- == 0) return 4;
            delayMicroseconds( 1 );
        }

                t = micros();

                loopCnt = 1000;
                while(digitalRead(DHTPIN) == HIGH)
        {
                if (loopCnt-- == 0) return -(i+100);
                delayMicroseconds( 1 );
        }

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


Output:
pi@lemaker ~ $ sudo ./TempSensor
Status[-102]
Status[-104]
Status[-104]
Status[-104]
Status[-102]
Status[-104]
Status[-101]
Status[-102]
Status[-103]
Status[-101]
Status[-101]

wiringpi for bananapi release in the V3 version,use that version image may do well.

MonkeyG  
Post Last Edited by tony_zhang at 2014-8-19 21:34

I am using V3.
I have placed some more debugs, and it seems that working with GPIO on Banana Pi is just too slow .
It takes a lot of time when calling: digitalWrite or pinMode.

For example:
/*Put the GPIO to HIGH and prepare to read data*/
    t4S = micros();   
    digitalWrite( DHTPIN, HIGH );
    pinMode( DHTPIN, INPUT );
    t5S = micros();

t5S - t4S here is around 600 us.
That is a lot, by that time the sensor already sent “Response Signal 80us” + "UP DATA 80us" + Part of Data Bits.

So actually in previous example posted here it seems that we were reading last couple of bits and not first. And after that signal stays HIGH as expected. Which makes sense.

MonkeyG  
I am using V3.
I have placed some more debugs, and it seems that working with GPIO on Banana Pi is just too slow .
It takes a lot of time when calling: digitalWrite or pinMode.

For example:
/*Put the GPIO to HIGH and prepare to read data*/
    t4S = micros();   
    digitalWrite( DHTPIN, HIGH );
    pinMode( DHTPIN, INPUT );
    t5S = micros();

t5S - t4S here is around 600 us.
That is a lot, by that time the sensor already sent “Response Signal 80us” + "ull UP DATA 80us" + Part of Data Bits.

So actually in previous example posted here it seems that we were reading last couple of bits and not first. And after that signal stays HIGH as expected. Which makes sense

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

Points Rules