diff --git a/edl21.sh b/edl21.sh index 77bd1a2..3fce033 100644 --- a/edl21.sh +++ b/edl21.sh @@ -2,7 +2,7 @@ # 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 +# Version 0.6 - 05.06.2019 # # License GPL3.0 or later # https://code.hw12.org/tilman/edl21/src/branch/master/LICENSE @@ -12,9 +12,9 @@ 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"; -PFAD=$(dirname "$(readlink -e "$0")"); #Database DB_USER="dbuser"; @@ -45,10 +45,13 @@ SEQ_180="070100010800FF"; SEQ_280="070100020800FF"; #Start of actual aktive power SEQ_PWR="070100100700FF"; -#Max possiple power value as mark of power direction +#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 @@ -70,19 +73,33 @@ let STRING_280=0x${STRING_280:20:10}; 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 ; @@ -92,4 +109,7 @@ INSERT INTO $DB_TAB (TimeStamp,Nearest5min,zaehlerstand_in,zaehlerstand_out,acti EOF #Delete temporary file -rm $FILE; \ No newline at end of file +rm $FILE; +} + +readNcalc;