Volkszähler

From BITPlan Wiki
Jump to navigation Jump to search

Install desktop

If you'd like to have the desktop configuration for the raspberry that you configured using the Volkzsähler image you can install the desktop with:

sudo apt-get install raspberrypi-ui-mods -y
sudo raspi-config # activate desktop via menu

Test USB S0 reader

readmeter

= Prerequsite

you need to install bc for the readmeter script to work

apt-get install bc
=== readmeter bash script ===
#!/bin/bash
# read and evaluate SML output received from EMH eHZ

getmeter() {
  # set serial device
  INPUT_DEV="/dev/ttyUSB0"

  #set $INPUT_DEV to 9600 8N1
  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

  SML_START_SEQUENCE="1B1B1B1B0101010176"
  METER_OUTPUT__START_SEQUENCE=""

  while [ "$METER_OUTPUT__START_SEQUENCE" != "$SML_START_SEQUENCE" ]
  do
    METER_OUTPUT=`cat $INPUT_DEV 2>/dev/null | xxd -p -u -l 460 | tr -d '[:space:]'`
    METER_OUTPUT__START_SEQUENCE=$(echo "${METER_OUTPUT:0:18}")
    if [ "$METER_OUTPUT__START_SEQUENCE" != "$SML_START_SEQUENCE" ];
    then
      echo "missed start and trying again..."
      sleep 0.1
      #exit 1 
    fi
  done

  echo $METER_OUTPUT
}

# 
# match
#  1: prefix
#  2: text
#  3: ofs
#  4: len
#
matchvalue() {
  local l_prefix="$1"
  local l_text="$2"
  local l_ofs="$3"
  local l_len="$4"
  local l_prefix_len=${#l_prefix}
  local l_text_len=${#l_text}
  local l_start=0
  let l_max=l_text_len-l_prefix_len
  #echo  "$l_prefix_len - $l_text_len - $l_max"
  while [ $l_start -lt $l_max ]
  do
    l_part=${l_text:$l_start:$l_prefix_len}
    if [ "$l_prefix" = "$l_part" ]
    then
      let l_start=l_start+l_prefix_len+l_ofs
      local l_value_hex=${l_text:$l_start:$l_len}
      let l_value=0x${l_value_hex}
      # uncomment to debug - but then do not use bc below
      # echo ${l_start}
      # echo ${l_value_hex}
      echo $l_value
      return 
    fi
    let l_start=l_start+1
  done
}

#
# get a value from the SML Sequence
#
getvalue() {
  local l_prefix="$1"
  local l_text="$2"
  local l_ofs="$3"
  local l_len="$4"
  local l_factor="$5"
  local l_title="$6"
  local l_unit="$7"
  #echo "ofs:$l_ofs len: $l_len factor: $l_factor"
  METER_180=$(matchvalue "$l_prefix" "$l_text" "$l_ofs" "$l_len")
  #echo $METER_180
  VALUE=$(echo "scale=2; $METER_180 / $l_factor" |bc)
  echo "$l_title" $VALUE "$l_unit"
}

echo "SML SEQUENCE:"
getmeter
getvalue "77070100010800FF" "$METER_OUTPUT" 20 10 10000 "Meter 1.8.0 (from plant):" "kWh"
getvalue "77070100020800FF" "$METER_OUTPUT" 20 10 10000 "Meter 2.8.0 (  to plant):" "kWh"
#getvalue "77070100010801FF" "$METER_OUTPUT" 20 10 10 "1.8.1"  "kW"
#getvalue "77070100010802FF" "$METER_OUTPUT" 20 10 10 "1.8.2"  "kW"
#getvalue "77070100020801FF" "$METER_OUTPUT" 20 10 10 "2.8.1" "kW" 
#getvalue "77070100020802FF" "$METER_OUTPUT" 20 10 10 "2.8.2" "kW"

Example output

./readmeter 
SML SEQUENCE:
missed start and trying again...
1B1B1B1B0101010176070012028F6A8262006200726301017601010700120C6D78D609080C2AED2D4C4E9F01016337390076070012028F6A836200620072630701770109080C2AED2D4C4E9F070100620AFFFF726201650C6DEC247A77078181C78203FF0101010104454D480177070100000009FF0101010109080C2AED2D4C4E9F0177070100010800FF6401018201621E52FF5600156312070177070100020800FF6401018201621E52FF5600093E97510177070100010801FF0101621E52FF5600156312070177070100020801FF0101621E52FF5600093E97510177070100010802FF0101621E52FF5600000000000177070100020802FF0101621E52FF56000000000001770701000F0700FF0101621B52FF6500001E330177078181C78205FF01726201650C6DEC2501018302D9801652DE264F8D45221BDF3EB993C9083F800F6F1319C9BD7148567D336D72DBB1E5A845CC56C4A3EE32157EF495560101016358FA0076070012028F6A846200620072630201710163529A000000001B1B1B1B1A0376561B1B1B1B0101010176070012028F6A8862006200726301017601010700120C6D78D809080C2AED2D4C4E9F010163797A0076070012028F6A896200620072630701770109
Meter 1.8.0 (from plant): 35881.42 kWh
Meter 2.8.0 (  to plant): 15509.69 kWh