GithubHelp home page GithubHelp logo

ams-han-decoder's Introduction

ams-han-decoder

Decoder of binary serial stream from HAN port of power meters in Norway.

Requires MBUS slave-to-serial adapter hardware. See main script for details on working configurations.

Running

See main script for details on how to use it. Run with -h for help.

Installing dependencies

Requires JSON CPAN module to work. Install dependencies using your system package manager or favorite CPAN client. If you're using a Debian-based system, it is usually enough to apt-get install libjson-perl.

If you're using a custom Perl environment or another installation method, you can either use cpanm --installdeps . to install the dependencies where your environment expects them, or use carton install to install them in local/ and use carton exec ams_han_decoder.pl to run the script. Or you can set the environment variable PERL5LIB to local/lib/perl5 and run the binary. See Carton for more details.

Installing as a systemd service shipping JSON over MQTT

This is an example of using the decoder together with mosquitto_pub to ship the JSON messages over MQTT to its destination.

[Unit]
Description=AMS HAN decoder

[Service]
Environment=HOME=/root
ExecStart=/usr/local/bin/ams-han-decoder -m AIDON_V0001 -k -c -p 'mosquitto_pub -l -t sensor/aidon' /dev/aidon
# Avoid memory leak eating all memory
MemoryHigh=50M
MemoryMax=100M

[Install]
WantedBy=multi-user.target

Caveats

A memory leak has been detected when you use the -p parameter. It is somewhat diminished when using the -k parameter (to keep the program running between messages). It is recommended to always use -k with -p to minimize this memory leak. If you're using systemd, as above, then the limits imposed on the service should keep it somewhat under control. If you have suggestions for how to resolve this issue, please create a ticket for it. Normal memory usage is around 9MB.

ams-han-decoder's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ams-han-decoder's Issues

Possible memory leak

Hi
First of all thanks for a good program!

After a couple of days running this on my Rpi3 it stopped sending data to HA and I was not able to SSH into the instance. The PI is a fresh install only running this service.

In syslog I found these:

Sep 13 22:47:45 raspberrypi ams_han_decoder.pl[245]: Decoding HDLC frame failed: Can't pipe to  mosquitto_pub -h 192.168.10.69 -s -t sensor/ams: Cannot allocate memory at /home/pi/ams-han-decoder/ams_han_decoder.pl line 115.
Sep 13 22:47:45 raspberrypi ams_han_decoder.pl[245]: #011main::send_json("{\x{a}   \"data\" : {\x{a}      \"power_active_import\" : {\x{a}         \"des"...) called at /home/pi/ams-han-decoder/ams_han_decoder.pl line 434
Sep 13 22:47:45 raspberrypi ams_han_decoder.pl[245]: #011main::decode_hdlc_frame("\x{a0}*A\x{8}\x{83}\x{13}\x{4}\x{13}\x{e6}\x{e7}\x{0}\x{f}\@\x{0}\x{0}\x{0}\x{0}\x{1}\x{1}\x{2}\x{3}\x{9}\x{6}\x{1}\x{0}\x{1}\x{7}\x{0}\x{ff}\x{6}\x{0}\x{0}\x{5}\x{da}\x{2}\x{2}\x{f}\x{0}\x{16}\x{1b}J\x{ad}", 42, 0, 10) called at /home/pi/ams-han-decoder/ams_han_decoder.pl line 189
Sep 13 22:47:45 raspberrypi ams_han_decoder.pl[245]: #011eval {...} called at /home/pi/ams-han-decoder/ams_han_decoder.pl line 189
Sep 13 22:47:45 raspberrypi ams_han_decoder.pl[245]: #011main::parse_stream(GLOB(0x1f61648)) called at /home/pi/ams-han-decoder/ams_han_decoder.pl line 83
Sep 13 22:47:47 raspberrypi ams_han_decoder.pl[245]: Decoding HDLC frame failed: Can't pipe to  mosquitto_pub -h 192.168.10.69 -s -t sensor/ams: Cannot allocate memory at /home/pi/ams-han-decoder/ams_han_decoder.pl line 115.
Sep 13 22:47:47 raspberrypi ams_han_decoder.pl[245]: #011main::send_json("{\x{a}   \"data\" : {\x{a}      \"power_active_import\" : {\x{a}         \"des"...) called at /home/pi/ams-han-decoder/ams_han_decoder.pl line 434

The program is running as a service in systemd:

[Unit]
Description=AMS HAN decoder

[Service]
Environment=HOME=/home/pi
ExecStart= /home/pi/ams-han-decoder/ams_han_decoder.pl -m AIDON_V0001 -p ' mosquitto_pub -h 192.168.10.69 -s -t sensor/ams' /dev/ttyUSB0

[Install]
WantedBy=multi-user.target

After rebooting I then watched the processes in htop and could see the memory use of the service stedely raising, which leads me to the conclution that that a memory leak is the reason behind my crash.

I was able to solve this by adding auto restart of the service in systemd, but this should not be needed.

So the "working" systemd service now looks like this

[Unit]
Description=AMS HAN decoder

[Service]
Environment=HOME=/home/pi
ExecStart= /home/pi/ams-han-decoder/ams_han_decoder.pl -m AIDON_V0001 -p ' mosquitto_pub -h 192.168.10.69 -s -t sensor/ams' /dev/ttyUSB0
Restart=always
RuntimeMaxSec=3600

[Install]
WantedBy=multi-user.target

A for line 2 on aidon

I can not seem to see A for L2 in the json, only for L1 and L3. Is this a bug or a feature? ;)

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.