DHT 11 on Raspberry 2

Moderator: leecollings

Smartie
Posts: 21
Joined: Monday 14 December 2015 15:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

DHT 11 on Raspberry 2

Post 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
Last edited by Smartie on Wednesday 23 December 2015 10:37, edited 1 time in total.
D'rMorris
Posts: 138
Joined: Thursday 01 May 2014 9:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands - Sittard
Contact:

Re: DHT 11 on Raspberry 2

Post 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!
Smartie
Posts: 21
Joined: Monday 14 December 2015 15:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DHT 11 on Raspberry 2

Post by Smartie »

Done have add a hint :)
Chiopas
Posts: 1
Joined: Wednesday 17 February 2016 13:15
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DHT 11 on Raspberry 2

Post 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.
macdaw
Posts: 1
Joined: Sunday 28 February 2016 16:47
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DHT 11 on Raspberry 2

Post 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 !
Smartie
Posts: 21
Joined: Monday 14 December 2015 15:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DHT 11 on Raspberry 2

Post 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=""
rickybee
Posts: 8
Joined: Thursday 31 March 2016 15:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DHT 11 on Raspberry 2

Post 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.
superkris
Posts: 13
Joined: Friday 25 March 2016 16:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DHT 11 on Raspberry 2

Post 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?
superkris
Posts: 13
Joined: Friday 25 March 2016 16:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DHT 11 on Raspberry 2

Post by superkris »

Nobody?
davidoliveirapt
Posts: 1
Joined: Friday 29 April 2016 23:56
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.483
Location: Portugal
Contact:

Re: DHT 11 on Raspberry 2

Post by davidoliveirapt »

superkris wrote:Nobody?
Crontab can't be executed to the second unit.
In mine i have configured it to 5 minutes sending.
Smartie
Posts: 21
Joined: Monday 14 December 2015 15:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DHT 11 on Raspberry 2

Post 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
ijobain
Posts: 7
Joined: Friday 09 January 2015 9:42
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DHT 11 on Raspberry 2

Post 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
Last edited by ijobain on Wednesday 14 December 2016 22:49, edited 1 time in total.
journeyman
Posts: 23
Joined: Sunday 04 December 2016 16:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Poland
Contact:

Re: DHT 11 on Raspberry 2

Post by journeyman »

File "dht11.py", line 12
sleep 5
^
SyntaxError: invalid syntax


What is wrong? :(
Spindi
Posts: 8
Joined: Sunday 05 February 2017 19:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DHT 11 on Raspberry 2

Post 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!!!
Spindi
Posts: 8
Joined: Sunday 05 February 2017 19:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DHT 11 on Raspberry 2

Post 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.
ijobain
Posts: 7
Joined: Friday 09 January 2015 9:42
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DHT 11 on Raspberry 2

Post 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.
Spindi
Posts: 8
Joined: Sunday 05 February 2017 19:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DHT 11 on Raspberry 2

Post by Spindi »

I have tried it with two different sensors, always the same result.
Spindi
Posts: 8
Joined: Sunday 05 February 2017 19:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DHT 11 on Raspberry 2

Post 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?
ijobain
Posts: 7
Joined: Friday 09 January 2015 9:42
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DHT 11 on Raspberry 2

Post 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
Spindi
Posts: 8
Joined: Sunday 05 February 2017 19:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DHT 11 on Raspberry 2

Post 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!!!!!!
Post Reply