-
-
Notifications
You must be signed in to change notification settings - Fork 511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Modbus #1893
base: master
Are you sure you want to change the base?
Add Modbus #1893
Conversation
Very nice work! This would absolutely be something I could use. Do you think that, expanding on this commit, making the same modbus data also available through modbus RTU would be possible? OpenDTU Fusion already includes a Renesas ISL3178E TTL to RS485 transceiver, meaning the hardware is already prepared for this. It's just not used yet at the moment. EDIT: |
Thank you.
eModbus supports RTU also, so it should be possible. My Fusionv2 board is used to monitor my system, so I would be a little bit reluctant to use it for testing Modbus RTU. I will have a look into RTU. Maybe there is a way to keep memory footprint low in case one or both servers are not used.
Good suggestion, will work on that. |
I'm very glad that I was an inspiration :) And I'm even more glad that someone took proper care of a problem that I personally didn't have time to deal with. |
77d2f85
to
04ebf74
Compare
Hello, |
Edit:
Added app.js.gz to the pull request. Now you can build the firmware without building the webapp before. |
Added modbus server for minimal SunSpec meter. Only provides AC power and AC yield as if measuring the output of all registered inverters. Same approach as for the "Total Inverter" but now disguised as a meter. To be used e.g. as a "Fronius Smart Meter TCP" as orginally done in https://github.com/AloisKlingler/OpenDTU-FroniusSM-MB. |
Hello,
And now the strangest thing when it comes to reading from a real DTU. Generally, real DTU has 8-bit registers, not 16-bit registers like real Modbus... The question is whether it would be worth adding a selection option. Especially if openDTU was to be compatible with various DTU libraries. |
Thx for testing and advise.
Changed now - just takes the values provided by OpenDTU. Does not check for not reachable anymore.
Added an alarm code in case the inverter is unreachable. Can you advise on the link status? I though it is the DTU communication status (not the inverter communication status).
Changed - Now 0 is provided for all inverter data list registers that are supported by the DTU if they are unused.
Changed - adapted to your original code. It is very strange that the start address for the data (0x1000)/ SN (0x2000) list in the documentation seems to be a "normal" Modbus address, but the following fields are per byte. I tested with your https://github.com/ArekKubacki/Hoymiles-Plant-DTU-Pro app for HomeAssistant. At least the application starts. This was tested without inverters. By the way during testing I triggered several Python exceptions due to configuration problems. Especially if the number of panels is > 0 and there are no inverters, the app won't start. Fault indication can only be found in the system log and it is somehow misleading as it is an index out of bounds fault. I don't own a Hoymiles DTU, so your help is very much appreciated. |
Hi, [HMSeriesMicroinverterData(data_type=0, serial_number='00000000003c', port_number=17, pv_voltage=Decimal('2494.7'), pv_current=Decimal('165.3'), grid_voltage=Decimal('409.7'), grid_frequency=Decimal('3.96'), pv_power=Decimal('20.1'), today_production=2325, total_production=327551956, temperature=Decimal('0'), operating_status=1384, alarm_code=58, alarm_count=12431, link_status=0, reserved=[107, 0, 3, 0, 0, 0, 0]), |
Hello @ArekKubacki , I did some more tests using synthetic test data (fixed values for 1 inverter with 6 channels) for the DTUPro data. The test data can be activated in platformio_override.ini by -DOPENDTU_SIM_DTUPRO:
I used this HomeAssistant configuration.yaml
As a result HomeAssistant shows the following. Test data seems to be correctly displayed. I this the expected output? |
Unfortunately not yet. today_production, total_production, pv_power, pv_current, pv_voltage should be for each port (string, panel), not for the entire inverter. port_number should change depending on the number of ports on the inverter. In my case 1-4. It is currently growing to 24, which is the maximum panels I have. |
Thank youu for testing. Will look into this. I hopefully can now understand your original code. |
2fb946c
to
9e826b5
Compare
Hello @ArekKubacki
Now switched to values per string.
Reported port numbers adapted to per inverter. Would be nice if you could test this version. I tried to make a one to one copy of your code. What is still open to me is the first byte (data type) at 0x1000. According to the specification it shall be 0x3C, you are using 0x0C. Do you know more? What does the DTUPro use here? Thanks again for testing. |
Looks very good. As far as register 1 is concerned, it does not matter. According to the specifications, it is 3C in the original DTU, at least in my case it is 0C. It is not used for anything so I think you can leave it as per specifications. Have you thought about making 2 choices? How is "DTU Clone" and how it should be according to the specification with normal 16-bit registers? This will give users more space to use. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
123
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1234
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1234
Sorry for confusion. Somehow this happened during testing in VSCode. |
@b0661 Bobby there is a request for Modbus RTU in this discussion: #2127 I do not know if this would be a rather complicated change, as the register addresses (4.3 vs. 5.3 Modbus Registers) are mostly the same, though the messages commands / functions (4.2 Modbus RTU Functions vs. 5.2 Modbus TCP Functions) are slightly different, for reason of RTU vs. TCP transport schemes. Also note that your PR may be accepted into OpenDTU-OnBattery rather than OpenDTU, as far as I understood @tbnobody BTW: If you need clarification on some of the registers we may have a look into the gitee.com code drop of the original DTU Pro Source Code. It definitely contains the original Modbus & Sunspec implementations from Hoymiles. Technical-Note-Modbus-implementation-using-3Gen-DTU-Pro-V1.2.pdf Modbus TCP Settings
5 Hoymiles Modus TCP Interface and RegistersThis section describes the register mapping for the monitoring data and remote control 5.1 Modbus TCP SettingsIn the Modbus TCP protocol, the DTU-Pro will work as the slave and receive the control from the third-party monitoring device. Usually users can directly connect the ethernet cable to the ethernet port to use Hoymiles Modbus protocol. 5.2 Modbus TCP FunctionsThe Modbus protocol is a protocol, with only one master and multiple slaves.
DTU or microinverter status registers can be read and written, and are intended for
DTU or microinverter status registers can be read and written, and are intended for
DTU or microinverter data registers are generally read-only and report voltage, current,
This writes a new value to a single or all DTU or microinverter status registers.
This writes a new value to multiple DTU or microinverter status registers. Please refer to 5.2.1 Read Single Device Status
5.2.2 Read Multiple Device Status
5.2.3 Read Device Data
5.2.4 Write Single/All Device Status
5.2.5 Write Multiple Microinverters Status
5.3 Modbus Registers5.3.1 Microinverter Status Register ListThe following registers provide a microinverter status register list, which can be both read and written.
Note:
5.3.2 Microinverter Data Register ListThe following registers provide a microinverter data register list, which can be read-only
5.3.3 Device SN Register ListThe following registers provide the device serial number register list, which can be read
Note:
5.3.4 DTU Ethernet Setting Register ListThe following registers provide DTU ethernet setting register list, which can be read and
Note: When the port number is changed, please restart the DTU. |
Hello @stefan123t, the current Sunspec Modbus implementation is a minimalistic one. Just enough Sunspec Modbus to suite the use case of getting the total power and energy. I did not think of a full Sunspec Modbus clone of DTU Pro. I'm happy to implement more Sunspec models if there are concrete requests and testers. I only have one inverter in production use and I do not own a DTU Pro - so my test scenario is very limited. Also feel free to provide pull requests. The Hoymiles Modbus implementation you mentioned is already provided (addresses 0x1xxx, 0x2xxx) . Not to the full extend, but enough to suffice @ArekKubacki 's home assistant integration. Thank you for mentioning #2127. I answered in #2134. Bobby |
OpenDTU is extended by a Modbus server. The Modbus server serves TCP at port 502. At Modbus ID 1 the server mimicks the Modbus registers in the original DTUPro. At Modbus ID 125 the server serves a SunSpec compatible pseudo inverter that provides the OpenDTU aggregated data from all registered inverters. At Modbus ID 243 the server serves a Sunspec meter that provides aggregated AC power and AC yield values of all registered inverters. The OpenDTU Modbus sources were imspired by : https://github.com/ArekKubacki/OpenDTU. See tbnobody#582 for the orignal pull request. The Modbus library used for Modbus communication is: https://github.com/eModbus/eModbus. Documentation for the library is here: https://emodbus.github.io/. The library was choosen to achieve a lower memory footprint. Signed-off-by: Bobby Noelte <[email protected]>
Add Modbus TCP configuration and info views. Signed-off-by: Bobby Noelte <[email protected]>
@b0661 your PR completely superseeds the PR #582 from @ArekKubacki or is there a delta left ? @schlimmchen is this something that would somehow better fit into OpenDTU-OnBattery or do you think @tbnobody would be willing to add Modbus and DPL support into OpenDTU itself ? |
The title is "Add Modbus", and there is a similar PR in the downstream project, which also lacks sufficient motivation to merge such a big feature. In case of this upstream project, I assume the amount of flash consumed is so considerably high that it warrants not merging it. This is only a guess based on the size of the changeset. For the downstream project, as I said, I don't understand why this is important and I wont't research it myself. I also don't understand why such a huge changeset should be necessary for what seems to me like only a small improvement/feature for a small audience. So, my honest opinion is currently, that this bridge fits best on an some sort of external third-party component. |
@stefan123t, this pull request supersedes PR #582. @ArekKubacki tested it and it works with his HomeAssistant integration for Hoymiles DTU Pro. @schlimmchen, I´m perfectly fine if this pull requests will never be merged into the main branch. I created it to give something back to the OpenDTU community. Nevertheless I would really appreciate if you refrain from wild guessing without a sound analysis of the change (large changeset, small improvement/feature, small audience). In my world open source is fun work to give something to the people. If it helps somebody else I'm happy. I don't do a business in need of big features for a big audience. For me Modbus is easier than MQTT. I don't need an MQTT server. I get actual data when I pull and do not have to wait for an MQTT update. The data set is standardised, either by Hoymiles or by Sunspec. It fits to my other "big" inverters that are all running on Modbus. Cheers |
@b0661 I think you may explain a bit better what this Modbus implementation can be used for. For me this is a feature that was requested since early in the projects Ahoy and OpenDTU both had plans to support Modbus protocol for either reading a Smartmeter and/or giving access to the DTU for others via Modbus / Sunspec. Actually the Hoymiles DTU (Pro ?) also supports the Sunspec protocol, hence it somehow should fit into our code without breaking the code size limits. If you need a copy of the original code for reference let me know, maybe I even have a version with translated comments ? @schlimmchen I understand your hesitation to merge new code into mainline / downstream projects perfectly fine, because not all functionality is needed by all / many users. I do not think that this changeset introduces a overly large footprint of code in binary form but that is something that can be checked best after compilation. On the other hand I also understand the reservation in this and the downstream project which just switched from the 4MB -> 8MB recommendation of flash size for the ESP32S3 MCU. Maybe it is going to be necessary / useful to start thinking of a kind of plugin system, which allows to compile in certain distinct feature sets and use a wizard like in Tasmota to choose the build time flags wanted by the user ? I had the same idea when looking at syslog feature, but also some of the OpenDTU-OnBattery features like Dynamic Power Limit or Power Meter reading which should be implemented as plugins / options in the upstream project IMHO. |
@b0661 Great work, exactly what I was looking for! Actually, while googling around how to implement this mod myself I found this PR and your fork. In the meantime, openDTU has been updated to a new version of the ESPAsyncWebServer that heavily clashes with eModbus' dependency to AsyncTCP. I only managed to solve this by either including eModbus as a static library within openDTU (after changing the dependency from me-no-dev/AsyncTCP to mathieucarbou/AsyncTCP) or using a modified fork of eModbus (with the same changes as just mentioned). I got everything merged in the current version of openDTU (v24.11.7), apart from the eModbus-problem it's just a few required changes. BR, |
@soylentOrange, good to hear that the PR fits your needs.
Did you play with platform.ini and the lib_exclude directive? This way I manged to exclude the clashing eModbus dependencies. Maybe this part did not make it into your changeset. Cheers |
@b0661 I tried for some time (and the eModbus' ethernet dependency is still suppressed this way). Yet, openDTU moved to mathieucarbou/ESPAsyncWebServer, which is depending on mathieucarbou/AsyncTCP while eModbus is depending on me-no-dev/AsyncTCP. If eModbus switches to mathieucarbou/AsyncTCP (and making it run on RP2040W alongside) then it wouldn't be any problem... BR, |
eModbus maintainer here. I would be willing to use the alternative AsyncTCP implementation if someone could point out the advantages over the classic me-no-dev version. |
@mathieucarbou is also a user of OpenDTU, maybe he can best summarise his changes over the me-no-dev version apart from what is visible on the project repo. https://github.com/mathieucarbou/ESPAsyncWebServer I only know there are a couple of fixes / updates to AsyncTCP on top of the ESPHome fork and the Middleware which he added lately. |
@Miq1 : https://github.com/mathieucarbou/AsyncTCP?tab=readme-ov-file#changes-in-this-fork Right now anyway you will need it if you plan to transition to Arduino Core 3.1.x RC2 (see mathieucarbou/AsyncTCP#27), because Matter introduction activates some lwip safeguards which will crash AsyncTCP original repo. You can read those links and look at the changes and you will see. FYI, there is also AyncTCPSock that exists and based on BSD sockets as an alternative. Here are some perf tests for http and sse with both: https://github.com/mathieucarbou/ESPAsyncWebServer?tab=readme-ov-file#performance Even if you can see that AyncTCPSock can serve faster for http, it remains problematic for some use cases especially concurrent connections on SEE and task cooperation. I have some apps that are blocking the UI served by ESPAsycnWS when some other background tasks are running (such as AsyncUDP tasks), although both lib having the same task priority. I didn't look into that because I did not create neither maintain AyncTCPSock and AyncTCP works fast enough I think. |
@mathieucarbou Thanks a lot, we will have a closer look at it. The performance increase is awesome indeed and the core update is hanging above us as well. @bertmelis Can you join us here, as the AsyncTCP is mainly your domain? |
was meant only half-serious. Sorry for triggering the stir up. Actually, I just wanted to integrate modbus into openDTU, which proved harder than expected as openDTU is depending on mathieucarbou/AsyncTCP while eModbus is depending on me-no-dev/AsyncTCP. I didn't found a better solution as just forking eModbus and changing the dependency to mathieucarbou/AsyncTCP (which worked flawlessly for my limited use-case). |
I don't mind changing the dependency but there are many forks of AsyncTCP (and ESPAsyncTCP for ESP8266). Preferably, I would just remove the automatic dependency and let the user decide. |
Like you did for the mqtt client I remember;-) |
Looks like we are living in a very small village here 🤣 |
OpenDTU is extended by a Modbus server.
The Modbus server serves TCP at port 502.
provides the OpenDTU aggregated data from all registered inverters.
The webapp is extended by Modbus configuration and info views.
Modbus can be enabled/ disabled. Only a minimal subset of DTUPro/ OpenDTU aggregated data
is currently supported.
Main intention was to have an easy feed to my HomeAssistant installation that already monitors
and controls my other inverters by Modbus.
The OpenDTU Modbus sources were inspired by : https://github.com/ArekKubacki/OpenDTU.
See #582 for the orignal pull request.
The Modbus library used for Modbus communication is: https://github.com/eModbus/eModbus.
Documentation for the library is here: https://emodbus.github.io/.
Hints
Get the firmware image
Here are some compiled firmware images:
Unzip the image before flashing to OpenDTU:
Most firmware images are untested as I do not own the hardware. Reports on working images are welcome.
Build the firmware image
To check out the pull request locally see https://docs.github.com/de/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally.
Compile following https://www.opendtu.solar/firmware/compile_vscode/ or https://www.opendtu.solar/firmware/compile_cli/.
You do not have to compile the webapp (see https://www.opendtu.solar/firmware/compile_webapp/) as an updated webapp is alread part of this pull request.
Modbus addresses
The SunSpec "total inverter" and SunSpec "power meter" addresses start at 40000. This is an address offset definition, don't subtract 40001 for holding register access. Here is a discussion #2134 covering the address definition.
There is currently no address documentation besides the source code. Please see https://github.com/tbnobody/OpenDTU/pull/1893/files: