m4p5
Posts: 7
Joined: Fri Nov 10, 2017 8:03 am
Location: Paris, France

DHT11 : C program not working but python program OK [solved]

Fri Nov 10, 2017 8:45 am

The C program writen above is not working on my PPi. It's not the hardware because a python programe is working well...

Code: Select all

/*
 *  dht11.c:
 *	Simple test program to test the wiringPi functions
 *	DHT11 test
 */

#include <wiringPi.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define MAXTIMINGS	85
#define DHTPIN		7
int dht11_dat[5] = { 0, 0, 0, 0, 0 };

void read_dht11_dat()
{
	uint8_t laststate	= HIGH;
	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 */
	pinMode( DHTPIN, OUTPUT );
	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 ( digitalRead( DHTPIN ) == laststate )
		{
			counter++;
			delayMicroseconds( 1 );
			if ( counter == 255 )
			{
				break;
			}
		}
		laststate = digitalRead( DHTPIN );

		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( void )
{
	printf( "Raspberry Pi wiringPi DHT11 Temperature test program\n" );

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

	while ( 1 )
	{
		read_dht11_dat();
		delay( 2000 ); /* wait 1sec to refresh */
	}

	return(0);
}
Last edited by m4p5 on Thu Nov 16, 2017 7:51 pm, edited 1 time in total.

User avatar
Edders
Posts: 18
Joined: Mon Mar 23, 2015 10:13 am

Re: DHT11 : C program not working but python program OK

Fri Nov 10, 2017 2:44 pm

Can you please elaborate on "not working"? Do you get errors while running? Does it not compile?

m4p5
Posts: 7
Joined: Fri Nov 10, 2017 8:03 am
Location: Paris, France

Re: DHT11 : C program not working but python program OK

Sat Nov 11, 2017 12:27 pm

Hi, thank you for your interest in my trouble.
Sorry I expressed poorly my request. Not working means that when executing the app, it return "data not found" which mean the DHT11 answer was not right, I suppose it doesn't respond at all. I add new element : I use a raspberry pi 3 B which is 1 month old. I suppose the address (7) is not right or the timer are not well calibrated. This code come from the tuto HOW TO SET UP THE DHT11 HUMIDITY SENSOR ON THE RASPBERRY PI :
http://www.circuitbasics.com/how-to-set ... pberry-pi/
There the code in Python work fine but the code in C fail.

User avatar
Edders
Posts: 18
Joined: Mon Mar 23, 2015 10:13 am

Re: DHT11 : C program not working but python program OK

Tue Nov 14, 2017 3:06 pm

Hi, unfortunately I can't find that error message in the C code you posted. I presume that means it was printed by a library that you're using - did it have some kind of prefix to show which library/function printed that error message?

m4p5
Posts: 7
Joined: Fri Nov 10, 2017 8:03 am
Location: Paris, France

Re: DHT11 : C program not working but python program OK

Tue Nov 14, 2017 5:23 pm

Sorry, I wrote it by memory, the exact message is "data not good, skip", as in the C code. Which means that when running the code, the dht11 response is not correct.

tknpow22
Posts: 1
Joined: Thu Nov 16, 2017 4:06 am

Re: DHT11 : C program not working but python program OK

Thu Nov 16, 2017 4:19 am

try this.
change `counter > 16` => `counter > 50`

Code: Select all

		/* ignore first 3 transitions */
		if ( (i >= 4) && (i % 2 == 0) )
		{
			/* shove each bit into the storage bytes */
			dht11_dat[j / 8] <<= 1;
			/* if ( counter > 50 ) */
			if ( counter > 50 )		/* <- !! here !! */
				dht11_dat[j / 8] |= 1;
			j++;
		}
This page was very helpful:
http://www.rpiblog.com/2012/11/interfac ... idity.html

#I am sorry that my English is not good.

m4p5
Posts: 7
Joined: Fri Nov 10, 2017 8:03 am
Location: Paris, France

Re: DHT11 : C program not working but python program OK

Thu Nov 16, 2017 7:29 pm

Thanks you, your solution work well. 3 to 6 results are good and one results in a "data not good, skip". This is good, I suppose that the timers are not accurate in rasbian...

raspidaan
Posts: 1
Joined: Wed Aug 15, 2018 9:03 pm

Re: DHT11 : C program not working but python program OK [solved]

Wed Aug 15, 2018 9:05 pm

Brilliant! After a long day, a simple change in the timings did the trick. (Change 16 to 50)!
Thanks mate!

cataline
Posts: 1
Joined: Sat Oct 20, 2018 11:09 am

Re: DHT11 : C program not working but python program OK [solved]

Sat Oct 20, 2018 11:12 am

Hi,

i've tried to change 16 to 50, but in my case the c++ code still not returning any data. Just that the data is not good.
The Adafruit python code is working.
Do you have any other ideas why this could happend?

Thank you

User avatar
[email protected]
Posts: 1989
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: DHT11 : C program not working but python program OK [solved]

Mon Oct 29, 2018 3:16 pm

wiringPi supports these sensors natively - if this works for you:

Code: Select all

gpio -xrht03:100:7 aread 100
Returns temp * 10. To get the humidity, aread 101.

then you can look at the source code to work it out. (the dht11 works the same as the rht03)

https://git.drogon.net/?p=wiringPi;a=bl ... es/rht03.c

-Gordon
--
Gordons projects: https://projects.drogon.net/

Return to “C/C++”