edl21/edl21.sh
tilman a84fd53daf „edl21.sh“ ändern
Clean up and translate comments.
Adds calculation of nearest 5 min time.
2019-03-03 21:10:50 +01:00

95 lines
2.9 KiB
Bash

#!/bin/bash
# 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
#Log file and temporary txt.
FILE=$PFAD"/"$DATE."txt";
LOG=$PFAD"/edl.log";
PFAD=$(dirname "$(readlink -e "$0")");
#Database
DB_USER="dbuser";
DB_PASS="dbpasswd";
DB_HOST="dbhost";
DB_NAME="dbname";
DB_TAB="dbtable";
#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";
#Ending sequence
SEQ_END="001B1B1B1B1A00";
#Start of counter 1.8.0 (incoming active energy)
SEQ_180="070100010800FF";
#Start of counter 2.8.0 (outgoing acive energy)
SEQ_280="070100020800FF";
#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};
#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;
else
STRING_PWR_IN=$(echo "$STRING_PWR / 10"|bc);
STRING_PWR_OUT=0;
fi
#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);
#Write to log file
echo $DATE";"$DATE_N5M";"$STRING_180";"$STRING_280";"$STRING_PWR_IN";"$STRING_PWR_OUT >> $LOG ;
#Send data to database
mysql -u $DB_USER -p$DB_PASS -h $DB_HOST -D $DB_NAME <<EOF
INSERT INTO $DB_TAB (TimeStamp,Nearest5min,zaehlerstand_in,zaehlerstand_out,active_in,active_out) VALUES ('$DATE','$DATE_N5M','$STRING_180','$STRING_280','$STRING_PWR_IN','$STRING_PWR_OUT');
EOF
#Delete temporary file
rm $FILE;