question

Jimmy Bergetun avatar image
Jimmy Bergetun asked

Anyone have python example how to read MQTT values from Venus

I want to display battery information from my victron setup on a web page though python scripting, but I am strugling to understand how Victron has implemented mqtt on its devices.

Anyone have an example how to connect and display a value (For example Battery voltage) from Venus MQTT over internet with python and mqtt?

cerbo gxMQTT
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

3 Answers
Ole Saether avatar image
Ole Saether answered ·

Below is a python script that prints the battery voltage and exits. You need to change the mqtt_broker for your installation. See at the bottom of this page on how to find the correct one:
https://github.com/victronenergy/dbus-mqtt



from time import sleep
import ssl
import json
import os
from paho.mqtt.client import Client 

username = "your VRM email"
password = "your VRM pasword"
portal_id = "your VRM portal ID"
mqtt_broker = "mqtt67.victronenergy.com"

def on_message(client, userdata, message):
    val = json.loads(message.payload)
    print(val["value"])
    client.loop_stop()
    client.disconnect()
    os._exit(0)


def on_connect(client, userdata, rc, *args): 
    client.subscribe("N/%s/system/0/Dc/Battery/Voltage" % portal_id)

client = Client("P1")
client.tls_set(cert_reqs=ssl.CERT_NONE)
client.tls_insecure_set(True)
client.username_pw_set(username, password=password)
client.connect(mqtt_broker, port=8883)
client.on_connect = on_connect
client.on_message = on_message

client.loop_start()

while True:
    sleep(1)


1 comment
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

Jimmy Bergetun avatar image Jimmy Bergetun commented ·

Thank you :)

0 Likes 0 ·
drdiesel avatar image
drdiesel answered ·

Can this be modified to talk to a local GX device and not VRM?

9 comments
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

Ole Saether avatar image Ole Saether commented ·

If you want to read the Venus locally I would recommend Modbus. Below is an example that read the system battery voltage and print it to the console.


from pymodbus.constants import Defaults
from pymodbus.constants import Endian
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
from pymodbus.payload import BinaryPayloadDecoder

Defaults.Timeout = 25
Defaults.Retries = 5
client = ModbusClient('ipaddress.of.venus', port='502')
result = client.read_input_registers(840, 2)
decoder = BinaryPayloadDecoder.fromRegisters(result.registers, byteorder=Endian.Big)
voltage=decoder.decode_16bit_uint()
print("Battery voltage: {0:.2f}V".format(voltage/10.0))


0 Likes 0 ·
Show more comments
laurenceh avatar image
laurenceh answered ·

Jimmy

I did some work on this some time ago as I wanted to do something similar when writing my own HTML GUI, my article is here:

https://community.victronenergy.com/articles/48977/simple-jsonrestajax-interface-to-venus-d-bus-writt.html

1) I did not use MQTTI I went straight into the d-bus data - this was after a prompt from Matthijs about using d-bus.

2) the d-bus code in that article is intended to run on the Venus OS device but I have linked it to run as a CGI service to return data in response to queries from network clients. The server side is in Python so you could extract my code example as a means of running python to read the d-bus data. Almost everything you would ever want is available on the d-bus once you have the code working.

3) the content is returned encoded as json because there are libraries to neatly unpick json responses and get them back into your html page. Not that the call to the function to render the response is embedded deep within the javascript to issue the request (easy to miss if you dont know what you are looking at).

4) You need to modify the hiawatha.conf file to enable cgi, there is a little more on making that change here: https://community.victronenergy.com/questions/47919/install-utilities-such-as-jq.html

The services and paths to the data are mostly by Victron, but some of the information may be out of date and there is now much more available via the d-bus than is documented by Victron here.

https://github.com/victronenergy/venus/wiki/dbus

Laurence


2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.