Skip to content

4. Adding PylonToMQTT to an existing system.

Graham Ross edited this page May 31, 2023 · 2 revisions

The following installation procedure describes how to install PylonToMQTT to an existing system running docker containers like HomeAssistant.

Get PylonToMQTT

  1. Copy this repository (if you understand git, you can get it that way too)
    cd ~
    wget https://github.com/ClassicDIY/PylonToMQTT/archive/refs/heads/main.zip
    
  2. Extract the zip file:
    unzip main.zip
    
  3. Change directory
    cd PylonToMQTT-main/Code/Python/
    

Run It

PylonToMQTT can be installed either native as a python app or in a docker container:

When it comes time to run the program, there are parameters that can be set or passed they are:
Parameters:

--pylon_port </dev/ttyUSB0>     : The USB port on the raspberry pi (default is /dev/ttyUSB0).  
--rack_name <Main>              : The name used to identify the battery rack. 
--mqtt_host <127.0.0.1>         : The IP or URL of the MQTT Broker, defaults to 127.0.0.1 if unspecified.  
--mqtt_port <1883>              : The port to you to connect to the MQTT Broker, defaults to 1883 if unspecified.  
--mqtt_root <PylonToMQTT>       : The root for your MQTT topics, defaults to PylonToMQTT if unspecified.  
--mqtt_user <username>          : The username to access the MQTT Broker (default is no user).  
--mqtt_pass <password>          : The password to access the MQTT Broker (default is no password).
--publish_rate <5>              : The amount of seconds between updates when in wake mode (default is 5 seconds).

1. Execute using Python

Make sure that you have access to an MQTT broker; either install one on your server or use one of the internet based ones like Dioty. Once you have that setup, make sure that you have a username and password defined, you will need it to both publish data and to get the data once it is published.

  1. To verify that you have Python 3 installed run python --version.
  2. Install these libraries: paho-mqtt, pyserial, construct using pip:
    pip install pyserial paho-mqtt construct
    
  3. Run the program from the command line where the pylon_to_mqtt.py is located with the proper parameters:
    cd ~/PylonToMQTT-main/Code/Python
    python3 pylon_to_mqtt.py --pylon_port /dev/ttyUSB0 --baud_rate 9600 --rack_name Main --mqtt_host <127.0.0.1> --mqtt_root <PylonToMQTT> --mqtt_user <username> --mqtt_pass <password> --publish_rate <5>
    

2. Added to docker

Using the "Dockerfile" in the ~/PylonToMQTT-main/Code/Python directory will allow an image to be built that can run the program. The Dockerfile uses a base image that already includes python and instructions to install the 4 needed libraries.

  • Issue the following command to build the docker image in the docker virtual environment (only need to do this once):
    cd ~/PylonToMQTT-main/Code/Python
    docker build -t pylon_to_mqtt .
    
    note: the period at the end is required

There are a number of parameters that may be set for the docker run command:

docker run --name pylon_to_mqtt --device=/dev/ttyUSB0:/dev/ttyUSB0 pylon_to_mqtt --pylon_port /dev/ttyUSB0 --baud_rate 9600 --rack_name Main --mqtt_host 127.0.0.1 --mqtt_port 1883 --mqtt_root PylonToMQTT --mqtt_user mqttuser --mqtt_pass mqttpassword --publish_rate 5

Docker run examples:

  1. If your Raspberry Pi is running the mosquitto MQTT broker (with no user/pw) and the broker's IP address is 192.168.86.23, the docker run command would look similar to this:
    docker run -d --name pylon_to_mqtt --device=/dev/ttyUSB0:/dev/ttyUSB0  pylon_to_mqtt --pylon_port /dev/ttyUSB0 --mqtt_host 192.168.86.23
    
  2. If you setup mosquitto using IOTStack from 3.1 Preparing the Raspberry Pi then you can specify the network and set the mqtt_host to the broker container name (mosquitto), the docker run command would look similar to this:
    docker run -d --restart unless-stopped --name pylon_to_mqtt --device=/dev/ttyUSB0:/dev/ttyUSB0 --network iotstack_default pylon_to_mqtt --pylon_port /dev/ttyUSB0 --mqtt_host mosquitto
    

If you get an error and need to re-run the docker run command, you may need to remove the old one. Use the following command.

docker container prune