How to use an RPi as a surrogate smart meter?

Moderator: leecollings

Post Reply
dbldutch
Posts: 6
Joined: Sunday 27 November 2016 12:40
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5877
Location: Netherlands
Contact:

How to use an RPi as a surrogate smart meter?

Post by dbldutch »

At this moment, I do not have a "really" smart meter with a P1 RJ-11 serial port. I will eventually get one and by then things should be easy. I think... :lol:

However, I am currently stuck with an Iskra MT171 meter. I just installed an optical pick-up with a TTL interface to communicate through the P0 port of the meter and that pick-up is connected to an RPi P1 connector, using the 3V3, GND, RTX and TXD connections. I also have a Python script running every 5 minutes by using cron, and that reads the Telegram from the meter and manipulates the Telegram information into a .csv file so I can import the readings in MS-Excel and make graphs. So that part works. So far so good.

I also have Domoticz running on that same RPi, and would like to use that as my back-end to display the meter data, but here I'm stuck at the moment. I'm pretty sure Domoticz cannot handle the optical pick-up communication and interrogate my meter by itself. My take is that the protocall is very different from the standard hardware definition. If there is a way, I have not found it, so please lead me the way to a solution if it is possible.

After searching for a few days, I have not found an easy enough method (for me :geek: ) to transfer the data that I collect on my RPi into Domoticz. At this moment, I would like to avoid the LUA script way if at all possible. Seems too complicated for just this one off task, and nothing I found is really applicable without many changes.

So, another method that I think could work is as follows. Because I will eventually, any time really soon, don't hold your breath, get a real smartmeter, that would solve my problem. No, I don't want to spend 70 Euros to get a meter sooner. To bridge that time period however, I would like to see if it is possible to let Domoticz interrogate my RPi, letting it think the RPi is the smartmeter. The Rpi would then need to offer the data to Domoticz as if it's coming from the real thing. As a bonus, one would have total flexibility in what to offer in terms of data, not only coming from the smartmeter itself.

Has anybody already done something like this, and if so are you willing to share how you did it? If not, is there interest by this community to create this temporary stop-gap? I am pretty good with RPi's, hardware and Python, but sorely miss any Domoticz skills. I need some serious guidance on this.

Thanks!
Last edited by dbldutch on Tuesday 06 December 2016 10:12, edited 1 time in total.
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: How to use an RPi as a surrogate smart meter?

Post by emme »

using a complete Raspberry could be too much just for counting pulses :P :P

have you ever think about MySensors?
https://www.mysensors.org/build/pulse_power

you would just need a ateway that can be configured in the Domoticz Server
The most dangerous phrase in any language is:
"We always done this way"
dbldutch
Posts: 6
Joined: Sunday 27 November 2016 12:40
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5877
Location: Netherlands
Contact:

Re: How to use an RPi as a surrogate smart meter?

Post by dbldutch »

Hi emme,

Thanks for replying, but note that I'm not using/counting pulses, I'm reading the data coming from the meter.

Counting pulses is the only way to get some data out of a dumb-meter.

I would call my Iskra MT171 meter half-smart and it has a P0 comm. port. There are four major differences between a half-smart and a smart meter.
One is that I need to initiate my half-smart meter to send me the Telegram, as opposed to the really smart meter, which sends the Telegram every 10 seconds without needing any prodding. The second difference is that the data from the half-smart meter only send whole kWh changes (no decimals) and a smart-meter can send up to 3 decimals. A third difference is that a smart meter sends out the current consumption in Watts, and this is the most important element of interest. And then a fourth difference is that a smart-meter can be interfaced with a gas meter.

The Telegrams coming from both meters are otherwise practically identical.

In the meantime, I found a solution to my problem. See the next post.
Last edited by dbldutch on Thursday 08 December 2016 6:13, edited 2 times in total.
User avatar
bbqkees
Posts: 407
Joined: Sunday 17 August 2014 21:01
Target OS: Linux
Domoticz version: 4.1x
Location: The Netherlands
Contact:

Re: How to use an RPi as a surrogate smart meter?

Post by bbqkees »

I don't really see the problem here.

Just create meters in Domoticz and update those with your script with simple GET requests.
http://www.domoticz.com/wiki/Domoticz_A ... counter.29
Bosch / Nefit / Buderus / Junkers / Worcester / Sieger EMS bus Wi-Fi MQTT Gateway and interface boards: https://bbqkees-electronics.nl/
dbldutch
Posts: 6
Joined: Sunday 27 November 2016 12:40
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5877
Location: Netherlands
Contact:

Re: How to use an RPi as a surrogate smart meter?

Post by dbldutch »

Well, a solution turned out to be a lot easier than I initially thought.

Let me start by saying first that this is only an exercise in getting something from my meter to Domotics. My half-smart meter does not send out the current consumption, so there is little of value coming out of Domotics if you only send out the total delivered hi and low counters with no decimal information, but the proof of concept works.

In a nut-shell, I'm collecting the Telegram from my half-smart meter by asking for it. The interface that I use to ask the meter to send me the Telegram, and to receive it, is through an optical pick-up. I purchased the TTL version, because I wanted to hook it up directly to the P1-connector of my Raspberry Pi.
Here are the connections from the optical pick-up to the RPi:
white lead to P1-1 3V3
yellow lead to P1-6 GND
brown lead to P1-8 TXD
green lead to P1-10 RXD

The python script I use reads the data from the meter, manipulates it to produce a .csv file for further processing (by hand) with MS-Excel. It now also produces a Telegram in the style of a smart meter, such that Domoticz can accept it. This took some trying but turned out to be quite simple.
My initial script came from http://www.SmartMeterDashboard.nl, and it is called "Iskra Datalogger V2.rar" I'm using the MT171 version, that I modified quite a bit.

I added the following function to that script:

Code: Select all

def write_iskra_telegram():
    with open("/var/tmp/Telegram", "w") as fout: # file is send to a tmpfs created RAM disk location
        fout.write ("/ISk5MT171-0133\n")
        # power consumption
        fout.write ("1-0:1.8.1(%0.3f*%s)\n" % (iskra_meterreading_1_1, iskra_unitmeterreading_1_1) )
        fout.write ("1-0:1:8.2(%0.3f*%s)\n" % (iskra_meterreading_1_2, iskra_unitmeterreading_1_2) )
        fout.write ("1-0:1.8.0(%0.3f %s)\n" % (iskra_meterreading_1_tot, iskra_unitmeterreading_1_tot) ) # not shown in report
        # power generation - not used
#        fout.write ("1-0:2.8.1(%0.3f*%s)\n" % (iskra_meterreading_2_1, iskra_unitmeterreading_2_1) )
#        fout.write ("1-0:2.8.2(%0.3f*%s)\n" % (iskra_meterreading_2_2, iskra_unitmeterreading_2_2) )
        # gas - not used
        fout.write ("!\n")
    return()
And in the main code, I simply call this function. Many of the changes I made were related to making sure the script does not hang. It always returns with an exit code, which is 0 (zero) for a successful execution, and non-zero otherwise. The comm port (/dev/ttyAMA0) that is used for this connection is always closed at the end of the script.

This script is executed every 5 minutes by cron. It makes no sense to do this more frequent because the half-smart meter only sends whole integer numbers as kWh.

I then created another script to read the Telegram, and send it over the the same serial port. Because the USB-serial port is connected in parallel to the Raspi P1 port, Domoticz can see the activity on the /dev/ttyUSB0 port and intercept it. I elected to execute this program right after the previous script, and such that it will not run when the previous script exited with an error. You can do that by using the double ampersand && between the execution of the two scripts. The command after the && will only get executed when the previous one ends with a clean exit.
This is what I have in cron :

Code: Select all

*/5 * * * * /usr/bin/python2.7 /home/pi/read_MT171.py && /usr/bin/python2.7 /home/pi/send_domo.py
My send_domo script looks as follows:

Code: Select all

#!/usr/bin/python2.7
#-------------------------------------------------------------------------------
# Name:        send_domo.py
# Purpose:     write to serial port /dev/ttyAMA0 & /dev/ttyUSB0
#
# Author:      paulv
#
# Created:     04-12-2016
# Copyright:   (c) paulv 2016
# Licence:     <your licence>
#-------------------------------------------------------------------------------

from __future__ import print_function
from time import sleep
import serial
import sys

TEST = True # used for debugging, False normally

def main():

    try:
        ser = serial.Serial(
            baudrate = 9600,
            bytesize = serial.SEVENBITS,
            parity = serial.PARITY_EVEN,
            stopbits = serial.STOPBITS_ONE,
            xonxoff = 0,
            rtscts = 0,
            timeout = 0.5,
            port = '/dev/tty/AMA0' # Serial from Raspi P1: TXD and RXD, but /dev/ttyUSB0 sees it too!
        )
    except Exception as e:
        print(str(e))
        sys.exit("Error opening comm port, aborting")

    try:
        at_END = 0
        i = 0

        with open("/var/tmp/Telegram", "r") as fin: # file location is on a RAM disk
            while not at_END:
                f_data = fin.readline()

                if TEST : print(f_data, end='') # end= this is a double ' , meaning no end of line
                ser.write(f_data)
                if f_data == "!":
                    at_END = True
                i += 1
                if i > 20 : break # don't get into a hung loop situation

        if TEST : print("Done")
        sys.exit(0)

    except IOError:
        sys.exit("Cannot open Telegram. Terminating")

    except KeyboardInterrupt:
        print("\nDone")
        try:
            if ser.isOpen():
                if TEST : print("Port is still open, closing it")
                ser.close()
        except:
            sys.exit("Error closing comm port. Terminating")

if __name__ == '__main__':
    main()
The USB serial adapter is plugged into one the the USB ports, and the four wires go the to the same pins of the P1 connector of the RPi.
The USB leads go as follows:
red lead to P1-1 3V3
black lead to P1-6 GND
white lead to P1-8 TXD
green lead to P1-10 RXD

I created a tiny PCB with some header pins to connect it all together.

The trick of the whole contraption is that I use the RPi serial device (/dev/ttyAMA0) to communicate with the meter, and also to send the Telegram out to the /dev/ttyUSB0 that is connected in parallel. Because the USB serial device (/dev/tty/USB0) that is activated by Domoticz, hangs off the same wires, it receives the data and Domoticz intercepts it. Make sure you don't manipulate the /dev/ttyUSB0 port, or close it, because Domoticz will stop using it.

As soon as I get a real smart meter, it's now fairly simple to make the required changes with what I have now.

Enjoy!
Last edited by dbldutch on Thursday 08 December 2016 6:11, edited 2 times in total.
dbldutch
Posts: 6
Joined: Sunday 27 November 2016 12:40
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5877
Location: Netherlands
Contact:

Re: How to use an RPi as a surrogate smart meter?

Post by dbldutch »

Hi bbqkees,

I don't know much about Domoticz, but from what I do know, I am pretty sure you cannot "just" create meters (with)in Domoticz, because half-smart meters need to be asked to send the data, they use a different comm port with a different baud rate, and the Telegram they blurt out is different from what Domoticz expects. Does that make sense?

Because I have not been able to find a solution from others, I managed to do it my way, see above.

Pointing to the API interface does not help me much as a Domoticz beginner.
Do you have an example to share, or can you point us to an example of what you mean? I can't find one.
dbldutch
Posts: 6
Joined: Sunday 27 November 2016 12:40
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5877
Location: Netherlands
Contact:

Re: How to use an RPi as a surrogate smart meter?

Post by dbldutch »

Although my Iskra MT171 does not create a power consumption number, I have found a way to calculate that and send it to Domoticz.

In essence, I start counting time (in seconds) as soon as the Total kWh counter increases, and stop the timer with the next increase.
The consumption is than calculated with P(kW) = kWh / lapse time(hrs).
It's not quite the same, because it is the consumption of the past, and will stay at the same rate for the whole timestamp duration, which can be a few hours, but it helps to track your consumption levels over periods.

The Telegram now looks like this:

Code: Select all

/ISk5MT171-0133
1-0:1.8.1(14921.000*kWh)
1-0:1:8.2(10669.000*kWh)
1-0:1.8.0(25590.000*kWh)
1-0:1.7.0(0.429*kW)
!
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests