Skip to content

Allow custom mac address definition in ETH library #10369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
MicSG-dev opened this issue Sep 23, 2024 · 10 comments
Closed
1 task done

Allow custom mac address definition in ETH library #10369

MicSG-dev opened this issue Sep 23, 2024 · 10 comments
Labels
Type: Feature request Feature request for Arduino ESP32

Comments

@MicSG-dev
Copy link

MicSG-dev commented Sep 23, 2024

Related area

Mac Address Ethernet W5500 SPI

Hardware specification

ESP32 DevKitC

Is your feature request related to a problem?

Currently the MAC address configuration in the ETH library happens internally, so the user cannot decide which MAC address he wants to use.

Describe the solution you'd like

I want it to be possible to change the MAC address through the begin function of the ETH.h library.

Describe alternatives you've considered

The solution would be to create a new method for the ETH library. See below:



File ETH.h, line 125:

BEFORE:

#if ETH_SPI_SUPPORTS_CUSTOM
  bool begin(eth_phy_type_t type, int32_t phy_addr, int cs, int irq, int rst, SPIClass &spi, uint8_t spi_freq_mhz = ETH_PHY_SPI_FREQ_MHZ);
#endif

AFTER:

#if ETH_SPI_SUPPORTS_CUSTOM
  bool begin(eth_phy_type_t type, int32_t phy_addr, int cs, int irq, int rst, SPIClass &spi, uint8_t spi_freq_mhz = ETH_PHY_SPI_FREQ_MHZ);
  bool begin(eth_phy_type_t type, int32_t phy_addr, int cs, int irq, int rst, SPIClass &spi, uint8_t *mac_addr, uint8_t spi_freq_mhz = ETH_PHY_SPI_FREQ_MHZ);
#endif



File ETH.cpp, line 810:

BEFORE:

#if ETH_SPI_SUPPORTS_CUSTOM
bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int cs, int irq, int rst, SPIClass &spi, uint8_t spi_freq_mhz) {

  return beginSPI(type, phy_addr, nullptr, cs, irq, rst, &spi, -1, -1, -1, SPI2_HOST, spi_freq_mhz);
}
#endif

AFTER:

#if ETH_SPI_SUPPORTS_CUSTOM
bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int cs, int irq, int rst, SPIClass &spi, uint8_t spi_freq_mhz) {

  return beginSPI(type, phy_addr, nullptr, cs, irq, rst, &spi, -1, -1, -1, SPI2_HOST, spi_freq_mhz);
}

bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int cs, int irq, int rst, SPIClass &spi, uint8_t *mac_addr, uint8_t spi_freq_mhz) {

  return beginSPI(type, phy_addr, mac_addr, cs, irq, rst, &spi, -1, -1, -1, SPI2_HOST, spi_freq_mhz);
}
#endif

Additional context

The usage of this new library method would be:

void setup() {
  Serial.begin(115200);
  Network.onEvent(onEvent);

  uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

  SPI.begin(ETH_SPI_SCK, ETH_SPI_MISO, ETH_SPI_MOSI);
  ETH.begin(ETH_PHY_TYPE, ETH_PHY_ADDR, ETH_PHY_CS, ETH_PHY_IRQ, ETH_PHY_RST, SPI, mac);
}

I have checked existing list of Feature requests and the Contribution Guide

  • I confirm I have checked existing list of Feature requests and Contribution Guide.
@MicSG-dev MicSG-dev added the Type: Feature request Feature request for Arduino ESP32 label Sep 23, 2024
@JAndrassy
Copy link
Contributor

JAndrassy commented Sep 24, 2024

you can use my EthernetESP32 library

#9522 (comment)

@as-iotex
Copy link

Hey @JAndrassy

I am using the ArduinoOTA bundled library, and I would like to use a custom MAC address or static IP. I am using the W5500 ethernet module
Does your custom EthernetESP32 library work with ArduinoOTA (which uses ETH.cpp) ?

If your library doesn't work with ArduinoOTA, could you point me to what you changed in order to set it. I could try and hack around ETH.cpp to port this particular change.

@me-no-dev tagging you as well in case you can help

@MicSG-dev
Copy link
Author

MicSG-dev commented Oct 21, 2024

Ei@JAndrassy

Estou usando a biblioteca empacotada ArduinoOTA e gostaria de usar um endereço MAC personalizado ou IP estático. Estou usando o módulo ethernet W5500. Sua biblioteca EthernetESP32 personalizada funciona com ArduinoOTA (que usa ETH.cpp)?

Se sua biblioteca não funcionar com ArduinoOTA, você poderia me indicar o que você alterou para configurá-la. Eu poderia tentar hackear o ETH.cpp para portar essa alteração em particular.

@me-no-devmarcando você também caso possa ajudar

Replace the ArduinoOTA library with the Update.h library, which is compatible with ETH.h (see a basic example here, but if you are going to use a webserver to perform the update, I suggest using it together with the ESPAsyncWebServer library).

To set a static IP address, use the config method (from the ETH library) with the following parameters:

  • local_ip: the static IP address;
  • gateway: the default gateway address;
  • subnet: the subnet mask;
  • dns1: the first DNS server;
  • dns2: the second DNS server (optional);
  • dns3: the third DNS server (optional).

Usage example:

#include <ETH.h>

void setup() {
  // Initialize the Ethernet interface
  ETH.begin();

  // Defining the parameters for the static IP
  IPAddress local_ip(192, 168, 1, 100);    // static IP
  IPAddress gateway(192, 168, 1, 1);       // Gateway
  IPAddress subnet(255, 255, 255, 0);      // Subnet mask
  IPAddress dns1(8, 8, 8, 8);              // First DNS server (Google DNS)
  IPAddress dns2(8, 8, 4, 4);              // Second DNS server (Google DNS)
  IPAddress dns3(1, 1, 1, 1);              // Third DNS server (Cloudflare DNS, optional)

 // Setting the static IP
  ETH.config(local_ip, gateway, subnet, dns1, dns2, dns3);

 // Your code here
}

void loop() {
 // Main code
}

To set a custom MAC address, I'm using a library, which I forked from this main one, that allows you to set a custom MAC address: github.com/MicSG-dev/ETH-Mac. Usage example:

uint8_t mac[6] = {0x00, 0x1A, 0x2B, 0x3C, 0x4D, 0x5D};
ETH.begin(ETH_PHY_TYPE, ETH_PHY_ADDR, ETH_PHY_CS, ETH_PHY_IRQ, ETH_PHY_RST, SPI, mac)

@as-iotex
Copy link

as-iotex commented Oct 22, 2024

@JAndrassy I tried your library and I am seeing this conflicting declaration error:

In file included from /home/user/Arduino/libraries/EthernetESP32/src/EthernetESP32.h:4,
                 from /home/user/projects/smartpadel/smartpadelFW/src/ProdinoOTA.cpp:51:
/home/user/Arduino/libraries/EthernetESP32/src/utility/EMACDriver.h:32:3: error: 'ETH_PHY_LAN8720' conflicts with a previous declaration
   32 |   ETH_PHY_LAN8720,
      |   ^~~~~~~~~~~~~~~
In file included from /home/user/projects/smartpadel/smartpadelFW/src/ProdinoOTA.cpp:1:
/home/user/.arduino15/packages/esp32/hardware/esp32/3.0.5/libraries/Ethernet/src/ETH.h:101:3: note: previous declaration 'eth_phy_type_t ETH_PHY_LAN8720'
  101 |   ETH_PHY_LAN8720,
      |   ^~~~~~~~~~~~~~~
/home/user/Arduino/libraries/EthernetESP32/src/utility/EMACDriver.h:33:3: error: 'ETH_PHY_TLK110' conflicts with a previous declaration
   33 |   ETH_PHY_TLK110,
      |   ^~~~~~~~~~~~~~
/home/user/.arduino15/packages/esp32/hardware/esp32/3.0.5/libraries/Ethernet/src/ETH.h:102:3: note: previous declaration 'eth_phy_type_t ETH_PHY_TLK110'
  102 |   ETH_PHY_TLK110,
      |   ^~~~~~~~~~~~~~
/home/user/Arduino/libraries/EthernetESP32/src/utility/EMACDriver.h:34:3: error: 'ETH_PHY_RTL8201' conflicts with a previous declaration
   34 |   ETH_PHY_RTL8201,
      |   ^~~~~~~~~~~~~~~
/home/user/.arduino15/packages/esp32/hardware/esp32/3.0.5/libraries/Ethernet/src/ETH.h:103:3: note: previous declaration 'eth_phy_type_t ETH_PHY_RTL8201'
  103 |   ETH_PHY_RTL8201,
      |   ^~~~~~~~~~~~~~~
/home/user/Arduino/libraries/EthernetESP32/src/utility/EMACDriver.h:35:3: error: 'ETH_PHY_DP83848' conflicts with a previous declaration
   35 |   ETH_PHY_DP83848,
      |   ^~~~~~~~~~~~~~~
/home/user/.arduino15/packages/esp32/hardware/esp32/3.0.5/libraries/Ethernet/src/ETH.h:104:3: note: previous declaration 'eth_phy_type_t ETH_PHY_DP83848'
  104 |   ETH_PHY_DP83848,
      |   ^~~~~~~~~~~~~~~
/home/user/Arduino/libraries/EthernetESP32/src/utility/EMACDriver.h:37:3: error: 'ETH_PHY_MAX' conflicts with a previous declaration
   37 |   ETH_PHY_MAX
      |   ^~~~~~~~~~~
/home/user/.arduino15/packages/esp32/hardware/esp32/3.0.5/libraries/Ethernet/src/ETH.h:117:3: note: previous declaration 'eth_phy_type_t ETH_PHY_MAX'
  117 |   ETH_PHY_MAX
      |   ^~~~~~~~~~~

exit status 1

Compilation error: exit status 1

Am I doing something wrong or is this a bug in the library?

Do I need to replace the bundled Ethernet library with yours?

@JAndrassy
Copy link
Contributor

don't use both ETH.h and EthernetESP32.h in same sketch

@as-iotex
Copy link

Thanks very much. I can confirm your library works like a charm

@Parsaabasi
Copy link

Hello,

This issue seems resolved. I close it for now. In case the issue persists, please feel free to reopen it.

Thanks

@JAndrassy
Copy link
Contributor

@Parsaabasi OP used my EthernetESP32 library instead of the ETH library. The ETH library still doesn't support custom MAC address setting

@Jason2866
Copy link
Collaborator

Not planned feature to spoof a random MAC address.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature request Feature request for Arduino ESP32
Projects
Development

No branches or pull requests

5 participants