DHT22 / Command line with AWK

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
Cognac
Posts: 8
Joined: Sunday 21 February 2016 16:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Newest
Contact:

DHT22 / Command line with AWK

Post by Cognac »

Hi there,

Hopefully anyone can help me changing the code below for transferring data of my DHT22 to Domoticz, my tempfile consists of the code: "Temp=8.4* Humidity=70.4%" instead of the code "Temp = 24 *C, Hum = 40 %" which works for below script.

I need 8.4 as Temp=# and 70.4 as HUM=#.

I think something need to be edited relating to to AWK command.


TEMP=$(cat $TMPFILE|grep Temp |awk '{print $3}')
if [ $TEMP ]
then
TEMP=$(cat $TMPFILE|grep Temp |awk '{print $3}')
HUM=$(cat $ $TMPFILE |grep Temp |awk '{print $7}')

Thank you very much in advance!

Of course I will post my steps on this forum when I get my DHT22 working under Domoticz! ;-)
pdjm43
Posts: 30
Joined: Friday 12 December 2014 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: UK
Contact:

WORKING DHT22 SCRIPT

Post by pdjm43 »

Hi there I just spent 7 hours doing this yesterday. The repo files for collecting the data that would work for you don't exist, as you see the data is all together now i.e Temp=20.0*.
I made this work by editing the example file that you use to test under command line, I just changed the data output.
from this --- print('Temp={0:0.1f}* Humidity={1:0.1f}%'.format(temperature, humidity))
to this --- print('Temp {0:0.1f} {1:0.1f}'.format(temperature, humidity))

and saved it as a new file.

My script called inside Domoticz is ---

Code: Select all

    #!/bin/bash

    # Domoticz server
    SERVER="127.0.0.1:8080"

    # DHT IDX Dummy Temp/Hum
    DHTIDX="45"
    #DHTPIN GPIO
    DHTPIN="17"

    # si vous avez un DHT22 modifiez plus bas sur la ligne Adafruit_DHT 11 par Adafruit_DHT 22
    # TMPFILE : chemin pour fichier temporaire a placer dans le RAMDRIVE pour eviter les
    # ecritures sur la SD card
    # sinon chemin ou sera ecrit le fichier contenant les temperature
    # /tmp/temper.txt est un bon choix si pas de RAMDRIVE installe
    # consultez www.easydomoticz.com pour tout savoir

    TMPFILE="/var/tmp/temper.txt"
    cpt=0
    while [ $cpt -lt 3 ]
    do
    TEMP=""
    sleep 3
    sudo nice -20 /home/pi/Adafruit_Python_DHT-master/examples/AdafruitDHTphil.py 22 $DHTPIN > $TMPFILE
    TEMP=$(cat $TMPFILE|grep Temp |awk '{print $1}')

    if [ $TEMP ]
    then
    TEMP=$(cat $TMPFILE|grep Temp |awk '{print $2}')
    HUM=$(cat $TMPFILE |grep Temp |awk '{print $3}')
    #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=$TEMP;$HUM;2"
    TEMP=""
    HUM=""
    rm $TMPFILE
    exit 0
    fi
    #echo $cpt
    cpt=$(($cpt+1))
    done
    exit 1
There you go.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest