Advertisement
lululombard

DHT11

May 16th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <wiringPi.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <string>
  6. #include <iostream>
  7. #define MAX_TIME 85
  8. #define DHT11PIN 7
  9. int dht11_val[5]={0,0,0,0,0};
  10. int humid;
  11. int temp;
  12. std::string dht11="";
  13.  
  14. void dht11_read_val()
  15. {
  16.   uint8_t lststate=HIGH;
  17.   uint8_t counter=0;
  18.   uint8_t j=0,i;
  19.   for(i=0;i<5;i++)
  20.      dht11_val[i]=0;
  21.   pinMode(DHT11PIN,OUTPUT);
  22.   digitalWrite(DHT11PIN,LOW);
  23.   delay(18);
  24.   digitalWrite(DHT11PIN,HIGH);
  25.   delayMicroseconds(40);
  26.   pinMode(DHT11PIN,INPUT);
  27.   for(i=0;i<MAX_TIME;i++)
  28.   {
  29.     counter=0;
  30.     while(digitalRead(DHT11PIN)==lststate){
  31.       counter++;
  32.       delayMicroseconds(1);
  33.       if(counter==255)
  34.         break;
  35.     }
  36.     lststate=digitalRead(DHT11PIN);
  37.     if(counter==255)
  38.        break;
  39.     if((i>=4)&&(i%2==0)){
  40.       dht11_val[j/8]<<=1;
  41.       if(counter>16)
  42.         dht11_val[j/8]|=1;
  43.       j++;
  44.     }
  45.   }
  46.   if((j>=40)&&(dht11_val[4]==((dht11_val[0]+dht11_val[1]+dht11_val[2]+dht11_val[3])& 0xFF))) {
  47.     temp = dht11_val[2];
  48.     humid = dht11_val[0];
  49.     dht11=temp + ";" + humid;
  50.   }
  51.   else
  52.     dht11="";
  53. }
  54.  
  55. int main(void)
  56. {
  57.   if(wiringPiSetup()==-1)
  58.     exit(1);
  59.   while(dht11 != "")
  60.   {
  61.      dht11_read_val();
  62.      if (dht11 != "")
  63.      delay(3000);
  64.   }
  65.   //printf(dht11);
  66.   std::cout << dht11 << std::endl;
  67.   return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement