diff --git a/edl21.sh b/edl21.sh index 23c8c8f..77bd1a2 100644 --- a/edl21.sh +++ b/edl21.sh @@ -1,50 +1,76 @@ #!/bin/bash -#Eingabegerät +# edl21.sh reads data from the EDL21 electricity meter and writes it up to a MySQL/MariaDB. +# https://code.hw12.org/tilman/edl21 +# +# Version 0.5 - 03.03.2019 +# +# License GPL3.0 or later +# https://code.hw12.org/tilman/edl21/src/branch/master/LICENSE + +#Device configuration INPUT_DEV="/dev/ttyUSB0"; stty -F $INPUT_DEV 1:0:8bd:0:3:1c:7f:15:4:5:1:0:11:13:1a:0:12:f:17:16:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0 -#Datum, Zeit und Datei. -DATE=$(/bin/date +%Y%m%d-%H%M%S); -PFAD=$(dirname "$(readlink -e "$0")"); +#Log file and temporary txt. FILE=$PFAD"/"$DATE."txt"; LOG=$PFAD"/edl.log"; +PFAD=$(dirname "$(readlink -e "$0")"); -#Datenbank -DB_USER=""; -DB_PASS=""; -DB_HOST=""; -DB_NAME=""; -DB_TAB="EdlData"; +#Database +DB_USER="dbuser"; +DB_PASS="dbpasswd"; +DB_HOST="dbhost"; +DB_NAME="dbname"; +DB_TAB="dbtable"; -#Startsequenz +#Get date and round to nearest 5min (for harmonized use with data from other scripts in pChart) +DATE_RAW=$(/bin/date +%s); +DATE=$(/bin/date -d @$DATE_RAW "+%Y-%m-%d %H:%M:%S"); +DATE_N5M=$(echo "($DATE_RAW % 300)"|bc); + +if [ $DATE_N5M -lt 150 ];then + DATE_N5M=$(echo "($DATE_RAW - ($DATE_RAW % 300))"|bc); +else + DATE_N5M=$(echo "($DATE_RAW - ($DATE_RAW % 300) + 300)"|bc); +fi +DATE_N5M=$(/bin/date -d @$DATE_N5M "+%Y-%m-%d %H:%M:%S"); + +#Starting sequence SEQ_START="1B1B1B1B01010101"; -#Endsequenz +#Ending sequence SEQ_END="001B1B1B1B1A00"; -#Beginn Zähler 1.8.0 (Wirkarbeit Bezug) +#Start of counter 1.8.0 (incoming active energy) SEQ_180="070100010800FF"; -#Beginn Zähler 2.8.0 (Wirkarbeit Lieferung) +#Start of counter 2.8.0 (outgoing acive energy) SEQ_280="070100020800FF"; -#Beginn aktuelle Wirkleistung +#Start of actual aktive power SEQ_PWR="070100100700FF"; - +#Max possiple power value as mark of power direction INOUT=2147483647; +#Read from device cat $INPUT_DEV 2>/dev/null | xxd -p -u -l 600 1> $FILE; +#Delete line breaks STRING=`tr -d "\n" < $FILE`; +#Find first starting sequence forwards and delete everything before STRING=${STRING#*$SEQ_START}; +#Find last ending sequence backwards and delete everything behind STRING=${STRING%%$SEQ_END*}; +#Identify incoming active energy value and transform from hex to decimal STRING_180=${STRING#*$SEQ_180}; let STRING_180=0x${STRING_180:20:10}; +#Identify outgoing active energy value and transform from hex to decimal STRING_280=${STRING#*$SEQ_280}; let STRING_280=0x${STRING_280:20:10}; +#Identify active power value and transform from hex to decimal. STRING_PWR=${STRING#*$SEQ_PWR}; let STRING_PWR=0x${STRING_PWR:14:8}; -#Richtung der Leistung abfangen und in W umrechnen.. +#Calculate direction of active power and convert in W (Watt) if [ $STRING_PWR -gt $INOUT ]; then STRING_PWR_OUT=$(echo "(4294967295 - $STRING_PWR) / (10)"|bc); STRING_PWR_IN=0; @@ -52,17 +78,18 @@ else STRING_PWR_IN=$(echo "$STRING_PWR / 10"|bc); STRING_PWR_OUT=0; fi -#Zählerstände in Wh! + +#Convert both active energy values in Wh (Watt hours) STRING_180=$(echo "scale=4; $STRING_180 / 10" |bc); STRING_280=$(echo "scale=4; $STRING_280 / 10" |bc); -#LOG -echo $DATE";"$STRING_180";"$STRING_280";"$STRING_PWR_IN";"$STRING_PWR_OUT >> $LOG ; +#Write to log file +echo $DATE";"$DATE_N5M";"$STRING_180";"$STRING_280";"$STRING_PWR_IN";"$STRING_PWR_OUT >> $LOG ; -#DATENBANK +#Send data to database mysql -u $DB_USER -p$DB_PASS -h $DB_HOST -D $DB_NAME <