ESP32 WROVER E not reaching the TCP host via Ethernet

Thread Starter

Vilius_Zalenas

Joined Jul 24, 2022
159
Hi,

I am quite new to ESP32 programming. I have a task to establish the TCP communication via ESP and PC (PC wont send anything to the ESP, It will be one way communication from ESP to PC). I am using the ESP Ethernet KIT (ESP32 WROVER E)

I took an existing basic ethernet example form the ESP IDF built in project examples and with a little help of AI and testing came up with this code:


C:
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_netif.h"
#include "esp_eth.h"
#include "esp_event.h"
#include "esp_log.h"
#include "lwip/sockets.h"
#include "lwip/dns.h"
#include "lwip/netdb.h"
#include "ethernet_init.h"
#include "sdkconfig.h"

static const char *TAG = "eth_example";
static const char *PC_IP = "10.10.16.111";
static const int PC_PORT = 5190;

// Static IP configuration
static const char *ESP_IP = "10.10.16.112"; // Change this to your desired static IP
static const char *ESP_NETMASK = "255.255.255.0";
static const char *ESP_GATEWAY = "10.10.16.1";

static EventGroupHandle_t eth_event_group;
static const int ETH_CONNECTED_BIT = BIT0;

static void send_message_to_pc();
static void periodic_task(void *pvParameters);

/** Event handler for Ethernet events */
static void eth_event_handler(void *arg, esp_event_base_t event_base,
                              int32_t event_id, void *event_data)
{
    esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;

    switch (event_id) {
    case ETHERNET_EVENT_CONNECTED:
        xEventGroupSetBits(eth_event_group, ETH_CONNECTED_BIT);
        ESP_LOGI(TAG, "Ethernet Link Up");
        break;
    case ETHERNET_EVENT_DISCONNECTED:
        xEventGroupClearBits(eth_event_group, ETH_CONNECTED_BIT);
        ESP_LOGI(TAG, "Ethernet Link Down");
        break;
    default:
        break;
    }
}

/** Event handler for IP_EVENT_ETH_GOT_IP */
static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
                                 int32_t event_id, void *event_data)
{
    // Configure static IP settings
    esp_netif_ip_info_t ip_info;
    ip_info.ip.addr = inet_addr(ESP_IP);
    ip_info.netmask.addr = inet_addr(ESP_NETMASK);
    ip_info.gw.addr = inet_addr(ESP_GATEWAY);

    // Apply static IP settings
    esp_netif_set_ip_info(esp_netif_get_handle_from_ifkey("ETH"), &ip_info);

    // Send a message to the PC when IP is obtained
    send_message_to_pc();
}

static void send_message_to_pc() {
    int sockfd;
    struct sockaddr_in server_addr;

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) {
        ESP_LOGE(TAG, "Unable to create socket");
        return;
    }

    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(PC_PORT);
    inet_aton(PC_IP, &server_addr.sin_addr);

    if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
        ESP_LOGE(TAG, "Connection failed: %s", strerror(errno));
        close(sockfd);
        return;
    }

    const char *message = "Hello from ESP32!";
    if (write(sockfd, message, strlen(message)) < 0) {
        ESP_LOGE(TAG, "Error sending message: %s", strerror(errno));
    } else {
        ESP_LOGI(TAG, "Message sent successfully");
    }

    close(sockfd);
}

static void periodic_task(void *pvParameters) {
    while (1) {
        // Wait for the Ethernet to be connected
        xEventGroupWaitBits(eth_event_group, ETH_CONNECTED_BIT, pdTRUE, pdTRUE, portMAX_DELAY);

        // Send a message to the PC periodically
        send_message_to_pc();

        // Adjust the delay based on your requirements
        vTaskDelay(pdMS_TO_TICKS(5000));  // Delay for 5 seconds
    }
}

void app_main(void)
{
    eth_event_group = xEventGroupCreate();

    uint8_t eth_port_cnt = 0;
    esp_eth_handle_t *eth_handles;
    ESP_ERROR_CHECK(example_eth_init(&eth_handles, &eth_port_cnt));

    ESP_ERROR_CHECK(esp_netif_init());
    ESP_ERROR_CHECK(esp_event_loop_create_default());

    if (eth_port_cnt == 1) {
        esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
        esp_netif_t *eth_netif = esp_netif_new(&cfg);
        ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handles[0])));
    } else {
        esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
        esp_netif_config_t cfg_spi = {
            .base = &esp_netif_config,
            .stack = ESP_NETIF_NETSTACK_DEFAULT_ETH
        };
        char if_key_str[10];
        char if_desc_str[10];
        char num_str[3];
        for (int i = 0; i < eth_port_cnt; i++) {
            itoa(i, num_str, 10);
            strcat(strcpy(if_key_str, "ETH_"), num_str);
            strcat(strcpy(if_desc_str, "eth"), num_str);
            esp_netif_config.if_key = if_key_str;
            esp_netif_config.if_desc = if_desc_str;
            esp_netif_config.route_prio -= i * 5;
            esp_netif_t *eth_netif = esp_netif_new(&cfg_spi);
            ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handles[i])));
        }
    }

    ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
    ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));

    // Create a periodic task to send messages to the PC
    xTaskCreate(&periodic_task, "periodic_task", 4096, NULL, 5, NULL);

    for (int i = 0; i < eth_port_cnt; i++) {
        ESP_ERROR_CHECK(esp_eth_start(eth_handles[i]));
    }
}
This should send message to the PC every 5 seconds (for now I am in testing phase). However, when I flash this code to the ESP, eventually I get these logs:

I (376) app_start: Starting scheduler on CPU0
I (380) app_start: Starting scheduler on CPU1
I (380) main_task: Started on CPU0
I (390) main_task: Calling app_main()
I (430) esp_eth.netif.netif_glue: 08:3a:8d:13:5a:73
I (430) esp_eth.netif.netif_glue: ethernet attached to netif
I (2030) main_task: Returned from app_main()
I (2030) eth_example: Ethernet Link Up
E (2030) eth_example: Connection failed: Host is unreachable

On the PC side, I also run a very simple python TCP listening script just to acknowledge the TCP packets:

C:
import socket

PC_IP = "10.10.16.111"
PC_PORT = 5190

def start_server():
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind((PC_IP, PC_PORT))
    server_socket.listen(1)

    print(f"Listening for connections on {PC_IP}:{PC_PORT}")

    while True:
        client_socket, client_address = server_socket.accept()
        print(f"Connection established from {client_address}")

        message = client_socket.recv(1024).decode("utf-8")
        print(f"Received message: {message}")

        client_socket.close()

if __name__ == "__main__":
    start_server()
I have created dedicated network adapter on the PC and disabled the DHCP for sure. I assigned the static IP, Subnet and Gateway for both PC and ESP. Picture of my ipconfig is in the attachments, although I am pretty confident I understand the network settings well enough and set everything up the right way...

I have also disabled both windows defender and firewall (once this caused enormous problems for me, but this time all of the protection is off.)

I also ran wireshark to check whats going on, but as expected, I did not see a single packet with the source of 10.10.16.112 (ESP IP).

These are all the tricks I did on my own, but it seems it is not enough for ESP... Any ideas how to establish the TCP commuication by reaching the host? (according to the error logs.) Thank you in advance.
 

Attachments

Thread Starter

Vilius_Zalenas

Joined Jul 24, 2022
159
Thank you for sharing this, when I run this code I get ESP logs:


I (0) cpu_start: App cpu up.
I (266) cpu_start: Pro cpu start user code
I (266) cpu_start: cpu freq: 240000000 Hz
I (266) cpu_start: Application information:
I (270) cpu_start: Project name: ESP32_ETHERNET
I (276) cpu_start: App version: 1
I (280) cpu_start: Compile time: Nov 28 2023 09:13:11
I (286) cpu_start: ELF file SHA256: f568f56fef6a9f47...
I (292) cpu_start: ESP-IDF: v5.1.1
I (297) cpu_start: Min chip rev: v0.0
I (302) cpu_start: Max chip rev: v3.99
I (307) cpu_start: Chip rev: v3.1
I (312) heap_init: Initializing. RAM available for dynamic allocation:
I (319) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (325) heap_init: At 3FFB3430 len 0002CBD0 (178 KiB): DRAM
I (331) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (337) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (344) heap_init: At 4008C920 len 000136E0 (77 KiB): IRAM
I (351) spi_flash: detected chip: generic
I (355) spi_flash: flash io: dio
I (359) app_start: Starting scheduler on CPU0
I (364) app_start: Starting scheduler on CPU1
I (364) main_task: Started on CPU0
I (374) main_task: Calling app_main()
I (414) esp_eth.netif.netif_glue: 08:3a:8d:13:5a:73
I (414) esp_eth.netif.netif_glue: ethernet attached to netif
I (2014) eth_example: Ethernet Started
I (2014) main_task: Returned from app_main()
I (2014) eth_example: Ethernet Link Up
I (2014) eth_example: Ethernet HW Addr 08:3a:8d:13:5a:73

Not sure what the last line means (is it IPv6 of my PC?) But it looks like I am getting somewhere. The only question is what shoul I do next? How can I send messages to PC? How do I make a TCP communication based on this example?
 

geekoftheweek

Joined Oct 6, 2013
1,183
Based on what I can see (I don't have much time at the moment to dig further) it looks like you are not getting an IP address. You may have to enable DHCP for the moment. It should print out IPv4 when calling
Code:
got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
I have only worked with the WiFi side of things with ESP so far. I'll do what I can.

I would say myself in that handler (got_ip_event_handler) I would create some sort of timer task to create the socket and connect to your PC at your chosen interval.

I just made the switch from using the Arduino IDE to the Espressif IDF so it's been a bit of a learning curve on my end with the RTOS.

Edit:
I forgot you said you were wanting to use a static IP. In that case it probably won't ever reach the got_ip_event_handler and we'll have to figure out something different. One step at a time...
 

geekoftheweek

Joined Oct 6, 2013
1,183
This page https://esp32.com/viewtopic.php?t=18353 looks to be a good direction although they don't show the finished code.

Here's what I imagine based on what it seems to show:

Code:
#include <string.h>
#include "sdkconfig.h"
#include "esp_event.h"
#include "esp_wifi.h"
#include "esp_wifi_default.h"

#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));

/************************** added fix ?!? */
    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);

/************************** end of added fix ?!? */

    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;
}
Unfortunately I can't try it myself. I hope it helps, but if not that is about the extent of what I can come up with.
 

Thread Starter

Vilius_Zalenas

Joined Jul 24, 2022
159
This page https://esp32.com/viewtopic.php?t=18353 looks to be a good direction although they don't show the finished code.

Here's what I imagine based on what it seems to show:

Code:
#include <string.h>
#include "sdkconfig.h"
#include "esp_event.h"
#include "esp_wifi.h"
#include "esp_wifi_default.h"

#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));

/************************** added fix ?!? */
    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);

/************************** end of added fix ?!? */

    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;
}
Unfortunately I can't try it myself. I hope it helps, but if not that is about the extent of what I can come up with.
Ok, want to thank you for your help first.

After no results I decided to switch to DHCP, now my PC TCP server python code looks like:

Python:
import socket

def start_server(host, port):
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind((host, port))
    server_socket.listen(1)
    print(f"Server listening on {host}:{port}")

    while True:
        client_socket, client_address = server_socket.accept()
        print(f"Accepted connection from {client_address}")

        try:
            while True:
                data = client_socket.recv(1024)
                if not data:
                    break

                # Echo client credentials
                client_socket.sendall(f"Server received data from {client_address}: {data}".encode())

        except Exception as e:
            print(f"Error: {e}")

        finally:
            print(f"Connection with {client_address} closed")
            client_socket.close()

if __name__ == "__main__":
    # Set your server's host and port
    server_host = "169.254.157.27"  # Change this to your server's IP address or use "0.0.0.0" for all available interfaces
    server_port = 5190        # Change this to your desired port number

    start_server(server_host, server_port)
Just a simple socket listener...
For the ESP, I took another esp-idf example (tcp_transport_client), configured it for the ethernet (in menuconfig) and only entered the IP and PORT numbers by myself, except those 2 lines, everything is just as in example, nothing is changed:
Python:
/*
 * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
 *
 * SPDX-License-Identifier: Unlicense OR CC0-1.0
 */
#include <string.h>
#include <sys/param.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_netif.h"
#include "protocol_examples_common.h"
#include "esp_transport.h"
#include "esp_transport_tcp.h"
#include "esp_transport_socks_proxy.h"

#define TARGET_ADDR "169.254.157.27"
#define TARGET_PORT 5190

#ifdef CONFIG_EXAMPLE_ENABLE_PROXY
#define PROXY_ADDR CONFIG_EXAMPLE_PROXY_ADDR
#define PROXY_PORT CONFIG_EXAMPLE_PROXY_PORT
#endif

static const char *TAG = "tcp_transport_client";
static const char *payload = "Message from ESP32\n";

static void tcp_transport_client_task(void *pvParameters)
{
    char rx_buffer[128];
    char host_ip[] = TARGET_ADDR;
    esp_transport_handle_t transport = esp_transport_tcp_init();

    #ifdef CONFIG_EXAMPLE_ENABLE_PROXY
    /*
     * The socks transport is a composed transport, so we save the previously created
     * handler to use it as a parent transport, so our transport is now socks over tcp.
     * We could have used the ssl transport as parent and we can use a socks transport as a
     * parent to websocket transport.
     *
     */
    esp_transport_handle_t parent = transport;
    esp_transport_socks_proxy_config_t proxy_config = {.port = PROXY_PORT, .address = PROXY_ADDR, .version = SOCKS4};
    transport = esp_transport_socks_proxy_init(parent, &proxy_config);
    #endif

  while (1) {
        if (transport == NULL) {
            ESP_LOGE(TAG, "Error occurred during esp_transport_proxy_init()");
            break;
        }
        int err = esp_transport_connect(transport, TARGET_ADDR, TARGET_PORT, -1);
        if (err != 0) {
            ESP_LOGE(TAG, "Client unable to connect: errno %d", errno);
            break;
        }
        ESP_LOGI(TAG, "Successfully connected");

        while (1) {
            int bytes_written = esp_transport_write(transport, payload, strlen(payload), 0);
            if (bytes_written < 0) {
                ESP_LOGE(TAG, "Error occurred during sending: esp_transport_write() returned %d, errno %d", bytes_written, errno);
                break;
            }
            int len = esp_transport_read(transport, rx_buffer, sizeof(rx_buffer) - 1, 0);
            // Error occurred during receiving
            if (len < 0) {
                ESP_LOGE(TAG, "recv failed: esp_transport_read() returned %d, errno %d", len, errno);
                break;
            }
            // Data received
            rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string
            ESP_LOGI(TAG, "Received %d bytes from %s:", len, host_ip);
            ESP_LOGI(TAG, "Received data : %s", rx_buffer);

            vTaskDelay(2000 / portTICK_PERIOD_MS);
        }

        ESP_LOGE(TAG, "Shutting down transport and restarting...");
        esp_transport_close(transport);
    }
    esp_transport_destroy(transport);

    #ifdef CONFIG_EXAMPLE_ENABLE_PROXY
    esp_transport_destroy(parent);
    #endif

  vTaskDelete(NULL);
}

void app_main(void)
{
    esp_log_level_set("transport", ESP_LOG_VERBOSE);
    esp_log_level_set("transport_base", ESP_LOG_VERBOSE);
    esp_log_level_set("transport_proxy", ESP_LOG_VERBOSE);

    ESP_ERROR_CHECK(nvs_flash_init());
    ESP_ERROR_CHECK(esp_netif_init());
    ESP_ERROR_CHECK(esp_event_loop_create_default());

    /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
     * Read "Establishing Wi-Fi or Ethernet Connection" section in
     * examples/protocols/README.md for more information about this function.
     */
    ESP_ERROR_CHECK(example_connect());

    xTaskCreate(tcp_transport_client_task, "tcp_transport_client", 4096, NULL, 5, NULL);
}
Now my logs are:

I (0) cpu_start: App cpu up.
I (347) cpu_start: Pro cpu start user code
I (347) cpu_start: cpu freq: 240000000 Hz
I (347) cpu_start: Application information:
I (352) cpu_start: Project name: ESPTCP
I (357) cpu_start: App version: 1
I (361) cpu_start: Compile time: Nov 30 2023 12:30:47
I (367) cpu_start: ELF file SHA256: c67c42535ed92630...
I (373) cpu_start: ESP-IDF: v5.1.1
I (378) cpu_start: Min chip rev: v0.0
I (383) cpu_start: Max chip rev: v3.99
I (388) cpu_start: Chip rev: v3.1
I (393) heap_init: Initializing. RAM available for dynamic allocation:
I (400) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (406) heap_init: At 3FFB3C70 len 0002C390 (176 KiB): DRAM
I (412) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (418) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (425) heap_init: At 4008D22C len 00012DD4 (75 KiB): IRAM
I (432) spi_flash: detected chip: generic
I (436) spi_flash: flash io: dio
I (440) app_start: Starting scheduler on CPU0
I (445) app_start: Starting scheduler on CPU1
I (445) main_task: Started on CPU0
I (455) main_task: Calling app_main()
I (525) esp_eth.netif.netif_glue: 08:3a:8d:13:5a:73
I (525) esp_eth.netif.netif_glue: ethernet attached to netif
I (2225) ethernet_connect: Waiting for IP(s).
I (2225) ethernet_connect: Ethernet Link Up
I (3485) ethernet_connect: Got IPv6 event: Interface "example_netif_eth" address: fe80:0000:0000:0000:0a3a:8dff:fe13:5a73, type: ESP_IP6_ADDR_IS_LINK_LOCAL

It seems it is still not getting IPv4 address from DHCP, which is enabled on my ethernet 2 adapter
My firewall is still off... Not even sure what is the plan for further debugging... Any ideas?
 

Attachments

geekoftheweek

Joined Oct 6, 2013
1,183
It seems it is still not getting IPv4 address from DHCP, which is enabled on my ethernet 2 adapter
My firewall is still off... Not even sure what is the plan for further debugging... Any ideas?
Unfortunately I'm out of ideas. I personally use Linux so I have no clue where or what to look at on Windows. I have not yet worked at all with IPv6 so I may be wrong, but it does look like you are getting an address assigned through that route.

Try pinging your IPv6 address from the log output and see if it comes back as valid. On my system I was able to ping my ethernet card like
Code:
ping fe80::ce96:e5ff:fe36:8786
It may be possible the ESP32 it is not requesting an IPv4 address for some reason or another. I wouldn't see why it wouldn't, but if it's getting one type of address and not the other it might be worth digging through menuconfig one more time.
 
Top