Raspberry Pi : Smart Meter Reading

I want graphs !

Right now I got 5700 Watt of Solar Power on my roof. See my blog about : Off Grid in the Netherlands and Why 2nd hand solar panels Since a few years I also got a Smart Meter for electricity and gas. My solar power inverters are without any bleutooth or wifi connection. Just with a serial connection. I had a program running, but that one was old and crashed an old laptop many times.

I wasn’t curious about how much my solar panels did deliver, but more what my consumption model was of electricity.

There are some free site where you can give access to your Smart meter data and that they give you some nice graphs. Right now I am using slimmemeterportal.nl

Because I got a Raspberry Pi, I wanted to create something so that I could see howmuch electricity and gas I am consuming. Therefor I was looking for a nice way to read my smart meter. I found the page of GEJanssen.com and that was my start.

First of all I had a running Raspberry Pi with apache and python installed on it.

My Smart Meter is connected with a P1 USB cable to the Raspberry Pi. You can also create one yourself, but sometimes I am lazy. 😉 The P1 port of the Smart Meter is a serial interface where all the Smart Meter data is going every 10 seconds. In the past I used the blinking led and a PIC controller to create a Smart Meter Reading. This is easier and nicer.

Smart Meter Cable

Smart Meter P1 Cable

I was using the example script and added some things to it.

uitlezen.sh :

It using the values of a file and put them in MRTG.

#!/bin/bash
# 1 keer per 5 minuten, uitlezen via seriele interface van de slimme meter

/bin/grep stand /home/pi/P1uitlezer.txt | /usr/bin/awk '{print$2}' > /home/pi/ga
sverbruik.txt

python /home/pi/gas.py > /home/pi/P1uitlezer.txt # 2>&1 >/dev/null
sudo mrtg /home/pi/power.conf
sudo mrtg /home/pi/gas.conf
# hierna is /home/gej/P1uitlezer.txt het bestand waar we mee verder werken.

##########################################################################
#####
# Bijwerken stroomverbruik
################################################################################
#####
# Extraheren van de juiste gegevens
powerin=`/bin/grep afgenomen /home/pi/P1uitlezer.txt | /usr/bin/awk '{print $3}'
`
powerout="-`/bin/grep teruggeleverd /home/pi/P1uitlezer.txt | /usr/bin/awk '{pri
nt $3}'`"

GAS.PY :

This is the python script to read the smartmeter and save the electricity and gas values.

#
# DSMR P1 uitlezer
# (c) 10-2012 - GJ - gratis te kopieren en te plakken

versie = "1.0"
import sys
import serial

################
#Error display #
################
def show_error():
 ft = sys.exc_info()[0]
 fv = sys.exc_info()[1]
 print("Fout type: %s" % ft )
 print("Fout waarde: %s" % fv )
 return


################################################################################
################################################################
#Main program
global filen
gascontent = str(open('/home/pi/gasverbruik.txt').read())

################################################################################
################################################################
print ("DSMR P1 uitlezer", versie)
print ("Control-C om te stoppen")

#Set COM port config
ser = serial.Serial()
ser.baudrate = 9600
ser.bytesize=serial.SEVENBITS
ser.parity=serial.PARITY_EVEN
ser.stopbits=serial.STOPBITS_ONE
ser.xonxoff=1
ser.rtscts=0
ser.timeout=20
ser.port="/dev/ttyUSB0"

#Open COM port
try:
 ser.open()
except:
 sys.exit ("Fout bij het openen van %s. Programma afgebroken." % ser.name)

#Initialize
# stack is mijn list met de 20 regeltjes.
p1_teller=0
stack=[]

while p1_teller < 20:
 p1_line=''
#Read 1 line
 try:
 p1_raw = ser.readline()
 except:
 sys.exit ("Seriele poort %s kan niet gelezen worden. Programma afgebroke
n." % ser.name )
 p1_str=str(p1_raw)
 #p1_str=str(p1_raw, "utf-8")
 p1_line=p1_str.strip()
 stack.append(p1_line)
# als je alles wil zien moet je de volgende line uncommenten
# print (p1_line)
 p1_teller = p1_teller +1

#Initialize
# stack_teller is mijn tellertje voor de 20 weer door te lopen. Waarschijnlijk m
ag ik die p1_teller ook gebruiken
stack_teller=0

while stack_teller < 20:
 if stack[stack_teller][0:9] == "1-0:1.8.1":
 print "daldag ", stack[stack_teller][10:15]
 elif stack[stack_teller][0:9] == "1-0:1.8.2":
 print "piekdag ", stack[stack_teller][10:15]
# Daltarief, teruggeleverd vermogen 1-0:2.8.1
 elif stack[stack_teller][0:9] == "1-0:2.8.1":
 print "dalterug ", stack[stack_teller][10:15]
# Piek tarief, teruggeleverd vermogen 1-0:2.8.2
 elif stack[stack_teller][0:9] == "1-0:2.8.2":
 print "piekterug ", stack[stack_teller][10:15]
# Huidige stroomafname: 1-0:1.7.0
 elif stack[stack_teller][0:9] == "1-0:1.7.0":
 print "afgenomen vermogen ", int(float(stack[stack_teller][10:17])*
1000), " W"
# Huidig teruggeleverd vermogen: 1-0:1.7.0
 elif stack[stack_teller][0:9] == "1-0:2.7.0":
 print "teruggeleverd vermogen ", int(float(stack[stack_teller][10:17])*
1000), " W"
# Gasmeter: 0-1:24.3.0
 elif stack[stack_teller][0:10] == "0-1:24.3.0":
 print "stand ", int(float(stack[stack_teller+1][1:10
])*1000), " dm3"
 print "Gas ", int(float(stack[stack_teller+1][1:10])*1000)-int(gasconten
t)
 else:
 pass
 stack_teller = stack_teller +1

#print (stack, "\n")

#Close port and show status
try:
 ser.close()
except:
 sys.exit ("Oops %s. Programma afgebroken." % ser.name )

When it was working, I used MRTG and make sure that crontab is running every 5 minutes.

Crontab is running under root, so use sudo crontab -e

*/5 * * * * /home/pi/Script/uitlezen.sh

 

When it’s working you will have some cool graphs !

 

 

2 Comments

  1. OliPy

Leave a Reply