Last active
July 26, 2023 23:59
-
-
Save KorbinianP/62bfa2b2140af78b977c6476bc6859ee to your computer and use it in GitHub Desktop.
Read two SmartMeters and send the values to a REST API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SUBSYSTEMS=="usb", ATTRS{idProduct}=="ea60", ATTRS{idVendor}=="10c4", ATTRS{serial}=="015AE1E8", SYMLINK+="ttyUSBlesekopf0", GROUP="dialout", MODE="0660", RUN+="/etc/openhab2/others/setupLesekopf0.sh" | |
SUBSYSTEMS=="usb", ATTRS{idProduct}=="ea60", ATTRS{idVendor}=="10c4", ATTRS{serial}=="015ADDC3", SYMLINK+="ttyUSBlesekopf1", GROUP="dialout", MODE="0660", RUN+="/etc/openhab2/others/setupLesekopf1.sh" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script is called cyclic via crontab and reads two SmartMeters. | |
# The values are then send to OpenHAB via REST API. | |
# Smart Meter 0: Logarex LK13BD202025 (Heating) | |
# Smart Meter 1: Logarex LK13BE803039 (Living) | |
# The commented stty calls are now inside separate scripts. | |
# Those are called by the udev rule on plugging in or booting. | |
#set -x | |
cutValue() { | |
value=$(echo $1 | cut -d'(' -f 2 | cut -d'*' -f 1) | |
value=$(echo $value | sed 's/^0*//') | |
if [ "$value" = ".0000" ]; then | |
value="0.0" | |
fi | |
} | |
postUpdate() { | |
curl -X POST --header "Content-Type: text/plain" --header "Accept: application/json" \ | |
-d "$2" "http://192.168.178.71:8080/rest/items/$1" | |
echo $1 $2 | |
} | |
readLesekopf0() { | |
#stty -F /dev/ttyUSBlesekopf0 300 parenb -parodd cs7 -cstopb raw hupcl cread clocal\ | |
#-crtscts -ignbrk -brkint -ignpar -parmrk inpck -istrip -inlcr -igncr\ | |
#-icrnl -ixon -ixoff -iuclc -ixany -imaxbel -iutf8 -opost -olcuc -ocrnl\ | |
#onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab1 bs0 vt0 ff1 -isig -icanon\ | |
#iexten -echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke | |
#sleep 1 | |
tmpfile=/tmp/ttyDump0.data | |
# open Serial Port | |
cat /dev/ttyUSBlesekopf0 >$tmpfile & | |
PID=$! | |
sleep 1 | |
/bin/echo -e "\x2f\x3f\x21\x0d" >/dev/ttyUSBlesekopf0 #Send WAKEUP | |
sleep 2 | |
/bin/echo -e "\x06\x30\x30\x30\x0d" >/dev/ttyUSBlesekopf0 #Send DATA REQUEST | |
sleep 11 | |
kill $PID | |
# close Serial Port | |
while read p; do | |
#echo $p | |
case "$p" in | |
*"1.8.0"*) | |
echo 'found 1.8.0' | |
cutValue $p | |
postUpdate "SmartMeter_Heizung_Bezug" $value | |
;; | |
*"1.8.1"*) | |
echo 'found 1.8.1' | |
cutValue $p | |
postUpdate "SmartMeter_Heizung_HT" $value | |
;; | |
*"1.8.2"*) | |
echo 'found 1.8.2' | |
cutValue $p | |
postUpdate "SmartMeter_Heizung_NT" $value | |
;; | |
*"2.8.0"*) | |
echo 'found 2.8.0' | |
cutValue $p | |
postUpdate "SmartMeter_Heizung_Einspeisung" $value | |
;; | |
*) ;; | |
esac | |
done <$tmpfile | |
} | |
readLesekopf1() { | |
#stty -F /dev/ttyUSBlesekopf1 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 \ | |
# 2>/dev/null | |
#sleep 1 | |
tmpfile=/tmp/ttyDump1.data | |
# open Serial Port | |
cat /dev/ttyUSBlesekopf1 >$tmpfile & | |
PID=$! | |
sleep 1 | |
/bin/echo -e "\x2f\x3f\x21\x0d\x0a" >/dev/ttyUSBlesekopf1 #Send WAKEUP | |
sleep 1 | |
/bin/echo -e "\x06\x30\x30\x30\x0d\x0a" >/dev/ttyUSBlesekopf1 #Send DATA REQUEST | |
sleep 11 | |
kill $PID | |
# close Serial Port | |
while read p; do | |
#echo $p | |
case "$p" in | |
*"1.8.0"*) | |
echo 'found 1.8.0' | |
cutValue $p | |
postUpdate "SmartMeter_Haushalt_Bezug" $value | |
#echo $value | |
;; | |
*"2.8.0"*) | |
echo 'found 2.8.0' | |
cutValue $p | |
postUpdate "SmartMeter_Haushalt_Einspeisung" $value | |
#echo $value | |
break | |
;; | |
*) ;; | |
esac | |
done <$tmpfile | |
} | |
# call both readouts in parallel | |
readLesekopf0 & | |
readLesekopf1 & | |
# wait until both have returned | |
wait %1 %2 | |
exit | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
touch /tmp/ttyUSBlesekopf0 | |
stty -F /dev/ttyUSBlesekopf0 300 parenb -parodd cs7 -cstopb raw hupcl cread clocal\ | |
-crtscts -ignbrk -brkint -ignpar -parmrk inpck -istrip -inlcr -igncr\ | |
-icrnl -ixon -ixoff -iuclc -ixany -imaxbel -iutf8 -opost -olcuc -ocrnl\ | |
onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab1 bs0 vt0 ff1 -isig -icanon\ | |
iexten -echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
touch /tmp/ttyUSBlesekopf1 | |
stty -F /dev/ttyUSBlesekopf1 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 \ | |
2>/dev/null | |
sleep 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment