Assign a static IP address to the the ethernet Port

pumpkin_pie
Posts: 5
Joined: Wed Oct 25, 2017 9:32 pm

Assign a static IP address to the the ethernet Port

Postby pumpkin_pie » Fri Jan 05, 2018 7:40 pm

Hello,

I am trying to assign a static IP address to the erthernet interface on my esp32. I am using the following code:

tcpip_adapter_ip_info_t ipInfo;

IP4_ADDR(&ipInfo.ip, 192,168,128,200);
IP4_ADDR(&ipInfo.gw, 192,168,128,100);
IP4_ADDR(&ipInfo.netmask, 255,255,255,0);
tcpip_adapter_set_ip_info(ESP_IF_ETH, &ipInfo);


When I run this, no IP addresses are set, and all the above parameters remain 0.0.0.0

Does anyone have any experience with this?

User avatar
Gfast2
Posts: 182
Joined: Fri Aug 11, 2017 1:52 am

Re: Assign a static IP address to the the ethernet Port

Postby Gfast2 » Sat Jan 06, 2018 2:33 am

Code: Select all

esp_err_t ret = ESP_OK;
    tcpip_adapter_ip_info_t ipInfo;

    // myIp -> structure that save your static ip settings
    inet_pton(AF_INET, myIp.ip,      &ipInfo.ip);
    inet_pton(AF_INET, myIp.gateway, &ipInfo.gw);
    inet_pton(AF_INET, myIp.netmask, &ipInfo.netmask);

    tcpip_adapter_init();
    ret = tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH); // ret=0x5000 -> tcpip_adapter_invalid_params, very old esp-idf didn't implementated this yet.
    ESP_LOGI(TAG, "dhcp client stop RESULT: %d", ret);
    tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_ETH, &ipInfo);

    esp_event_loop_init(system_event_cb, NULL);
    eth_config_t config = DEFAULT_ETHERNET_PHY_CONFIG; // Set the PHY address in the example configuration
    config.phy_addr = CONFIG_PHY_ADDRESS;
    config.gpio_config = eth_gpio_config_rmii;
    config.tcpip_input = tcpip_adapter_eth_input;
#ifdef CONFIG_PHY_USE_POWER_PIN
    // Replace the default 'power enable' function with an example-specific
    // one that toggles a power GPIO.
    config.phy_power_enable = phy_device_power_enable_via_gpio;
#endif
    ret = esp_eth_init(&config); // Init the ETH interface of ESP32

    if (ret == ESP_OK) {
        ESP_LOGI(TAG, "ETH interface get inited correctly!!!");
        esp_eth_enable();
    } else {
        ESP_LOGE(TAG, "ETH interface inited not correct!");
    }   

Cheers

Gfast2

krzysiekorion
Posts: 1
Joined: Fri May 24, 2019 6:26 pm

Re: Assign a static IP address to the the ethernet Port

Postby krzysiekorion » Fri May 24, 2019 6:54 pm

Hello,
I have the same problem, I cannot set static IP:
failed: esp_err_t 0x5001 (ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS)

I use ESP-IDF v4.0-dev-478-gdf61612 and ESP32 DevKit V1

Code: Select all

void eth_init()
{
	tcpip_adapter_init();

	ESP_ERROR_CHECK(esp_event_loop_init(eth_event_handler, NULL));
	ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_ETH));
	tcpip_adapter_ip_info_t info;
	memset(&info, 0, sizeof(info));
	IP4_ADDR(&info.ip, 192, 168, 1, 100);
	IP4_ADDR(&info.gw, 192, 168, 1, 1);
	IP4_ADDR(&info.netmask, 255, 255, 255, 0);
	ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_ETH, &info));

	eth_config_t config = DEFAULT_ETHERNET_PHY_CONFIG;
	config.phy_addr = CONFIG_PHY_ADDRESS;
	config.gpio_config = eth_gpio_config_rmii;
	config.tcpip_input = tcpip_adapter_eth_input;
	config.clock_mode = CONFIG_PHY_CLOCK_MODE;
#ifdef CONFIG_PHY_USE_POWER_PIN
	/* Replace the default 'power enable' function with an example-specific one
	 that toggles a power GPIO. */
	config.phy_power_enable = phy_device_power_enable_via_gpio;
#endif

	ESP_ERROR_CHECK(esp_eth_init(&config));
	ESP_ERROR_CHECK(esp_eth_enable()) ;
}
I can see that function: tcpip_adapter_dhcps_stop can work stil only for TCPIP_ADAPTER_IF_AP:

Code: Select all

/* only support ap now */
    if (tcpip_if != TCPIP_ADAPTER_IF_AP || tcpip_if >= TCPIP_ADAPTER_IF_MAX) {
Is my version too old? I work on the master branch.

akitonozaki
Posts: 1
Joined: Tue May 28, 2019 2:17 pm

Re: Assign a static IP address to the the ethernet Port

Postby akitonozaki » Tue May 28, 2019 2:20 pm

Make sure you are disabling the client (dhcpc). Your code is disabling the server (dhcps)

ParnikaGupta
Posts: 1
Joined: Mon Dec 28, 2020 9:54 am

Re: Assign a static IP address to the the ethernet Port

Postby ParnikaGupta » Mon Dec 28, 2020 10:02 am

This is working.

Put the code below: eth_netif = esp_netif_new(&cfg); in the cmd_ethernet.c file register_ethernet() function.

Code: Select all

ESP_ERROR_CHECK(esp_netif_dhcpc_stop(eth_netif));

    char* ip= "*.*.*.*";
    char* gateway = "*.*.*.1";
    char* netmask = "255.255.255.0";
    esp_netif_ip_info_t info_t;
    memset(&info_t, 0, sizeof(esp_netif_ip_info_t));
    info_t.ip.addr = esp_ip4addr_aton((const char *)ip);
    info_t.gw.addr = esp_ip4addr_aton((const char *)gateway);
    info_t.netmask.addr = esp_ip4addr_aton((const char *)netmask);
    esp_netif_set_ip_info(eth_netif, &info_t);    

    ESP_ERROR_CHECK(esp_eth_set_default_handlers(eth_netif));

Who is online

Users browsing this forum: axellin and 116 guests