-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(common_examples): Add common examples aplicable for all phys
- Loading branch information
1 parent
2ca2628
commit 1ea002e
Showing
38 changed files
with
541 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# For more information about build system see | ||
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html | ||
# The following five lines of boilerplate have to be in your project's | ||
# CMakeLists in this exact order for cmake to work correctly | ||
cmake_minimum_required(VERSION 3.16) | ||
|
||
include($ENV{IDF_PATH}/tools/cmake/project.cmake) | ||
project(iperf) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Iperf example | ||
|
||
This exmaple provides a simple way to measure network performance using iperf. | ||
|
||
## About the example | ||
The example uses `iperf` and `iperf-cmd` components for an iperf implementation and provides command line interface for it. It provides DHCP server functionality for connecting to another ESP32, instead of a PC. | ||
|
||
## Configuring the example | ||
Using `idf.py menuconfig` set up the Ethernet configuration, in the `Example option` you can enable DHCP server with `Act as a DHCP server`. This will make ESP32 run an instance of DHCP server per interface assignign IP addresses in the subnet 192.168.1.0/24. | ||
|
||
## Running the example | ||
You will see `esp>` prompt appear in ESP32 console. Run `iperf -h` to see iperf command options. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
idf_component_register(SRCS "iperf.c" | ||
INCLUDE_DIRS ".") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
menu "Example options" | ||
config EXAMPLE_ACT_AS_DHCP_SERVER | ||
bool "Act as a DHCP server" | ||
default n | ||
help | ||
Set ESP32 to act as DHCP server instead of as a client. | ||
endmenu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
dependencies: | ||
espressif/ethernet_init: | ||
override_path: '../../../ethernet_init/' | ||
espressif/iperf-cmd: "^0.1.1" | ||
espressif/iperf: "^0.1.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include "esp_netif.h" | ||
#include "esp_eth.h" | ||
#include "esp_event.h" | ||
#include "esp_console.h" | ||
#include "ethernet_init.h" | ||
#include "iperf_cmd.h" | ||
#include "sdkconfig.h" | ||
|
||
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; | ||
esp_eth_handle_t *eth_handles; | ||
esp_netif_config_t cfg; | ||
esp_netif_inherent_config_t eth_netif_cfg; | ||
esp_netif_init(); | ||
esp_event_loop_create_default(); | ||
ethernet_init_all(ð_handles, ð_port_cnt); | ||
|
||
#if CONFIG_EXAMPLE_ACT_AS_DHCP_SERVER | ||
// Config for ESP32 to act as DHCP server | ||
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 | ||
}; | ||
#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 | ||
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 | ||
}; | ||
for (int i = 0; i < eth_port_cnt; i++) { | ||
sprintf(if_key_str, "ETH_%d", i); | ||
sprintf(if_desc_str, "eth%d", i); | ||
eth_netif_cfg.if_key = if_key_str; | ||
eth_netif_cfg.if_desc = if_desc_str; | ||
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]); | ||
} | ||
esp_console_repl_t *repl = NULL; | ||
esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT(); | ||
esp_console_dev_uart_config_t uart_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT(); | ||
esp_console_new_repl_uart(&uart_config, &repl_config, &repl); | ||
app_register_iperf_commands(); | ||
esp_console_start_repl(repl); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CONFIG_EXAMPLE_ACT_AS_DHCP_SERVER=n |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CONFIG_EXAMPLE_ACT_AS_DHCP_SERVER=y |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# For more information about build system see | ||
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html | ||
# The following five lines of boilerplate have to be in your project's | ||
# CMakeLists in this exact order for cmake to work correctly | ||
cmake_minimum_required(VERSION 3.16) | ||
|
||
include($ENV{IDF_PATH}/tools/cmake/project.cmake) | ||
project(tcp_client) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# TCP client example | ||
|
||
This example provides a basic implementation of TCP client for ESP32 with drivers from this repository | ||
|
||
## About the example | ||
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 | ||
|
||
## 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.`. | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
idf_component_register(SRCS "tcp_client.c" | ||
INCLUDE_DIRS ".") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
menu "Example options" | ||
config EXAMPLE_SERVER_IP_ADDRESS | ||
string "Server IP address" | ||
default "192.168.1.1" | ||
help | ||
TCP server IP. | ||
endmenu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
dependencies: | ||
idf: | ||
version: '>=5.0' | ||
ethernet_init: | ||
version: '*' | ||
# For local development use the local copy of the component | ||
override_path: '../../../ethernet_init/' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include "freertos/FreeRTOS.h" | ||
#include "freertos/task.h" | ||
#include "esp_netif.h" | ||
#include "esp_eth.h" | ||
#include "esp_event.h" | ||
#include "esp_log.h" | ||
#include "ethernet_init.h" | ||
#include "lwip/sockets.h" | ||
#include "sdkconfig.h" | ||
|
||
#define SOCKET_PORT 5000 | ||
#define SOCKET_MAX_LENGTH 128 | ||
|
||
static const char *TAG = "tcp_client"; | ||
static SemaphoreHandle_t x_got_ip_semaphore; | ||
|
||
/** 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 *data) | ||
{ | ||
ip_event_got_ip_t *event = (ip_event_got_ip_t *) data; | ||
const esp_netif_ip_info_t *ip_info = &event->ip_info; | ||
|
||
ESP_LOGI(TAG, "Ethernet Got IP Address"); | ||
ESP_LOGI(TAG, "~~~~~~~~~~~"); | ||
ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip)); | ||
ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask)); | ||
ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw)); | ||
ESP_LOGI(TAG, "~~~~~~~~~~~"); | ||
xSemaphoreGive(x_got_ip_semaphore); | ||
} | ||
|
||
void app_main(void) | ||
{ | ||
// Create default event loop that running in background | ||
ESP_ERROR_CHECK(esp_event_loop_create_default()); | ||
// Initialize semaphore | ||
x_got_ip_semaphore = xSemaphoreCreateBinary(); | ||
// Initialize Ethernet driver | ||
uint8_t eth_port_cnt = 0; | ||
esp_eth_handle_t *eth_handles; | ||
char if_key_str[10]; | ||
char if_desc_str[10]; | ||
ESP_ERROR_CHECK(ethernet_init_all(ð_handles, ð_port_cnt)); | ||
esp_netif_init(); | ||
// Register user defined event handers | ||
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL)); | ||
// Create instance(s) of esp-netif for Ethernet(s) | ||
if (eth_port_cnt == 1) { | ||
// Use ESP_NETIF_DEFAULT_ETH when just one Ethernet interface is used and you don't need to modify | ||
// default esp-netif configuration parameters. | ||
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH(); | ||
esp_netif_t *eth_netif = esp_netif_new(&cfg); | ||
// Attach Ethernet driver to TCP/IP stack | ||
ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handles[0]))); | ||
} else { | ||
// Use ESP_NETIF_INHERENT_DEFAULT_ETH when multiple Ethernet interfaces are used and so you need to modify | ||
// esp-netif configuration parameters for each interface (name, priority, etc.). | ||
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 | ||
}; | ||
|
||
for (int i = 0; i < eth_port_cnt; i++) { | ||
sprintf(if_key_str, "ETH_%d", i); | ||
sprintf(if_desc_str, "eth%d", i); | ||
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); | ||
|
||
// Attach Ethernet driver to TCP/IP stack | ||
ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handles[i]))); | ||
esp_eth_start(eth_handles[i]); | ||
} | ||
} | ||
// Wait until IP address is assigned to this device | ||
xSemaphoreTake(x_got_ip_semaphore, portMAX_DELAY); | ||
ESP_LOGI(TAG, "TCP client is started, waiting for the server to accept a connection."); | ||
int client_fd; | ||
struct sockaddr_in server; | ||
char rxbuffer[SOCKET_MAX_LENGTH] = {0}; | ||
char txbuffer[SOCKET_MAX_LENGTH] = {0}; | ||
client_fd = socket(AF_INET, SOCK_STREAM, 0); | ||
server.sin_family = AF_INET; | ||
server.sin_port = htons(SOCKET_PORT); | ||
server.sin_addr.s_addr = inet_addr(CONFIG_EXAMPLE_SERVER_IP_ADDRESS); | ||
ESP_ERROR_CHECK(connect(client_fd, (struct sockaddr *)&server, sizeof(struct sockaddr))); | ||
int transmission_cnt = 0; | ||
while (1) { | ||
snprintf(txbuffer, SOCKET_MAX_LENGTH, "Transmission #%d. Hello from ESP32 TCP client", ++transmission_cnt); | ||
ESP_LOGI(TAG, "Transmitting: \"%s\"", txbuffer); | ||
write(client_fd, txbuffer, SOCKET_MAX_LENGTH); | ||
read(client_fd, rxbuffer, SOCKET_MAX_LENGTH); | ||
ESP_LOGI(TAG, "Received \"%s\"", rxbuffer); | ||
memset(txbuffer, 0, SOCKET_MAX_LENGTH); | ||
memset(rxbuffer, 0, SOCKET_MAX_LENGTH); | ||
vTaskDelay(pdMS_TO_TICKS(500)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/env python3 | ||
import socket | ||
import argparse | ||
import logging | ||
import signal | ||
|
||
parser = argparse.ArgumentParser(description='Serve TCP connection using berkley sockets and wait for connections', epilog='Part of the tcp_client example for esp_eth_drivers') | ||
parser.add_argument('ip') | ||
args = parser.parse_args() | ||
|
||
SOCKET_PORT = 5000 | ||
|
||
# setup sigint handler | ||
signal.signal(signal.SIGINT, lambda s, f : exit(0)) | ||
|
||
logger = logging.getLogger("tcp_server") | ||
logging.basicConfig(format="%(name)s :: %(levelname)-8s :: %(message)s", level=logging.DEBUG) | ||
logger.info("Listening on %s:%d", args.ip, SOCKET_PORT) | ||
|
||
# init server | ||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
sock.bind((args.ip, 5000)) | ||
# listen for incoming connections | ||
sock.listen(1) | ||
|
||
counter = 1 | ||
while True: | ||
conn, address = sock.accept() | ||
logger.debug("Accepted connection from %s:%d", address[0], address[1]) | ||
while True: | ||
try: | ||
data = conn.recv(128).decode() | ||
except ConnectionAbortedError: | ||
logger.info("Connection closed by client") | ||
break | ||
logger.debug("Received: \"%s\"", data) | ||
msg = f"Transmission {counter}: Hello from Python" | ||
logger.debug("Transmitting: \"%s\"", msg) | ||
conn.sendall(str.encode(msg)) | ||
counter += 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# For more information about build system see | ||
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html | ||
# The following five lines of boilerplate have to be in your project's | ||
# CMakeLists in this exact order for cmake to work correctly | ||
cmake_minimum_required(VERSION 3.16) | ||
|
||
set(COMPONENT main) | ||
|
||
include($ENV{IDF_PATH}/tools/cmake/project.cmake) | ||
project(tcp_server) |
Oops, something went wrong.