generate random mac address on first start

This commit is contained in:
technyon
2022-04-29 21:56:19 +02:00
parent 6bd6900198
commit 6fe1d70521
4 changed files with 60 additions and 8 deletions

View File

@@ -39,7 +39,7 @@ void Network::setupDevice(const NetworkDeviceType hardware)
{
case NetworkDeviceType::W5500:
Serial.println(F("Network device: W5500"));
_device = new W5500Device(_hostname);
_device = new W5500Device(_hostname, _preferences);
break;
case NetworkDeviceType::WiFi:
Serial.println(F("Network device: Builtin WiFi"));

View File

@@ -12,6 +12,10 @@
#define preference_cred_user "crdusr"
#define preference_cred_password "crdpass"
#define preference_presence_detection_timeout "prdtimeout"
#define preference_has_mac_saved "hasmac"
#define preference_has_mac_byte_0 "macb0"
#define preference_has_mac_byte_1 "macb1"
#define preference_has_mac_byte_2 "macb2"

View File

@@ -1,9 +1,27 @@
#include "W5500Device.h"
#include "../Pins.h"
#include "../PreferencesKeys.h"
W5500Device::W5500Device(const String &hostname)
: NetworkDevice(hostname)
W5500Device::W5500Device(const String &hostname, Preferences* preferences)
: NetworkDevice(hostname),
_preferences(preferences)
{
initializeMacAddress(_mac);
Serial.print("MAC Adress: ");
for(int i=0; i < 6; i++)
{
if(_mac[i] < 10)
{
Serial.print(F("0"));
}
Serial.print(_mac[i], 16);
if(i < 5)
{
Serial.print(F(":"));
}
}
Serial.println();
}
W5500Device::~W5500Device()
@@ -29,9 +47,7 @@ void W5500Device::initialize()
Serial.println();
dhcpRetryCnt++;
byte mac[] = {0xB0,0xCD,0xAE,0x0F,0xDE,0x10};
if (Ethernet.begin(mac, 1000, 1000) == 0)
if (Ethernet.begin(_mac, 1000, 1000) == 0)
{
Serial.println(F("Failed to configure Ethernet using DHCP"));
// Check for Ethernet hardware present
@@ -53,7 +69,7 @@ void W5500Device::initialize()
subnet.fromString("255.255.255.0");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
Ethernet.begin(_mac, ip);
Ethernet.setSubnetMask(subnet);
delay(2000);
@@ -93,3 +109,30 @@ bool W5500Device::isConnected()
{
return _ethClient->connected();
}
void W5500Device::initializeMacAddress(byte *mac)
{
memset(mac, 0, 6);
mac[0] = 0x00; // wiznet prefix
mac[1] = 0x08; // wiznet prefix
mac[2] = 0xDC; // wiznet prefix
if(_preferences->getBool(preference_has_mac_saved))
{
mac[3] = _preferences->getChar(preference_has_mac_byte_0);
mac[4] = _preferences->getChar(preference_has_mac_byte_1);
mac[5] = _preferences->getChar(preference_has_mac_byte_2);
}
else
{
mac[3] = random(0,255);
mac[4] = random(0,255);
mac[5] = random(0,255);
_preferences->putChar(preference_has_mac_byte_0, mac[3]);
_preferences->putChar(preference_has_mac_byte_1, mac[4]);
_preferences->putChar(preference_has_mac_byte_2, mac[5]);
_preferences->putBool(preference_has_mac_saved, true);
}
}

View File

@@ -2,11 +2,12 @@
#include "NetworkDevice.h"
#include <Ethernet.h>
#include <Preferences.h>
class W5500Device : public NetworkDevice
{
public:
explicit W5500Device(const String& hostname);
explicit W5500Device(const String& hostname, Preferences* _preferences);
~W5500Device();
virtual void initialize();
@@ -18,7 +19,11 @@ public:
private:
void resetDevice();
void initializeMacAddress(byte* mac);
EthernetClient* _ethClient = nullptr;
PubSubClient* _mqttClient = nullptr;
Preferences* _preferences = nullptr;
byte _mac[6];
};