Hello friends,
I’m compilited this code for interface BBB with DHT11(temperature-humidity sensor) this code is compiled and run successfully but OUTPUT
is 0’c temperature - 0% humidity …
Plz , find error this code…
Code-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdint.h>
#include <sys/time.h>
#include <unistd.h>
#define MAXTIMINGS 85
#define DHTPIN 48
int dht11_dat[5] = { 0, 0, 0, 0, 0 };
int readpin(int pinnum);
void setDirection(int pin, int dir);
void writepin(int pinnum, int value);
void pauseSec(int sec);
void pauseNanoSec(long nano);
void setDirection(int pin, int dir)
{
FILE *fpin, *fpinDir;
char pinDir[50];
fpin = fopen("/sys/class/gpio/export", “w”);
fprintf(fpin, “%i”, pin);
fflush(fpin);
sprintf(pinDir, “/sys/class/gpio/gpio%i/direction”, pin);
fpinDir = fopen(pinDir, “w”);
fprintf(fpinDir, dir ? “in” : “out”);
fflush(fpinDir);
fclose(fpinDir);
fclose(fpin);
}
int readpin(int pinnum)
{
int value;
FILE *inval;
inval = fopen("/sys/class/gpio/gpio48/value", “r”);
fseek(inval,0,SEEK_SET);
fscanf(inval,"%d",&value);
fclose(inval);
return value;
}
void writepin(int pinnum, int value)
{
FILE *val;
char buf[5];
char buf2[50] = “/sys/class/gpio/gpio”;
sprintf(buf,"%i",pinnum);
strcat(buf2,strcat(buf,"/value"));
val = fopen(buf2, “w”);
fseek(val,0,SEEK_SET);
fprintf(val,"%d",value);
fflush(val);
fclose(val);
}
void pauseSec(int sec)
{
time_t now,later;
now = time(NULL);
later = time(NULL);
while((later - now) < (double)sec)
later = time(NULL);
}
void pauseNanoSec(long nano)
{
struct timespec tmr1;
tmr1.tv_sec = 0;
tmr1.tv_nsec = nano;
}
void read_dht11_dat()
{
uint8_t laststate = 1;
uint8_t counter = 0;
uint8_t j = 0, i;
float f; /* fahrenheit */
dht11_dat[0] = dht11_dat[1] = dht11_dat[2] = dht11_dat[3] = dht11_dat[4] = 0;
/* pull pin down for 18 milliseconds */
setDirection(48,0); //OUT PUT Direction
writepin(48,0);
usleep(18000);
/* then pull it up for 40 microseconds */
writepin(48,1);
usleep(40);
/* prepare to read the pin */
setDirection(48,1); //IN PUT Direction
/* detect change and read data */
for ( i = 0; i < MAXTIMINGS; i++ )
{
counter = 0;
while ( readpin(48) == laststate )
{
counter++;
usleep( 1 );
if ( counter == 255 )
{
break;
}
}
laststate = readpin(48);
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++;
}
}
/*
* 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” );
}*/
}
int main()
{
while ( 1 )
{
read_dht11_dat();
pauseSec( 1 ); /* wait 1sec to refresh */
}
return(0);
}
Thank you…