working lol_dht(for DHT22 with wiringPi)

Post Reply
StonieMalony
Posts: 13
Joined: Sun Mar 08, 2015 4:22 pm
languages_spoken: english
ODROIDs: Odroid C1
Has thanked: 0
Been thanked: 0
Contact:

working lol_dht(for DHT22 with wiringPi)

Unread post by StonieMalony » Wed Jun 24, 2015 11:01 pm

Hi,

I got lol_dht working accidentally :) I add the code and then give the story :)

Code: Select all

/*
 *      dht22.c:
 *      Simple test program to test the wiringPi functions
 *      Based on the existing dht11.c
 *      Amended by technion@lolware.net
 */

#include <wiringPi.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <unistd.h>

#define MAXTIMINGS 85
static int DHTPIN = 7;
static int dht22_dat[5] = {0,0,0,0,0};

static uint8_t sizecvt(const int read)
{
  /* digitalRead() and friends from wiringpi are defined as returning a value
  < 256. However, they are returned as int() types. This is a safety function */

  if (read > 255 || read < 0)
  {
    printf("Invalid data from wiringPi library\n");
    exit(EXIT_FAILURE);
  }
  return (uint8_t)read;
}

static int read_dht22_dat()
{
  uint8_t laststate = HIGH;
  uint8_t counter = 0;
  uint8_t j = 0, i;

  dht22_dat[0] = dht22_dat[1] = dht22_dat[2] = dht22_dat[3] = dht22_dat[4] = 0;

  // pull pin down for 18 milliseconds
  pinMode(DHTPIN, OUTPUT);
  digitalWrite(DHTPIN, HIGH);
  delay(10);
  digitalWrite(DHTPIN, LOW);
  delay(18);
  // then pull it up for 40 microseconds
  digitalWrite(DHTPIN, HIGH);
  delayMicroseconds(40);
  // prepare to read the pin
  pinMode(DHTPIN, INPUT);

  // detect change and read data
  for ( i=0; i< MAXTIMINGS; i++) {
    counter = 0;
    while (sizecvt(digitalRead(DHTPIN)) == laststate) {
      counter++;
      delayMicroseconds(2);
      if (counter == 255) {
        break;
      }
    }
    laststate = sizecvt(digitalRead(DHTPIN));
//    printf("me here, trying to figure out what is going on in the world %i \n", laststate);
//    delay(1);

    if (counter == 255) break;

    // ignore first 3 transitions
    if ((i >= 4) && (i%2 == 0)) {
      // shove each bit into the storage bytes
      dht22_dat[j/8] <<= 1;
      if (counter > 16)
        dht22_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) &&
      (dht22_dat[4] == ((dht22_dat[0] + dht22_dat[1] + dht22_dat[2] + dht22_dat[3]) & 0xFF)) ) {
        float t, h;
        h = (float)dht22_dat[0] * 256 + (float)dht22_dat[1];
        h /= 10;
        t = (float)(dht22_dat[2] & 0x7F)* 256 + (float)dht22_dat[3];
        t /= 10.0;
        if ((dht22_dat[2] & 0x80) != 0)  t *= -1;


    printf("Humidity = %.2f %% Temperature = %.2f *C \n", h, t );
    return 1;
  }
  else
  {
    printf("Data not good, skip\n");
    return 0;
  }
}

int main (int argc, char *argv[])
{
  int lockfd;

  if (argc != 2)
    printf ("usage: %s <pin>\ndescription: pin is the wiringPi pin number\nusing 7 (GPIO 4)\n",argv[0]);
  else
    DHTPIN = atoi(argv[1]);


  printf ("Raspberry Pi wiringPi DHT22 reader\nwww.lolware.net\n") ;


  if (wiringPiSetup () == -1)
    exit(EXIT_FAILURE) ;

  if (setuid(getuid()) < 0)
  {
    perror("Dropping privileges failed\n");
    exit(EXIT_FAILURE);
  }

  while (read_dht22_dat() == 0)
  {
     delay(1000); // wait 1sec to refresh
  }

  delay(1500);

  return 0 ;
}
well the story :) my project I been working for long time already is based heavily on dht22 sensors and before last weekend my problem with odroid was that C code I used to get dht22 sensor data on banana and raspberry didnt work. I couldnt find any help from internet and then of course there was that time problem (we never have enough time to do things we want to do really :)). But this weekend I had little time so I thought that I look at that code and see can I make heads or tails out of it. But I found solution with first line I entered into code( printf("me here, trying to figure out what is going on in the world %i \n", laststate); ) ... I wanted to see these laststate numbers just hoping that maybe they will have some meaning that I can understand, they didnt but for my big surprise code started working :). After extensive testing (it didnt make sense to me at first - how can) I formulated a theory that maybe c1 is just that much quicker than banana and raspberry that it runs the program that quick that it greatest timing issues. So I commented my line and just before that there was delayMicroseconds(1); I changed it into delayMicroseconds(2); and that did the trick. the code it working.

Well right now the solution is bit poweruser method and well it's works. But could somebody look at the code who really understands what's written there :). My programming skills are all based on copy'n'paste more or less, never the less I think I have copied and pasted some pretty amazing things but this code is bit beyond me. Maybe there's some better way to make it work.

In my house I got right now 11 dht22 sensors connected to raspberry banana and now odroid. My system also logs sensor error messages, time to time they fail to get the reading in reasonable time and give the error. I believe now that it have to do something with how busy or not busy the system is and this related to timing. The modiefied code works with raspberry and banana but as before error count was close to zero with raspberry and it was quiet few with banana then now these positions have been replaced, there's rarely error with banana and there are quiet few with raspberry. There are quiet a lot of errors with odroid, not enoug that I start to worry about it but still amount worth mentioning :).

Most of odroid errors are related to sensor on wiringPi 0, there are only few errors of other sensors connected to odroid. Is it possible that this pin in handled somewhat differently or what can be the reason of it ?

rowan194
Posts: 146
Joined: Tue Jun 02, 2015 1:43 am
languages_spoken: english
ODROIDs: C1, XU4, C2
Has thanked: 0
Been thanked: 0
Contact:

Re: working lol_dht(for DHT22 with wiringPi)

Unread post by rowan194 » Mon Sep 07, 2015 2:07 am

Just a quick bump to say thank you for posting this code. This is the first time I've ever used the C1 expansion pins, and I'm pleased to report that my DHT22 works. Did you end up figuring out your error issues?

ludwich
Posts: 13
Joined: Wed Jan 21, 2015 3:46 am
languages_spoken: german, english
ODROIDs: C1
Has thanked: 0
Been thanked: 0
Contact:

not working lol_dht (for DHT22 / AM2302 with wiringPi)

Unread post by ludwich » Tue Sep 22, 2015 7:15 pm

Not Found -lwiringPi ?

? i made this on an Raspberry and it is working well
? on my C1 i receive following line : "checking for wiringPiSetup in -lwiringPi... no"
--> no! ?

This are my Stepps on C1
1. git clone https://github.com/hardkernel/wiringPi
2. cd wiringPi
./build

3. git clone https://github.com/technion/lol_dht22
cd lol_dht22
4.Edit the Timevalue 1ms in 2 ms (other Post dht22 working on C1)
5. ./configure
make

6: i found in /usr/lib/
@libwiringPi.so
*libwiringPi.so.2.0
@libwiringPiDev.so
*libwiringPiDev.so.2.0

--> Where is my problem?
--> Please give me a hint?

ludwich

Code: Select all

odroid@koreander:~/lol_dht22$ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking whether make sets $(MAKE)... (cached) yes
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking wiringPi usability... no
checking wiringPi presence... no
checking for wiringPi... no
checking /usr/local/include/wiringPi.h usability... yes
checking /usr/local/include/wiringPi.h presence... yes
checking for /usr/local/include/wiringPi.h... yes
checking for wiringPiSetup in -lwiringPi... no
configure: error: Fail to find wiringPi library

--

User avatar
odroid
Site Admin
Posts: 31297
Joined: Fri Feb 22, 2013 11:14 pm
languages_spoken: English
ODROIDs: ODROID
Has thanked: 44 times
Been thanked: 179 times
Contact:

Re: working lol_dht(for DHT22 with wiringPi)

Unread post by odroid » Mon Sep 28, 2015 3:27 pm

Could you run other basic wiringpi examples?

rowan194
Posts: 146
Joined: Tue Jun 02, 2015 1:43 am
languages_spoken: english
ODROIDs: C1, XU4, C2
Has thanked: 0
Been thanked: 0
Contact:

Re: working lol_dht(for DHT22 with wiringPi)

Unread post by rowan194 » Wed Sep 30, 2015 11:19 am

ludwich, did you manage to get it working? If you don't need the newest bleeding edge version you can install the library via apt-get, instead of compiling the source.

sudo apt-get install wiringPi

edit: actually now that I think of it, I believe I had exactly the same problem - library installed, but lol_dht22 configure, via official git, couldn't find it. Not sure why configure is needed for such a simple program; I ended up compiling it manually.

gcc -o dht22 dht22.c locking.c -lwiringPi -pthread

ludwich
Posts: 13
Joined: Wed Jan 21, 2015 3:46 am
languages_spoken: german, english
ODROIDs: C1
Has thanked: 0
Been thanked: 0
Contact:

Re: working lol_dht(for DHT22 with wiringPi)

Unread post by ludwich » Sat Oct 03, 2015 6:18 am

Thank you - It works now

:)
great ---<

ludwich

cogitator
Posts: 6
Joined: Wed Oct 28, 2015 9:53 pm
languages_spoken: english german
ODROIDs: 4x XU4
Location: Mönchengladbach, Germany
Has thanked: 0
Been thanked: 0
Contact:

Re: working lol_dht(for DHT22 with wiringPi)

Unread post by cogitator » Fri Nov 13, 2015 6:55 am

gcc -o dht22 dht22.c locking.c -lwiringPi -pthread
Simple, but gold! Thank you!

kenshirou
Posts: 1
Joined: Tue Jul 09, 2019 7:15 pm
languages_spoken: english
ODROIDs: C1+,C2,N2
Has thanked: 0
Been thanked: 0
Contact:

Re: working lol_dht(for DHT22 with wiringPi)

Unread post by kenshirou » Tue Jul 09, 2019 7:17 pm

I had to do it like this gcc -o dht22 dht22.c locking.c -lwiringPi -pthread -lcrypt -lm -lrt

Post Reply

Return to “General Topics”

Who is online

Users browsing this forum: No registered users and 1 guest