Smartmeter

From wiki
Revision as of 15:35, 16 September 2018 by Hdridder (talk | contribs) (Created page with "Reading out a 'Landis & Gyr E350' smartmeter with a raspberry Pi. Main input from GJ After getting the correct cable ch...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Reading out a 'Landis & Gyr E350' smartmeter with a raspberry Pi. Main input from [[1]]

After getting the correct cable check the line speed

stty -F /dev/ttyUSB0 -a
speed 115200 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z;
rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 0; time = 0;
-parenb -parodd -cmspar cs8 hupcl -cstopb 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 tab0 bs0 vt0 ff0
-isig -icanon -iexten -echo -echoe -echok -echonl -noflsh -xcase -tostop -echoprt -echoctl -echoke

The python-deamon to read the port:

#!/usr/bin/env python2

version = "1.2"
import sys
import serial
import time
import os
import re

##############################################################################
#Main program
##############################################################################
print ("read DSMR P1 ",  version)
print ("Control-C to stop")
print ("Modify serial port settings if needed.")

#Set COM port config
ser = serial.Serial()
ser.baudrate = 115200
ser.bytesize=serial.EIGHTBITS
ser.parity= serial.PARITY_NONE
ser.stopbits=serial.STOPBITS_ONE
ser.xonxoff=0
ser.rtscts=0
ser.timeout=20
ser.port="/dev/ttyUSB0"

#Open COM port
try:
    ser.open()
except:
    sys.exit ("Error opening %s. "  % ser.name)


#Initialize

select={
 "1-0:1.8.1":"demandlow",
 "1-0:1.8.2":"demandtop",
 "0-0:96.14.0":"toplow",
 "1-0:1.7.0":"demand",
 "0-1:24.2.1":"gas"
}

result = {}

while 0 == 0:
  p1_count=0
  result = {}
  while p1_count < 200:
    p1_line=''
    try:				 #Read 1 line from the port
      p1_raw = ser.readline()
    except:
      sys.exit ("Serial port %s can not be read." % ser.name )
    p1_str=str(p1_raw)
    p1_line=p1_str.strip()
    m = re.match('/',p1_line)
    if m:
      p1_count=100
    p1_count = p1_count +1
    if p1_count > 100:
      r = re.match('^(.+?)\((.*)?\D',p1_line)
      if r:
        #print "group(0)"+r.group(0)+", group(1)"+r.group(1)+", group(2)"+r.group(2)+"\n"
        result[r.group(1)] = r.group(2)
      m = re.match('!',p1_line)
     if m:
       p1_count=200

  outfile="/tmp/smartmeter"+str(time.time())+".txt"
  with open (outfile,"w") as file:
    for id in result:
  	if id in select:
  	  if select[id] == "gas":
  	    #print "Gas found\n"
  	    m = re.search('\((.+)',result[id])
  	    if m:
  	  	#print "result found\n"
  	  	result[id] = m.group(1)
  	  file.write(select[id]+","+result[id]+"\n")
  	else:
  	  file.write(id+","+result[id]+"\n")
  os.rename(outfile,outfile+".ready")
  #time.sleep(9)
  	
#Close port and show status
try:
    ser.close()
except:
    sys.exit ("Oops %s. program aborted. Could not close port." % ser.name )
#!/usr/bin/env python2
# Convert P1 output to a RRD

version = "1.1"
import sys
import re
import rrdtool

##############################################################################
#Main program
##############################################################################


#Initialize
rrd='<apath>/smartmeter.rrd'

select={
	"1-0:1.8.1":"demandlow",
	"1-0:1.8.2":"demandtop",
	"0-0:96.14.0":"toplow",
	"1-0:1.7.0":"demand",
	"0-1:24.2.1":"gas"
}

result = {}
for id in select:
	result[select[id]] = 0
infile = sys.argv[1]
timestamp = 'N'

r = re.search('(\d+)\.\d+\.txt\.ready$',infile)
if r:
	timestamp = r.group(1)

with open (infile,"r") as file:
	for p1_str in file:
		p1_line=p1_str.strip()
		r = re.match('^(.+?)\((.*\()?([\d\.]+)',p1_line)
		if r:
			#print "Match found: ",p1_line
			#print "group(1)"+r.group(1)+",  group(3)"+r.group(3)+"\n"
			if r.group(1) in select:
				result[select[r.group(1)]] = re.sub('\.','',r.group(3))
	rrdtool.update(rrd,timestamp+':'+result['demandlow']+':'+result['demandtop']+':'+result['toplow']+':'+result['demand']+':'+result['gas'])