Page 1 of 2

DHT 11 on Raspberry 2

Posted: Wednesday 23 December 2015 9:33
by Smartie
HINT: STRG is GERMAN Layout STRG Mean CTRL

Now its working...
The DHT 11 Script from here Dosnt work for me.

So i have change a lot from the script here at the Forum
other post: viewtopic.php?f=12&t=2006

To Install
(Pin1) 3,3V
(Pin6) GND
(Pin4) GPIO
reeding at the other Post

Login SSH
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential python-dev python-openssl build-dep git nano
cd home/pi/domoticz/scripts
git clone https://github.com/adafruit/Adafruit_Python_DHT.git && cd Adafruit_Python_DHT
sudo python setup.py install
cd examples

now Check that it works:
$ sudo ./AdafruitDHT.py 11 4
Temp=24.0* Humidity=41.0%

If it works so its right.

now we make a dht11.sh to loop

First add a Dummy Switch at Domticz and see the IDX Number

then go back to the shell and make the script.

sudo nano dht_11.sh
now add the follow script:

Code: Select all


#!/bin/sh

# Domoticz server
SERVER="127.0.0.1:8080"
# DHT IDX
DHTIDX="26"

# DHTPIN
DHTPIN="4"

sleep 5

sudo nice -20 python /home/pi/domoticz/scripts/Adafruit_Python_DHT/examples/AdafruitDHT.py 11 $DHTPIN > /var/tmp/temp.txt

#TEMP=$(cat /var/tmp/temp.txt | grep "Temp" | awk '{ print $3 }')
#TEMP=$(cat /var/tmp/temp.txt | grep "Temp")

TEMP=$(awk ' /Temp/ {print substr ($0,6,4)}' /var/tmp/temp.txt)
#HUM=$(awk ' /Hudmidity/ {print substr ($0,0)}' /var/tmp/temp.txt)
#HUM=$(awk ' /Humidity/ {print 1$}' /var/tmp/temp.txt)
HUM=$(awk ' /Humidity/ {print substr ($0,22,4)}' /var/tmp/temp.txt)
echo $TEMP
echo $HUM

# Send data
curl -s -i -H "Accept: application/json" "http://$SERVER/json.htm?type=command&param=udevice&idx=$DHTIDX&nvalue=0&svalue=$TE$
#echo curl -s -i -H "Accept: application/json" "http://$SERVER/json.htm?type=command&param=udevice&idx=$DHTIDX&nvalue=0&sval$

TEMP=""
HUM=""
Change the IDX Number to Your nuber
Then STRG + O to save and STRG + X to go out from the editor mode.

Then add a Crontab with Crontab -e
add follow command
*/5 * * * * /home/pi/domoticz/scripts/dht_11.sh

save with STRG +O and exit with STRG + X

Now the cronjob refresh every 5 Minutes the Temp

Re: DHT 11 on Raspberry 2

Posted: Wednesday 23 December 2015 9:38
by D'rMorris
Thanks for the how-to guide :). Could you please change STRG to CTRL? STRG is only known on keyboards with a German layout ;). Thanks!

Re: DHT 11 on Raspberry 2

Posted: Wednesday 23 December 2015 10:38
by Smartie
Done have add a hint :)

Re: DHT 11 on Raspberry 2

Posted: Wednesday 17 February 2016 13:31
by Chiopas
I encountered one error:

22.0
36.0
dht_11.sh: 28: dht_11.sh: sval$: not found

What am I doing wrong? I have temp and humidity in temp.txt file but the dummy device is not updating.

Re: DHT 11 on Raspberry 2

Posted: Sunday 28 February 2016 16:51
by macdaw
My solution.

Change line in dth_11.script on :

curl -s -i -H "Accept: application/json" "http://$SERVER/json.htm?type=command&param=udevice&idx=$DHTIDX&nvalue=0&svalue=$TEMP;$HUM;2"

Works !

Re: DHT 11 on Raspberry 2

Posted: Wednesday 16 March 2016 19:21
by Smartie
New Json format After Update dosn't work update more.
Plase change the

curl -s -i -H "Accept: application/json" "http://$SERVER/json.htm?type=command&param=udevice&idx=$DHTIDX&nvalue=0&svalue=$TEMP;$HUM;2"

to

# Send data
curl -s -i -H "Accept: application/json" "http://$SERVER/json.htm?type=command&c=getauth&param=udevice&idx=$DHTIDX&nvalue=0&svalue=$TEMP;$HUM;2"

so it works

------------------------------------------------------------------------------------------------------------------------------------------------------------

#!/bin/sh

# Domoticz server
SERVER="127.0.0.1:8080"
# DHT IDX
DHTIDX="21"

# DHTPIN
DHTPIN="4"

sleep 5

sudo nice -20 python /home/pi/Adafruit_Python_DHT/examples/AdafruitDHT.py 11 $DHTPIN > /var/tmp/temp.txt

#TEMP=$(cat /var/tmp/temp.txt | grep "Temp" | awk '{ print $3 }')
#TEMP=$(cat /var/tmp/temp.txt | grep "Temp")

TEMP=$(awk ' /Temp/ {print substr ($0,6,4)}' /var/tmp/temp.txt)
#HUM=$(awk ' /Hudmidity/ {print substr ($0,0)}' /var/tmp/temp.txt)
#HUM=$(awk ' /Humidity/ {print 1$}' /var/tmp/temp.txt)
HUM=$(awk ' /Humidity/ {print substr ($0,22,4)}' /var/tmp/temp.txt)
echo $TEMP
echo $HUM

# Send data
curl -s -i -H "Accept: application/json" "http://$SERVER/json.htm?type=command&c=getauth&param=udevice&idx=$DHTIDX&nvalue=0&svalue=$TEMP;$HUM;2"


TEMP=""
HUM=""

Re: DHT 11 on Raspberry 2

Posted: Thursday 31 March 2016 15:35
by rickybee
I am trying to get this to work but failing. The sensor is working, the above script creates the temp.txt file with correct data but I get the following output error...

22.0
37.0
HTTP/1.1 200 OK
Content-Length: 24
Content-Type: application/json;charset=UTF-8
Cache-Control: no-cache
Pragma: no-cache
Access-Control-Allow-Origin: *

{
"status" : "ERR"
}

Help?

***** Figured it out ******
Being new to this I didn't realize that the hardware device IDX number is different from the Virtual Sensor IDX number. When I put the sensor one in the script it worked.

Re: DHT 11 on Raspberry 2

Posted: Tuesday 05 April 2016 12:38
by superkris
Thanks for this guide. I'm a bit of a noob when it comes to making scripts and working with linux based systems.

After some trying i got things working. The DHT11 sensor works in Domoticz, but it doesnt update.

AdafruitDHT.py runs fine when i run it.
dht_11.sh seems to work fine too now. Then i run it manually it prints the values and i see no errors.

However, the value in domoticz does not refresh. When i manually run dht_11.sh again, it does seem to refresh, but it only takes a data point every 5 minutes.

1st question: When i run the script manually every minute, it still only updates every 5 minutes in domoticz. I dont know bash very well, but it looks like dht_11.sh can read temperature every 5 seconds instead of just 5 minutes. Is the 5 minute update rate a limitation of domiticz, or is this caused by something else?

I dont know anything about Crontab, but i gues this is what makes the dht_11.sh script update every 5 minutes. I tried to follow the steps from the first post, but the value in domotics just stays the same.

Question 2: Am i using Crontab wrong? I think i had to make a new file. Do i need to do something to make this run?

I'm also running a phyton scrip that controls physical buttons for functions like shutdown. Its stats at boot because i added it at the rc.local file. Can i use this file to update the script too?

Re: DHT 11 on Raspberry 2

Posted: Monday 11 April 2016 22:36
by superkris
Nobody?

Re: DHT 11 on Raspberry 2

Posted: Friday 29 April 2016 23:57
by davidoliveirapt
superkris wrote:Nobody?
Crontab can't be executed to the second unit.
In mine i have configured it to 5 minutes sending.

Re: DHT 11 on Raspberry 2

Posted: Monday 02 May 2016 13:17
by Smartie
Crontab is only crontab -e and add the link.

But for Cronjob you must install mail so try apt-get install mail and reboot then add the cronjob every 5 minutes ;)
its a bug from the Domoticz image

Re: DHT 11 on Raspberry 2

Posted: Monday 24 October 2016 19:49
by ijobain
Hi all
I have no values or graphics at Domoticz Temperature panel

if i type
pi@raspberrypi:~/domoticz/scripts $ AdafruitDHT.py 11 16
Temp=25.0* Humidity=36.0%

pi@raspberrypi:~/domoticz/scripts $ ./dht11.sh
25.0*
37.0%

In browser url:
http://192.168.1.45:8080/json.htm?type= ... EMP;$HUM;2
{
"status" : "OK",
"title" : "Update Device"
}

dht11.sh

Code: Select all

#!/bin/bash
# Domoticz server
SERVER="192.168.1.45:8080"
# DHT  IDX Dummy
DHTIDX="5"
#
#DHTPIN Gpio Pin
DHTPIN="16"

TEMP=""

until  [ -n "$TEMP" ] ; do
sleep 5
sudo nice -20 AdafruitDHT.py 11 $DHTPIN > temp.txt
TEMP=$(cat temp.txt|grep Temp|cut -d"=" -f2|cut -d" " -f1)

done
TEMP=$(cat temp.txt|cut -d"=" -f2|cut -d" " -f1)
HUM=$(cat temp.txt|cut -d"=" -f3|cut -d" " -f1)
echo $TEMP
echo $HUM
# Send data
#curl  -s -i -H "Accept: application/json" "http://192.168.1.45:8080/json.htm?type=command&param=udevice&idx=$DHTIDX&nvalue=0&svalue=$TEMP;$HUM;2"
TEMP=""
HUM=""
rm temp.txt
Where are the errors ?

SOLVED
dht11.sh edited --> /json"(two spaces)"http

Code: Select all

#!/bin/sh
# Domoticz server
SERVER="192.168.1.45:8080"
# DHT IDX
DHTIDX="22"

# DHTPIN
DHTPIN="16"

# TEMP FILE
TMPFILE="/var/tmp/temp.txt"

cpt=0
while [ $cpt -lt 6 ]
do
TEMP=""

sleep 5

sudo nice -20 AdafruitDHT.py 11 $DHTPIN > /var/tmp/temp.txt
#TEMP=$(cat /var/tmp/temp.txt | grep "Temp" | awk '{ print $3 }')
#TEMP=$(cat /var/tmp/temp.txt | grep "Temp")

TEMP=$(awk ' /Temp/ {print substr ($0,6,4)}' /var/tmp/temp.txt)
#HUM=$(awk ' /Hudmidity/ {print substr ($0,0)}' /var/tmp/temp.txt)
#HUM=$(awk ' /Humidity/ {print 1$}' /var/tmp/temp.txt)
HUM=$(awk ' /Humidity/ {print substr ($0,22,4)}' /var/tmp/temp.txt)
echo $TEMP
echo $HUM

# Send data
curl -s -i -H "Accept: application/json"  "http://192.168.1.45:8080/json.htm?type=command&c=getauth&param=udevice&idx=$DHTIDX&nva$

TEMP=""
HUM=""

#rm $TMPFILE
exit 0
#fi
#echo $cpt
cpt=$(($cpt+1))
done
exit 1
Image

Re: DHT 11 on Raspberry 2

Posted: Sunday 04 December 2016 20:39
by journeyman
File "dht11.py", line 12
sleep 5
^
SyntaxError: invalid syntax


What is wrong? :(

Re: DHT 11 on Raspberry 2

Posted: Sunday 05 February 2017 19:18
by Spindi
Hello guys!

I have a little bit of trouble with my DHT11 Sensor on my Raspberry,
I've tried to follow your steps as mentioned but if i type in "./AdafruitDHT.py 11 4" I get in about 8 of 10 tries wrong information’s back.
2-times: Temp=22.0* Humidity=21.0%
8-times: Temp=11.0* Humidity=138.0%

I don't know where to search my error. I've tried two different sensors.

May someone can help me?

Thanks a lot!!!

Re: DHT 11 on Raspberry 2

Posted: Sunday 12 February 2017 19:53
by Spindi
Hi guys!

Second try, I have upset the domoticz a second time and followed the instructions point for point again.
Two errors have raised:
1. When i run the following line:
sudo apt-get install build-essential python-dev python-openssl build-dep git nano
I always see this error:
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package build-dep


2. Whe i run the following line:
sudo ./AdafruitDHT.py 11 4
I get back this:
Temp=11.0* Humidity=139.0%
But this is wrong!

Please may somebody can help me!!!!

Thx,
by the side. I run a Raspberry Pi 3 with the latest Raspbian Jessie version.

Re: DHT 11 on Raspberry 2

Posted: Wednesday 15 February 2017 19:46
by ijobain
Spindi wrote:Hi guys!

...

2. Whe i run the following line:
sudo ./AdafruitDHT.py 11 4
I get back this:
Temp=11.0* Humidity=139.0%
But this is wrong!

Please may somebody can help me!!!!

Thx,
by the side. I run a Raspberry Pi 3 with the latest Raspbian Jessie version.

Try with another DHT sensor.

Re: DHT 11 on Raspberry 2

Posted: Thursday 16 February 2017 19:44
by Spindi
I have tried it with two different sensors, always the same result.

Re: DHT 11 on Raspberry 2

Posted: Thursday 16 February 2017 20:07
by Spindi
I now tried the sensor on an other port (GPIO 17 by the way) but if i try:
sudo ./AdafruitDHT.py 11 17
the response is:
Failed to get reading. Try again!

Normally?

Re: DHT 11 on Raspberry 2

Posted: Friday 17 February 2017 13:15
by ijobain
Spindi wrote:I now tried the sensor on an other port (GPIO 17 by the way) but if i try:
sudo ./AdafruitDHT.py 11 17
the response is:
Failed to get reading. Try again!

Normally?
Do you have Pull-up resistor ?
Image

Re: DHT 11 on Raspberry 2

Posted: Friday 17 February 2017 16:42
by Spindi
Ohhh, no actually not.
Is this essentially?
I've read this in another forum but doesn't tried it.
I'm going to get one and try this.
Thank you for this hint!!!!!!