question

lisaar avatar image
lisaar asked

Simpler local MQTT usage

(Not a question, but rather an informal post aimed at saving others time figuring things out.)

When I went searching the forums for information on how to get the Venus's MQTT server to allow local access to the information I wanted, I found a lot of posts with solutions that either didn't work, required an internet connection, relied on a child MQTT server, or required an always on server sending keepalive messages to the Venus device. In our usage environment, a cruising sailboat, relying on an internet connection was a non-starter, and since we power our MQTT instrument data via an Arduino, powering a Raspberry pie or similar just to feed cookies to our Octo GX seemed silly.

For people familiar with MQTT, but new to Venus: You'll see a lot of mentions of keepalives in these discussion, and you might gloss over this, already being familiar with the keepalive mechanism of the MQTT protocol, but this is not that keepalive. The keepalive that causes all the trouble is a internal Venus thing that is used to stop bridging information from their internal data bus to their local MQTT server (which is just mosquitto, which you may be familiar with). It's the timing out of the bridging into MQTT which causes most of the frustration.

That out of the way, here are the steps to making a local only MQTT Venus without a separate, keepalive feeding server. To do this, you'll want something that can explore MQTT topics, such as MQTT Explorer, something you can publish a topic with, I'll show this with mosquitto_pub since it's the tool I know the best, but there are probably easier ways (you will need this once per power cycle of the Venus), and your favorite MQTT dashboard app, I use Virtuino 6, but use whatever appeals to you the most. This setup assumes that your system is on a secure network, if that is not the case, you will want to do extra steps to configure access controls to the Venus MQTT server.

  • Turn on MQTT via the remote console's Settings -> Services (I turned on both plaintext and SSL, but only used the former)
  • Disable VRM via Settings -> VRM online portal (If you actually want VRM, I don't recommend this solution since it will add a lot of updates to what is sent to Victron's server)
  • I recommend turning off auto updates as well, since an update will undo one piece of this solution and you might be left scratching your head as to what happened. This is not strictly required. Settings -> Firmware -> Online Updates
  • Give yourself root access (usual disclaimers apply here, you can mess up and it can be bad, this is only for technical users, etc) Settings -> General set Access level to superuser and make sure to give a password.
  • ssh into your Venus via ssh root@venus.local and do the usual ssh dance.
  • Next you will turn off part of the keepalive mechanism, that which requires periodic MQTT publishes. The easiest way to do this is to modify /opt/victronenergy/service-templates/dbus-mqtt/run to add " --keep-alive 0" to the end of the line that starts dbus_mqtt.py. I saw several posts saying to do the same to a file in /service, but at least on my system that does not work as that file is created on bootup from the file I pointed you at.
  • reboot the Venus device
  • Next we need to find our system id, which we can do with MQTT Explorer by connecting to venus.local and expanding the "N" topic, which should have child whose name is your 12 hex digit id.
  • At this point if you explore the tree, you'll find nothing of use outside of statics about the MQTT server itself. This is because the dbus->mqtt bridge defaults to being conservative and not bridging information. Next we'll populate the tree to find what topics we are interested in.
  • Since Victron doesn't appear to publish a topic list the easiest way to see what's out there is to tell the data bus to MQTT bridge to just bridge everything. I did this with "mosquitto_pub -h venus.local -n -t 'R/<your id here>/system/0/Serial'" substituting my id for <your id here>.
  • I then explored around and built the first pass of my dashboard using MQTT Explorer to find the topics I needed.
  • Since we're now asking Venus to bridge a lot of information into mosquitto via a relatively expensive mechanism, it's best to now go back and selectively tell the bridge what we're interested in. To do this, start with a Venus reboot (either via the command lien or the remote console mechanism).
  • To selectively tell the bridge what we want to see, we publish to the topic "R/<your id here>/keepalive a JSON encoded array of topics with the top "N/<your id here/" stripped off. Basically the topics relative to the controller.'s point of view. In my simple example of getting data from my battery monitor (289 on my setup) I used the command:
"mosquitto_pub -h venus.local -m '["battery/289/Dc/0/Voltage", "battery/289/Dc/0/Current", "battery/289/Soc", "battery/289/Dc/0/Power", "battery/289/ConsumedAmphours", "battery/289/TimeToGo", "battery/289/History/TimeSinceLastFullCharge", "battery/289/Dc/0/Temperature"]' -t 'R/<your id here>/keepalive'"
  • Obviously you'll want to expand this, tune it to your setup, and make it your thing. It's a good idea to make a script to do this, or save the publication message so as you can do it easily since you will need to do this publication after each Venus reset.

Attached is an quick example dashboard I made with Virtuino. (Ignore the connection error at the top, that was to our instrument server which was off at the time).

Hopefully this saves someone some frustration along the way.

Lisasignal-2022-10-14-140927-002.png

MQTTmodifications
2 |3000

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

8 Answers
dominik-feser avatar image
dominik-feser answered ·

Thank you for this guide!

Can you confirm it is working with the latest large release? I am trying it since 2.90 but no luck. I see the process for dbus-mqtt constantly being restarted - no clue if this is normal but I can not see anything being published on the local mosquitto, other than $SYS.


Here you can see, that the process is only running very shortly and then a new instance gets started:

root@raspberrypi2:/opt/victronenergy/dbus-mqtt# ps | grep dbus_mqtt.py
17885 root     11660 R    {dbus_mqtt.py} /usr/bin/python3 -u /opt/victronenergy/dbus-mqtt/dbus_mqtt.py --init-broker --keep-alive 0
17887 root      2692 S    grep dbus_mqtt.py
root@raspberrypi2:/opt/victronenergy/dbus-mqtt# ps | grep dbus_mqtt.py
17885 root     30436 R    {dbus_mqtt.py} /usr/bin/python3 -u /opt/victronenergy/dbus-mqtt/dbus_mqtt.py --init-broker --keep-alive 0
17900 root      2692 S    grep dbus_mqtt.py
root@raspberrypi2:/opt/victronenergy/dbus-mqtt# ps | grep dbus_mqtt.py
17885 root     34912 R    {dbus_mqtt.py} /usr/bin/python3 -u /opt/victronenergy/dbus-mqtt/dbus_mqtt.py --init-broker --keep-alive 0
17903 root      2692 S    grep dbus_mqtt.py
root@raspberrypi2:/opt/victronenergy/dbus-mqtt# ps | grep dbus_mqtt.py
17915 root     24416 R    {dbus_mqtt.py} /usr/bin/python3 -u /opt/victronenergy/dbus-mqtt/dbus_mqtt.py --init-broker --keep-alive 0
17927 root      2692 S    grep dbus_mqtt.py

Do you see the same behavior on your working installation?

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.

lisaar avatar image lisaar commented ·

I'm running 2.91 and dbus_mqtt.py seems to be running healthy.

root@beaglebone:~# ps |grep dbus_mqtt.py
 2018 root     39864 S    {dbus_mqtt.py} /usr/bin/python3 -u /opt/victronenergy/dbus-mqtt/dbus_mqtt.py --init-broker --keep-alive 0
13864 root      2708 S    grep dbus_mqtt.py
root@beaglebone:~# ps |grep dbus_mqtt.py
 2018 root     39864 S    {dbus_mqtt.py} /usr/bin/python3 -u /opt/victronenergy/dbus-mqtt/dbus_mqtt.py --init-broker --keep-alive 0
13867 root      2708 S    grep dbus_mqtt.py
root@beaglebone:~# ps |grep dbus_mqtt.py
 2018 root     39864 S    {dbus_mqtt.py} /usr/bin/python3 -u /opt/victronenergy/dbus-mqtt/dbus_mqtt.py --init-broker --keep-alive 0
13874 root      2708 S    grep dbus_mqtt.py
root@beaglebone:~# ps |grep dbus_mqtt.py
 2018 root     39864 S    {dbus_mqtt.py} /usr/bin/python3 -u /opt/victronenergy/dbus-mqtt/dbus_mqtt.py --init-broker --keep-alive 0
13881 root      2708 S    grep dbus_mqtt.py
root@beaglebone:~# ps |grep dbus_mqtt.py
 2018 root     39864 S    {dbus_mqtt.py} /usr/bin/python3 -u /opt/victronenergy/dbus-mqtt/dbus_mqtt.py --init-broker --keep-alive 0
13907 root      2708 S    grep dbus_mqtt.py
root@beaglebone:~# ps |grep dbus_mqtt.py
 2018 root     39864 S    {dbus_mqtt.py} /usr/bin/python3 -u /opt/victronenergy/dbus-mqtt/dbus_mqtt.py --init-broker --keep-alive 0
13927 root      2708 S    grep dbus_mqtt.


0 Likes 0 ·
elvis avatar image
elvis answered ·

I guess I don't understand the part about the on all the time server. why would you need an on all the time server? simply send the keep alive request when the program you're running requires it.

2 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.

lisaar avatar image lisaar commented ·
That's not an easy automation from typical mobile dashboard apps and it's annoying to instigate manually and messes up graphing features.
0 Likes 0 ·
Show more comments
gainestr avatar image
gainestr answered ·

i don’t understand what the issue is with the keepalive command. Please explain what I’m missing. I use node red and an inject node along with a mqqt out node and send the topic R/YOUR_VRM_ID/keepalive every 30 seconds That’s it. I can access all output locally with nothing else to do. And if I enable two way communications on the VRM website, I can access everything remotely with no need for python scripts or separate servers sending a keepalive command. I must be missing something here since I’m not having any troubles with Venus OS Large and node red.

2 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.

lisaar avatar image lisaar commented ·

It's for people not running nodered and connecting directly to the MQTT sources from mobile devices and have limited connectivity (such as when at sea).

0 Likes 0 ·
gainestr avatar image gainestr lisaar commented ·
I understand now. Thanks
1 Like 1 ·
dominik-feser avatar image
dominik-feser answered ·

@Elvis & @gainestr, can you do me a favor and check, if the pid of this output stays the same or changes, when you run the command several times?

root@raspberrypi2:/opt/victronenergy/dbus-mqtt# ps | grep dbus_mqtt.py
17885 root     11660 R    {dbus_mqtt.py} /usr/bin/python3 -u /opt/victronenergy/dbus-mqtt/dbus_mqtt.py --init-broker --keep-alive 0
2 |3000

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

gainestr avatar image
gainestr answered ·

When running the command /usr/bin/python3 -u /opt/victronenergy/dbus-mqtt/dbus_mqtt.py it disconnects. Not sure what’s going on with that command but mqtt is clearly running and not disconnecting on my system both locally and remotely.

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.

dominik-feser avatar image dominik-feser commented ·
The script should run as soon as you are activating MQTT in the menu. What I wanted to know is, if on your installation this script runs uninterrupted or if it gets constantly restarted with a new process id (pid).

You can find this out by typing in "ps | grep dbus_mqtt.py" a couple of times with 1 second in between. In my example the pid is "17885" and it changes every time I do the "ps..." command.

0 Likes 0 ·
gainestr avatar image
gainestr answered ·

When I activate mqtt and run the command above that you requested, my output is consistent and does not change the process ID. b8f10be1-38c6-4af3-8d00-5ff18a484d6d.jpeg


3 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.

dominik-feser avatar image dominik-feser commented ·
Thank you, @gainestr! This proofs, that my installation does not run as designed.
0 Likes 0 ·
gainestr avatar image gainestr dominik-feser commented ·
Honestly, I’m not sure what to think. I let my system run for about 5 minutes and ran It again and got a different process ID but then tried it again 30 min later and the process ID stayed the same so not sure what to think. I did loose Bluetooth connectivity with my raspberry pi though so maybe it’s related? Wish I could help you more.
0 Likes 0 ·
dominik-feser avatar image dominik-feser gainestr commented ·
For you the process at least gets a chance to work :-) You can look into /var/log/dbus-mqtt/current. If I do this I see issues over and over - for you, I guess, it would be different.
0 Likes 0 ·
taylortops avatar image
taylortops answered ·

I have it running on javascript on Raspberry Pi if anyone needs a leg-up with this approach:

screen-shot-2022-10-21-at-121952.png


2 |3000

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

dominik-feser avatar image
dominik-feser answered ·

I found the issue. I am using https://github.com/mitchese/shm-et340 to read my SMA SunnyHomeManager 2.0. This executable seems to send data to dbus that breaks dbus-mqtt.

3 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.

gainestr avatar image gainestr commented ·
Glad you figured it out. I'm sure it was frustrating at the very least.
0 Likes 0 ·
dominik-feser avatar image dominik-feser gainestr commented ·
Yeah, but sadly no sign of life from the developer of this code... Seems like I have to look for another SMA SunnyHomeManager integration.
0 Likes 0 ·
dominik-feser avatar image dominik-feser commented ·

I found a solution to my issue. Documenting it here...

After ending up in the python code of python package "dbus", I got the impression that maybe the fact, shm-et340 being the last registration to dbus causes the issue. So I flipped the rc.local entries and it works!

Now my rc.local with shm-et340 & dbus-serialbattery looks like this:

  
                    
  1. root@raspberrypi2:~# cat /data/rc.local
  2. #!/bin/bash
  3. sleep 10
  4. setsid /data/shm-et340 > /dev/null 2>/dev/null &
  5. sh /data/etc/dbus-serialbattery/reinstalllocal.sh

Now I can see the topics being published after keepaliving dbus-mqtt like described in the git documentation.

0 Likes 0 ·

Related Resources

Victron MQTT readme

Experiments, Modifications and Adaptions. Mods (Modifications) can be made to Victron Software by the Community. Please use the modifications space for enthusiasts who want to push what is possible, without official Victron Support.