ESP32-PoE-ISO how to set macaddress with arduino

Started by fabio.soggia, June 24, 2022, 11:42:37 AM

Previous topic - Next topic

fabio.soggia

Hello. Could someone explain me how to set macaddress properly on the lan interface? No problem with network communication, both dhcp and fixed ip work
My code (does not work, macaddress is 00:00:00:00:00:00):
#include <ETH.h>

void setup()
{
  Serial.begin(115200);
  uint8_t newMACAddress[] = {0x32, 0xAE, 0xA4, 0x07, 0x0D, 0x66};//only for test
  ETH.begin(*newMACAddress);
  ETH.config(IPAddress(192, 168, 0, 2),
    IPAddress(192, 168, 0, 1),
    IPAddress(255, 255, 255, 0),
    IPAddress(192, 168, 0, 1),
    IPAddress(192, 168, 0, 1)
  );
}
void loop()
{
  Serial.print("ETH MAC: ");
  Serial.print(ETH.macAddress());
  Serial.print(", IPv4: ");
  Serial.println(ETH.localIP());
  delay(2000);
}
Many thanks

VilCoyote

Documentation say :

esp_err_t esp_base_mac_addr_set(uint8_t *mac)
Set base MAC address with the MAC address which is stored in BLK3 of EFUSE or external storage e.g. flash and EEPROM.

Base MAC address is used to generate the MAC addresses used by the networking interfaces. If using base MAC address stored in BLK3 of EFUSE or external storage, call this API to set base MAC address with the MAC address which is stored in BLK3 of EFUSE or external storage before initializing WiFi/BT/Ethernet.

Return
ESP_OK on success
Parameters
mac: base MAC address, length: 6 bytes.

But code doesn't work :

uint8_t g_GoldMacAddress[6] = {0x24, 0x4C, 0xAB, 0x28, 0xAB, 0x30};
    if (g_GoldMacAddress[0] & 0x01) Serial.println("Base MAC must be a unicast MAC");
    esp_err_t ret = esp_base_mac_addr_set(g_GoldMacAddress);
    Serial.print("result : ");
    Serial.println(ret);
    ETH.begin();

return :

default MAC : 24:4C:AB:28:AB:EC
result : 0
[  1553][V][WiFiGeneric.cpp:435] _arduino_event_cb(): Ethernet Started
[  1553][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 18 - ETH_START
ETH Started[  1555][V][WiFiGeneric.cpp:428] _arduino_event_cb(): Ethernet Link Up

[  1566][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 20 - ETH_CONNECTED
ETH Connected
[  2527][V][WiFiGeneric.cpp:443] _arduino_event_cb(): Ethernet got newip:155.105.149.110
[  2528][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 22 - ETH_GOT_IP
[  2531][D][WiFiGeneric.cpp:1027] _eventCallback(): ETH IP: 155.105.149.110, MASK: 255.255.254.0, GW: 155.105.148.1
ETH MAC: 24:4C:AB:28:AB:EF, IPv4: 155.105.149.110, FULL_DUPLEX, 100Mbps


Anyone have clues ?

Thanks a lot
Vil

LubOlimex

There are many tutorials on how to change the MAC address of ESP32 in Arduino IDE. Do not give up after the first one.
Technical support and documentation manager at Olimex

VilCoyote

I agree LubOlimex
but most of thoses for the Wifi part...

seems that ethernet is more untested...

LubOlimex

You can get all MAC addresses printed with the following code:

#include <WiFi.h>
#include <esp_now.h>

void setup(){
  Serial.begin(115200);
  Serial.print("ESP Board BASE MAC Address:  ");
  Serial.println(WiFi.macAddress());
 
  uint8_t sta_mac[6];
  char buffer [6];
  esp_efuse_mac_get_default(sta_mac);

  esp_read_mac(sta_mac, ESP_MAC_WIFI_SOFTAP);

  Serial.print("SOFT AP MAC Address:  ");
 
  sprintf(buffer, "%02X:%02X:%02X:%02X:%02X:%02X", sta_mac[0], sta_mac[1], sta_mac[2], sta_mac[3], sta_mac[4], sta_mac[5], sta_mac[6]);
  Serial.println(buffer);

  esp_read_mac(sta_mac, ESP_MAC_WIFI_STA);

  Serial.print("STATION MAC Address:  ");
 
  sprintf(buffer, "%02X:%02X:%02X:%02X:%02X:%02X", sta_mac[0], sta_mac[1], sta_mac[2], sta_mac[3], sta_mac[4], sta_mac[5], sta_mac[6]);
  Serial.println(buffer);

  esp_read_mac(sta_mac, ESP_MAC_BT);

  Serial.print("BLUETOOTH MAC Address:  ");
 
  sprintf(buffer, "%02X:%02X:%02X:%02X:%02X:%02X", sta_mac[0], sta_mac[1], sta_mac[2], sta_mac[3], sta_mac[4], sta_mac[5], sta_mac[6]);
  Serial.println(buffer);

  esp_read_mac(sta_mac, ESP_MAC_ETH);

  Serial.print("ETHERNET MAC Address:  ");
 
  sprintf(buffer, "%02X:%02X:%02X:%02X:%02X:%02X", sta_mac[0], sta_mac[1], sta_mac[2], sta_mac[3], sta_mac[4], sta_mac[5], sta_mac[6]);
  Serial.println(buffer);
}
void loop(){}

It is just printed one time so restart the board to get it over the serial. You should get something like:

Board BASE MAC Address:  3C:71:BF:0D:8D:38
15:08:39.415 -> SOFT AP MAC Address:  3C:71:BF:0D:8D:39
15:08:39.415 -> STATION MAC Address:  3C:71:BF:0D:8D:38
15:08:39.415 -> BLUETOOTH MAC Address:  3C:71:BF:0D:8D:3A
15:08:39.415 -> ETHERNET MAC Address:  3C:71:BF:0D:8D:3B
Technical support and documentation manager at Olimex

VilCoyote

Finally it's working...

First, must use espefuse.py to burn custom MAC :
C:\WINDOWS\system32>espefuse.py -p COM3 burn_custom_mac DE:AD:BE:EF:FE:ED
espefuse.py v4.3
Connecting..............
Detecting chip type... Unsupported detection protocol, switching and trying again...
Connecting.......
Detecting chip type... ESP32

=== Run "burn_custom_mac" command ===
    - 'MAC_VERSION' (Version of the MAC field) 0x00 -> 0x1
    - 'CUSTOM_MAC' (Custom MAC) 0x000000000000 -> 0xedfeefbeadde
    - 'CUSTOM_MAC_CRC' (CRC of custom MAC) 0x00 -> 0x45

Check all blocks for burn...
idx, BLOCK_NAME,          Conclusion
[03] BLOCK3               is empty, will burn the new value
.
This is an irreversible operation!
Type 'BURN' (all capitals) to continue.
BURN
BURN BLOCK3  - OK (write block == read block)
Reading updated efuses...
Custom MAC Address version 1: de:ad:be:ef:fe:ed (CRC 0x45 OK)
Successful

Then use ETH.Begin() with overload :
ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE, true);


Then it's work :
ets Jul 29 2019 12:21:46

rst:0x1 (POWERON_RESET),boot:0x1b (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:1184
load:0x40078000,len:13160
load:0x40080400,len:3036
entry 0x400805e4
[     4][D][esp32-hal-cpu.c:244] setCpuFrequencyMhz(): PLL: 480 / 2 = 240 Mhz, APB: 80000000 Hz
DE:AD:BE:EF:FE:ED
[  1654][V][WiFiGeneric.cpp:435] _arduino_event_cb(): Ethernet Started
[  1654][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 18 - ETH_START
ETH Started
[  1656][V][WiFiGeneric.cpp:428] _arduino_event_cb(): Ethernet Link Up
[  1666][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 20 - ETH_CONNECTED
ETH Connected
[  2619][V][WiFiGeneric.cpp:443] _arduino_event_cb(): Ethernet got newip:155.105.149.154
[  2620][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 22 - ETH_GOT_IP
[  2623][D][WiFiGeneric.cpp:1027] _eventCallback(): ETH IP: 155.105.149.154, MASK: 255.255.254.0, GW: 155.105.148.1
ETH MAC: DE:AD:BE:EF:FE:F0, IPv4: 155.105.149.154, FULL_DUPLEX, 100Mbps

connecting to google.com
HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/