Modbus TCP - which Hardware?

Hi everyone,

I am pretty new to Home assistant, therefore sorry if this question is bad… However I want to to hook up my Modbus TCP enabled heating system to talk to my home assistant Raspberry Pi 4B with 4Gb of Ram. But for the live of me, I cannot find a description of the Hardware setup. There is plenty of guides on software, for example the Home assistant modbus integration, someone who setup his heating system with modbus tcp (in german), or general modbus talk with the RPi. But for the live of me, I could not find anyone who talked about how the Modbus was physically hooked up to the RPi. I think I do miss something, could someone tell me how I can hook up my RPi 4 to modbus? Do I need extra hardware? Or a dongle?

Any advice is appreciated

Best,
Phil

A very sensible question.

Executive summary: you don’t need any hardware, just the IP address and port number of the heating system/controller, which should be connected to your local network along with your Raspberry Pi.

Detail:

To work with a Modbus device we need to talk to it and either ask what is in a register or try and write a value to a register. Devices are mostly slaves, where they sit there and wait to be spoken to. As it is a ‘bus’ one device (and one only) can be a master and initiate the conversation. Many slave devices can sit on the bus, each with an ID address, and wait to be spoken to. Speaking therefore needs an address (ID) a register and type of register, and if writing, the data to write.

All this is designed to run over some physical communication, and for industrial controllers that is usually a serial coms link. The basic one is RS232, but these days most devices are designed to use RS485. This is two wires (plus a ground if you insist) where the wires can be +/- or -/+, and the switching between the two states provides the basic binary communication. RS485 is a standard that operates over long distances for, mostly, industrial use. Devices are all connected to two wires in a long daisy chain.

Modbus started out by being over RS485, and most devices still use this standard. Since ethernet networks have become common, Modbus can also operate over TCP (the communication protocol used in computer networks). Modbus therefore now comes in two flavours - Modbus over serial (dedicated pair of wires) and Modbus over TCP (network/internet).

The good news is that, as long as you have the right hardware, the software usually looks after the rest.
So - for serial communication a device will have a serial port to which we need to connect our computer via a similar serial port. If both serial ports use RS485, and we get the baud rate, parity, stop bits and the wires round the right way, it will all work. Software (say HA) on my computer can format the correct things to say in Modbus to ask what is in register 1001 on device 1, push this to my computer serial port driver so that the serial port waggles the lines up and down correctly, the device I am talking to can read the waggles, reformat this into Modbus, and the software running on the device recognises this as ‘please tell me what is in register 1001’ and it response in reverse.

All well and good, but what about Modbus over TCP I hear you ask.

Well, most home computers these days don’t have old type RS232 serial ports. Bit of a waste as no one uses them. So most of us buy little USB dongles. USB ports are actually serial ports, but almost everything uses them now and all we have to do is plug them in. Purchase a USB to RS485 dongle/device/thingy adaptor and your computer can be set up with a serial port that talks RS485. Wire that to the Modbus device RS485 port, and bingo, Bob’s your uncle!
All computers though do have TCP ports - the network port. This is either hard wired or WiFi, but this can be used to carry Modbus commands, just over TCP rather than RS485 serial.

How does this work? Well you need no adaptor on the computer provided you have a network connection. Your computer can talk over TCP to any IP address on the network. The practical bit now sits with the device you are trying to talk to, since that has to expose Modbus to the world either via a serial port, or via a TCP network port eg over WiFi. There has to be an address, both IP and port, where Modbus commands can be received.

I have a solar inverter that is a Modbus device. It has a RS485 port, into which usually sits a data logging stick. The stick talks Modbus over RS485, and then talks over the network to the internet.

To connect to my solar inverter device I can use a USB to RS485 dongle on my HA and a pair of wires from the dongle to the RS485 port

OK but the inverter is in the garage, so I bought an RS485 to ethernet adaptor instead. The adaptor RS485 port is wired to the inverter RS485 port, the adaptor ethernet port is plugged into my router/network, and my HA talks over TCP.

The data logging stick I have is effectively also a RS485/TCP dongle. Inside the stick is a server port that can communicate Modbus TCP. To use that all I need to know is the IP address of the stick (set up as static) and the port of the inbuilt server.

To make life easier I do all my Modbus work on Node-Red. With Node-Red I don’t need to keep rebooting HA every the I change the config etc. Getting these things to work can be fiddly. Node red is, I think, also a great deal simpler to use.

I have HA running on an odroid with Node-Red as an add-on, but I also have a Raspberry Pi running Node-Red for development. Here is the (incredibly complex) code I need to connect Modbus to my inverter data logging stick, over TCP.

No hardware, no wires.

Modbus getter node (from the node-red-contrib-modbus set) - nothing else required.

Unit ID is the ‘address’ of the slave unit. Most likely to be 1 but do check your documentation. Even though this is virtual over a TCP network, the rules for uniquely individually addressed slaves still apply.
FC type, register address, quantity are defined by your heating device controller.

The working bit is the server. This is used to create a config node.

Set the Type up as TCP
Set the host as IP address of your controller, and the port as given in documentation
TCP type default works for me
Everything else was as default and generally only change things if they don’t work.

When enabled correctly the Modbus node will show ‘active’ if connected and working.

Should take all of 5 minutes to setup.

20 Likes

wow thanks for the detailed write up, that is very detailed and informative!

However, one more question though: Ideally I would like to to directly connect a USB to RS485 converter to my Raspberry Pi on which Home Assistant IOS is running. Is that also possible, or is such a direct integration not possible?

There are a variety of (quite cheap) USB dongles out there. I have tried a couple of USB to RS232 adaptors, which worked. I have HA on Odroid, and have plugged in a USB WiFi dongle, so I know that you can connect and find USB devices on the USB port(s). Same for Raspberry Pi. The fun part is in getting everything to line up and connect, but it can certainly be done though I am no expert. I went down the TCP route and do everything over the network connection with my adaptors at the other end.
Various posts talk about difficulties with getting some of the cheaper USB dongles to work as there are several chip sets out there and some of them have ‘issues’. Just have to buy one and try it.

1 Like

Cool, thanks for your input. I will!

Okay as it turns out, the Solvis SC3 controller (that is the heater brand) already is hooked up to my WIFI network. Therefore I could simply directly send the Modbus TCP packages. No connector, dedicated hardware or anything else needed… Sometimes it can be that simple :smiley:

Yes indeed, if you are already WiFi connected you should find you can use Modbus without any hardware.

I’m almost totally stumped with the German, however I did use Google Translate on your link (https://smarthome.ms1.de/projekte/solvis-heizung/
which suggest that a ‘remote’ is required to do the physical connection and to offer Modbus over TCP, but if you have that then you are almost home and dry.
The port seems to be 502, and the post even has all the yaml config as well, and I found the Solvis Modbus protocol document at https://solvis-files.s3.eu-central-1.amazonaws.com/seiten/produkte/solvisremote/Download/SolvisRemote+Modbus+Spezifikationen+201906.pdf

Clearly an enlightened and helpful manufacturer. Good luck with your project.

Yeah, the guy uses a Solvis SC2 controller, which requires said ‘remote’. I on the other hand have a SC3 controller, which already features this functionality.
But you are right, I configured it exactly as mentioned on that page, including port 502 :smiley: .

Just one additional Info:
If you have an Solvis Remote and a Heating Controller SC2 before Hardware Version MA205 you can use
GitHub - Lurchi70/ha-solvis-heating: Integrate your Solvis heating (e.g. SolvisMax) via solvis remote extension into home assistant as integration.

1 Like

This integration is running absolutely smoothly. Thanks to Lurchi70 I am finally I am able to integrate my Solvis Max SC2 (MA 150) - thank you so much!

You seem to have a lot of experience with Modbus/RS485 :sunglasses:

I’m looking for or a while now how to get my SMA inverter integrated into Home Assistant and Modbus seems to be the only way. SMA has an RS485 module. Reading this thread it seems I can use an RS485 to USB dongle or a Wifi enabled RS485 adaptor. My HA server is about 30m away from the inverter so I could run a cable connection to HA. If on the other hand the Wifi enabled adapters are reliable that could be an attractive workaround.

Do you have experience with both types? Would you mind sharing what exact adapters you’re using/have tested so I can consider this. Thanks

Firstly, I am not an expert and I have limited experience with Modbus, so I can only offer minimal support.

However, my ‘knowledge’ is free so I am happy to respond and you are more than welcome to pick over my reply and take any or none of it as you please!

To allow any two devices to communicate (using Modbus as a protocol) we need two adaptors (one at each end) and something connecting them. Modbus was designed to use serial communication as RS485, and many devices such as inverters have an interface that presents RS485 (serial). The ‘de facto’ method of connection is therefore to have an RS485 serial adaptor connected to HA, and connect both adaptors with a pair of wires. After that, it is a question of making sure the wires are the right way around, the Baud rates are set correctly, and any terminating resistors etc are in place.

It rather depends on what device you run HA as to what ‘adaptors’ you can use. I have HA on an Odroid Blue, and I do not want to open the case and poke about with the inside trying to connect something (my solar system cost many thousands of pounds, why try and save a few euros with a ‘do it yourself’ bit of kit that could fry both HA and the inverter?)

So, my Odroid will only take USB adaptors, and these are available as USB to RS485 converters. Good news is that USB is a serial system, so not much is required to get USB to RS485. Bad news is that there are different chipsets in use, and some USB devices are noted for being temperamental or difficult to get working. It is a case of buy something and seeing if it works. Not the cheapest, and not the most expensive…good place for a trial run with a 30 metre patch cable from study to garage!

RS485 compliant devices are very good at working over long distances and (electrically) noisy conditions. For short distances of a few metres, then any wiring will do, but for 30m the wiring would have to be a twisted pair (something like CAT cabling). The use of terminating resistors becomes important - some devices include them, others have dip switches that can turn them on/off. USB devices are commonly very simple, whereas a better ‘industrial’ adaptor, although costing more, will have a dedicated power supply, better circuitry, and additional features such as in-built terminating resistors. For long distances, pull-up resistors may be required also, at which point RS485 starts looking more and more like hard work.

It should also be remembered that you have now introduced a physical wire between your HA device (nice little box operating on 5 volts) and your inverter (bigger box operating on 230 v AC and 300-600 v DC) The wire is also a very nice arial for picking up RF noise (I won’t talk about lightening, but all old UK standard telephone wiring had a lightening arrestor at the master socket where the street cable enters the house…)

Early on I decided I did not want to use the serial approach, but rather TCP.

Modbus as standard is a serial communication (RTU) but it is now supported over ethernet. Modbus over TCP has several advantages. The Modbus package is simplified and re-packaged, and then managed by TCP which includes error checking and packet recovery. Modbus over TCP works very well, and there are many ways of connecting devices.
First plus is that HA already has a TCP adaptor since it is connected to the LAN. HA is already Modbus over TCP aware, and I use Node-RED which also works with Modbus over TCP. Basically, no adaptor required at the HA end!
Next question, how to connect the inverter. Some, enlightened manufactures, are now offering Modbus over TCP, where the device already has an ethernet (cable or WIFI) connection available. In this case, no adaptor is required - just connect to the LAN and set up the IP addresses.
My inverter only offers an RS485 port, so I bought a RS485 to Ethernet adaptor. I use a ‘semi industrial’ unit. It goes on the wall in the garage. It connects to ethernet, and presents a web-page based configuration setup. This is a USR-TCP232-410s

The unit is the middle one in the box - I am no longer using the right hand unit, so the serial wire goes direct to the inverter and I have a terminating resistor at the unit (the inverter has this inbuilt).

To connect the RS485/ethernet adaptor to my LAN (ie back to the router) I first used a power-line adaptor, one in the house and one in the garage. I have tried several different makes, and some work better than others, but in the end they do suffer from noise interference and drop-out. I have tried WIFI connectors to get ethernet to the garage, but again the building walls precluded a fully reliable connection. Inverters are, of their nature, a big source of electrical and RF interference. In the end I ran a CAT cable from the house to the garage, and connected one router port out to an ethernet switch in the garage. This setup allows me to plug in more than one adaptor (I have a RS232 adaptor to talk to my batteries, and a CAN-bus adaptor to monitor the battery-inverter CAN-bus link).

The benefit of having hard-wired ethernet to the Inverter is that I have a great and very stable connection. The wiring is protected (the ethernet signals are not physically connected at either end, and the adaptor has isolation circuitry included). Noise is also less of a problem, as TCP manages error checking and package recovery.

In use, my HA is always talking to the adaptor connected to the inverter, however I can also use Node-RED on my Raspberry PI, so I have great flexibility. Rather than use a wire for just the single RS485 connection, I have a wire down which I can run many TCP ‘virtual’ connections.

As a note - any RS485 to Ethernet adaptor will convert TCP to RS485, but ONLY ‘Modbus Gateway’ adaptors will converter Modbus TCP to Modbus RTU. My adaptor (the USR-TCP232-410) is a low cost ‘industrial’ unit, but this particular model will do the necessary work to unpack the Modbus RTU package at the serial port and modify it for Modbus TCP (and vice versa). If you go down the TCP route, make sure that the adaptor will work fully with Modbus (RTU - TCP) and not just ‘RS485 to TCP’.

There are other manufacturers of such devices. I note that there are now WIFI devices (RS485 / Modbus to WIFI) from this manufacturer, but I have decided that a wire is always better than WIFI. If I was starting again I would probably have tried the WIFI version first, but with several units the use of cable/switch hub meets all of my needs without issue.

I hope this helps!

Good luck with your project.

1 Like

Thanks for that elaborate outline. Based on this I tend to flavor a TCP based solution and I’m with you re Wifi vs Ethernet. I had the foresight when building our house to pull Ethernet ports into all rooms - but the garage :slight_smile: but the cable run from the server rack to the garage is easy enough so that’s probably the way to go. From there it should be an easy run with the 3 serial wires to the RS485 piggyback module (from SMA) for my SMA inverter.

Hi, I have an USR-TCP232-410s device which I cannot get to communicate at all right now. Lots of trial and error. Can you please send me a screenshot of your RS485 config settings on the device and also a sample of your HA modbus integration config?
Thanks!

I would like to help but I am away from my system at the moment and don’t have access to all devices.

I can suggest that you check your LAN connection, and that you have access to the Web config page, and have set a fixed IP address.

Check that you are connected to the RS485 port (I assume that you are talking to an inverter over Modbus). Wires only A and B, and to the correct pins of the inverter RS485 comms port.

Baud rates must match, as well as parity and bits.

From memory there is a specific Modbus setting in the config page, and of course you need Modbus over TCP. Only this model is fully Modbus aware.

I vaguely recall this company USR having downloadable test software for loop back testing, which can help debug the TCP part.

For the actual use, I use Node-RED exclusively as it is so much easier to test and debug than HA yaml config. The Modbus nodes use a configuration node to connect to the device, and this will only show “connected” when working.

Of course it almost goes without saying but you have to send Modbus read commands with the correct register, type etc before you get anything back.

Ok, no worries. I just got lazy when I saw that you had the same device up and running. Did a bunch of initial tests directly from HA and that didn’t work.
Got a stand-alone working connection with QModMaster and a cheap USB-to-Modbus stick towards the Modbus device with just A/B wires.
The USR-TCP232-410s can be reached and configurated but the wording for some protocol specific features seems to differ between the USR-TCP232-410s, Testing applications and HA integration.
The USR-TCP232-410s also has a ground wire pin that I haven’t tried to connect yet. Perhaps it’s needed for that bridging device?
I can see that you are using it so that might be the culprit.

RS485 wiring for short distance can be very basic, but to avoid noise interference and signal reflection at the ends, you should use twisted pair. With 120 Ohm terminating resistors, but only where not already provided. The earth, if you wish, should be along with the signal wire but only connected at one end.

And yes, Modbus can take quite a bit of effort and debugging, so take one step at a time and use whatever testing software / loop back /secondary device you have.