#include
#include
#include
#include
#include
#define MAXTIMINGS 85
#define DHTPIN 7
int main(void){
int dht11_dat[5] = {0, 0, 0, 0, 0};
int laststate = 1;
int counter = 0;
int j = 0;
int i = 0;
float f;
int DHTValue = 0;
void delay (unsigned int howLong) {
struct timespec sleeper, dummy ;
sleeper.tv_sec = (time_t)(howLong / 1000) ;
sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
nanosleep (&sleeper, &dummy) ;
}
printf("Read from DHT11 sensor.\n");
/* pull pin down for 18 milliseconds */
FILE *fp;
fp = fopen("/sys/class/gpio/gpio35/direction","w");
fputs("out", fp);
fclose(fp);
//digitalWrite( DHTPIN, LOW );
fp = fopen("/sys/class/gpio/gpio35/value","w");
fputs("0", fp);
fclose(fp);
delay( 18 );
/* then pull it up for 40 microseconds */
//digitalWrite( DHTPIN, HIGH );
fp = fopen("/sys/class/gpio/gpio35/value","w");
fputs("1", fp);
fclose(fp);
usleep( 40 );
/* prepare to read the pin */
//pinMode( DHTPIN, INPUT );
fp = fopen("/sys/class/gpio/gpio35/direction","w");
fputs("in", fp);
fclose(fp);
/* detect change and read data */
for ( i = 0; i < MAXTIMINGS; i++ ) {
fp = fopen("/sys/class/gpio/gpio35/value","r");
DHTValue = fgetc(fp);
fclose(fp);
counter = 0;
while ( DHTValue == laststate ) {
counter++;
usleep( 1 );
IF ( counter == 255 )
{
break;
}
}
laststate = DHTValue;
if ( counter == 255 )
break;
/* ignore first 3 transitions */
if ( (i >= 4) && (i % 2 == 0) )
{
/* shove each bit into the storage bytes */
dht11_dat[j / 8] <<= 1;
if ( counter > 16 )
dht11_dat[j / 8] |= 1;
j++;
printf( "Humidity = %d.%d %% Temperature = %d.%d *C (%.1f *F)\n",
dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3], f );
printf("%s\n",j);
}
}
/*
* check we read 40 bits (8Bit x 5 ) + verify checksum in the last byte
* print it out if data is good
*/
if ( (j >= 40) &&
(dht11_dat[4] == ( (dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF) ) )
{
f = dht11_dat[2] * 9. / 5. + 32;
printf( "Humidity = %d.%d %% Temperature = %d.%d *C (%.1f *F)\n",
dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3], f );
}else {
printf( "Data not good, skip\n" );
}
return(0);
}