ФЭНДОМ


Датчики влажности DHT11, DHT22, DHT2302, DHT2301Править

root@raspberrypi:/home/pi# cat dht.c 
// How to access GPIO registers from C-code on the Raspberry-Pi
// Example program
// 15-January-2012
// Dom and Gert
//
 
/*
$ wget http://www.open.com.au/mikem/bcm2835/bcm2835-1.8.tar.gz
$ tar -zxvf bcm2835-1.8.tar.gz
$ cd bcm2835-1.8
$ ./configure
$ make
$ sudo make install
Then compile the Adafruit_DHT program with
gcc DHT.c -l bcm2835 -std=gnu99 -o DHT
*/
 
// Access from ARM Running Linux
 
 
#define BCM2708_PERI_BASE 0x20000000
#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */
 
 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <fcntl.h>
#include <assert.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <bcm2835.h>
#include <unistd.h>
#define MAXTIMINGS 100
 
//#define DEBUG
 
#define DHT11 11
#define DHT22 22
#define AM2302 22
 
 
 int is=0;
 
 
int readDHT(int type, int pin) {
 
  int counter = 0;
  int laststate = HIGH;
  int j=0;
 
 int bits[250], data[100];
 int bitidx = 0;
 
 
 
 
  // Set GPIO pin to output
  bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_OUTP);
 
 
 
  bcm2835_gpio_write(pin, HIGH);
  usleep(500000);  // 500 ms
  bcm2835_gpio_write(pin, LOW);
  usleep(20000);
 
  bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_INPT);
 
  data[0] = data[1] = data[2] = data[3] = data[4] = 0;
 
 
  // wait for pin to drop?
 
  while(bcm2835_gpio_lev(pin) == 1) {
// printf("ddsdsd4 \n");
    if (is > 100) break;
 
 is++;
 
    usleep(1);
 
  }
 
 
  // read data!
  for (int i=0; i< MAXTIMINGS; i++) {
 
    counter = 0; 
    while ( bcm2835_gpio_lev(pin) == laststate) {
 
 
 
 
  if (is > 100) break;
 
 
 
 
 
 
 
 
 
counter++;
//nanosleep(1); // overclocking might change this?
 
        if (counter == 1000)
break;
    }
 
 
 
 
    laststate = bcm2835_gpio_lev(pin);
    if (counter == 1000) break;
    bits[bitidx++] = counter;
 
    if ((i>3) && (i%2 == 0)) {
      // shove each bit into the storage bytes
      data[j/8] <<= 1;
      if (counter > 200)
        data[j/8] |= 1;
      j++;
    }
  }
 
 
  if ((j >= 39) &&(data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ){
 
if ( data[4] == 0 )
 	{
 		printf("DHT not found\n");
exit(-1);
}
else
{
 
if (type == DHT11)
{
printf("Temp=%d,Hum=%d\%\n", data[2], data[0]); 
} 
if (type == DHT22) 
{
float f, h;
h = data[0] * 256 + data[1];
h /= 10;
f = (data[2] & 0x7F)* 256 + data[3];
f /= 10.0;
if (data[2] & 0x80) f *= -1;
printf("Temp=%.1f,Hum=%.1f\%\n", f, h); return 1;
} 
}
return 1; }
  else {
 
return 0;
}
 
 
//else { printf("%d\n");
//return 0;}
 
 
}
 
 
 
int main(int argc, char **argv)
{
int t=0;
int h=0;
 
 
 
  if (!bcm2835_init())
        return 1;
 
  if (argc != 3) {
 
 
printf("usage: %s [11|22|2302] GPIOpin#\n", argv[0]);
printf("example: %s 2302 4 - Read from an AM2302 connected to GPIO #4\n", argv[0]);
return 2;
  }
  int type = 0;
  if (strcmp(argv[1], "11") == 0) type = DHT11;
  if (strcmp(argv[1], "22") == 0) type = DHT22;
  if (strcmp(argv[1], "2302") == 0) type = AM2302;
  if (type == 0) {
printf("Select 11, 22, 2302 as type!\n");
return 3;
  }
 
  int dhtpin = atoi(argv[2]);
 
  if (dhtpin <= 0) {
printf("Please select a valid GPIO pin #\n");
return 3;
  }
int m=0; 
do 
{m = readDHT(type, dhtpin); /*printf("m = %i\n", m);*/}
while (m==0);
  return 0;
 
} // main