How to assign Ethernet Static IP address

RON91JP
Posts: 1
Joined: Tue Dec 01, 2020 4:49 am

How to assign Ethernet Static IP address

Postby RON91JP » Tue Dec 01, 2020 5:07 am

Hello,

I am now using a board named ESP32-EVB and tried to build an Ethernet connection.
I want to disable the DHCP and want to assign a static IP.
I used the below program but I couldn't get an IP address.

Code: Select all

#include <string.h>
#include "sdkconfig.h"
#include "esp_event.h"
#include "esp_wifi.h"
#include "esp_wifi_default.h"
#if CONFIG_EXAMPLE_CONNECT_ETHERNET
#include "esp_eth.h"
    ret = tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH);
    tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &ip_config);
    tcpip_adapter_ip_info_t ip_config;
    ip4addr_aton(CONFIG_ETHERNET_HELPER_STATIC_IP4_ADDRESS, &ip_info.ip);
    ip4addr_aton(CONFIG_ETHERNET_HELPER_STATIC_IP4_GATEWAY, &ip_info.netmask);
    ip4addr_aton(CONFIG_ETHERNET_HELPER_STATIC_IP4_NETMASK, &ip_info.gw);
    ESP_LOGI(TAG, ret);
#endif
#include "esp_log.h"
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "lwip/err.h"
#include "lwip/sys.h"

#include "ethernet_connect.h"

#define GOT_IPV4_BIT BIT(0)
#define GOT_IPV6_BIT BIT(1)
#define CONNECTED_BITS (GOT_IPV4_BIT)

static EventGroupHandle_t s_connect_event_group;
static esp_ip4_addr_t s_ip_addr;
static const char *s_connection_name;
static esp_netif_t *s_example_esp_netif = NULL;

 

static const char *TAG = "online_connection";
static void start(void);
static void stop(void);

 
static void on_got_ip(void *arg, esp_event_base_t event_base,
                      int32_t event_id, void *event_data)
{

     ESP_LOGI(TAG, "Got IP event!");
    ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
    memcpy(&s_ip_addr, &event->ip_info.ip, sizeof(s_ip_addr));
    xEventGroupSetBits(s_connect_event_group, GOT_IPV4_BIT);
}

esp_err_t example_connect(void)
{
    if (s_connect_event_group != NULL)
    {
        return ESP_ERR_INVALID_STATE;
    }

    s_connect_event_group = xEventGroupCreate();
    start();
    ESP_ERROR_CHECK(esp_register_shutdown_handler(&stop));
    ESP_LOGI(TAG, "Waiting for IP");
   
    ESP_LOGI(TAG, "Connected to %s", s_connection_name);
    ESP_LOGI(TAG, "IPv4 address: " IPSTR, IP2STR(&s_ip_addr));
    xEventGroupWaitBits(s_connect_event_group, CONNECTED_BITS, true, true, portMAX_DELAY);
    ESP_LOGI(TAG, "IPv4 address: " IPSTR, IP2STR(&s_ip_addr));
    return ESP_OK;
}
esp_err_t example_disconnect(void)
{
    if (s_connect_event_group == NULL) {
        return ESP_ERR_INVALID_STATE;
    }
    vEventGroupDelete(s_connect_event_group);
    s_connect_event_group = NULL;
    stop();
    ESP_LOGI(TAG, "Disconnected from %s", s_connection_name);
    s_connection_name = NULL;
    return ESP_OK;
}


#ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET

static esp_eth_handle_t eth_handle = NULL;
static esp_eth_mac_t *s_mac = NULL;
static esp_eth_phy_t *s_phy = NULL;
static void *s_eth_glue = NULL;

static void start(void)
{
    esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
    esp_netif_t *eth_netif = esp_netif_new(&cfg);
    assert(eth_netif);
    s_example_esp_netif = eth_netif;

 

    // Set default handlers to process TCP/IP stuffs
    ESP_ERROR_CHECK(esp_eth_set_default_handlers(eth_netif));
    ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip, NULL));


  //Configuration using LAN8720
    eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
    eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();

    phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR;
    phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO;
    mac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO;
    mac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO;

    s_mac = esp_eth_mac_new_esp32(&mac_config);
    s_phy = esp_eth_phy_new_lan8720(&phy_config);
    esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy);

    ESP_ERROR_CHECK(esp_eth_driver_install(&config, &eth_handle));
    ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle)));
    ESP_ERROR_CHECK(esp_eth_start(eth_handle));

    s_connection_name = "ETH";
}

static void stop(void)
{
    ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip));
    ESP_ERROR_CHECK(esp_eth_stop(eth_handle));
    ESP_ERROR_CHECK(esp_eth_del_netif_glue(s_eth_glue));
    ESP_ERROR_CHECK(esp_eth_clear_default_handlers(s_example_esp_netif));
    ESP_ERROR_CHECK(esp_eth_driver_uninstall(eth_handle));
    ESP_ERROR_CHECK(s_phy->del(s_phy));
    ESP_ERROR_CHECK(s_mac->del(s_mac));

    esp_netif_destroy(s_example_esp_netif);
    s_example_esp_netif = NULL;
}
#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET


esp_netif_t *get_example_netif(void)
{
    return s_example_esp_netif;
}
ethernet_connect.h

Code: Select all

#pragma once

 

#ifdef __cplusplus
extern "C" {
#endif

 

#include "esp_err.h"
#include "esp_netif.h"

 

#define CONFIG_ESP32_SPIRAM_SUPPORT 1
#define CONFIG_MBEDTLS_EXTERNAL_MEM_ALLOC 1
#define CONFIG_EXAMPLE_CONNECT_ETHERNET 1
#define CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET 1
#define CONFIG_EXAMPLE_ETH_PHY_LAN8720 1
#define CONFIG_EXAMPLE_ETH_MDC_GPIO 23
#define CONFIG_EXAMPLE_ETH_MDIO_GPIO 18
#define CONFIG_EXAMPLE_ETH_PHY_ADDR 0
#define CONFIG_EXAMPLE_ETH_PHY_RST_GPIO 12

 

#ifdef CONFIG_USE_STATIC_IP
#define CONFIG_ETHERNET_HELPER_STATIC_IP4_ADDRESS "192.168.4.3"
#define CONFIG_ETHERNET_HELPER_STATIC_IP4_GATEWAY "192.168.4.1"
#define CONFIG_ETHERNET_HELPER_STATIC_IP4_NETMASK "255.255.255.0"
#endif //CONFIG_USE_STATIC_IP

 

#ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET
//#define EXAMPLE_INTERFACE get_example_netif()

 

#define EXAMPLE_INTERFACE TCPIP_ADAPTER_IF_ETH
#endif

 

#ifdef CONFIG_EXAMPLE_CONNECT_WIFI
//#define EXAMPLE_INTERFACE get_example_netif()
#define EXAMPLE_INTERFACE TCPIP_ADAPTER_IF_STA
#endif

 

// #define CONFIG_EXAMPLE_CONNECT_IPV6 

 

/**
 * @brief Configure Wi-Fi or Ethernet, connect, wait for IP
 *
 * This all-in-one helper function is used in protocols examples to
 * reduce the amount of boilerplate in the example.
 *
 * It is not intended to be used in real world applications.
 * See examples under examples/wifi/getting_started/ and examples/ethernet/
 * for more complete Wi-Fi or Ethernet initialization code.
 *
 * Read "Establishing Wi-Fi or Ethernet Connection" section in
 * examples/protocols/README.md for more information about this function.
 *
 * @return ESP_OK on successful connection
 */
esp_err_t example_connect(void);

 

/**
 * Counterpart to example_connect, de-initializes Wi-Fi or Ethernet
 */
esp_err_t example_disconnect(void);
esp_netif_t *get_example_netif(void);

 

#ifdef __cplusplus
}
#endif
Output
I (523) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (611) system_api: Base MAC address is not set
I (611) system_api: read default base MAC address from EFUSE
I (641) esp_eth.netif.glue: 98:f4:ab:79:ae:fb
I (641) esp_eth.netif.glue: ethernet attached to netif
I (651) online_connection: Waiting for IP
I (651) online_connection: Connected to ETH
I (651) online_connection: IPv4 address: 0.0.0.0

ESP_cermak
Posts: 69
Joined: Thu Nov 01, 2018 8:32 am

Re: How to assign Ethernet Static IP address

Postby ESP_cermak » Tue Dec 01, 2020 9:24 am

The code is almost correct (assume you've accidentally pasted the code somewhere into the include section, but the principle seems okay), but you won't receive an IP event when using static address. Also I'd suggest not mixing up the esp-netif and tcpip_adapter API.

Here's an example of using esp-netif code place just after netif starts:

Code: Select all

 esp_netif_dhcps_stop(netif);
 char* ip= "192.168.X.X";
 char* gateway = "192.168.X.X";
 char* netmask = "255.255.X.X";
 esp_netif_ip_info_t info_t;
 memset(&info_t, 0, sizeof(esp_netif_ip_info_t));
 ip4addr_aton((const char *)ip, &info_t.ip.addr);
 ip4addr_aton((const char *)gateway, &info_t.gw.addr);
 ip4addr_aton((const char *)netmask, &info_t.netmask.addr);
 esp_netif_set_ip_info(netif, &info_t);

User avatar
urbanze
Posts: 301
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Re: How to assign Ethernet Static IP address

Postby urbanze » Wed Jul 06, 2022 8:13 pm

ESP_cermak wrote:
Tue Dec 01, 2020 9:24 am
The code is almost correct (assume you've accidentally pasted the code somewhere into the include section, but the principle seems okay), but you won't receive an IP event when using static address. Also I'd suggest not mixing up the esp-netif and tcpip_adapter API.

Here's an example of using esp-netif code place just after netif starts:

Code: Select all

 esp_netif_dhcps_stop(netif);
 char* ip= "192.168.X.X";
 char* gateway = "192.168.X.X";
 char* netmask = "255.255.X.X";
 esp_netif_ip_info_t info_t;
 memset(&info_t, 0, sizeof(esp_netif_ip_info_t));
 ip4addr_aton((const char *)ip, &info_t.ip.addr);
 ip4addr_aton((const char *)gateway, &info_t.gw.addr);
 ip4addr_aton((const char *)netmask, &info_t.netmask.addr);
 esp_netif_set_ip_info(netif, &info_t);
I trying to set static IP on ETHERNET and another static IP in WIFI_STA, but doesnt work.
Ethernet init

Code: Select all

esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH();
esp_netif_t *eth_netif = esp_netif_new(&netif_cfg);
...
esp_netif_ip_info_t ip_info;
inet_aton(st_ip,  &ip_info.ip);
inet_aton(st_gw,  &ip_info.gw);
inet_aton(st_msk, &ip_info.netmask);
esp_netif_dhcpc_stop(eth_netif);
esp_netif_set_ip_info(eth_netif, &ip_info);

WiFi init

Code: Select all

esp_netif_t *netif_sta = esp_netif_create_default_wifi_sta();
...
esp_netif_ip_info_t ip_info;
inet_aton(st_ip,  &ip_info.ip);
inet_aton(st_gw,  &ip_info.gw);
inet_aton(st_msk, &ip_info.netmask);
esp_netif_dhcpc_stop(netif_sta);
esp_netif_set_ip_info(netif_sta, &ip_info);

The code works but when both are active (logically with different ips), only the WiFi is configured and the ETH remains without the static ip (being in DHCP).

However, the strangest thing is that when I remove the static ip from WiFi, the ETH get static ip correctly.

Can anyone help me with this static ip problem on both interfaces simultaneously?

User avatar
urbanze
Posts: 301
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Re: How to assign Ethernet Static IP address

Postby urbanze » Tue Jul 12, 2022 8:18 pm

Someone can help me?

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

Re: How to assign Ethernet Static IP address

Postby ESP_ondrej » Wed Jul 13, 2022 6:43 am

That's really strange. What version of ESP-IDF do you use? We try to reproduce.

User avatar
urbanze
Posts: 301
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Re: How to assign Ethernet Static IP address

Postby urbanze » Wed Jul 13, 2022 10:16 pm

ESP_ondrej wrote:
Wed Jul 13, 2022 6:43 am
That's really strange. What version of ESP-IDF do you use? We try to reproduce.
I use release/v4.2 in my company products. Thanks for help!

Andrr Filippov
Posts: 2
Joined: Wed Jul 27, 2022 11:02 am

Re: How to assign Ethernet Static IP address

Postby Andrr Filippov » Wed Jul 27, 2022 1:41 pm

Hi !
I have tried to reproduce your issue on release/v4.2.3.
I was able to set static IP for both Ethernet/Wi-Fi and ping it.
This is my esp-netif configs:
Wi-FI:

Code: Select all

 esp_netif_dhcpc_stop(esp_netif);

 char* ip= "192.168.137.113";
 char* gateway = "192.168.137.1";
 char* netmask = "255.255.255.0";
 esp_netif_ip_info_t info_t;
 memset(&info_t, 0, sizeof(esp_netif_ip_info_t));
 ipaddr_aton((const char *)ip, &info_t.ip.addr);
 ipaddr_aton((const char *)gateway, &info_t.gw.addr);
 ipaddr_aton((const char *)netmask, &info_t.netmask.addr);
 esp_netif_set_ip_info(esp_netif, &info_t);
Ethernet:

Code: Select all

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

Please, provide additional information -> commit ID, debugging logs, sdkconfig.
Also, please, look at my code above and check with yours again.
Maybe you set incorrect network configs such as (ip/gateway/netmask)

Who is online

Users browsing this forum: No registered users and 197 guests