How to fix esp_eth_new_netif_glue() returning NULL

Problem

While initializing your ESP32’s Ethernet interface using esp_eth_new_netif_glue(), you get a NULL pointer back.

esp_eth_netif_glue_handle_t netif_glue = esp_eth_new_netif_glue(eth_handle);
if(netif_glue == nullptr) {
    log_e("esp_eth_new_netif_glue failed");
}

Solution

The typical issue why this fails is that the default event loop is not running which in turn causes esp_event_handler_instance_register() to fail with ESP_ERR_INVALID_STATE.

Start the default event loop before calling esp_eth_new_netif_glue():

esp_err_t err = esp_event_loop_create_default();
if(err != ESP_OK) {
    log_e("esp_event_loop_create_default failed");
    return;
}