budulinek / arduino-modbus-rtu-tcp-gateway

Arduino-based Modbus RTU to Modbus TCP/UDP gateway with web interface. Allows you to connect Modbus RTU slaves (such as sensors, energy meters, HVAC devices) to Modbus TCP/UDP masters (such as home automation systems). You can adjust settings through web interface.
GNU General Public License v3.0
165 stars 48 forks source link
arduino arduino-eeprom arduino-webserver home-assistant loxone modbus-rtu modbus-tcp modbus-udp

Modbus RTU ⇒ Modbus TCP/UDP Gateway

Arduino-based Modbus RTU to Modbus TCP/UDP gateway with web interface. Allows you to connect Modbus RTU slaves (such as sensors, energy meters, HVAC devices) to Modbus TCP/UDP masters (such as home automation systems). You can adjust settings through web interface.

What is it good for?

Allows you to connect your Modbus devices (such as sensors, energy meters, HVAC devices) to monitoring systems and home automation systems (such as Loxone, Home Assistant, OpenHAB and other). You do not need commercial Modbus gateways. Arduino (with an ethernet shield and a cheap TTL to RS485 module) can do the job! You can easily change settings of your Arduino Modbus gateway via web interface, your settings are automatically stored in EEPROM.

Technical specifications

Hardware

Get the hardware (cheap clones from China are sufficient) and connect together:

fritzing

Here is my HW setup: Terminal shield + Arduino Nano + W5500 ethernet shield (from RobotDyn, no longer available) + TTL to RS485 module (automatic flow control)

HW

Firmware

You can either:

Connect your Arduino to ethernet and use your web browser to access the web interface on default IP: http://192.168.1.254 Enjoy :-)

Settings

System Info

modbus1

EEPROM Health. Keeps track of EEPROM write cycles (this counter is persistent, never cleared during factory resets). Replace your Arduino once you reach 100 000 write cycles (with 6 hours EEPROM_INTERVAL you have more than 50 years lifespan).

Ethernet Sockets. Max number of usable sockets. See Limitations bellow. One socket is reserved for Modbus UDP, remaining sockets are shared between Modbus TCP and WebUI.

Ethernet Chip. Wiznet chip on the ethernet shield.

MAC Address. First 3 bytes are fixed 90:A2:DA, remaining 3 bytes are random. You can also set manual MAC in IP Settings.

Modbus Status

modbus2

Modbus RTU Request. Send a Modbus RTU request directly from web UI. First byte (slave address) and second byte (function code) are mandatory, no need to calculate CRC. Gateway remembers last request for your convenience.

Modbus RTU Response. Shows response to the last Modbus request. Response is shown in Modbus RTU format (incl. CRC). Due to memory limitations, only few initial bytes are shown.

Run Time.** Days, hours, minutes and seconds since boot or stat reset.

RTU Data.** Counter for bytes sent and received via RS485.

Ethernet Data.** Counter for bytes sent and received via Modbus TCP/UDP.

Requests Queue. Monitors internal request queue (buffer). The limits for bytes and for the number of requests stored in the queue can be configured in advanced settings.

Modbus Statistics.

Modbus Masters. Shows IP addresses for Modbus TCP or UDP masters:

Modbus Slaves. Shows the slave address (in hex) and the last status (error) for all slaves who responded to a slave scan or who were recipients of a Modbus request.

Scan Slaves. An attempt is made to find Modbus RTU slaves connected to the RS485 interface:

IP Settings

modbus3

MAC Address. Change MAC address. "Randomize" button will generate new random MAC (first 3 bytes fixed 90:A2:DA, last 3 bytes will be random).

Auto IP.* Once enabled, Arduino will receive IP, gateway, subnet and DNS from the DHCP server.

Static IP. Set new static IP address. Gateway automatically redirect the web interface to the new IP.

Submask.

Gateway.

DNS.*

TCP/UDP Settings

modbus4

Modbus TCP Port.

Modbus UDP Port. Can be the same as Modbus TCP Port.

Web Port. Gateway automatically redirect the web interface to the new Web UI port.

Modbus Mode. Modbus TCP/UDP or Modbus RTU over TCP/UDP. Be aware that "Modbus RTU over TCP/UDP" is not a standard Modbus protocol. In this mode, the gateway expects to recieve Modbus RTU request (incl. CRC) via TCP or UDP. Responses from RS485 line are forwarded as they are in Modbus RTU format (incl. CRC).

Modbus TCP Idle Timeout. Amount of time that a connection is always held alive (open) with no incoming traffic from a Modbus TCP master. This timeout should be longer than polling period (scan rate) set on your Modbus TCP master device.

RTU Settings

modbus5

Baud Rate. Choose baud rate from a pre-aranged list. The list can be adjusted in advanced settings.

Data Bits. Data bits available on arduino HW serial line: 5, 6, 7, 8.

Parity. Parity options available on arduino HW serial line: None, Even, Odd.

Stop Bits. 1 or 2 stop bits.

Inter-frame Delay. Delay (ms) between the end of reading Modbus RTU frame and writing new frame. Higher Frame Delay is needed for RS485 modules with automatic flow control. Increase Frame Delay if you have very short polling period and Response Timeouts in stats. The minimum Inter-frame Delay is calculated from baud rate according to Modbus standards.

Response Timeout. Timeout for Modbus RTU response. Increase Response Timeout if you see Response Timeouts in Modbus statistics.

Attempts. Number of attempts before error (Code 11) is sent back to the Modbus TCP/UDP master.

Tools

modbus6

Load Default Settings. Loads default settings (see DEFAULT_CONFIG in advanced settings). MAC address is retained.

Reboot.

Integration

This gateway adheres to the Modbus protocol specifications, so you can use it to connect any compliant Modbus RTU slave (Modbus device) with any compliant Modbus TCP/UDP master (such as home automation system). Here is a quick overview how you can integrate the gateway into the most popular home automation systems:

Loxone

Loxone Miniserver (both the current Miniserver and the old Miniserver Gen. 1) supports:

Modbus TCP. You can use this Arduino Modbus gateway as a fully-fledged replacement of the Loxone Modbus Extension. In Loxone Config, go to Network Perifery > Add Network Device > Modbus Server. Specify IP and port of your gateway. Timeout [ms] should be longer than Response Timeout set in the gateway.

In the next step, add individual Modbus devices. Adding and configuring Modbus devices connected to your Arduino Modbus gateway ("Modbus Server" in Loxone Config) is identical to configuring devices connected through Loxone Modbus Extension. You can either:

Please note that the implementation of Modbus RTU (= Loxone Modbus Extension) and Modbus TCP (= Arduino Modbus gateway connected as "Modbus Server") in Loxone has some restrictions:

Modbus UDP. If you want to avoid the above mentioned limitations, you can use Modbus UDP as a communication protocol between Loxone and this Arduino Modbus gateway. See Loxone_ModbusUDP.md on how to implement Modbus UDP in Loxone with Virtual UDP output and Virtual UDP input.

Home Assistant

Supports Modbus TCP, Modbus UDP, Modbus RTU over TCP. Follow this official tutorial.

OpenHAB

Supports Modbus TCP. Follow this official tutorial.

Node-RED

Supports Modbus TCP. Import and configure the node-red-contrib-modbus package. You can use Node RED as:

Limitations and Known Issues

Portability

The code was tested on Arduino Nano, Uno and Mega, ethernet chips W5100 and W5500. It may work on other platforms, but:

Ethernet sockets

The number of used sockets is determined (by the Ethernet.h library) based on microcontroller RAM. Therefore, even if you use W5500 (which has 8 sockets available) on Arduino Nano, only 4 sockets will be used due to limited RAM on Nano.

Memory

Not everything could fit into the limited flash memory of Arduino Nano / Uno. If you have a microcontroller with more memory (such as Mega), you can enable extra settings in the main sketch by defining ENABLE_DHCP and/or ENABLE_EXTENDED_WEBUI in advanced settings.

Links and Credits

https://en.wikipedia.org/wiki/Modbus

https://modbus.org/specs.php

http://www.simplymodbus.ca/FAQ.htm

https://github.com/jandrassy/StreamLib

https://werner.rothschopf.net/202003_arduino_webserver_post_en.htm

Big thanks to the authors of these libraries and tutorials!

Version History

For version history see arduino-modbus-rtu-tcp-gateway.ino