espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
13.71k stars 7.43k forks source link

Allow custom mac address definition in ETH library #10369

Open MicSG-dev opened 2 months ago

MicSG-dev commented 2 months ago

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

JAndrassy commented 2 months ago

you can use my EthernetESP32 library

https://github.com/espressif/arduino-esp32/pull/9522#issuecomment-2066815363

as-iotex commented 1 month ago

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 commented 1 month ago

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:

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)
JAndrassy commented 1 month ago

@as-iotex https://github.com/Networking-for-Arduino/EthernetESP32/blob/master/examples/BasicOTA/BasicOTA.ino

as-iotex commented 1 month ago

@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 commented 1 month ago

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

as-iotex commented 1 month ago

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