X10 Community Forum

📱🖥️PiX10Hub => 💬General Discussion => 📝GPIO Projects => Topic started by: Tuicemen on January 01, 2021, 03:57:58 PM

Title: Temperature/Humidity Sensor Project
Post by: Tuicemen on January 01, 2021, 03:57:58 PM
There are several projects on the web for these inexpensive DHT11 & DHT22 modules. The AM2302 is essentially the DHT22 inside an enclosure.
For my project I had planed to utilize the DHT addon program in the HG Package manager however the code is out dated and throws errors.
As well when looking for ways to fix the errors I discovered many were saying the C sharp library for these was unreliable. Since the sensors themselves can be unreliably accurate this seemed problematic. :( The readings don't have to be dead accurate for my initial use but they need to be there.

 I found several of the web examples using python2 or python3 and the Adafruit_DHT libraries so I tested A few out.
Some examples I couldn't get to work but these two did  DHT22 (https://www.instructables.com/Raspberry-Pi-Tutorial-How-to-Use-the-DHT-22/) or DHT11 (https://www.thegeekpub.com/236867/using-the-dht11-temperature-sensor-with-the-raspberry-pi/)

I didn't wish to use a breadboard but wished to connect directly to the GPIO pins with Jumper wires. The sensors attached to a small circuit board like ones shown it the attachments work great this way. The AM2302 sensor has that board inside with wires attached. those wires were just the right diameter to fit in the jumper wires. :)%

Since the code worked fine in the command line I went about trying to put something together in HG using the IronPython scripting for the HG Programs. I encountered a couple of issues one (pulling in a library from another location) I was able to resolve the next issue was the Iron Python code got the same error I got with python script while trying to run a DHT22. B:( It seemed Like I was going in circles >*< Looking at Java I discovered it isn't suited for this type of operation thus no library for using the sensors. B:(

 I decided to make a HG program using a C sharp script to run the python3 script and pass the info to a HG widget.
The short story is I got that to work with the HG sensor widget. :)%

 My primary reasoning for setting up a sensor was for my off grid place so I can check and see the inside temperature, seeing the inside humidity is just a bonus. Next I wanted to expand on this new HG add on Program to turn off/on (at least) modules based on values from the sensor. Not being at the place now I couldn't think of anything use other then just informational, but may be in the city place.

 I have a portable humidifier so this seemed like one thing I could use the humidity reading for. My computer room gets very dry in the winter months and a pellet stove doesn't help. I guess if my office was still in the basement it could be used to trigger a dehumidifier

 I have a couple of those ceramic heaters which My wife has been trying to get me to use in my computer room. Seems like I found a use for the temperature value.
Since I figured I may find other uses I added code so any module in HG could be triggered from these values. I used less then and greater then the values to limit the triggering.

This has been working great for the last couple of days as is.
I have a few more tweaks I wish to to do the HG program so once those are done & been tested I'll post the program in the PiX10HuB/General/User created HG script (http://forums.x10.com/index.php?board=172.0) section.
The Program works for any of the modules pictured below but is currently hard coded for GPIO pin 4. I plan to make that a selectable option before posting the program.
Title: Re: Temperature/Humidity Sensor Project
Post by: Tuicemen on January 02, 2021, 08:44:17 AM
Wiring: wiring the above modules is a simple task using jumper wires. Find a 3 volt pin on the header pins,modules are marked "+" for that pin or use the red wire of the Am2302. Find a ground pin grounds are marked on the sensors with "-" or the black wire the last wire or pin on the sensor goes to a GPIO header pin. See on of the links posted above for more clarifications.
Note: the GPIO pin designations were different on my DHT-22 sensor, verses the DHT-11.
Title: Re: Temperature/Humidity Sensor Project
Post by: Tuicemen on January 02, 2021, 04:02:34 PM
The Code:
The Python code supplied in the links posted in the OP work fine for the command line. However in order to pull the info into HG you'll need to rework the code so it only displays the values once and you make the call from HG.
Here is my original you can test from the command line once you have Python3 and the Adafruit_DHT libray installed installed.
Code: [Select]
import Adafruit_DHT as dht
#Set DATA pin
DHT = 4
#Read Temp and Hum from DHT22
h,t = dht.read_retry(dht.DHT22, DHT)
#Print Temperature and Humidity on Shell window
print('Temp={0:0.1f}C  Humidity={1:0.1f}%'.format(t,h))

Note: this is for a DHT22 or AM2302  on GPIO pin4 if you use a DHT11 you'll get some real weird values spit out, to fix this just change any DHT22 instance in the code to DHT11
Title: Re: Temperature/Humidity Sensor Project
Post by: Tuicemen on January 04, 2021, 02:00:45 PM
Pro & Cons:
This project is meant as a Simple indoor temperature and humidity reader for HG.
The sensors can vary in accuracy even between



It should be noted that the precision and accuracy of these sensors can be over come for some automation triggering of events with some experimenting. Also I didn't include programing skill required to get the info into HG as I'll provide the HG program to do this in the user created Programs thread.