Skip to content

Commit fc4ad86

Browse files
committed
simplify DHCP renew
1 parent b8a45f5 commit fc4ad86

File tree

4 files changed

+27
-39
lines changed

4 files changed

+27
-39
lines changed

README.md

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Modbus RTU ⇒ Modbus TCP/UDP 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.
2+
Arduino-based Modbus RTU to Modbus TCP/UDP gateway with web interface. Build your own Modbus gateway from off-the-shelf components (Arduino + ethernet shield + TTL to RS485 module). The gateway allows you to connect Modbus RTU slaves (sensors, energy meters, HVAC devices, etc.) to Modbus TCP/UDP masters (such as home automation systems). Once you have your Modbus gateway up and running, you can adjust its settings through web interface.
33

44
* [What is it good for?](#what-is-it-good-for)
55
* [Technical specifications](#technical-specifications)
@@ -23,7 +23,7 @@ Arduino-based Modbus RTU to Modbus TCP/UDP gateway with web interface. Allows yo
2323

2424
# What is it good for?
2525

26-
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.
26+
Allows you to connect your Modbus devices (sensors, energy meters, HVAC devices, etc.) to monitoring systems and home automation systems (such as Loxone, Home Assistant, OpenHAB and other). You do not need commercial Modbus gateways, build your own Modbus gateway from off-the-shelf components (Arduino + ethernet shield + TTL to RS485 module)! Once you have your Modbus gateway up and running, you can easily change its settings via web interface, your settings are automatically stored in EEPROM.
2727

2828
# Technical specifications
2929

@@ -60,23 +60,20 @@ Allows you to connect your Modbus devices (such as sensors, energy meters, HVAC
6060
- requests to responding slaves are prioritized over requests to non-responding slaves
6161
- queue size configured in advanced settings (sketch)
6262
* user settings:
63-
- can be changed via web interface (see screenshots bellow)
63+
- can be changed via web interface (see screenshots bellow), all web UI inputs have proper validation
6464
- stored in EEPROM
6565
- retained during firmware upgrade (only in case of major version change, Arduino loads factory defaults)
66-
- all web interface inputs have proper validation
67-
- factory defaults for user settings can be specified in advanced_settings.h
68-
- settings marked \* are only available if ENABLE_DHCP is defined in the sketch
69-
- settings marked \*\* are only available if ENABLE_EXTENDED_WEBUI is defined in the sketch
66+
- factory defaults for user settings can be changed in advanced_settings.h
7067
* advanced settings:
71-
- can be changed in sketch (advanced_settings.h)
68+
- can be changed in sketch before comnpilation (advanced_settings.h)
7269
- stored in flash memory
7370

7471
# Hardware
75-
Get the hardware (cheap clones from China are sufficient) and connect together:
72+
Get the hardware and connect together:
7673

7774
* **Arduino Nano, Uno or Mega** (and possibly other boards with ATmega chips).<br>On Mega you have to configure Serial in advanced settings in the sketch.
7875
* **Ethernet shield with WIZnet chip (W5100, W5200 or W5500)**.<br>The ubiquitous W5100 shield for Uno/Mega is sufficient. If available, I recommend W5500 Ethernet Shield. You can also use combo board MCU + ethernet (such as ATmega328 + W5500 board from Keyestudio).<br>ATTENTION: Ethernet shields with ENC28J60 chip will not work !!!
79-
* **TTL to RS485 module with an automatic flow direction control**.<br>You can buy cheap modules with MAX3485, MAX13487, SP485 or SP3485 chips (some of these modules are branded as "XY-017", "XY-485", "XY-G485", etc.) from Aliexpress and other marketplaces.<br>ATTENTION: Modules with MAX485 chip will work (use pin 6 for DE+RE), but are NOT recommended (no auto-direction, no ESD protection, no hot-swap protection) !!!
76+
* **TTL to RS485 module with an automatic flow direction control**.<br>You can buy cheap modules with MAX485, MAX3485, MAX13487, SP485 or SP3485 chips (some of these modules are branded as "XY-017", "XY-485", "XY-G485", etc.) from Aliexpress and other marketplaces.<br>ATTENTION: Some TTL module manufacturers invert TX and RX !!! If you experience problems, try to swap TXD and RXD.<br>ATTENTION: Avoid those ubiquitous MAX485 modules with manual direction control (DE and RE pins). They will work (use Arduino pin 6 for DE+RE), but are NOT recommended (no auto-direction, no ESD protection, no hot-swap protection) !!!
8077
* **External power supply**.<br>Use regulated 5V external power supply for both the Arduino (+ the ethernet shield) and the RS485 module.<br>ATTENTION: By using the 5V pin, you are bypassing Arduino's built-in voltage regulator and reverse-polarity protection curcuit. Make sure your external power supply does not exceed 5,5V !!!
8178

8279
<img src="pics/fritzing.png" alt="fritzing" />
@@ -131,9 +128,9 @@ Enjoy :-)
131128
* **Slave Responded with Error (Codes 1~8)**. Slave responded, but with an error. For the list of error codes see [Wikipedia](https://en.wikipedia.org/wiki/Modbus#Exception_responses).
132129
* **Gateway Overloaded (Code 10)**. Request queue is full (either the number of bytes stored or the number of requests stored). Request was dropped and the gateway responded with an error code 10.
133130
* **Slave Failed to Respond (Code 11)**. Slave is not responding. Response timeouts have passed, all attempts have failed. The gateway responded with an error code 11.
134-
* **Invalid TCP/UDP Request**. Invalid request was received via TCP or UDP. Request was dropped, no response was sent by the gateway. Validation criteria depends on the Modbus mode:
131+
* **Invalid TCP/UDP Request**. Invalid request was received via TCP or UDP. Request was dropped, no response was sent by the gateway. Validation criteria depend on the Modbus mode:
135132
- Modbus TCP/UDP: MBAP header (protocol identifier is 0x0000, length is < 255 and corresponds to the number of bytes in the remainder of the Modbus request)
136-
- Modbus RTU over TCP/UDP: CRC ckeck
133+
- Modbus RTU over TCP/UDP: CRC check
137134
* **Invalid RTU Response**. Invalid data were recieved via RS485. Could be caused by wrong Modbus RTU settings, short response timeout (any response arriving after timeout is invalid) or Arduino delays in processing the response. Validation criteria:
138135
- silence between individual bytes is shorter than char timeout specified in Modbus RTU standards
139136
- CRC check
@@ -145,13 +142,13 @@ Enjoy :-)
145142
* **UDP**. Only the last Modbus UDP master is shown, because all UDP masters connect to the same socket.
146143
* **TCP**. All connected Modbus TCP masters are shown. Each Modbus TCP connection occupies one socket.
147144

148-
**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.
145+
**Modbus Slaves**. Shows slave addresses (in hex) and last statuses (errors) for all slaves which responded to a slave scan or which were recipients of a Modbus request.
149146

150147
**Scan Slaves**. An attempt is made to find Modbus RTU slaves connected to the RS485 interface:
151148
- scan is launched automaticaly after boot or manualy
152149
- scans all slave addresses 1 - 247
153150
- dummy requests are sent to each slave address for two different Modbus functions (configured in advanced settings)
154-
- fixed response timeout (very short, configured in advanced settings), only one attempt
151+
- fixed response timeout is ised (very short, configured in advanced settings), only one attempt is made
155152
- gateway marks the slave as "Slave Responded" if any response is sent by the slave (even error)
156153

157154
## IP Settings
@@ -202,7 +199,7 @@ Enjoy :-)
202199
## Tools
203200
<img src="pics/modbus6.png" alt="modbus6" style="zoom:100%;" />
204201

205-
**Load Default Settings**. Loads default settings (see DEFAULT_CONFIG in advanced settings). MAC address is retained.
202+
**Load Default Settings**. Erases user settings stored in EEPROM (only **EEPROM Health** and **MAC Address** are retained). Loads default settings from flash memory (see DEFAULT_CONFIG in advanced_settings.h).
206203

207204
**Reboot**.
208205

arduino-modbus-rtu-tcp-gateway/01-interfaces.ino

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,26 @@ uint32_t frameDelay() {
4646
*/
4747
/**************************************************************************/
4848
void startEthernet() {
49-
if (ETH_RESET_PIN != 0) {
50-
pinMode(ETH_RESET_PIN, OUTPUT);
51-
digitalWrite(ETH_RESET_PIN, LOW);
52-
delay(25);
53-
digitalWrite(ETH_RESET_PIN, HIGH);
54-
delay(ETH_RESET_DELAY);
55-
}
49+
#ifdef ETH_RESET_PIN
50+
pinMode(ETH_RESET_PIN, OUTPUT);
51+
digitalWrite(ETH_RESET_PIN, LOW);
52+
delay(25);
53+
digitalWrite(ETH_RESET_PIN, HIGH);
54+
delay(ETH_RESET_DELAY);
55+
#endif
56+
5657
#ifdef ENABLE_DHCP
58+
dhcpSuccess = false;
5759
if (data.config.enableDhcp) {
5860
dhcpSuccess = Ethernet.begin(data.mac);
5961
}
60-
if (!data.config.enableDhcp || dhcpSuccess == false) {
62+
if (!dhcpSuccess) {
6163
Ethernet.begin(data.mac, data.config.ip, data.config.dns, data.config.gateway, data.config.subnet);
6264
}
6365
#else /* ENABLE_DHCP */
6466
Ethernet.begin(data.mac, data.config.ip, {}, data.config.gateway, data.config.subnet); // No DNS
6567
#endif /* ENABLE_DHCP */
68+
6669
W5100.setRetransmissionTime(TCP_RETRANSMISSION_TIMEOUT);
6770
W5100.setRetransmissionCount(TCP_RETRANSMISSION_COUNT);
6871
modbusServer = EthernetServer(data.config.tcpPort);
@@ -101,19 +104,6 @@ void checkEthernet() {
101104
checkEthTimer.sleep(CHECK_ETH_INTERVAL);
102105
}
103106

104-
/**************************************************************************/
105-
/*!
106-
@brief Maintains DHCP lease.
107-
*/
108-
/**************************************************************************/
109-
#ifdef ENABLE_DHCP
110-
void maintainDhcp() {
111-
if (data.config.enableDhcp && dhcpSuccess == true) { // only call maintain if initial DHCP request by startEthernet was successfull
112-
Ethernet.maintain();
113-
}
114-
}
115-
#endif /* ENABLE_DHCP */
116-
117107
/**************************************************************************/
118108
/*!
119109
@brief Maintains uptime in case of millis() overflow.

arduino-modbus-rtu-tcp-gateway/advanced_settings.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ const byte SCAN_DATA_ADDRESS = 0x01; // Data address sent during Modbus RTU
6666
const uint16_t SCAN_TIMEOUT = 200; // Timeout (ms) for Modbus scan requests
6767

6868
const byte MAC_START[3] = { 0x90, 0xA2, 0xDA }; // MAC range for Gheo SA
69-
const byte ETH_RESET_PIN = 7; // Ethernet shield reset pin (deals with power on reset issue on low quality ethernet shields)
69+
#define ETH_RESET_PIN 7 // Ethernet shield reset pin (deals with power on reset issue on low quality ethernet shields) \
70+
// Comment out to disable the functionality
7071
const uint16_t CHECK_ETH_INTERVAL = 2000; // Interval (ms) to check SPI connection with ethernet shield
7172
const uint16_t ETH_RESET_DELAY = 500; // Delay (ms) during Ethernet start, wait for Ethernet shield to start (reset issue on low quality ethernet shields)
7273
const uint16_t WEB_IDLE_TIMEOUT = 400; // Time (ms) from last client data after which webserver TCP socket could be disconnected, non-blocking.

arduino-modbus-rtu-tcp-gateway/arduino-modbus-rtu-tcp-gateway.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
v7.2 2023-10-20 Disable DHCP renewal fallback, better advanced_settings.h layout
2626
ENABLE_EXTENDED_WEBUI and ENABLE_DHCP is set by default for Mega
2727
v7.3 2024-01-16 Bugfix Modbus RTU Request form, code comments
28-
v7.4 2024-XX-XX CSS improvement
28+
v7.4 2024-XX-XX CSS improvement, code optimization, simplify DHCP renew
2929
*/
3030

3131
const byte VERSION[] = { 7, 4 };
@@ -273,6 +273,6 @@ void loop() {
273273
maintainUptime(); // maintain uptime in case of millis() overflow
274274
#endif /* ENABLE_EXTENDED_WEBUI */
275275
#ifdef ENABLE_DHCP
276-
maintainDhcp();
276+
Ethernet.maintain();
277277
#endif /* ENABLE_DHCP */
278278
}

0 commit comments

Comments
 (0)