Skip to content

Commit 0fa8e34

Browse files
committed
Only store last 3 bytes of MAC
Only store last 3 bytes of MAC in EEPROM.
1 parent 911737f commit 0fa8e34

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,18 @@ void startEthernet() {
6565
digitalWrite(ethResetPin, HIGH);
6666
delay(500);
6767
}
68+
byte mac[6];
69+
memcpy(mac, MAC_START, 3); // set first 3 bytes
70+
memcpy(mac + 3, localConfig.macEnd, 3); // set last 3 bytes
6871
#ifdef ENABLE_DHCP
6972
if (localConfig.enableDhcp) {
70-
dhcpSuccess = Ethernet.begin(localConfig.mac);
73+
dhcpSuccess = Ethernet.begin(mac);
7174
}
7275
if (!localConfig.enableDhcp || dhcpSuccess == false) {
73-
Ethernet.begin(localConfig.mac, localConfig.ip, localConfig.dns, localConfig.gateway, localConfig.subnet);
76+
Ethernet.begin(mac, localConfig.ip, localConfig.dns, localConfig.gateway, localConfig.subnet);
7477
}
7578
#else /* ENABLE_DHCP */
76-
Ethernet.begin(localConfig.mac, localConfig.ip, localConfig.dns, localConfig.gateway, localConfig.subnet);
79+
Ethernet.begin(mac, localConfig.ip, localConfig.dns, localConfig.gateway, localConfig.subnet);
7780
localConfig.enableDhcp = false; // Make sure Dhcp is disabled in config
7881
#endif /* ENABLE_DHCP */
7982
modbusServer = EthernetServer(localConfig.tcpPort);
@@ -143,7 +146,7 @@ void generateMac()
143146
uint32_t randomBuffer = (seed1 << 16) + seed2; /* 32-bit random */
144147

145148
for (byte i = 0; i < 3; i++) {
146-
localConfig.mac[i + 3] = randomBuffer & 0xFF;
149+
localConfig.macEnd[i] = randomBuffer & 0xFF;
147150
randomBuffer >>= 8;
148151
}
149152
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,10 @@ void processPost(char postParameter[]) {
312312
switch (action) {
313313
case FACTORY:
314314
{
315-
byte tempMac[6];
316-
memcpy(tempMac, localConfig.mac, 6); // keep current MAC
315+
byte tempMac[3];
316+
memcpy(tempMac, localConfig.macEnd, 3); // keep current MAC
317317
localConfig = defaultConfig;
318-
memcpy(localConfig.mac, tempMac, 6);
318+
memcpy(localConfig.macEnd, tempMac, 3);
319319
break;
320320
}
321321
case MAC:

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@
3737
v3.0 2021-11-07 Improve POST parameters processing, bugfix 404 and 204 error headers.
3838
v3.1 2022-01-28 Code optimization, bugfix DHCP settings.
3939
v3.2 2022-06-04 Reduce program size (so that it fits on Nano), ethernet data counter only available when ENABLE_EXTRA_DIAG.
40+
v4.0 2022-11-26 Only store last 3 bytes of MAC in EEPROM,
4041
4142
*/
4243

43-
const byte version[] = {3, 2};
44+
const byte version[] = {4, 0};
4445

4546
#include <SPI.h>
4647
#include <Ethernet.h>
@@ -84,7 +85,7 @@ const byte scanCommand[] = {0x03, 0x00, 0x00, 0x00, 0x01}; // Command sent duri
8485

8586
typedef struct
8687
{
87-
byte mac[6];
88+
byte macEnd[3];
8889
bool enableDhcp;
8990
IPAddress ip;
9091
IPAddress subnet;
@@ -109,7 +110,7 @@ typedef struct
109110
*/
110111

111112
const config_type defaultConfig = {
112-
{ 0x90, 0xA2, 0xDA }, // mac (bytes 4, 5 and 6 will be generated randomly)
113+
{}, // macEnd (last 3 bytes)
113114
false, // enableDhcp
114115
{192, 168, 1, 254}, // ip
115116
{255, 255, 255, 0}, // subnet
@@ -140,6 +141,8 @@ byte maxSockNum = MAX_SOCK_NUM;
140141

141142
bool dhcpSuccess = false;
142143

144+
const byte MAC_START[3] = { 0x90, 0xA2, 0xDA };
145+
143146
EthernetUDP Udp;
144147
EthernetServer modbusServer(502);
145148
EthernetServer webServer(80);

0 commit comments

Comments
 (0)