Skip to content

Commit 452ef5b

Browse files
committed
MAC address randomly generated and stored in EEPROM
1 parent 05f3a6a commit 452ef5b

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

arduino-modbus-tcp-rtu-gateway.ino

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
#include <SPI.h>
2121
#include <Ethernet.h>
2222
#include <EthernetUdp.h>
23-
#include <CircularBuffer.h>
24-
#include <BitBool.h>
23+
#include <CircularBuffer.h> // CircularBuffer https://github.com/rlogiacco/CircularBuffer
24+
#include <BitBool.h> // BitBool https://github.com/Chris--A/BitBool
25+
#include <TrueRandom.h> // https://github.com/sirleech/TrueRandom
26+
#include <EEPROM.h>
2527

2628
// RS485 settings
2729
#define BAUD 9600
@@ -31,7 +33,7 @@
3133
#define MAX_RETRY 5 // maximum number of retries for sending Modbus RTU request
3234

3335
// TCP and UDP settings
34-
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC - change to something more random...
36+
3537
IPAddress ip(192, 168, 1, 10);
3638
IPAddress gateway(192, 168, 1, 1);
3739
IPAddress subnet(255, 255, 255, 0);
@@ -50,7 +52,8 @@ IPAddress subnet(255, 255, 255, 0);
5052
#define SerialTxControl 6 // Arduino Pin for RS485 Direction control
5153
#define ethResetPin 7 // OPTIONAL: Ethernet shield reset pin (deals with power on reset issue of the ethernet shield)
5254

53-
55+
// App will generate unique MAC address, bytes 4, 5 and 6 will hold random value
56+
byte mac[6] = { 0x90, 0xA2, 0xDA, 0x00, 0x00, 0x00 };
5457
#define UDP_TX_PACKET_MAX_SIZE BUFFER_SIZE
5558

5659
typedef struct {
@@ -104,11 +107,10 @@ class MicroTimer {
104107
};
105108

106109
boolean MicroTimer::isOver() {
107-
if (micros() - timestampLastHitMs < sleepTimeMs) {
108-
return false;
110+
if ((unsigned long)(micros() - timestampLastHitMs) > sleepTimeMs) {
111+
return true;
109112
}
110-
timestampLastHitMs = micros();
111-
return true;
113+
return false;
112114
}
113115

114116
void MicroTimer::start(unsigned long sleepTimeMs) {
@@ -126,11 +128,10 @@ class Timer {
126128
};
127129

128130
boolean Timer::isOver() {
129-
if (millis() - timestampLastHitMs < sleepTimeMs) {
130-
return false;
131+
if ((unsigned long)(millis() - timestampLastHitMs) > sleepTimeMs) {
132+
return true;
131133
}
132-
timestampLastHitMs = millis();
133-
return true;
134+
return false;
134135
}
135136

136137
void Timer::start(unsigned long sleepTimeMs) {
@@ -160,8 +161,21 @@ void setup() /****** SETUP: RUNS ONCE ******/
160161
digitalWrite(ethResetPin, HIGH);
161162
delay(500);
162163
pinMode(ethResetPin, INPUT);
163-
delay(500);
164+
delay(1000);
164165
#endif
166+
// MAC stored in EEPROM
167+
if (EEPROM.read(1) == '#') {
168+
for (int i = 3; i < 6; i++)
169+
mac[i] = EEPROM.read(i);
170+
}
171+
// MAC not set, generate random value
172+
else {
173+
for (int i = 3; i < 6; i++) {
174+
mac[i] = TrueRandom.randomByte();
175+
EEPROM.write(i, mac[i]);
176+
}
177+
EEPROM.write(1, '#');
178+
}
165179

166180
Ethernet.begin(mac, ip);
167181
Ethernet.setRetransmissionTimeout(20); // speed up ethernet
@@ -386,7 +400,7 @@ void processUdpTcp()
386400

387401
void sendSerial()
388402
{
389-
if (sendingData == true) {
403+
if (sendingData == true && txDelay.isOver() && !rxNdx) { // avoid bus collision, only send when we are not receiving data
390404
if (Serial.availableForWrite() > 0 && txNdx == 0 && digitalRead(SerialTxControl) == RS485Receive) {
391405
digitalWrite(SerialTxControl, RS485Transmit); // Enable RS485 Transmit
392406
crc = 0xFFFF;

0 commit comments

Comments
 (0)