/energy-meter-logger

Log your Energy Meter data on a Raspberry Pi and plot graphs of your energy consumption

Primary LanguageC++GNU General Public License v3.0GPL-3.0

Update an modify scripts to run in python3 from original proyect https://github.com/samuelphy/energy-meter-logger, change to modbus_tk module and add new industrial devices metters. Add support for ModbusTCP and add bridge TCP to RTU vía ESP8266. Added possibility to use more than one InfluxDB server (or database). Thanks to unverbuggt, for the update to influx 2.7

Energy Meter Logger

Log your Energy Meter data on a Raspberry Pi/Orange Pi and plot graphs of your energy consumption. Its been verified to work with a Raspberry Pi and Orange Pi One / Zero with a Linksprite RS485 shield and USB to RS485 adapter or use Modbus-Gateway-esp8266 for reading values from SDM120M (SDM120CTM, SDM120CT-MV), SDM220M, SDM230M, SDM630M, YG194E-9SY, YG194E-95Y, YG889E-9SY, PZEM-016 and DDS238-1 ZN. By changing the meters.yml file and making a corresponding [model].yml file it should be possible to use other modbus enabled models.

Add support for ModbusTCP and add bridge TCP to RTU vía ESP8266 and MAX485.

Requirements

Hardware

Software

  • Rasbian or armbian or dietpi (buster)
  • Python 3.4 and PIP3
  • PyYAML 5.1 ((pip3 install -U PyYAML or python3 -m pip install -U PyYAML) if installed)
  • modbus_tk
  • InfluxDB
  • Grafana

Prerequisite

This project has been documented at Hackster. Please follow the instructions there for more detailed information.

Installation

Install InfluxDB*

Step-by-step instructions
  • Add the InfluxData repository
    $ wget -q https://repos.influxdata.com/influxdata-archive_compat.key
    $ echo '393e8779c89ac8d958f81f942f9ad7fb82a25e133faddaf92e15b16e6ac9ce4c influxdata-archive_compat.key' | sha256sum -c && cat influxdata-archive_compat.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/influxdata-archive_compat.gpg > /dev/null
    $ echo 'deb [signed-by=/etc/apt/trusted.gpg.d/influxdata-archive_compat.gpg] https://repos.influxdata.com/debian stable main' | sudo tee /etc/apt/sources.list.d/influxdata.list
  • Download and install
    $ sudo apt-get update && sudo apt-get install influxdb2
  • Start the influxdb service
    $ sudo systemctl start influxdb
  • Create the database (databases are named buckets in influxdb2)
    $  influx bucket create -n db_modbus --org myorg
    or user webui at http://localhost:8086
  • Create a token
    $ influx auth create -o myorg --all-access
    or user webui at http://localhost:8086

*manual installation without apt

Install Grafana*

Step-by-step instructions
  • Add APT Repository
    $ echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
  • Add Bintray key
    $ curl https://packages.grafana.com/gpg.key | sudo apt-key add -
  • Now install
    $ sudo apt-get update && sudo apt-get install grafana
  • Start the service using systemd:
    $ sudo systemctl daemon-reload
    $ sudo systemctl start grafana-server
    $ systemctl status grafana-server
  • Enable the systemd service so that Grafana starts at boot.
    $ sudo systemctl enable grafana-server.service
  • Go to http://localhost:3000 and login using admin / admin (remember to change password) *source

Install Energy Meter Logger:

  • Download and install from Github and install pip3

    $ sudo apt-get install git
    $ git clone https://github.com/GuillermoElectrico/energy-meter-logger
    $ sudo apt-get install python3-pip
  • Run setup script (must be executed as root (sudo) if the application needs to be started from rc.local, see below)

    $ cd energy-meter-logger
    $ sudo python3 setup.py install

    If error appears use previously:

     $ sudo pip3 install setuptools
  • Make script file executable

    $ chmod 777 read_energy_meter.py
  • Edit meters.yml and influx_config.yml to match your configuration

  • Test the configuration by running:

    ./read_energy_meter.py
    ./read_energy_meter.py --help # Shows you all available parameters

    If the error appears:

     /usr/bin/env: ‘python3\r’: No such file or directory
    

    Use dos2unix to fix it.

     $ sudo apt install dos2unix
     $ dos2unix /PATH/TO/YOUR/FILE
    
  • To run the python script at system startup. Add to following lines to the end of /etc/rc.local but before exit:

    # Start Energy Meter Logger
    /home/pi/energy-meter-logger/read_energy_meter.py --interval 10 > /var/log/energy_meter.log &

    Log with potential errors are found in /var/log/energy_meter.log

Optional, Install and Configure RTC DS3231

In the case of not having internet in the installation where you have the meter with the raspberry pi, you can install an RTC DS3231 module to be able to correctly register the date and time in the database and grafana.

Step-by-step instructions
  • First connect the RTC module Connect to the corresponding pins +3.3V, SDA1 (GPIO2), SCL1 (GPIO3) and GND of the raspberry pi (depending on the model, in google there are examples).

  • Enable I2C port vía raspi-config*

    $ sudo raspi-config

    Reboot after enabled.

    *If you use orange pi or similar, consult documentation.

  • Install i2c-tools and verify that the i2c bus and the RTC module are working (Optional)

    $ sudo apt-get install i2c-tools
    $ sudo i2cdetect -y 1
        0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          -- -- -- -- -- -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
    70: -- -- -- -- -- -- -- --
  • Now check the time of the module, and if it is the case, update the date and time.

    Enable RTC module:

     $ sudo bash
     # echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
     $ exit

    With this the time is read from the RTC:

     $ sudo hwclock -r --rtc /dev/rtc0

    *If you get an error or can not find /dev/rtc0, check the name of the rtc with:

     $ ls /dev/rtc?

    The system time can be seen with:

    $ date
    jue may  5 23:02:46 CLST 2016

    To set the system time, this command is used:

    $ sudo date -s "may 5 2016 23:09:40 CLST"
    jue may  5 23:09:40 CLST 2016

    Now as the system clock is fine, you can set the time in the RTC as:

     $ sudo hwclock -w --rtc /dev/rtc0
  • To set the date from the rtc each time the system is started Add to following lines to the end of /etc/rc.local but before exit:

    $ sudo nano /etc/rc.local
     echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
     sleep 1
     hwclock -s --rtc /dev/rtc0

Optional, Configure Grafana to anonymous login and redirect to port 80

If you need to access grafana without adding port 3000 in the address, and do not want to have to log in every time you want to see. Follow the steps below.

Step-by-step instructions
  • First install nginx

    $ sudo apt-get update && sudo apt-get install nginx
  • Configure nginx default config

    $ sudo mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.old
    $ sudo nano /etc/nginx/sites-available/default

    Add

     server {
     	listen 80;
     	server_name your-domain-name.com;
     	location / {
     		proxy_set_header   X-Real-IP $remote_addr;
     		proxy_set_header   Host      $http_host;
     		proxy_pass         http://127.0.0.1:3000;
     	}
     }

    Exit and save.

  • Edit grafana config

    $ sudo nano /etc/grafana/grafana.ini

    Modify this line to enable Anonymoun Auth

    [auth.anonymous]
    # enable anonymous access
    enabled = true

    Exit and save.

    Reboot the system a enjoy it