DHT22 sensor value's into Domoticz

Moderator: leecollings

User avatar
ILoveIOT
Posts: 59
Joined: Sunday 25 March 2018 17:47
Target OS: Linux
Domoticz version: 2020.2
Location: NL
Contact:

Re: DHT22 sensor value's into Domoticz

Post by ILoveIOT »

There as "donate" button on the domoticz main page
Oh ;) I always "donate" my very limited wisdom, now you are a full C programmer, you can do the same :lol:

I guess $ will helps too, Domoticz is a nice product that I support where I can !! (I'am not part of the team)
1x OPI PC 5.8.16 Debian 10, MiniDNLA,SMD230,EpEver,MPD,800GB,Wiegand,7 temps,DHT,32/64 I/O,2022.1
2x OPI PC 4.19.25 Debian 9, MPD,7 temps,16/32 I/O,BMP,4.10717
1x IntelNUC 5.10 Debian 10, 2TB,DalyBMS,2023.1
3x ESPEasy NodeMCU V3, 16/16 I/O
lwolf
Posts: 216
Joined: Saturday 10 November 2018 18:29
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Hungary
Contact:

Re: DHT22 sensor value's into Domoticz

Post by lwolf »

Just in case: RPIEasy is a python based webGUI which can forward sensor datas, like DHT to Domoticz by HTTP or MQTT. ;)

Image
User avatar
ILoveIOT
Posts: 59
Joined: Sunday 25 March 2018 17:47
Target OS: Linux
Domoticz version: 2020.2
Location: NL
Contact:

Re: DHT22 sensor value's into Domoticz

Post by ILoveIOT »

Hehe i sed NO Python code, but nice, I made the most already in C code on this forum, mcp23017, wiegand, dht, dallas, nothing beats good C code :lol:

But I must remember this project, looks nice, I use espeasy with the wifi chips allot (nodemcu v3), if I can use C, it will be C, otherwise python.
1x OPI PC 5.8.16 Debian 10, MiniDNLA,SMD230,EpEver,MPD,800GB,Wiegand,7 temps,DHT,32/64 I/O,2022.1
2x OPI PC 4.19.25 Debian 9, MPD,7 temps,16/32 I/O,BMP,4.10717
1x IntelNUC 5.10 Debian 10, 2TB,DalyBMS,2023.1
3x ESPEasy NodeMCU V3, 16/16 I/O
Freelance
Posts: 10
Joined: Wednesday 07 October 2020 12:34
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DHT22 sensor value's into Domoticz

Post by Freelance »

One last thing, i got everything working incl the blocky script for relay switching.

But, when i exit putty the dht script stops. I tried to run it with /dht & command for background but it still outputs in the putty terminal and stops when i close putty.

How to make sure that it always runs?
User avatar
ILoveIOT
Posts: 59
Joined: Sunday 25 March 2018 17:47
Target OS: Linux
Domoticz version: 2020.2
Location: NL
Contact:

Re: DHT22 sensor value's into Domoticz

Post by ILoveIOT »

When system starts, put somewhere in a script. (i have /root/scripts/dht &)

You have to also UNCOMMENT the output, otherwise you get output everywhere, the console output is just for a test, so you can make small steps, got output, yes ok next step. :)
1x OPI PC 5.8.16 Debian 10, MiniDNLA,SMD230,EpEver,MPD,800GB,Wiegand,7 temps,DHT,32/64 I/O,2022.1
2x OPI PC 4.19.25 Debian 9, MPD,7 temps,16/32 I/O,BMP,4.10717
1x IntelNUC 5.10 Debian 10, 2TB,DalyBMS,2023.1
3x ESPEasy NodeMCU V3, 16/16 I/O
User avatar
fabry91
Posts: 67
Joined: Sunday 09 December 2018 16:52
Target OS: NAS (Synology & others)
Domoticz version: 2022.2
Location: Italy
Contact:

Re: DHT22 sensor value's into Domoticz

Post by fabry91 »

Hello,

I'm trying to get it work but I think I'm losing something, I compiled the script as described:

Code: Select all

/*
 * dht.c:
 * Read temperature and humidity from DHT11 or DHT22 sensor on a Orange/Raspberry PI and put the value's into Domoticz every 3 minutes.
 *
 * Customize the DHT_PIN and Domoticz IDX below, make a dummy temp/hum in Domoticz.
 * Console output is default off, silent in console output, enable below.
 * Its seems normal that you see "Data not good, skip" a few times, if enabled below.
 * IP address is set to 127.0.0.1 change below.
 *
 *
 * For compiling use   : cd /root/scripts/gpio/ ; gcc -Wall -o dht dht.c -lwiringPi
 * For console use     : /root/scripts/gpio/dht
 * For background use  : /root/scripts/gpio/dht &
 * 
 * More info : http://www.uugear.com/portfolio/read-dht1122-temperature-humidity-sensor-from-raspberry-pi/
 *
 * Updated : 04-03-2019 by ILoveIoT.
 * 
 */

#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#define MAX_TIMINGS	85



// gpio readall BCM10 |  wPi24 |  GPIO.24 | ALT3 | 0 | Physical 35 |
#define DHT_PIN 4	/* GPIO-4 */

// Set the Domoticz IDX from the dummy device (temp/hum) you created
#define DOM_IDX 19	/* Domoticz IDX 19 */



int data[5] = { 0, 0, 0, 0, 0 };

void read_dht_data()
{
	uint8_t laststate	= HIGH;
	uint8_t counter		= 0;
	uint8_t j			= 0, i;

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

	/* pull pin down for 18 milliseconds */
	pinMode( DHT_PIN, OUTPUT );
	digitalWrite( DHT_PIN, LOW );
	delay( 30 );

	/* prepare to read the pin */
	pinMode( DHT_PIN, INPUT );

	/* detect change and read data */
	for ( i = 0; i < MAX_TIMINGS; i++ )
	{
		counter = 0;
		while ( digitalRead( DHT_PIN ) == laststate )
		{
			counter++;
			delayMicroseconds( 2 );
			if ( counter == 255 )
			{
				break;
			}
		}
		laststate = digitalRead( DHT_PIN );

		if ( counter == 255 )
			break;

		/* ignore first 3 transitions */
		if ( (i >= 4) && (i % 2 == 0) )
		{
			/* shove each bit into the storage bytes */
			data[j / 8] <<= 1;
			if ( counter > 16 )
				data[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) &&
	     (data[4] == ( (data[0] + data[1] + data[2] + data[3]) & 0xFF) ) )
	{
		float h = (float)((data[0] << 8) + data[1]) / 10;
		if ( h > 100 )
		{
			h = data[0];	// for DHT11
		}
		float c = (float)(((data[2] & 0x7F) << 8) + data[3]) / 10;
		if ( c > 125 )
		{
			c = data[2];	// for DHT11
		}
		if ( data[2] & 0x80 )
		{
			c = -c;
		}
		
		// For console temp & hum readings uncomment these 2 lines
		//float f = c * 1.8f + 32;
		//printf( "Humidity = %.1f %% Temperature = %.1f *C (%.1f *F)\n", h, c, f );

	
		// Making the string with sensor data for Domoticz
		char str1[] = "/bin/bash -c 'curl -i -s \"http://192.168.0.101:8080/json.htm?type=command&param=udevice&idx=";
		
		// Put variable c (Value celsius) into a string
		char str2[25]; //size of the number
		sprintf(str2, "%g", c);
		
		// Some comma separated value's in between
		char str3[] = ";";
		
		// Put variable h (Value humidity) into a string
		char str4[25]; //size of the number
		sprintf(str4, "%g", h);
		
		// And the final end string
		char str5[] = ";0\"' > /dev/null 2>&1";
		
		// Setting the domoticz IDX var from above
		char str6[25]; //size of the number
		sprintf(str6, "%i", DOM_IDX);
		char str7[] = "&";
		char str8[] = "nvalue=0&svalue=";

		// Concentrate all the str1/str8 and resulting in 1 string is stored in str1.
		strcat(str6,str7);
		strcat(str6,str8);
		strcat(str1,str6);
		strcat(str2,str3);
		strcat(str2,str4);
		strcat(str2,str5);
		strcat(str1,str2);	
		
		// If you wanna view the output of the Domoticz string on console
		//puts(str1);  
	
		// Case of invalid celsius temps, ignore more then +80 or -40 or 0, and recheck in 3sec
		if ( c > -40 && c < 80 && c != 0 )
		{		
			// Send the string/data to Domoticz server
			system(str1);
			
			//delay( 2000 ); /* wait 2 seconds before next read */
			//delay( 300000 ); /* wait 5 minutes before next read */
			delay( 180000 ); /* wait 3 minutes before next read */
			
		} else {
			delay( 3000 ); /* wait 3 seconds before next try */
		}

		
		
	} else {
		// Uncomment this line below if you dont see any data coming
		//printf( "Data not good, skip\n" );
	}
}

int main( void )
{
	//printf( "Orange/Raspberry PI DHT11/DHT22 temperature/humidity test\n" );

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

	while ( 1 )
	{
		read_dht_data();
		delay( 5000 ); /* wait 5 seconds before next try */
	}

	return(0);
}
I tested with the script https://easydomoticz.com/dht-11-22-rasp ... che-enfin/ running "sudo python name.py" I get the temperature/humidity.

When I try to run your script I get the error: What I am doing wrong? :oops:
Attachments
Untitled.jpg
Untitled.jpg (14.76 KiB) Viewed 2473 times
User avatar
ILoveIOT
Posts: 59
Joined: Sunday 25 March 2018 17:47
Target OS: Linux
Domoticz version: 2020.2
Location: NL
Contact:

Re: DHT22 sensor value's into Domoticz

Post by ILoveIOT »

Hehe, sudo /root/scripts/gpio/dht ?
1x OPI PC 5.8.16 Debian 10, MiniDNLA,SMD230,EpEver,MPD,800GB,Wiegand,7 temps,DHT,32/64 I/O,2022.1
2x OPI PC 4.19.25 Debian 9, MPD,7 temps,16/32 I/O,BMP,4.10717
1x IntelNUC 5.10 Debian 10, 2TB,DalyBMS,2023.1
3x ESPEasy NodeMCU V3, 16/16 I/O
User avatar
fabry91
Posts: 67
Joined: Sunday 09 December 2018 16:52
Target OS: NAS (Synology & others)
Domoticz version: 2022.2
Location: Italy
Contact:

Re: DHT22 sensor value's into Domoticz

Post by fabry91 »

ILoveIOT wrote: Thursday 24 December 2020 20:48 Hehe, sudo /root/scripts/gpio/dht ?
Hello LoveIOT, what do you mean? I don't have anything in these folder!
User avatar
fabry91
Posts: 67
Joined: Sunday 09 December 2018 16:52
Target OS: NAS (Synology & others)
Domoticz version: 2022.2
Location: Italy
Contact:

Re: DHT22 sensor value's into Domoticz

Post by fabry91 »

fabry91 wrote: Thursday 24 December 2020 20:36 Hello,

I'm trying to get it work but I think I'm losing something, I compiled the script as described:

Code: Select all

/*
 * dht.c:
 * Read temperature and humidity from DHT11 or DHT22 sensor on a Orange/Raspberry PI and put the value's into Domoticz every 3 minutes.
 *
 * Customize the DHT_PIN and Domoticz IDX below, make a dummy temp/hum in Domoticz.
 * Console output is default off, silent in console output, enable below.
 * Its seems normal that you see "Data not good, skip" a few times, if enabled below.
 * IP address is set to 127.0.0.1 change below.
 *
 *
 * For compiling use   : cd /root/scripts/gpio/ ; gcc -Wall -o dht dht.c -lwiringPi
 * For console use     : /root/scripts/gpio/dht
 * For background use  : /root/scripts/gpio/dht &
 * 
 * More info : http://www.uugear.com/portfolio/read-dht1122-temperature-humidity-sensor-from-raspberry-pi/
 *
 * Updated : 04-03-2019 by ILoveIoT.
 * 
 */

#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#define MAX_TIMINGS	85



// gpio readall BCM10 |  wPi24 |  GPIO.24 | ALT3 | 0 | Physical 35 |
#define DHT_PIN 4	/* GPIO-4 */

// Set the Domoticz IDX from the dummy device (temp/hum) you created
#define DOM_IDX 19	/* Domoticz IDX 19 */



int data[5] = { 0, 0, 0, 0, 0 };

void read_dht_data()
{
	uint8_t laststate	= HIGH;
	uint8_t counter		= 0;
	uint8_t j			= 0, i;

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

	/* pull pin down for 18 milliseconds */
	pinMode( DHT_PIN, OUTPUT );
	digitalWrite( DHT_PIN, LOW );
	delay( 30 );

	/* prepare to read the pin */
	pinMode( DHT_PIN, INPUT );

	/* detect change and read data */
	for ( i = 0; i < MAX_TIMINGS; i++ )
	{
		counter = 0;
		while ( digitalRead( DHT_PIN ) == laststate )
		{
			counter++;
			delayMicroseconds( 2 );
			if ( counter == 255 )
			{
				break;
			}
		}
		laststate = digitalRead( DHT_PIN );

		if ( counter == 255 )
			break;

		/* ignore first 3 transitions */
		if ( (i >= 4) && (i % 2 == 0) )
		{
			/* shove each bit into the storage bytes */
			data[j / 8] <<= 1;
			if ( counter > 16 )
				data[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) &&
	     (data[4] == ( (data[0] + data[1] + data[2] + data[3]) & 0xFF) ) )
	{
		float h = (float)((data[0] << 8) + data[1]) / 10;
		if ( h > 100 )
		{
			h = data[0];	// for DHT11
		}
		float c = (float)(((data[2] & 0x7F) << 8) + data[3]) / 10;
		if ( c > 125 )
		{
			c = data[2];	// for DHT11
		}
		if ( data[2] & 0x80 )
		{
			c = -c;
		}
		
		// For console temp & hum readings uncomment these 2 lines
		//float f = c * 1.8f + 32;
		//printf( "Humidity = %.1f %% Temperature = %.1f *C (%.1f *F)\n", h, c, f );

	
		// Making the string with sensor data for Domoticz
		char str1[] = "/bin/bash -c 'curl -i -s \"http://192.168.0.101:8080/json.htm?type=command&param=udevice&idx=";
		
		// Put variable c (Value celsius) into a string
		char str2[25]; //size of the number
		sprintf(str2, "%g", c);
		
		// Some comma separated value's in between
		char str3[] = ";";
		
		// Put variable h (Value humidity) into a string
		char str4[25]; //size of the number
		sprintf(str4, "%g", h);
		
		// And the final end string
		char str5[] = ";0\"' > /dev/null 2>&1";
		
		// Setting the domoticz IDX var from above
		char str6[25]; //size of the number
		sprintf(str6, "%i", DOM_IDX);
		char str7[] = "&";
		char str8[] = "nvalue=0&svalue=";

		// Concentrate all the str1/str8 and resulting in 1 string is stored in str1.
		strcat(str6,str7);
		strcat(str6,str8);
		strcat(str1,str6);
		strcat(str2,str3);
		strcat(str2,str4);
		strcat(str2,str5);
		strcat(str1,str2);	
		
		// If you wanna view the output of the Domoticz string on console
		//puts(str1);  
	
		// Case of invalid celsius temps, ignore more then +80 or -40 or 0, and recheck in 3sec
		if ( c > -40 && c < 80 && c != 0 )
		{		
			// Send the string/data to Domoticz server
			system(str1);
			
			//delay( 2000 ); /* wait 2 seconds before next read */
			//delay( 300000 ); /* wait 5 minutes before next read */
			delay( 180000 ); /* wait 3 minutes before next read */
			
		} else {
			delay( 3000 ); /* wait 3 seconds before next try */
		}

		
		
	} else {
		// Uncomment this line below if you dont see any data coming
		//printf( "Data not good, skip\n" );
	}
}

int main( void )
{
	//printf( "Orange/Raspberry PI DHT11/DHT22 temperature/humidity test\n" );

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

	while ( 1 )
	{
		read_dht_data();
		delay( 5000 ); /* wait 5 seconds before next try */
	}

	return(0);
}
I tested with the script https://easydomoticz.com/dht-11-22-rasp ... che-enfin/ running "sudo python name.py" I get the temperature/humidity.

When I try to run your script I get the error: What I am doing wrong? :oops:
Figured it out with the french script! Everything is working now, with an update of the temp/hum every 10 minute :D
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests