Alles over…
Alles over…

P1-Monitor docker container for smart meters

Description

This project is describing the porting of the P1-Monitor software running on Raspberry Pi to a container version to be able to run it on any Operating System. The original software is maintained by

Since I’m running a Linux server already I didn’t want to start using a dedicated Raspberry Pi board for monitoring my smart meter. But the software provided by ZTATZ is only available as a complete system image running on a Raspberry Pi. That’s why I started converting that software to a container version suitable for running on any system with the help of docker, docker-compose, portainer or any system.

You can find the container version on Docker hub and I’m using GitHub to maintain the repository. The container version has the same functionality as the full version (although some options related to Operating System are not being used of course). Docker hub also holds the versions for other architectures so you can also run the container on ARM etc (such as Raspberry Pi).

Connectors

Since I’m not using a serial port anymore (who does?) I’m using a standard USB to serial adaptor and I changed a standard phone cable (for the RJ11 connector in the Smart meter) and connected it to the serial as below. Of course its even more easy to buy a standard ‘Smart meter USB cable‘ for 15 euro or so but it was weekend and I needed the cable now šŸ™‚

PIN nummer P1 poortRJ-11RS-232
n.c.1
Request (RTS)24
Ground (GND)35
n.c.4
Data(RxD)52
n.c.6
Connect above pins when using the smart meter port

Install docker

For the ones not familiar with containers: Start with installing docker and docker-compose on your system. I selected docker-compose because it is easier to start and maintain a configuration

Docker script

To start with docker-compose create a new directory and create a file named docker-compose.yml. Copy the contents of below docker-compose file example to that file and save it. If needed you can modify it using your own parameters such as the location of the data (in the example script in the subdirectory alldata). That subdirectory is for creating persistence; if the container is recreated or an update is available all data is preserved since it is being stored outside the container. If your server is being rebooted the container will automatically be started again.

Next you start your containerised P1-Monitor using theĀ docker-compose up -d command. This way a small local network will be created and the container will be started in the background. After a short while (initially the container image needs to be downloaded from Docker hub) the P1-Monitor will be available on selected port. In below example it will be running on standard port 80 but you can easily change that by modifying the port mapping parameter from 80:80 to for instance 81:80 if you want the container to be available at port 81.

Similar is the devices section; it is mapping your local serial device to /dev/ttyUSB0 in the container

Docker-compose file example

version: '3.8'
services:

  p1monitor:
    hostname: p1mon
    image: mclaassen/p1mon
    ports:
      - 80:80
    volumes:
      - ./alldata/data:/p1mon/data
      - ./alldata/usbdisk:/p1mon/mnt/usb
      - ./alldata/mnt/ramdisk:/p1mon/mnt/ramdisk
    tmpfs:
      - /run
      - /tmp
    devices:
      - "/dev/ttyUSB0:/dev/ttyUSB0"
    restart: unless-stopped

Upgrade to latest version

If a new container is released (usually shortly after the original P1-Monitor software has been updated) you can stop the container instance using theĀ docker-compose downĀ command. Do not delete any files but issue the command docker-compose pull to retrieve the latest image from Docker hub. After the pull is complete start the container again using the docker-compose up command. All historic data will be reused (if you do not change the docker-compose configuration file of course).

Migrate from original P1-Monitor instance

To migrate from the original P1-Monitor (the one you had running on the dedicated Raspberry Pi) it is easiest to export the data. To do so use the menu Settings (Instellingen) and select In- Export. In your new instance use the same menu to import the extracted data again.

Parameters

The docker-compose file supports below parameters:

  • ports
    mapping van ports from outside to inside. If you want to use port 81 to access your instance use the setting 81:80
  • volumes
    these are the directories being used to store the persistent data. In the example above subdirectory alldata will be automatically created (if started for the first time) and all collected information will be stored here.
  • devices
    mapping of your external to internal serial device. Adjustment is only needed in case you are using a different USB port on your host system.

Extra docker-compose parameters

Additional parameters can be passed to the container using the option environment. As example in above configuration adding the parameter DISABLECPUTEMP would be:

     tmpfs:
       - /run
       - /tmp
     devices:
       - "/dev/ttyUSB0:/dev/ttyUSB0"
     restart: unless-stopped
     environment:
       - DISABLECPUTEMP=true
       - PROXYPATH=p1mon

DISABLECPUTEMP

On certain Operating Systems the CPU temperature is not available causing the WatchDog script to crash. Adding parameter DISABLECPUTEMP=true will disable this option. Default value of this option is set to false

PROXYPATH

Since I would like access to the data also when not at home I added a reverse proxy option. By adding parameter PROXYPATH=p1mon the configuration in the container is adjusted so you can use the subdirectory p1mon as a path in your url. Your outside url would be something like https://myhomenetwork.nl/p1mon.
Don’t forget to protect your reverse proxy (e.g. username/password) as the P1-Monitor version is not build to be internet facing.

Using a ramdisk

The original software of P1 Monitor is using a ramdisk for writing to the databases. Reason is to avoid to many write actions to the SD cards (as they can become corrupted or something). Every now and then data from ramdisk is written to the filesystem. When running on a Linux server that is not a real issue as the file IO is not that much. If you still want to use a ramdisk you can use below modification in the docker-compose file. Remove the ramdisk from the volumes section and add it to the tmpfs section as shown below:

version: '3.8'
services:

  p1monitor:
    hostname: p1mon
    image: mclaassen/p1mon
    ports:
      - 81:80
    volumes:
      - ./alldata/data:/p1mon/data
      - ./alldata/usbdisk:/p1mon/mnt/usb
    tmpfs:
      - /run
      - /tmp
      - /p1mon/mnt/ramdisk
    devices:
      - "/dev/ttyUSB0:/dev/ttyUSB0"
    restart: unless-stopped

Version information

This is the list of changes for the different container versions. the latest tag (if you omit the tag latest will be used) will take you to the newest version. If you would like to use a different version add it to the image tag in the compose file. E.g. If you want use container version 1.4.1b modify the image tag to mclaassen/p1mon:1.4.1b

  • 1.5.0 
    Updates to latest released version 1.5.0
  • 1.4.1b 
    Changed to running as p1mon, fixed issues, updated vulnerable packages
  • 1.4.1a 
    Fixed issue tariff status as described by author
  • 1.4.1 
    Based on the 1.4.1 image of ZTATZ. Also added reverse proxy capability
  • 1.4.0 
    Based on the withdrawn image of ZTATZ. Fixed 1 second issue. Image was withdrawn by the original author but due to the nature of docker container most bugs are not applicable here.
  • 1.3.1d 
    Fixed temperature error in VM using environment variable. Modified cron
  • 1.3.1c 
    Fixed import/export functions as well as missing ping utility
  • 1.3.1b 
    added health check function. Container will show health status and restart on failure
  • 1.3.1a 
    Updates fixing some vulnerabilities in python packages and dpkg
  • 1.3.1 
    Update to latest released version 1.3.1
  • 1.3.0 
    Update to latest released version 1.3.0
  • 1.2.0 
    Initial released version 1.2.0

More information

For more information on P1-Monitor such as latest updates, news and forum take a look at ZTATZ, GitHub, P1Monitor forum en op Docker Hub

Een reactie plaatsen

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *