Plz. Chack this Code For DHT11 with BBB code .......

37 views
Skip to first unread message

Sumit Bhut

unread,
Mar 26, 2016, 7:46:11 AM3/26/16
to BeagleBoard
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......

Dennis Lee Bieber

unread,
Mar 26, 2016, 3:28:26 PM3/26/16
to beagl...@googlegroups.com
On Sat, 26 Mar 2016 04:46:10 -0700 (PDT), Sumit Bhut
<smb...@gmail.com> declaimed the following:

>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...
>
As I recall, you posted this not quite a week ago, and I provided some
suggestions at that time. Have you even looked at them?

I'll admit that they aren't the most specific -- I had to download the
spec sheet for the device and looked at an Arduino library for reading the
device too...

My strongest suggestion is to change your algorithm -- drop that whole
"laststate" toggle and the modulo operation. Per the spec sheet, the return
data starts each bit with a short LOW, then produces a HIGH whose duration
determines the difference between a 0 and 1 bit.

So reading a bit should consist of (this presumes you enter the
bit-read when the signal is in the LOW state):

loop until the signal goes HIGH;
loop until the signal goes LOW -- counting loop cycles;
if count is > (mid-point between 0-count and 1-count)
bit is a 1
else
bit is a 0
shift bit into received data byte

repeat for additional bits

Use similar logic to detect the start-of-response code...

Basically, the way the check works is that you expect to be in the LOW
at the start, and are looking for the transition to HIGH as the start of
data; you process that data on the next transition to LOW.

Or you get ambitious and port the Arduino library -- since it includes
TIMEOUT conditions should it take too long to detect a response (the simple
code I illustrate above will loop forever is the signal never transitions).

My other suggestion is to clean-up your code. You are passing pin
number to functions that never use it as you have hard-coded the path in
them, etc. Either remove the pin number passing completely, and hard-code
all the access functions; or pass the pin number to all the functions and
use it in the function to create the path.
--
Wulfraed Dennis Lee Bieber AF6VN
wlf...@ix.netcom.com HTTP://wlfraed.home.netcom.com/

Reply all
Reply to author
Forward
0 new messages