Skip to content

Commit c337041

Browse files
committed
Disable DHCP renewal fallback, recover after short power loss
1 parent da71369 commit c337041

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,24 @@ void startEthernet() {
114114

115115
void (*resetFunc)(void) = 0; //declare reset function at address 0
116116

117+
void checkEthernet() {
118+
static byte attempts = 0;
119+
IPAddress tempIP = Ethernet.localIP();
120+
if (tempIP[0] == 0) {
121+
attempts++;
122+
if (attempts >= 3) {
123+
resetFunc();
124+
}
125+
} else {
126+
attempts = 0;
127+
}
128+
checkEthTimer.sleep(CHECK_ETH_INTERVAL);
129+
}
130+
117131
#ifdef ENABLE_DHCP
118132
void maintainDhcp() {
119133
if (data.config.enableDhcp && dhcpSuccess == true) { // only call maintain if initial DHCP request by startEthernet was successfull
120-
byte maintainResult = Ethernet.maintain();
121-
if (maintainResult == 1 || maintainResult == 3) { // renew failed or rebind failed
122-
dhcpSuccess = false;
123-
startEthernet(); // another DHCP request, fallback to static IP
124-
}
134+
Ethernet.maintain();
125135
}
126136
}
127137
#endif /* ENABLE_DHCP */
@@ -208,7 +218,7 @@ void manageSockets() {
208218
byte webListening = MAX_SOCK_NUM;
209219
byte dataAvailable = MAX_SOCK_NUM;
210220
byte socketsAvailable = 0;
211-
// SPI.beginTransaction(SPI_ETHERNET_SETTINGS); // begin SPI transaction
221+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS); // begin SPI transaction
212222
// look at all the hardware sockets, record and take action based on current states
213223
for (byte s = 0; s < maxSockNum; s++) { // for each hardware socket ...
214224
byte status = W5100.readSnSR(s); // get socket status...
@@ -294,8 +304,8 @@ void manageSockets() {
294304
disconSocket(oldest);
295305
}
296306

297-
// SPI.endTransaction(); // Serves to o release the bus for other devices to access it. Since the ethernet chip is the only device
298-
// we do not need SPI.beginTransaction(SPI_ETHERNET_SETTINGS) or SPI.endTransaction()
307+
SPI.endTransaction(); // Serves to o release the bus for other devices to access it. Since the ethernet chip is the only device
308+
// we do not need SPI.beginTransaction(SPI_ETHERNET_SETTINGS) or SPI.endTransaction() ??
299309
}
300310

301311
void disconSocket(byte s) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const uint16_t SCAN_TIMEOUT = 200; // Timeout (ms) for Modbus scan request
2020

2121
const byte MAC_START[3] = { 0x90, 0xA2, 0xDA }; // MAC range for Gheo SA
2222
const byte ETH_RESET_PIN = 7; // Ethernet shield reset pin (deals with power on reset issue on low quality ethernet shields)
23+
const uint16_t CHECK_ETH_INTERVAL = 2000; // Interval (ms) to check SPI connection with ethernet shield
2324
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)
2425
const uint16_t WEB_IDLE_TIMEOUT = 400; // Time (ms) from last client data after which webserver TCP socket could be disconnected, non-blocking.
2526
const uint16_t TCP_DISCON_TIMEOUT = 500; // Timeout (ms) for client DISCON socket command, non-blocking alternative to https://www.arduino.cc/reference/en/libraries/ethernet/client.setconnectiontimeout/

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
v6.1 2023-04-12 Code optimization
2323
v7.0 2023-07-21 Manual MAC, better data types
2424
v7.1 2023-08-25 Simplify EEPROM read and write, Tools page
25+
v7.2 2023-XX-XX Disable DHCP renewal fallback, recover after short power loss
2526
*/
2627

27-
const byte VERSION[] = { 7, 1 };
28+
const byte VERSION[] = { 7, 2 };
2829

2930
#include <SPI.h>
3031
#include <Ethernet.h>
@@ -167,7 +168,8 @@ void Timer::sleep(uint32_t sleepTimeMs) {
167168

168169
MicroTimer recvMicroTimer;
169170
MicroTimer sendMicroTimer;
170-
Timer eepromTimer; // timer to delay writing statistics to EEPROM
171+
Timer eepromTimer; // timer to delay writing statistics to EEPROM
172+
Timer checkEthTimer; // timer to check SPI connection with ethernet shield
171173

172174
#define RS485_TRANSMIT HIGH
173175
#define RS485_RECEIVE LOW
@@ -242,6 +244,10 @@ void loop() {
242244
updateEeprom();
243245
}
244246

247+
if (CHECK_ETH_INTERVAL > 0 && checkEthTimer.isOver() == true) {
248+
checkEthernet();
249+
}
250+
245251
if (rollover()) {
246252
resetStats();
247253
updateEeprom();

0 commit comments

Comments
 (0)