Help with ETH - unable to get IP address DHCP

pinchy
Posts: 3
Joined: Tue Aug 13, 2024 7:10 am

Help with ETH - unable to get IP address DHCP

Postby pinchy » Mon Sep 23, 2024 3:32 am

Hi guys!

I've got an issue getting ETH working, I'm connecting and getting a link up, but am not getting an IP address and NetworkClient calls fail.

I've got an ESP32-S3 connected via custom GPIOs to a W5500 chip on a custom board. I'm trying to get the ethernet library working (eventually with mqtt). I'm porting over a project that used to use WiFi but we're moving to Ethernet.

I'm loosely basing the trials on: https://docs.espressif.com/projects/ard ... ernet.html

The relevant sections of code look like this, called from setup():

Code: Select all

 SPI.begin(_pins.SCK, _pins.MISO, _pins.MOSI);
 bool r = ETH.begin(ETH_PHY_TYPE, ETH_PHY_ADDR, _pins.SS, _pins.IRQ, _pins.RESET, SPI);
 
   // IPAddress ip(192, 168, 1, 98);
    // IPAddress dns(1, 1, 1, 1);
    // IPAddress gateway(192, 168, 1, 2);
    // IPAddress subnet(255, 255, 255, 0);
    // ETH.config(ip, gateway, subnet, dns);
  
With an event handler:

Code: Select all

void PostOfficeEthernetESP32::parseNetworkEvent(arduino_event_id_t event, arduino_event_info_t info)
{
    this->_event = event;
    switch (event)
    {
        case ARDUINO_EVENT_ETH_START:
            ESP_LOGI(TAG, "Ethernet Started");
            ETH.setHostname("fertigator-0");
            if (this->_onNetworkEvent[ETH_START] != nullptr) this->_onNetworkEvent[ETH_START]();
            break;

        case ARDUINO_EVENT_ETH_STOP:
            ESP_LOGW(TAG, "Ethernet Stopped");
            this->_connected = false;
            if (this->_onNetworkEvent[ETH_STOP] != nullptr) this->_onNetworkEvent[ETH_STOP]();
            break;

        case ARDUINO_EVENT_ETH_CONNECTED:
            ESP_LOGI(TAG, "Ethernet Connected");
            ESP_LOGI(TAG, "Link Speed: %d", ETH.linkSpeed());
            if (this->_onNetworkEvent[ETH_CONNECTED] != nullptr) this->_onNetworkEvent[ETH_CONNECTED]();
            break;

        case ARDUINO_EVENT_ETH_DISCONNECTED:
            ESP_LOGW(TAG, "Ethernet Disconnected");
            this->_connected = false;
            if (this->_onNetworkEvent[ETH_DISCONNECTED] != nullptr) this->_onNetworkEvent[ETH_DISCONNECTED]();
            break;

        case ARDUINO_EVENT_ETH_GOT_IP:

            ESP_LOGI(TAG, "Ethernet got IP: '%s'", esp_netif_get_desc(info.got_ip.esp_netif));
            Serial.println(ETH);
            this->_connected = true;
            if (this->_onNetworkEvent[ETH_GOT_IP] != nullptr) this->_onNetworkEvent[ETH_GOT_IP]();
            break;

        case ARDUINO_EVENT_ETH_LOST_IP:
            ESP_LOGW(TAG, "Ethernet lost IP");
            this->_connected = false;
            if (this->_onNetworkEvent[ETH_LOST_IP] != nullptr) this->_onNetworkEvent[ETH_LOST_IP]();
            break;


        default:
            break;
    }
}


Calling ETH.config
Now if I uncomment out the static IP assignment block in the setup then the ARDUINO_EVENT_ETH_START , ARDUINO_EVENT_ETH_CONNECTED and ARDUINO_EVENT_ETH_GOT_IP events fire. However if I comment it out, with the intention to get a DCHP dynamic IP, then the ARDUINO_EVENT_ETH_GOT_IP event never fires.

Output of the serial monitor with the comment block commented:

Code: Select all

============ Before Setup End ============
 ~snip~
[  1875][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type SPI_MASTER_SCK (34) successfully set to 0x42049204
[  1887][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type SPI_MASTER_MISO (35) successfully set to 0x4204912c
[  1899][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type SPI_MASTER_MOSI (36) successfully set to 0x42049054
[  1911][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type SPI_MASTER_SS (37) successfully set to 0x42048f40
[  1923][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type GPIO (1) successfully set to 0x4206262c
[  1934][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 39 successfully set to type GPIO (1) with bus 0x28
[  1944][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 39 successfully set to type SPI_MASTER_SCK (34) with bus 0x1
[  1955][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type GPIO (1) successfully set to 0x4206262c
[  1966][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 41 successfully set to type GPIO (1) with bus 0x2a
[  1976][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 41 successfully set to type SPI_MASTER_MISO (35) with bus 0x1
[  1986][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type GPIO (1) successfully set to 0x4206262c
[  1997][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 40 successfully set to type GPIO (1) with bus 0x29
[  2007][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 40 successfully set to type SPI_MASTER_MOSI (36) with bus 0x1
[  2018][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type ETHERNET_SPI (47) successfully set to 0x42047e1c
[  2030][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type GPIO (1) successfully set to 0x4206262c
[  2041][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 45 successfully set to type GPIO (1) with bus 0x2e
[  2051][V][esp32-hal-periman.c:174] perimanSetPinBusExtraType(): Successfully set extra_type ETH_CS[0] for pin 45
3[    22009934]][[VV]][[eEsTpH32-hal-p.ercipmpa:n1.0c4:]1 6_0o]n EptehrEivmeanntS(e)t:P ientBhu0s (S)t:a rPtiend 
8 successfully set to type ETHERNET_SPI (47) with bus 0x3fc9a898
[  2110][V][NetworkEvents.cpp:119] checkForEvent(): Network Event: 1 - ETH_START
[  2117][I][PostOfficeEthenetESP32.cpp:82] parseNetworkEvent(): [PostOfficeEthernetESP32] Ethernet Started
[  2110][V][esp32-hal-periman.c:174] perimanSetPinBusExtraType(): Successfully set extra_type ETH_IRQ for pin 38
[  2136][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 37 successfully set to type ETHERNET_SPI (47) with bus 0x3fc9a898
[  2148][V][esp32-hal-periman.c:174] perimanSetPinBusExtraType(): Successfully set extra_type ETH_RST for pin 37

~snip~

============ After Setup End =============
[  4095][V][ETH.cpp:95] _onEthEvent(): eth0 Connected
[  4100][V][NetworkEvents.cpp:119] checkForEvent(): Network Event: 3 - ETH_CONNECTED
[  4107][I][PostOfficeEthenetESP32.cpp:94] parseNetworkEvent(): [PostOfficeEthernetESP32] Ethernet Connected
[  4117][I][PostOfficeEthenetESP32.cpp:95] parseNetworkEvent(): [PostOfficeEthernetESP32] Link Speed: 100

After a few minutes, I get a continuous stream of:

Code: Select all

E (186889) w5500.mac: received frame was truncated
Calling ETH.config
Output of the serial monitor with the comment block _uncommented_:

Code: Select all

[  1875][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type SPI_MASTER_SCK (34) successfully set to 0x420491d8
[  1887][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type SPI_MASTER_MISO (35) successfully set to 0x42049100
[  1899][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type SPI_MASTER_MOSI (36) successfully set to 0x42049028
[  1911][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type SPI_MASTER_SS (37) successfully set to 0x42048f14
[  1923][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type GPIO (1) successfully set to 0x42062600
[  1934][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 39 successfully set to type GPIO (1) with bus 0x28
[  1944][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 39 successfully set to type SPI_MASTER_SCK (34) with bus 0x1
[  1955][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type GPIO (1) successfully set to 0x42062600
[  1966][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 41 successfully set to type GPIO (1) with bus 0x2a
[  1976][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 41 successfully set to type SPI_MASTER_MISO (35) with bus 0x1
[  1986][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type GPIO (1) successfully set to 0x42062600
[  1997][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 40 successfully set to type GPIO (1) with bus 0x29
[  2007][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 40 successfully set to type SPI_MASTER_MOSI (36) with bus 0x1
[  2018][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type ETHERNET_SPI (47) successfully set to 0x42047df0
[  2030][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type GPIO (1) successfully set to 0x42062600
[  2041][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 45 successfully set to type GPIO (1) with bus 0x2e
[  2051][V][esp32-hal-periman.c:174] perimanSetPinBusExtraType(): Successfully set extra_type ETH_CS[0] for pin 45
n  2094][V][ETH.cp[p : 120049]4 ]_[oVn]E[tehsEpv3e2n-th(al-perima)n:. ce:t1h600 ]S tpaerrtiemda
SetPinBus(): Pin 38 successfully set to type ETHERNET_SPI (47) with bus 0x3fc9a898
[  2110][V][NetworkEvents.cpp:119] checkForEvent(): Network Event: 1 - ETH_START
[  2117][I][PostOfficeEthenetESP32.cpp:77] parseNetworkEvent(): [PostOfficeEthernetESP32] Ethernet Started
[  2110][V][esp32-hal-periman.c:174] perimanSetPinBusExtraType(): Successfully set extra_type ETH_IRQ for pin 38
[  2136][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 37 successfully set to type ETHERNET_SPI (47) with bus 0x3fc9a898
[  2148][V][esp32-hal-periman.c:174] perimanSetPinBusExtraType(): Successfully set extra_type ETH_RST for pin 37

~snip~

============ After Setup End =============
[  4094][V][ETH.cpp:95] _onEthEvent(): eth0 Connected
[  4099][V][NetworkEvents.cpp:119] checkForEvent(): Network Event: 3 - ETH_CONNECTED
[  4099][V][NetworkInterface.cpp:97] _onIpEvent(): eth0 Got New IP: 192.168.1.98 MASK: 255.255.255.0 GW: 192.168.1.2
[  4117][I][PostOfficeEthenetESP32.cpp:89] parseNetworkEvent(): [PostOfficeEthernetESP32] Ethernet Connected
[  4127][I][PostOfficeEthenetESP32.cpp:90] parseNetworkEvent(): [PostOfficeEthernetESP32] Link Speed: 100
[  4141][V][NetworkEvents.cpp:119] checkForEvent(): Network Event: 5 - ETH_GOT_IP
[  4148][I][PostOfficeEthenetESP32.cpp:102] parseNetworkEvent(): [PostOfficeEthernetESP32] Ethernet got IP: 'eth0'
*eth0: <UP,100M,FULL_DUPLEX,AUTO,ADDR:0x1> (DHCPC_OFF,GARP,IP_MOD)
      ether 36:B7:DA:5B:1E:7C
      inet 192.168.1.98 netmask 255.255.255.0 broadcast 192.168.1.255
      gateway 192.168.1.2 dns 1.1.1.1

During program loop each time I call NetworkClient (every 10 seconds), I get the following:

Code: Select all

 [ 43010][I][NetworkClient.cpp:257] connect(): select returned due to timeout 3000 ms for fd 48
Followed by a continuous stream of the following after about 2 minutes:

Code: Select all

E (84708) w5500.mac: unexpected error 0x104
E (84712) w5500.mac: emac_w5500_alloc_recv_buf(627): invalid frame length 2

I've also tried setting the ip address to 0 but this didn't work either.

Code: Select all

    IPAddress ip(192, 168, 1, 98);
    IPAddress dns(1, 1, 1, 1);
    IPAddress gateway(192, 168, 1, 2);
    IPAddress subnet(255, 255, 255, 0);
    ETH.config(ip, gateway, subnet, dns);
As it results in the same issue as not calling ETH.config() (ARDUINO_EVENT_ETH_GOT_IP is never called)


The ETH API docs (linked above) are a little light on detail, and I'm struggling to get this working. Any assistance would be greatly appreciated!

lbernstone
Posts: 828
Joined: Mon Jul 22, 2019 3:20 pm

Re: Help with ETH - unable to get IP address DHCP

Postby lbernstone » Mon Sep 23, 2024 6:53 pm

So, static assignment works, but is the device actually connected? Can you ping it if you make a static IP? Can it reach the outside world (eg, sntp)?

ESP_ondrej
Posts: 211
Joined: Fri May 07, 2021 10:35 am

Re: Help with ETH - unable to get IP address DHCP

Postby ESP_ondrej » Tue Sep 24, 2024 8:07 am

That W5500 error messages indicate that there is something wrong with SPI communication. How is your W5500 connected to ESP32-S3? Is it custom board or via wires? What Arduino version do you use? Is other device connected to the same SPI bus?

Who is online

Users browsing this forum: No registered users and 135 guests