mitchese / shm-et340

Sunny Home Manager emulator in a Victron ESS System (replaces ET340)
GNU General Public License v3.0
30 stars 9 forks source link

undefined: dbus.ConnectSystemBus #1

Closed ricott closed 3 years ago

ricott commented 3 years ago

Hi,

Trying to get this to work on a Cerbo GX and after spending some time setting it all up I'm so far that the test script works fine. When trying to run this script it fails though with ./shm-et340.go:20:17: undefined: dbus.ConnectSystemBus

It picked up version 4.1 of the dbus package automatically but added a +incompatible ending to it. Which version of github.com/godbus/dbus did you use when building this script?

Any clue what is wrong?

ricott commented 3 years ago

Changed imports to use v5 github.com/godbus/dbus/v5

Problem is still the same though.

ricott commented 3 years ago

Could it be that line 20 should read var conn, err = dbus.SystemBus()

That compiles, and this is printed to console INFO[0129] Meter update received: 23691.53 kWh bought and 9127.26 kWh sold, 2157.4 W currently flowing

Took a little time but now I see the values in VRM as well :)

Did you make any progress around startup script, etc. They mention this briefly here, that there are standard options to achieve this, https://www.victronenergy.com/live/ccgx:root_access. "Hooks to install/run own code at boot"

ricott commented 3 years ago

I compile the program and added the file /data/rc.local Script only contains one line, to start the program - worked like a charm on first reboot.

Schnema1 commented 3 years ago

Hi @ricott

My you share your knowledge how you got the script to run automaticly at every boot?

Many thanks to @mitchese . I could run the test script on an ubuntu live machine. Now I try to get your script on the Multi Plus II GX. If you are interested about the results, so let me know.

My goal is to integrate the Multiplus and let it charge the batteries when the inverters produces enought energy. So the Multi can feed the server and when the batteries run out of energy the multi swichtes back to AC. The energymeter is a SMA Home Manager 2.0

ricott commented 3 years ago

I have three multiplus 5k, a Cerbo GX and the SMA Home Manager 2. To run a Victron ESS my understanding is that a grid meter is required. Since I already have SMA HM2 I want to use that and not have to install an ET340. So far so good.

I have only played around a little with my setup, not connected any batteries yet, and currently I have the Cerbo turned off. My understanding is the below; This script doesn't create a grid meter in GX, it will give us the values from a simulated one, but if you go to list meters there is no meter there. You have to install golang binaries on the GX device, took me a while to find the right binary for correct HW platform. I put everything on the /data volume so it survives firmware upgrades. Think this is the version that works on the Cerbo (assuming I kept the line that worked at the end) wget -c https://golang.org/dl/go1.15.6.linux-armv6l.tar.gz

I looked at a golang install guide and this is what I kept in my docs tar -C /data/sma/ -xzf go1.15.6.linux-armv6l.tar.gz

export PATH=$PATH:/data/sma/go/bin export GOROOT=/data/sma/go export GOPATH=/data/sma/goscripts

I then compiled the updated script, had to do the changes I mentioned in previous comments above. Thinking that since it is compiled I don't have to work with making the exports above permanent or even keep the pretty big golang binaries on the Cerbo.

There was some more debugging to be done though since I couldn't run the script or compile it. I had to pass a few flags to get it running. This worked for me GO111MODULE=on CGO_ENABLED=0 go build -o bin/shm-et340

Created the file /data/rc.local and added lines below #!/bin/sh /data/sma/goscripts/shm-et340/bin/shm-et340 > /dev/null 2>&1

It doesn't seem to work every time though. Since I am not that far with my other stuff I have not spent more time on this yet. For this to be a permanent solution I think I need to improve the resilience a bit. Starting with adding some logging to find out why it doesn't work on startup consistently.

Schnema1 commented 3 years ago

Thank you @ricott

You got a nice setup. So to my surprise I had less trouble then expected.

I have 2 SMA Tripower inverter and a SMA Storage Boy 3.6 all connected with an SMA Home Manager 2.0. As I got 20kWh lifepo for really cheap I had to find a solution. Sunny Island can not be combined with SMA Storage Boy (how stupid is that!?). So I bought an Multiplus 2 GX 48/5000. I have to mention, that the Victron will serve to power my server 24/7 and thus does no grid feed in. This is the Storage Boys job.

But I can explain you how I managed to get the ESS and Energymeter working.

First I did compile the .go script on my windows machine into an .arm file. Then I enabled the remote console on the multi and enabled SSH access. I then chmod +x the .arm file and ran it. It published the values as expected and printed nicely on the screen. With the VEconifig I could programm the Multiplus and enable the ESS Assistand together with the custom lifepo settings.

On the Victron Portal it published the data as desired and I could compare them with the sunny portal. They matched up. To make further progress, I enabled the modbus on the three SMA inverters and found them on the Multiplus. So I set the Multiplus to read the production out of the inverters. It even detects, the Storage Boy feeding in the grid. The Energy meter is not found, but the data is there. But only as long as the computer is connected through the SSH. To get rid of that, I had to create a config file in the /etc/init.d/ folder, calling the .arm file.

I still have to test the settings, as I want the Multplus to charge if the batteries run out of energy at night or rainy day. Check if the server can survive a switch from grid to inverter mode. Further to check if the batteries are used for the server only an recharched if pv is available or SoC 0%.

ricott commented 3 years ago

compile the .go script on my windows machine into an .arm file

This I should have thought of, to compile it on my mac and just move the binary to avoid having to install golang on the Cerbo. Do you have a good guide you followed for doing that?

Schnema1 commented 3 years ago

Not really. I had a lot of problems with the envs. See my description on the fork project and take the medium link as first approch. You may want to rename the git folders or maybe do a proper go configuration on your mac.

Pay attention: My guide is for a windows machine. But the commands are similar. https://github.com/Schnema1/sma_home_manager_printer

https://medium.com/@georgeok/cross-compile-in-go-golang-9f0d1261ee26

Schnema1 commented 3 years ago

Do you have an idea how to add the script to boot up? https://community.victronenergy.com/questions/78137/multiplus-2-gx-run-own-scrip-at-startup.html

ricott commented 3 years ago

I stumbled upon this project the other day, https://github.com/Louisvdw/dbus-serialbattery. Was planning on looking at how they solved the startup. Have not come so far yet, and I'll be off for a week now so expect no progress. I'm keen to hear back if you make progress on the topic of course :)

mitchese commented 3 years ago

Hey! Sorry for the long offline-ness, been busy with life and baby so I haven't had much time to look into this. That coupled with our solar system over winter doesn't produce enough to make playing with storage worthwhile, so this has been shelved over the winter.

I've pushed some updates here, based on the comments and details ... PRs are welcome to fix or change any of my (bad) go. This was a first 'it works' push, and I wanted to get my progress out, because as far as I can tell, this is the first time anyone has had this working.

@ricott did your /data/rc.local work just directly calling it? Mine tries to start shm-et340 before the network is up (despite being in the rc.local and not rcS.local), which then fails because it can't get a listening address. My adding a sleep there could be improved upon, but it does work where it just blindly waits and then starts.

Also the way it is, you don't need to compile it yourself, you can get the pre-compiled version from the releases here on github... I've updated the readme to reflect this.

ricott commented 3 years ago

/data/rc.local worked randomly, most often NOT. But it worked the first time and at the time I thought it was a done deal. I remember doing a reboot from inside VRM this first time, maybe network acts differently during a reboot vs cold start? No clue.

Still assembling my battery packs and have not started top balancing so will take a while longer until I'm at this for real again. Great to see you active here again. My Golang skills are limited as you can tell ;)

mitchese commented 3 years ago

Maybe give it a go as I described in the updated Readme, I would guess if it works randomly then it is depending on if the interface is up or not which may take more or less time depending on random delays in the network ... when there are no delays it works and when there are slight delays it fails. I was doing some tests today and could only get it reliably working with the sleep; from what I can tell the rc.local is called on every boot, and sometimes shm-et340 fails to start; the only start fail which I caught was due to no network.

Schnema1 commented 3 years ago

@mitchese Great you took some time to help us here and improved the readme. I know very well how it feels like with family and lack of time. Anyway, I am sure many others will profit of your work. And your .go is really nice work!

Concerning the autostart I have asked in Victron forum about it. There I got directed to this Victron commit. https://github.com/victronenergy/meta-victronenergy/commit/2dbd16c560ff7cdf70b1d676c0616013169c7484

However I do not know how they implemented it and how I should understand it. There is no readme. A special the very first sentence is puzzeling me. When custom-rc.d is installed, somehow it seems they should have an out of the box solution. But I also have a lack of programming knowledge. Maybe you might understand it better than me.

mitchese commented 3 years ago

In my tests yesterday, I found that every boot the rc.local was in fact triggered, but in most cases the shm-et340 was not running, so it's "My problem" and not Victron's. After implementing the sleep, it correctly came up after 5 or 6 restarts, so I assume that is a passable workaround. My test yesterday involved just running "touch" to a file and inspecting the timestamp... see the commented out line below

If it's not working for you, make sure your /data/rc.local matches this exactly

!#/bin/bash

# touch /data/home/root/rclocal_was_triggered. #<-- If you run ls -l in root's home folder, you should see a file named "rclocal_was_triggered" with a timestamp of the last boot ... if it's uncommented

sleep 15
setsid /data/home/root/shm-et340 > /dev/null 2>/dev/null &

where I have just copied it to Root's homefolder (which is also on the persistent partition and should survive upgrades). You also need to make sure it's executable (command here is chmod 755 /data/rc.local and to verify, have a look at ls -l /data/rc.local which should have a rwxr-xr-x as the permissions... important here are the x's)

mitchese commented 3 years ago

By the way, thanks for that dbus-serialbattery link. I'm using DIYBMS which currently doesn't track system charge, but I may be able to come up with an estimate based on the battery voltage, and use that to inform victron. Right now I'm using the built-in capacity which is really annoying, especially because I'm still trying to get my DIY powerwall balanced right and the reflected charge is almost always wrong

ricott commented 3 years ago

Slow response from my side, but on the other hand good news. Used the precompiled binary, also used the /data/rc.local script - and it seems to work also for cold starts. Ie power cycle. Been solid so far for me, but I'm only 4 days into being live with my system.

Thanks :)