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.
Allows you to connect your Modbus RTU slaves (such as sensors, energy meters, HVAC devices) to Modbus TCP/UDP masters (such as monitoring systems, home automation systems). You do not need commercial Modbus gateways. Arduino (with an ethernet shield and a cheap MAX485 module) can do the job!
Change settings of your Arduino-based Modbus RTU to Modbus TCP/UDP gateway via web interface (settings are automatically stored in EEPROM).
- slaves are connected via RS485 interface
- master(s) are connected via ethernet interface
- up to 247 Modbus RTU slaves
- up to 7 Modbus TCP masters
- unlimited number of Modbus UDP masters
- RS485 interface protocols:
- Modbus RTU
- Ethernet interface protocols:
- Modbus TCP
- Modbus UDP
- Modbus RTU over TCP
- Modbus RTU over UDP
- supports broadcast (slave address 0x00) and error codes
- supports all Modbus function codes
- diagnostics and Modbus RTU scan via web interface
- optimized queue for Modbus requests (queue will accept only one requests to non-responding slaves)
- settings can be changed via web interface, they are stored in EEPROM
- user settings are retained during firmware upgrade (only in case of major VERSION change, Arduino loads factory defaults).
Load Default Settings. Loads default settings (see DEFAULT_CONFIG in arduino-modbus-rtu-tcp-gateway.ino)
Reboot.
Generate New MAC. Generate new MAC address. Fisrt 3 bytes are fixed 90:A2:DA, remaining 3 bytes are true random.
Run Time.
Modbus Statistics.
Requests Queue.
Modbus TCP/UDP Masters.
Modbus Slaves.
Scan Slaves.
Auto IP. Only if ENABLE_DHCP. Once enabled, Arduino will receive IP, gateway, subnet and DNS from the DHCP server.
Static IP.
Submask.
Gateway.
DNS. Only if ENABLE_DHCP.
Modbus TCP Port.
Modbus UDP Port.
Web Port.
Modbus Mode. Modbus TCP/UDP or Modbus RTU over TCP/UDP.
Baud Rate.
Data Bits.
Parity.
Stop Bits.
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 see Response Timeouts in stats.
Response Timeout.
Attempts.
Get the hardware (cheap clones from China are sufficient) and connect together:
- Arduino Nano, Uno or Mega (and possibly other). On Mega you have to configure Serial in ADVANCED SETTINGS in the sketch.
- W5100, W5200 or W5500 based Ethernet shield (for Nano, I recommend W5500 Ethernet Shield from RobotDyn)
- TTL to RS485 module:
- with hardware automatic flow control (recommended)
Arduino <-> Module
Tx1 <-> Tx
Rx0 <-> Rx - with flow controlled by pin (such as MAX485 module)
Arduino <-> MAX485
Tx1 <-> DI
Rx0 <-> RO
Pin 6 <-> DE,RE
- with hardware automatic flow control (recommended)
Here is my setup: Terminal shield + Arduino Nano + W5500 eth shield (RobotDyn) + TTL to RS485 module (HW automatic flow control)
Download this repository (all *.ino files) and open arduino-modbus-rtu-tcp-gateway.ino in Arduino IDE. Download all required libraries (both are available in "library manager"). If you want, you can check the default factory settings (can be later changed via web interface) and advanced settings (can only be changed in sketch). Compile and upload your program to Arduino. Connect your Arduino to ethernet, connect your Modbus RTU slaves to MAX485 module. Use your web browser to access the web interface on default IP http://192.168.1.254 Enjoy :-)
https://en.wikipedia.org/wiki/Modbus
http://www.simplymodbus.ca/FAQ.htm
Feel free to use this sketch as a template for a web interface within your own project. Look into the main file (arduino-modbus-rtu-tcp-gateway.ino) for how settings are stored in and loaded from EEPROM during boot. Ethernet interface and Webserver is started via function in 01-interfaces.ino. All other functions related to the web server (reading from clients, sending pages to clients) can be found in separate files (04-webserver.ino , 05-pages.ino ). Feel free to adjust them.
The key to success is:
- use StreamLib https://github.com/jandrassy/StreamLib
- use F macros for your HTML code
- use for() loop for repetitive code
- use POST method (rather than GET) for your webforms, see this tutorial https://werner.rothschopf.net/202003_arduino_webserver_post_en.htm
Big thanks to the authors of these libraries and tutorials!
The code was tested on Arduino Nano, Uno and Mega, ethernet chips W5100 and W5500. It may work on other platforms, but:
- The pseudorandom generator (for random MAC) is seeded through watch dog timer interrupt - this will work only on Arduino (credits to https://sites.google.com/site/astudyofentropy/project-definition/timer-jitter-entropy-sources/entropy-library/arduino-random-seed)
- The restart function will also work only on Arduino.
The default Ethernet.h library determines MAX_SOCK_NUM by microcontroller RAM (not by Ethernet chip type). So if you use W5500 (which has 8 sockets available) on Arduino Nano, only 4 sockets will be used. If you want to force the library to use 8 sockets, redefine MAX_SOCK_NUM in advanced settings in the sketch.
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 features in the main sketch by uncommenting:
- #define ENABLE_DHCP will allow you to set "Auto IP" via DHCP in the IP settings web interface. Leased IP is automatically renewed.
- #define ENABLE_EXTRA_DIAG shows extra info on "Current status" page: per socket diagnostics, run time counter, ethernet data counter.
For version history see: