From 1a9b41ea862c601f5396b0cd54cd2f49a07f30f1 Mon Sep 17 00:00:00 2001 From: Bogdan Kolendovskyy Date: Fri, 3 May 2024 15:19:57 +0200 Subject: [PATCH] Assign different subnets to different network interfaces --- common_examples/tcp_client/README.md | 20 +++++- common_examples/tcp_server/README.md | 2 +- common_examples/tcp_server/main/tcp_server.c | 67 ++++++++++++-------- 3 files changed, 58 insertions(+), 31 deletions(-) diff --git a/common_examples/tcp_client/README.md b/common_examples/tcp_client/README.md index 1ed02ad..b67e114 100644 --- a/common_examples/tcp_client/README.md +++ b/common_examples/tcp_client/README.md @@ -6,15 +6,29 @@ This example provides a basic implementation of TCP client for ESP32 with driver TCP client is designed to periodically transmit to the server, and accept responses, which are printed to the console. ## Configuring the example -Configure the example using `idf.py menuconfig`, according to your setup, provide settings for Ethernet initialization. Next, go to `Example options` and input the IP address of your PC in the local network to which ESP32 is connected. If you want to connect this to another ESP32 device running `tcp_server` example, make sure to the the `Server IP address` to 196.168.1.1 +Configure the example using `idf.py menuconfig`, according to your setup, provide settings for Ethernet initialization. Next, go to `Example options` and set the `Server IP address`. + +### Determining the server IP address +If you are running a TCP server on your PC using `tcp_server.py` - run a command to see your IP: +* Windows - `ipconfig /all` +* macOS/Linux - `ifconfig` + +If you are running a TCP server on another ESP32 along with a DHCP server - see the output of your server for lines such as: +``` +I (6349) tcp_server: -------- +I (6359) tcp_server: Network Interface 0: 192.168.0.1 +I (6359) tcp_server: Network Interface 1: 192.168.1.1 +I (6369) tcp_server: -------- +``` +It will list all your network interfaces and the IP address assigned to it. Depending on the interface you are connected to - set the `Server IP address` accordingly. ## Running the example -After you obtain an IP address you will see a message reading `TCP client is started, waiting for the server to accept a connection.`. +After you obtain an IP address you will see a message reading `TCP client is started, waiting for the server to accept a connection.` If you are connecting the device to a PC - start the server with `tcp_server.py`. The command to do so is `tcp_server.py IP` to start listening on the specified IP address. **Important**: use the IP address of your PC in the local network to which ESP32 is connected. It **must** match the IP address you've set in the `Example options`. -You will see incoming messages in ESP32 console or as an as output of `tcp_server.py` if you have ran it. \ No newline at end of file +You will see incoming messages in the ESP32 console or as an output of `tcp_server.py` if you are connecting the ESP32 to your PC and running a TCP server using a script. \ No newline at end of file diff --git a/common_examples/tcp_server/README.md b/common_examples/tcp_server/README.md index ceaeb05..3484d3a 100644 --- a/common_examples/tcp_server/README.md +++ b/common_examples/tcp_server/README.md @@ -7,7 +7,7 @@ TCP server is designed to accept transmissions from the client, print them to th ## Configuring the example Configure the example using `idf.py menuconfig`, according to your setup, provide settings for Ethernet initialization. If you want to connect to another ESP32, set the -`Act as DHCP server` option in the `Example options`, the IP address of the device will be 192.168.1.1. +`Act as DHCP server` option in the `Example options`, each IP addres will be assigned an IP 192.168.n.1, where `n` is the number of the interface. ## Running the example To transmit data between your PC and ESP32 you need to wait until it obtains an IP address from DHCP server and run tcp client script. diff --git a/common_examples/tcp_server/main/tcp_server.c b/common_examples/tcp_server/main/tcp_server.c index 73f1c76..4ba9809 100644 --- a/common_examples/tcp_server/main/tcp_server.c +++ b/common_examples/tcp_server/main/tcp_server.c @@ -13,23 +13,11 @@ #define SOCKET_MAX_LENGTH 128 static const char *TAG = "tcp_server"; -esp_netif_ip_info_t ip_info = { - .ip = {.addr = ESP_IP4TOADDR(192, 168, 1, 1)}, - .netmask = {.addr = ESP_IP4TOADDR(255, 255, 255, 0)}, - .gw = {.addr = ESP_IP4TOADDR(192, 168, 1, 1)} -}; - -static void start_dhcp_server_at_connection(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data) -{ - esp_netif_dhcpc_stop(esp_netif); - esp_netif_set_ip_info(esp_netif, &ip_info); - esp_netif_dhcps_start(esp_netif); -} - - void app_main(void) { uint8_t eth_port_cnt = 0; + char if_key_str[10]; + char if_desc_str[10]; esp_eth_handle_t *eth_handles; esp_netif_config_t cfg; esp_netif_inherent_config_t eth_netif_cfg; @@ -38,27 +26,55 @@ void app_main(void) ethernet_init_all(ð_handles, ð_port_cnt); #if CONFIG_EXAMPLE_ACT_AS_DHCP_SERVER - // Config for ESP32 to act as DHCP server + esp_netif_ip_info_t *ip_infos; + + ip_infos = calloc(eth_port_cnt, sizeof(esp_netif_ip_info_t)); + eth_netif_cfg = (esp_netif_inherent_config_t) { .get_ip_event = IP_EVENT_ETH_GOT_IP, .lost_ip_event = 0, .flags = ESP_NETIF_DHCP_SERVER, - .ip_info = &ip_info, - .if_key = "ETH_DHCPS", - .if_desc = "eth", .route_prio = 50 }; + cfg = (esp_netif_config_t) { + .base = ð_netif_cfg, + .stack = ESP_NETIF_NETSTACK_DEFAULT_ETH + }; + + for (uint8_t i = 0; i < eth_port_cnt; i++) { + sprintf(if_key_str, "ETH_S%d", i); + sprintf(if_desc_str, "eth%d", i); + + esp_netif_ip_info_t ip_info_i = { + .ip = {.addr = ESP_IP4TOADDR(192, 168, i, 1)}, + .netmask = {.addr = ESP_IP4TOADDR(255, 255, 255, 0)}, + .gw = {.addr = ESP_IP4TOADDR(192, 168, i, 1)} + }; + ip_infos[i] = ip_info_i; + + eth_netif_cfg.if_key = if_key_str; + eth_netif_cfg.if_desc = if_desc_str; + eth_netif_cfg.route_prio -= i * 5; + eth_netif_cfg.ip_info = &(ip_infos[i]); + esp_netif_t *eth_netif = esp_netif_new(&cfg); + ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handles[i]))); + esp_eth_start(eth_handles[i]); + esp_netif_dhcpc_stop(eth_netif); + esp_netif_dhcps_start(eth_netif); + } + ESP_LOGI(TAG, "--------"); + for (uint8_t i = 0; i < eth_port_cnt; i++) { + ESP_LOGI(TAG, "Network Interface %d: " IPSTR, i, IP2STR(&ip_infos[i].ip)); + } + ESP_LOGI(TAG, "--------"); #else if (eth_port_cnt == 1) { // Use default config when using one interface eth_netif_cfg = *(ESP_NETIF_BASE_DEFAULT_ETH); } else { - // Set behavioral config to support multiple interfaces + // Set config to support multiple interfaces eth_netif_cfg = (esp_netif_inherent_config_t) ESP_NETIF_INHERENT_DEFAULT_ETH(); } -#endif - char if_key_str[10]; - char if_desc_str[10]; cfg = (esp_netif_config_t) { .base = ð_netif_cfg, .stack = ESP_NETIF_NETSTACK_DEFAULT_ETH @@ -71,13 +87,10 @@ void app_main(void) eth_netif_cfg.route_prio -= i * 5; esp_netif_t *eth_netif = esp_netif_new(&cfg); ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handles[i]))); - -#if CONFIG_EXAMPLE_ACT_AS_DHCP_SERVER - esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, start_dhcp_server_at_connection, eth_netif); -#endif - esp_eth_start(eth_handles[i]); } +#endif + int server_fd, client_fd; struct sockaddr_in server, client; char rxbuffer[SOCKET_MAX_LENGTH] = {0};