„edl21.sh“ ändern

Clean up and translate comments.
Adds calculation of nearest 5 min time.
This commit is contained in:
tilman 2019-03-03 21:10:50 +01:00
parent c09655e736
commit a84fd53daf

View File

@ -1,50 +1,76 @@
#!/bin/bash #!/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"; 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 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. #Log file and temporary txt.
DATE=$(/bin/date +%Y%m%d-%H%M%S);
PFAD=$(dirname "$(readlink -e "$0")");
FILE=$PFAD"/"$DATE."txt"; FILE=$PFAD"/"$DATE."txt";
LOG=$PFAD"/edl.log"; LOG=$PFAD"/edl.log";
PFAD=$(dirname "$(readlink -e "$0")");
#Datenbank #Database
DB_USER=""; DB_USER="dbuser";
DB_PASS=""; DB_PASS="dbpasswd";
DB_HOST=""; DB_HOST="dbhost";
DB_NAME=""; DB_NAME="dbname";
DB_TAB="EdlData"; 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"; SEQ_START="1B1B1B1B01010101";
#Endsequenz #Ending sequence
SEQ_END="001B1B1B1B1A00"; SEQ_END="001B1B1B1B1A00";
#Beginn Zähler 1.8.0 (Wirkarbeit Bezug) #Start of counter 1.8.0 (incoming active energy)
SEQ_180="070100010800FF"; SEQ_180="070100010800FF";
#Beginn Zähler 2.8.0 (Wirkarbeit Lieferung) #Start of counter 2.8.0 (outgoing acive energy)
SEQ_280="070100020800FF"; SEQ_280="070100020800FF";
#Beginn aktuelle Wirkleistung #Start of actual aktive power
SEQ_PWR="070100100700FF"; SEQ_PWR="070100100700FF";
#Max possiple power value as mark of power direction
INOUT=2147483647; INOUT=2147483647;
#Read from device
cat $INPUT_DEV 2>/dev/null | xxd -p -u -l 600 1> $FILE; cat $INPUT_DEV 2>/dev/null | xxd -p -u -l 600 1> $FILE;
#Delete line breaks
STRING=`tr -d "\n" < $FILE`; STRING=`tr -d "\n" < $FILE`;
#Find first starting sequence forwards and delete everything before
STRING=${STRING#*$SEQ_START}; STRING=${STRING#*$SEQ_START};
#Find last ending sequence backwards and delete everything behind
STRING=${STRING%%$SEQ_END*}; STRING=${STRING%%$SEQ_END*};
#Identify incoming active energy value and transform from hex to decimal
STRING_180=${STRING#*$SEQ_180}; STRING_180=${STRING#*$SEQ_180};
let STRING_180=0x${STRING_180:20:10}; let STRING_180=0x${STRING_180:20:10};
#Identify outgoing active energy value and transform from hex to decimal
STRING_280=${STRING#*$SEQ_280}; STRING_280=${STRING#*$SEQ_280};
let STRING_280=0x${STRING_280:20:10}; let STRING_280=0x${STRING_280:20:10};
#Identify active power value and transform from hex to decimal.
STRING_PWR=${STRING#*$SEQ_PWR}; STRING_PWR=${STRING#*$SEQ_PWR};
let STRING_PWR=0x${STRING_PWR:14:8}; 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 if [ $STRING_PWR -gt $INOUT ]; then
STRING_PWR_OUT=$(echo "(4294967295 - $STRING_PWR) / (10)"|bc); STRING_PWR_OUT=$(echo "(4294967295 - $STRING_PWR) / (10)"|bc);
STRING_PWR_IN=0; STRING_PWR_IN=0;
@ -52,17 +78,18 @@ else
STRING_PWR_IN=$(echo "$STRING_PWR / 10"|bc); STRING_PWR_IN=$(echo "$STRING_PWR / 10"|bc);
STRING_PWR_OUT=0; STRING_PWR_OUT=0;
fi 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_180=$(echo "scale=4; $STRING_180 / 10" |bc);
STRING_280=$(echo "scale=4; $STRING_280 / 10" |bc); STRING_280=$(echo "scale=4; $STRING_280 / 10" |bc);
#LOG #Write to log file
echo $DATE";"$STRING_180";"$STRING_280";"$STRING_PWR_IN";"$STRING_PWR_OUT >> $LOG ; 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 <<EOF mysql -u $DB_USER -p$DB_PASS -h $DB_HOST -D $DB_NAME <<EOF
INSERT INTO $DB_TAB (TimeStamp,zaehlerstand_in,zaehlerstand_out,active_in,active_out) VALUES (CURRENT_TIMESTAMP,'$STRING_180','$STRING_280','$STRING_PWR_IN','$STRING_PWR_OUT') 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 EOF
#AUFRAEUMEN #Delete temporary file
rm $FILE; rm $FILE;