Advertisement
Guest User

Untitled

a guest
Jan 25th, 2018
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #include <wiringPi.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5.  
  6. #define MAXTIMINGS 85
  7. #define DHTPIN 7
  8.  
  9. int dht11_dat[5] = { 0, 0, 0, 0, 0 };
  10.  
  11. void read_dht11_dat() {
  12. uint8_t laststate = HIGH;
  13. uint8_t counter = 0;
  14. uint8_t j = 0, i;
  15. float f;
  16.  
  17. dht11_dat[0] = dht11_dat[1] = dht11_dat[2] = dht11_dat[3] = dht11_dat[4] = 0;
  18.  
  19. pinMode(DHTPIN, OUTPUT);
  20. digitalWrite(DHTPIN, LOW);
  21. delay(18);
  22. digitalWrite(DHTPIN, HIGH);
  23.  
  24. delayMicroseconds(40);
  25.  
  26. pinMode(DHTPIN, INPUT);
  27.  
  28. for (i = 0; i < MAXTIMINGS; i++) {
  29. counter = 0;
  30. while(digitalRead(DHTPIN) == laststate) {
  31. counter++;
  32. delayMicroseconds(1);
  33. if (counter ==255) break;
  34. }
  35.  
  36. laststate = digitalRead(DHTPIN);
  37.  
  38. if (counter == 255)
  39. break;
  40.  
  41. if ((i >= 4) && (i % 2 == 0)) {
  42. dht11_dat[j/8] <<= 1;
  43. if (counter > 16)
  44. dht11_dat[j/8] |= 1;
  45. j++;
  46. }
  47. }
  48.  
  49. if ((j >= 40) && (dht11_dat[4] == ( (dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF ))) {
  50. f = dht11_dat[2] * 9. / 5. + 32;
  51. printf("Humidity = %d.%d %% Temperature = %d.%d C (%.1f F)\n",
  52. dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3], f);
  53. } else {
  54. printf("Data not good, skip \n");
  55. }
  56. }
  57.  
  58. int main(void) {
  59. printf("Raspberry Pi wiriingPi DHT11 Temperature test program\n");
  60.  
  61. if (wiringPiSetup() == -1) {
  62. exit(-1);
  63. }
  64. while (1) {
  65. read_dht11_dat();
  66. delay(1000);
  67. }
  68.  
  69. return(0);
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement