Skip to content

Commit 39956f8

Browse files
committed
Modbus Status
Modbus statistics on separate page, significant code optimization.
1 parent 93f2740 commit 39956f8

13 files changed

+572
-299
lines changed

README.md

Lines changed: 74 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,13 @@ Allows you to connect your Modbus RTU slaves (such as sensors, energy meters, HV
77

88
Change settings of your Arduino-based Modbus RTU to Modbus TCP/UDP gateway via web interface (settings are automatically stored in EEPROM).
99

10-
Screenshots of the web interface:
11-
12-
<img src="/pics/modbus1.png" alt="01" style="zoom:100%;" />
13-
14-
<img src="/pics/modbus2.png" alt="02" style="zoom:100%;" />
15-
16-
<img src="/pics/modbus3.png" alt="03" style="zoom:100%;" />
17-
18-
<img src="/pics/modbus4.png" alt="04" style="zoom:100%;" />
19-
20-
<img src="/pics/modbus5.png" alt="05" style="zoom:100%;" />
21-
2210
## What are the technical specifications?
2311

2412
* slaves are connected via RS485 interface
2513
* master(s) are connected via ethernet interface
2614
* up to 247 Modbus RTU slaves
27-
* up to 8 TCP/UDP sockets for Modbus TCP/UDP masters and for the web interface
15+
* up to 7 Modbus TCP masters
16+
* unlimited number of Modbus UDP masters
2817
* RS485 interface protocols:
2918
- Modbus RTU
3019
* Ethernet interface protocols:
@@ -34,10 +23,81 @@ Screenshots of the web interface:
3423
- Modbus RTU over UDP
3524
* supports broadcast (slave address 0x00) and error codes
3625
* supports all Modbus function codes
37-
* settings can be changed via web interface, stored in EEPROM
3826
* diagnostics and Modbus RTU scan via web interface
3927
* optimized queue for Modbus requests (queue will accept only one requests to non-responding slaves)
28+
* settings can be changed via web interface, they are stored in EEPROM
29+
* user settings are retained during firmware upgrade (only in case of major VERSION change, Arduino loads factory defaults).
30+
31+
<img src="/pics/modbus1.png" alt="01" style="zoom:100%;" />
32+
33+
### System Info:
34+
35+
**Load Default Settings**. Loads default settings (see DEFAULT_CONFIG in arduino-modbus-rtu-tcp-gateway.ino)
36+
37+
**Reboot**.
38+
39+
**Generate New MAC**. Generate new MAC address. Fisrt 3 bytes are fixed 90:A2:DA, remaining 3 bytes are true random.
40+
41+
<img src="/pics/modbus2.png" alt="02" style="zoom:100%;" />
42+
43+
### Modbus Status:
44+
45+
**Run Time**.
46+
47+
**Modbus Statistics**.
48+
49+
**Requests Queue**.
50+
51+
**Modbus TCP/UDP Masters**.
52+
53+
**Modbus Slaves**.
54+
55+
**Scan Slaves**.
56+
57+
<img src="/pics/modbus3.png" alt="03" style="zoom:100%;" />
58+
59+
### IP Settings:
60+
61+
**Auto IP**. Only if ENABLE_DHCP. Once enabled, Arduino will receive IP, gateway, subnet and DNS from the DHCP server.
62+
63+
**Static IP**.
64+
65+
**Submask**.
66+
67+
**Gateway**.
68+
69+
**DNS**. Only if ENABLE_DHCP.
70+
71+
<img src="/pics/modbus4.png" alt="04" style="zoom:100%;" />
72+
73+
### TCP/UDP Settings:
74+
75+
**Modbus TCP Port**.
76+
77+
**Modbus UDP Port**.
78+
79+
**Web Port**.
80+
81+
**Modbus Mode**. Modbus TCP/UDP or Modbus RTU over TCP/UDP.
82+
83+
<img src="/pics/modbus5.png" alt="05" style="zoom:100%;" />
84+
85+
### RS485 Settings:
86+
87+
**Baud Rate**.
88+
89+
**Data Bits**.
90+
91+
**Parity**.
92+
93+
**Stop Bits**.
94+
95+
**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.
96+
97+
**Response Timeout**.
4098

99+
**Attempts**.
100+
41101
## How can I build it myself?
42102
Get the hardware (cheap clones from China are sufficient) and connect together:
43103

README.md.backup

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
# arduino-modbus-rtu-tcp-gateway
2+
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.
3+
4+
## What is it good for?
5+
6+
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!
7+
8+
Change settings of your Arduino-based Modbus RTU to Modbus TCP/UDP gateway via web interface (settings are automatically stored in EEPROM).
9+
10+
<img src="/pics/modbus4.png" alt="04" style="zoom:100%;" />
11+
12+
<img src="/pics/modbus5.png" alt="05" style="zoom:100%;" />
13+
14+
## What are the technical specifications?
15+
16+
* slaves are connected via RS485 interface
17+
* master(s) are connected via ethernet interface
18+
* up to 247 Modbus RTU slaves
19+
* up to 7 Modbus TCP masters
20+
* unlimited number of Modbus UDP masters
21+
* RS485 interface protocols:
22+
- Modbus RTU
23+
* Ethernet interface protocols:
24+
- Modbus TCP
25+
- Modbus UDP
26+
- Modbus RTU over TCP
27+
- Modbus RTU over UDP
28+
* supports broadcast (slave address 0x00) and error codes
29+
* supports all Modbus function codes
30+
* diagnostics and Modbus RTU scan via web interface
31+
* optimized queue for Modbus requests (queue will accept only one requests to non-responding slaves)
32+
* settings can be changed via web interface, they are stored in EEPROM
33+
* user settings are retained during firmware upgrade (only in case of major VERSION change, Arduino loads factory defaults).
34+
35+
<img src="/pics/modbus1.png" alt="01" style="zoom:100%;" />
36+
37+
### System Info:
38+
39+
**Load Default Settings**. Loads default settings (see DEFAULT_CONFIG in arduino-modbus-rtu-tcp-gateway.ino)
40+
41+
**Reboot**.
42+
43+
**Generate New MAC**. Generate new MAC address. Fisrt 3 bytes are fixed 90:A2:DA, remaining 3 bytes are true random.
44+
45+
<img src="/pics/modbus2.png" alt="02" style="zoom:100%;" />
46+
47+
### Modbus Status:
48+
49+
**Run Time**.
50+
51+
**Modbus Statistics**.
52+
53+
**Requests Queue**.
54+
55+
**Modbus TCP/UDP Masters**.
56+
57+
**Modbus Slaves**.
58+
59+
**Scan Slaves**.
60+
61+
<img src="/pics/modbus3.png" alt="03" style="zoom:100%;" />
62+
63+
### IP Settings:
64+
65+
**Auto IP**. Only if ENABLE_DHCP. Once enabled, Arduino will receive IP, gateway, subnet and DNS from the DHCP server.
66+
67+
**Static IP**.
68+
69+
**Submask**.
70+
71+
**Gateway**.
72+
73+
**DNS**. Only if ENABLE_DHCP.
74+
75+
### TCP/UDP Settings:
76+
77+
**Modbus TCP Port**.
78+
79+
**Modbus UDP Port**.
80+
81+
**Web Port**.
82+
83+
**Modbus Mode**. Modbus TCP/UDP or Modbus RTU over TCP/UDP.
84+
85+
### RS485 Settings:
86+
87+
**Baud Rate**.
88+
89+
**Data Bits**.
90+
91+
**Parity**.
92+
93+
**Stop Bits**.
94+
95+
**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.
96+
97+
**Response Timeout**.
98+
99+
**Attempts**.
100+
101+
## How can I build it myself?
102+
Get the hardware (cheap clones from China are sufficient) and connect together:
103+
104+
* Arduino Nano, Uno or Mega (and possibly other). On Mega you have to configure Serial in ADVANCED SETTINGS in the sketch.
105+
* W5100, W5200 or W5500 based Ethernet shield (for Nano, I recommend W5500 Ethernet Shield from RobotDyn)
106+
* TTL to RS485 module:
107+
- with hardware automatic flow control (recommended)<br>
108+
Arduino <-> Module<br>
109+
Tx1 <-> Tx<br>
110+
Rx0 <-> Rx
111+
- with flow controlled by pin (such as MAX485 module)<br>
112+
Arduino <-> MAX485<br>
113+
Tx1 <-> DI<br>
114+
Rx0 <-> RO<br>
115+
Pin 6 <-> DE,RE
116+
117+
118+
Here is my setup:
119+
Terminal shield + Arduino Nano + W5500 eth shield (RobotDyn) + TTL to RS485 module (HW automatic flow control)
120+
<img src="/pics/HW.jpg" alt="01" style="zoom:100%;" />
121+
122+
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 :-)
123+
124+
## Where can I learn more about Modbus protocols?
125+
126+
https://en.wikipedia.org/wiki/Modbus
127+
128+
https://modbus.org/specs.php
129+
130+
http://www.simplymodbus.ca/FAQ.htm
131+
132+
## Can I use just the web interface for my own project?
133+
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.
134+
135+
The key to success is:
136+
137+
* use StreamLib https://github.com/jandrassy/StreamLib
138+
* use F macros for your HTML code
139+
* use for() loop for repetitive code
140+
* use POST method (rather than GET) for your webforms, see this tutorial https://werner.rothschopf.net/202003_arduino_webserver_post_en.htm
141+
142+
Big thanks to the authors of these libraries and tutorials!
143+
144+
## Limitations
145+
146+
#### Portability
147+
148+
The code was tested on Arduino Nano, Uno and Mega, ethernet chips W5100 and W5500. It may work on other platforms, but:
149+
150+
* 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)
151+
* The restart function will also work only on Arduino.
152+
153+
#### Ethernet socket
154+
155+
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.
156+
157+
#### Memory
158+
159+
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:
160+
161+
* #define ENABLE_DHCP will allow you to set "Auto IP" via DHCP in the IP settings web interface. Leased IP is automatically renewed.
162+
163+
<img src="/pics/modbus6.png" alt="06" style="zoom:100%;" />
164+
165+
* #define ENABLE_EXTRA_DIAG shows extra info on "Current status" page: per socket diagnostics, run time counter, ethernet data counter.
166+
167+
<img src="/pics/modbus1x.png" alt="01x" style="zoom:100%;" />
168+
169+
## Version history
170+
171+
For version history see:
172+
173+
https://github.com/budulinek/arduino-modbus-rtu-tcp-gateway/blob/master/arduino-modbus-rtu-tcp-gateway/arduino-modbus-rtu-tcp-gateway.ino#L27

0 commit comments

Comments
 (0)