Udev rule for Victron VE.Direct -> USB on Pi

Looking for some suggestions here.

I’m experimenting with a remote solar setup for a LoRa gateway and using a Victron SmartSolar solar charge controller. The Victron products seem superior in every way to other competitors, including providing a VE.Direct port, which when combined with their USB cable going in to the Pi based gateway lets me monitor solar panel output, battery performance, and more.

I’m interested in using a UDEV rule to detect when the USB cable is plugged in - including potentially kicking off a script that parses the VE.Direct serial messages and piping them to an MQTT server dynamically.

I just did a test by running udevadm monitor — environment in the host container and unplugging and plugging back in the Victron → USB adapter.

Resulting in:

KERNEL[32329.783736] add      /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.1 (usb)
KERNEL[32329.794573] add      /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.1/1-1.1:1.0 (usb)
KERNEL[32329.794839] add      /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.1/1-1.1:1.0/ttyUSB0 (usb-serial)
KERNEL[32329.801311] add      /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.1/1-1.1:1.0/gpiochip2 (gpio)
KERNEL[32329.801411] add      /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.1/1-1.1:1.0/gpio/gpiochip500 (gpio)
KERNEL[32329.801492] add      /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.1/1-1.1:1.0/ttyUSB0/tty/ttyUSB0 (tty)
KERNEL[32329.801565] bind     /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.1/1-1.1:1.0/ttyUSB0 (usb-serial)
KERNEL[32329.801656] bind     /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.1/1-1.1:1.0 (usb)
KERNEL[32329.801758] bind     /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.1 (usb)
UDEV  [32329.803754] add      /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.1 (usb)
UDEV  [32329.810711] add      /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.1/1-1.1:1.0 (usb)
UDEV  [32329.822091] add      /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.1/1-1.1:1.0/gpiochip2 (gpio)
UDEV  [32329.823936] add      /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.1/1-1.1:1.0/gpio/gpiochip500 (gpio)
UDEV  [32329.834082] add      /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.1/1-1.1:1.0/ttyUSB0 (usb-serial)
UDEV  [32329.868650] add      /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.1/1-1.1:1.0/ttyUSB0/tty/ttyUSB0 (tty)
UDEV  [32329.873661] bind     /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.1/1-1.1:1.0/ttyUSB0 (usb-serial)
UDEV  [32329.880226] bind     /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.1/1-1.1:1.0 (usb)
UDEV  [32329.886126] bind     /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.1 (usb)

I’m not quite sure how to translate this output into a UDEV rule that would run on the correct events from udev. Any suggestions?

1 Like

Hey @barryjump this looks like an amazing project that I would like to learn more :slight_smile:

Just found this blogpost which can give you hints on how to do it → Hot plugging USB devices in balenaOS | by Tomás Migone | The Startup | Medium

or this Balena base images - Balena Documentation

Let me know if this works! Looking forward to test this on my BluesSolar (mine is not smart enough)!

1 Like

Thanks @mpous. I did see those docs originally, which got me up to this point.
I think my trouble is not knowing the best practice for which event to hook the rule up to. The output above wasn’t clear enough for me to know which of those events specifically I need to use.

Anyways, udev aside, the experiment is going really well.

(1) 50w Solar Panel
(1) Victron Smart Charge Controller
(1) 12vx6ah LifePo4 (providing 72 watt hours) Battery
(1) VE.Direct to USB cable
(1) Sixfab PowerHAT v2
(1) Raspi 4 (will eventually replace with a CM4 based Lora gateway)

What I realized is that the battery is too small at 12v 6ah. On sunny days it will get me through the night, but on anything less that perfect sun I only get between 14 and 18 hours of runtime.

I’ve learned a good rule of thumb is about 72 hours of runtime is best, so I’ll need to replace the primary battery with a 12v 50ah on the high end. Especially when considering that the Pi4 during testing isn’t running the LoRa concentrator or cell modem which would increase the power draw.

1 Like

@mpous Forgot to add this image… Here’s the ve.direct protocol → usb → an mqtt container that I’m pushing into a dashboard.

Not much power right now as the sun is just starting to come up.

2022-07-29 07.08.12

1 Like

This is amazing @barryjump is this open source? :slight_smile: i would love to test here!

Currently it’s just local, but I’ll pop it on github and make it public tonight.

@mpous here you go:

2 Likes

Awesome! Looking fwd to play with it :slight_smile:

Some time ago i created an excel sheet with a calculator for solar panel and battery sizes withsingle board computers. Happy to play again with it :smiley:

El dv., 29 de jul. 2022, 17:19, barryjump via balenaForums <notifications@balena.discoursemail.com> va escriure:

Hey, Balena engineer here, love the project. I’m a die-hard fan of VE products too, having used many other brands for solar stuff. I actually have a camper van with solar with 4 Tesla Model S batteries from EV West connected to a VE multiplus, and it has worked very well for years for me despite the rough treatment in a camper van.

But on to your query…it’s been a while since I’ve done this, but you can see a list of attributes on your USB device with lsusb -D /dev/ttyUSB0, and use those in the rule, e.g.

SUBSYSTEM=="tty", ATTRS{idVendor}=="03f0",ATTRS{idProduct}=="03f0", ACTION=="add", RUN+="/path/to/your/script.sh"

There’s a great article about udev rules here which has a lot more information

Let us know how it works out, and I might use your project in the future!

2 Likes

I guess I should explain a little further… the idVendor corresponds to the product vendor (victron energy) and the idProduct to the actual product (ve connect)…this means that this script will be portable because those variables won’t change from computer to computer or if you plug the device into a different bus.

Loving this project. I really want to experiment with solar (and campervans). Are you going to be able to push it to the Balena Hub soon?

This project may be helpful on UDEV too: GitHub - balena-io-examples/balena-storage: Sample project to showcase storage mounting on balenaOS.

I was reading this the other day on power consumption which might also be interesting: A deep dive into Raspberry Pi Zero 2 W's power consumption - CNX Software

1 Like

Hey Maggie, it’s not technically a balena project, more just a recreational thing for my fiance and me, but I have been thinking of ways to make a Balena “Camper” project, and I think something like this is definitely a step in that direction!

2 Likes

Thats awesome thank you - exactly what I was hoping for.

I had let the code do its thing on my solar pi and it’s running so well that I forgot about it. Will try your technique in a bit and update the repo.

1 Like

Btw, open to suggestions on how to improve Tronmon.

I’m also experimenting on the side with QuestDB which would add a very light weight, super fast on device time series database for users who want to keep their data local. From their it’s trivial to drop in Grafana for viz.

A local time-series database sound like it’s exactly what you’d need @zwhitchcox in your camper where network connectivity may be shaky thus rendering MQTT to a cloud broker useless.

1 Like

Btw, @zwhitchcox thats really cool you can purchase used Tesla batteries and repurpose them that way.

There is a MASSIVE market opportunity in domestically “assembled”, high quality lithium batteries with good BMS’s, sourced from re-purposed but still good cells.
Lots a cheap hacks on the internet trying to capture the end-of-life turnover market on lead acid and the growing off-grid movement.

1 Like

@barryjump looking forward to see the Victron block on balenaHub.

@barryjump just received the cable! looking forward to test this :slight_smile:

any update here?

@mpous Sorry was out of commission for a while. No updates recently. Did the cable work for you?

1 Like