edl21/edl21.sh

116 lines
3.4 KiB
Bash
Raw Permalink Normal View History

#!/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
#
# 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.
PFAD=$(dirname "$(readlink -e "$0")");
FILE=$PFAD"/"$DATE."txt";
LOG=$PFAD"/edl.log";
#Database
DB_USER="dbuser";
DB_PASS="dbpasswd";
DB_HOST="dbhost";
DB_NAME="dbname";
DB_TAB="EdlData";
DB_TAB_SNAP="EdlSnapshot";
#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 (0x7FFFFFFF)
INOUT=2147483647;
#Max useful power value for error handling (upper threshold in W)
PWRMAX=10000;
#Read from device
readNcalc() {
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};
checkNconvert;
}
checkNconvert() {
#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;
if [ $STRING_PWR_OUT -gt $PWRMAX ]; then
readNcalc;
fi
else
STRING_PWR_IN=$(echo "$STRING_PWR / 10"|bc);
STRING_PWR_OUT=0;
if [ $STRING_PWR_IN -gt $PWRMAX ]; then
readNcalc;
fi
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);
logNrock;
}
logNrock() {
#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}');
UPDATE $DB_TAB_SNAP SET TimeStamp='${DATE}',Nearest5min='${DATE_N5M}',zaehlerstand_in='${STRING_180}',zaehlerstand_out='${STRING_280}',active_in='${STRING_PWR_IN}',active_out='${STRING_PWR_OUT}';
EOF
#Delete temporary file
rm $FILE;
}
readNcalc;