GithubHelp home page GithubHelp logo

hansrune / hanmeter Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 1.0 25 KB

A python3 tool to read/decode/debug AMS/HAN data from smart electrical meter

License: GNU Lesser General Public License v3.0

Python 100.00%

hanmeter's Introduction

HANmeter

A python3 tool to read/decode/debug AMS/HAN data from smart electrical meter

This is based on aidon_obis.py from https://github.com/skagmo/meter_reading

Tested to work with Aidon and Hafslund smart meter using M-bus interfaces like this one and this one. The first type is preferred as the serial chip is a USB-2 device with some buffering, which provides fewer packets lost on a busy system.

Dependencies

You will need to install python3 serial and crcmod, i.e on a Raspberry Pi

sudo apt-get install python3-serial python3-crcmod

Setup

The implementation described here is for a Raspberry Pi, but should work on any similar system

/dev/HANserial port

I find it useful to use a /dev/HANserial device as a persistent name. Any serial port name will do, so this step is optional

Rules for /etc/udev/rules.d are provided in the udev rule file. Copy to /etc/udev/rules.d/71-HANserial.rules, attach your device, then reboot

HANdomo systemctl service

Before setting up as a systemctl service, please make sure things work as desciribed for Standalone use (below)

A systemctl service setup is provided in the service systemctl service file. Copy to /lib/systemd/system/HANdomo.service You can change some behavior options by copying the config file to /etc/default/HANdomo. The DOMOIDX variables are set to 0 by default to do nothing. If you use Domoticz home automation, you can change these to update a local Domoticz kW-meter and kWh-meter device indexes as needed

To enable the service, start / enable and check progress:

sudo systemctl daemon-reload
sudo systemctl enable HANdomo
sudo systemctl start HANdomo
sudo journalctl -f -u HANdomo

Standalone use

You can use HANdomo.py script as a HAN utility in a number of ways

Read from /dev/HANserial

You can read from a HAN serial port and display packet hexdumps (-x), OBIS fields (-f), packet processing debug (-p) in any combination like

HANdomo.py -x -f -p /dev/HANserial

Log to file and replay

You can read from a HAN serial port and log the binary data stream to a file. Any input path including /dev in the name is opened and read as a serial device. Everything else assumes the input is a file.

HANdomo.py -l /tmp/packets.log /dev/HANserial

... then later replay the same data

 HANdomo.py -x -f -p /tmp/packets.log

Replaying files captured with the utilities in the han-port-c-lib library also works.

Examples

Output from a running service

This service instance runs with -f (OBIS field outputs). Domoticz is updated just every minute or so

$ journalctl -f -u HANdomo
Mar 15 11:10:21 xxxx-pi1 python3[599]: act_pow_neg = 0
Mar 15 11:10:21 xxxx-pi1 python3[599]: act_pow_pos = 307
Mar 15 11:10:21 xxxx-pi1 python3[599]: curr_L1 = 1.41
Mar 15 11:10:21 xxxx-pi1 python3[599]: curr_L2 = 0.99
Mar 15 11:10:21 xxxx-pi1 python3[599]: curr_L3 = 0.44
Mar 15 11:10:21 xxxx-pi1 python3[599]: meter_ID = b'5706567270xxxxxx'
Mar 15 11:10:21 xxxx-pi1 python3[599]: meter_model = b'6841121BN243101040'
Mar 15 11:10:21 xxxx-pi1 python3[599]: react_pow_neg = 4
Mar 15 11:10:21 xxxx-pi1 python3[599]: react_pow_pos = 0
Mar 15 11:10:21 xxxx-pi1 python3[599]: volt_L1 = 241
Mar 15 11:10:21 xxxx-pi1 python3[599]: volt_L2 = 241
Mar 15 11:10:21 xxxx-pi1 python3[599]: volt_L3 = 243
Mar 15 11:10:21 xxxx-pi1 python3[599]: URL http://127.0.0.1:8080/json.htm?type=command&param=udevice&idx=1938&nvalue=0&svalue=203 --> b'{\n\t"status" : "OK",\n\t"title" : "Update Device"\n}\n'

Packet hex dumps

Packet hex dumps will look something like this:

 $ HANdomo.py -x /tmp/packets.log
 7e <-- Start of frame
 a0 d2 <-- ExpectedLength 210
 41 08 83 13 82 d6 e6 e7 00 0f 40 00 00 00 00 01 09 02 ...
 02 02 09 06 00 00 60 01 07 ff 0a 04 36 35 31 35 02 03 ...
 09 06 01 00 01 07 00 ff 06 00 00 03 3a 02 02 0f 00 16 ...
 .
 . 
 .
 7e <-- End of frame at length= 210
 CRC OK on packet of length 210 value 5272

Packet structural dumps

Packet structual dumps will look something like this:

$ HANdomo.py -p /tmp/packets.log

Frame length is 207
     Data type 01 remaining packet length is 193 index 17 level 1
     ARRAY: 9 elements at level 1
         Data type 02 remaining packet length is 191 index 19 level 2
         STRUCT: 2 elements at level 2
             Data type 09 remaining packet length is 189 index 21 level 3
             Object 1 OCTETS b'\x01\x01\x00\x02\x81\xff'
             Data type 0a remaining packet length is 181 index 29 level 3
             Object 2 STRING b'AIDON_V0001'
         Data type 02 remaining packet length is 168 index 42 level 2
         STRUCT: 2 elements at level 2
             Data type 09 remaining packet length is 166 index 44 level 3
             Object 3 OCTETS b'\x00\x00`\x01\x00\xff'
             Data type 0a remaining packet length is 158 index 52 level 3
             Object 4 STRING b'7359992890xxxxxx'
         Data type 02 remaining packet length is 140 index 70 level 2
         STRUCT: 2 elements at level 2
             Data type 09 remaining packet length is 138 index 72 level 3
             Object 5 OCTETS b'\x00\x00`\x01\x07\xff'
             Data type 0a remaining packet length is 130 index 80 level 3
             Object 6 STRING b'6515'
         Data type 02 remaining packet length is 124 index 86 level 2
         STRUCT: 3 elements at level 2
             Data type 09 remaining packet length is 122 index 88 level 3
             Object 7 OCTETS b'\x01\x00\x01\x07\x00\xff'
             Data type 06 remaining packet length is 114 index 96 level 3
             Object 8 UINT32 826
             Data type 02 remaining packet length is 109 index 101 level 3
             STRUCT: 2 elements at level 3
                 Data type 0f remaining packet length is 107 index 103 level 4
                 Object 9 SCALAR 0
                 Data type 16 remaining packet length is 105 index 105 level 4
                 Object 10 VARIABLE 27
                 .
                 .
                 .
                 .

Packet field dumps

Packet OBIS field dumps will look something like this:

$ HANdomo.py -f /tmp/packets.log
act_pow_neg = 0
act_pow_pos = 826
curr_L1 = 3.9000000000000004
meter_ID = b'7359992890xxxxxx'
meter_model = b'6515'
obis_list_version = b'AIDON_V0001'
react_pow_neg = 78
react_pow_pos = 0
volt_L1 = 236.20000000000002

hanmeter's People

Contributors

hansrune avatar

Stargazers

Alex avatar Martin Gabrielsen avatar Gerdriaan Mulder avatar Karl Martin Sola avatar Espen Bø avatar

Watchers

 avatar Runar Andersstuen avatar

Forkers

moorgrove

hanmeter's Issues

feature requests: examples

Hello.
I tride your script and it works grate... But I need more exampels.
How can I get the values out to somthing I can read and use.

I have tride to use the log function but it looks like it's in a binary format.
I have tride to
pi@piUNFI:~/python3/HANmeter $ python3 HANdomo.py -f /dev/ttyUSB0 > /home/pi/test.txt
but the test.txt file don't get any updated data..

I tride to find a way to import the Json data. "http://127.0.0.1:8080/json.htm?type=createvirtualsensor&idx=762&sensorname=Energy&sensortype=113"

Inn to node-red but I don't think i doing it rigth. I get data when I use links

pi@piUNFI:~ $ links http://127.0.0.1:8080/json.htm?type=createvirtualsensor&idx=762&sensorname=Energy&sensortype=113
[102] 19154
[103] 19155
[104] 19156
[100]   Done                    idx=762
[101]   Done                    sensorname=Energy
[103]   Done                    idx=762

Exampels I would like

  • How to get the data in to Node-Red
  • How to get the data in to Influxdb
  • How to get the data in text to a file.

Hei. (Dårlig i engelsk så tar litt på norsk også)
Synes scriptet ditt ser veldig bra ut. Det er en god beskrivelse på hvordan jeg skal installere og få det til å fungere mot min kamstrupmåler. Tusen takk.
Alt ser ut til å fungere, men desverre så har jeg stoppet opp. Jeg får opp data når jeg bruker de forskjellige debug funksjonene. Men jeg klarer ikke å få brukt dataen videre.

Logg filen får jeg ikke lest noe vettu ut av. Og når jeg prøver og sende data til en tekstfil med
python3 HANdomo.py -f /dev/ttyUSB0 > /home/pi/test.txt
Så blir ikke fila fylt med data.

Så hvis du har tid og lyst, blir jeg veldig glad om du lager litt flere eksempler.

Tusen takk.
Skal prøve og google mere jeg.

Send to output

Hello
I could not get the script to send output to a text file
python3 HANdomo.py -f /dev/ttyUSB0 > test.txt

If you add in the script 'sys.stdout.flush()' it works.

---------- Orginal ----------------

def gen_callback(fields):
    global npower, sumpower, lastupdatetime, nenergy, sumenergy
    if logdata:
        outputFile.flush()
    if debugfields:
        for key in sorted(fields.keys()):
            print(key,"=",fields[key])
        print("")
    if poweridx == 0:

----------- Endret ------------

def gen_callback(fields):
    global npower, sumpower, lastupdatetime, nenergy, sumenergy
    if logdata:
        outputFile.flush()
    if debugfields:
        for key in sorted(fields.keys()):
            print(key,"=",fields[key])
        print("")
        sys.stdout.flush()
    if poweridx == 0:

Then it works to use
python3 HANdomo.py -f /dev/ttyUSB0 > test.txt``

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.