Running a Web Server on the WT32-ETH01 Module Using ESP-IDF
Introduction
The WT32-ETH01 is a powerful development board that combines the capabilities of the ESP32 microcontroller with Ethernet connectivity. This module is ideal for projects that require reliable and high-speed network communication, such as industrial automation, IoT gateways, and networked sensors.
In this guide, we’ll walk you through setting up a simple web server on the WT32-ETH01 module using Visual Studio Code with the ESP-IDF extension.
By the end of this tutorial, you’ll have a functioning web server that responds with “Hello, World!” when accessed via a web browser.
Components Needed
- WT32-ETH01 module
- USB-to-Serial adapter ( FTDI FT232RL)
- Ethernet cable
- Jumper wires(F-F)
- Router
Softwares Needed
- Visual Studio Code
The WT32-ETH01 module is suitable for various applications, including:
- Industrial Control Systems: For using Ethernet’s reliability for controlling machinery and processes.
- IoT Gateways: Collect data from sensors and transmit it over a wired network.
- Home Automation: Integrate devices like smart thermostats and security systems into a wired network.
- Data Loggers: Store and transmit large amounts of data without the limitations of Wi-Fi.
Circuit Diagram
Before uploading the code,
- TXD0 ↔ RXD (on USB-to-Serial adapter)
- RXD0 ↔ TXD (on USB-to-Serial adapter)
- GND ↔ GND
- 5V ↔ VCC (set to 5V on USB-to-Serial adapter)
- IO0 ↔ GND (for programming mode)
- Connect the EN pin to GND momentarily and disconnect. (ESP32 will reach the bootloader mode)
Upload the code.
After Uploading,
- Disconnect IO0 from GND for normal operation.
- Momentarily connect the EN pin to GND to reset the ESP32. (This can be used to reset the module during programming and after uploading the code.)
Note: Connect the ethernet cable from WT32-ETH01 to the router.
Software Setup
Install Visual Studio Code and ESP-IDF Extension
- If you haven’t already, download and install Visual Studio Code.
- Open VS Code.
- Go to the “Extensions” tab (Ctrl+Shift+X).
- Search for “Espressif IDF”.
- Click “Install” on the “Espressif IDF” extension.
Install ESP-IDF Toolchain
- Press “F1” to open the command palette.
- Type “ESP-IDF: Configure ESP-IDF extension” and select it.
- Follow the on-screen instructions to install ESP-IDF and its dependencies.
Project Setup
- Create a New ESP-IDF Project.
- Open the “Command Palette” (F1) and select “ESP-IDF: New Project”
- Select a path for your project, and give a name. For example “wt32_eth_webserver”
- Choose a template.
- Open the new project folder in VS Code.
- Replace “main.c” with the following Code(main-> main.c)
#include "esp_err.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_eth.h"
#include "esp_netif.h"
#include "nvs_flash.h"
#include "driver/gpio.h"
#include "esp_http_server.h"
// Define logging tag
static const char *TAG = "ethernet_example";
// Global Ethernet handle
static esp_eth_handle_t eth_handle = NULL;
// HTTP server handle
static httpd_handle_t server = NULL;
// Forward declaration of functions
static void start_webserver(void);
static void stop_webserver(void);
static esp_err_t hello_get_handler(httpd_req_t *req);
// 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_netif_t *eth_netif = (esp_netif_t *)arg;
if (event_id == ETHERNET_EVENT_CONNECTED) {
ESP_LOGI(TAG, "Ethernet Link Up");
} else if (event_id == ETHERNET_EVENT_DISCONNECTED) {
ESP_LOGI(TAG, "Ethernet Link Down");
stop_webserver();
} else if (event_id == ETHERNET_EVENT_START) {
ESP_LOGI(TAG, "Ethernet Started");
} else if (event_id == ETHERNET_EVENT_STOP) {
ESP_LOGI(TAG, "Ethernet Stopped");
stop_webserver();
}
}
// Event handler for IP events
static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
ESP_LOGI(TAG, "Got IP Address: " IPSTR, IP2STR(&event->ip_info.ip));
start_webserver();
}
// HTTP GET handler for "/"
static esp_err_t hello_get_handler(httpd_req_t *req)
{
const char* resp_str = "Hello, World!";
httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
return ESP_OK;
}
// Function to start the web server
static void start_webserver(void)
{
if (server != NULL) {
ESP_LOGI(TAG, "Web server already running");
return;
}
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
// Start the httpd server
ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
if (httpd_start(&server, &config) == ESP_OK) {
// Register URI handlers
httpd_uri_t hello = {
.uri = "/",
.method = HTTP_GET,
.handler = hello_get_handler,
.user_ctx = NULL
};
httpd_register_uri_handler(server, &hello);
} else {
ESP_LOGI(TAG, "Error starting server!");
}
}
// Function to stop the web server
static void stop_webserver(void)
{
if (server) {
// Stop the httpd server
httpd_stop(server);
server = NULL;
}
}
// Ethernet initialization function for ESP32 with LAN8720 PHY
void init_ethernet()
{
// Initialize TCP/IP stack
ESP_ERROR_CHECK(esp_netif_init());
// Create default event loop if not already created
ESP_ERROR_CHECK(esp_event_loop_create_default());
// Create netif for Ethernet
esp_netif_config_t netif_config = ESP_NETIF_DEFAULT_ETH();
esp_netif_t *eth_netif = esp_netif_new(&netif_config);
// Configure GPIO16 to enable the oscillator
gpio_reset_pin(GPIO_NUM_16);
gpio_set_direction(GPIO_NUM_16, GPIO_MODE_OUTPUT);
gpio_set_level(GPIO_NUM_16, 1); // Set GPIO16 high to enable the oscillator
// Configure default Ethernet MAC and PHY settings
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
// Configure the SMI (MDC and MDIO) GPIO pins
esp32_emac_config.smi_gpio.mdc_num = GPIO_NUM_23; // MDC pin
esp32_emac_config.smi_gpio.mdio_num = GPIO_NUM_18; // MDIO pin
// Create MAC instance using the internal ESP32 EMAC
esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
// Configure PHY settings
eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
phy_config.phy_addr = 1; // Adjust based on your PHY address
phy_config.reset_gpio_num = -1; // Set to -1 if the PHY reset pin is not used
// Create PHY instance for LAN8720
esp_eth_phy_t *phy = esp_eth_phy_new_lan87xx(&phy_config);
// Ethernet configuration linking MAC and PHY
esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
// Install Ethernet driver
ESP_ERROR_CHECK(esp_eth_driver_install(ð_config, ð_handle));
// Attach Ethernet driver to TCP/IP stack
ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle)));
// Register the event handler for Ethernet events, passing the netif as argument
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, eth_netif));
// Register IP event handler
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
// Start the Ethernet driver
ESP_ERROR_CHECK(esp_eth_start(eth_handle));
}
void app_main()
{
// Initialize NVS (non-volatile storage)
ESP_ERROR_CHECK(nvs_flash_init());
// Initialize Ethernet and establish a connection
init_ethernet();
// Main loop (optional)
while (true) {
vTaskDelay(pdMS_TO_TICKS(1000)); // Polling interval
}
}
To upload the code,
- Make the esp32 to bootloader mode as per the instructions in the top of the article.
- Then select the com port from the bottom bar. (Bottom left)
- Press “F1” and type “Build, Flash and Start a monitor on your device)
Note: If you don’t have F1 on your keyboard, Go to “View” -> “Commad palette”.
- After code is uploaded, remove IO0 from GND and momentarily connect EN with GND to operate normally, then you will see the ip assigned on the serial monitor.
- Then you need to copy the assigned IP and paste it on a browse,while connecting your PC to the same network. Ours is “192.168.8.165”.
- Then you will see,
Understanding the Code
Main Functions Used
static void start_webserver(void);
static void stop_webserver(void);
static esp_err_t hello_get_handler(httpd_req_t *req);
start_webserver: Function to start the web server.
stop_webserver: Function to stop the web server.
hello_get_handler: HTTP GET handler for incoming requests.
Event Handlers
Ethernet Event Handler
static void eth_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
{
// Event handling code
}
- Listens for Ethernet events such as link up/down, start, and stop.
- Starts or stops the web server based on the network status.
IP Event Handler
static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
// Event handling code
}
- Triggered when the device obtains an IP address.
- Starts the web server upon successful IP acquisition.
HTTP Server Functions
HTTP GET Handler
static esp_err_t hello_get_handler(httpd_req_t *req)
{
const char* resp_str = "Hello, World!";
httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
return ESP_OK;
}
- Responds to HTTP GET requests at the root URI.
- Sends a “Hello, World!” message to the client.
HTTP Server Functions
static void start_webserver(void)
{
// Code to start the HTTP server
}
Stop Web Server
static void stop_webserver(void)
{
// Code to stop the HTTP server
}
Ethernet Initialization
void init_ethernet()
{
// Initialization code
}
- Initializes the TCP/IP stack and event loop.
- Configures the Ethernet interface and PHY settings.
- Installs the Ethernet driver and starts the Ethernet interface.
Main Function
void app_main()
{
// Initialize NVS
ESP_ERROR_CHECK(nvs_flash_init());
// Initialize Ethernet and establish a connection
init_ethernet();
// Main loop
while (true) {
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
- Initializes non-volatile storage (NVS).
- Calls ‘init_ethernet()’ to set up the Ethernet interface.
- Contains an infinite loop to keep the main task alive.
Now you’ve successfully set up a simple web server on the WT32-ETH01 module using Ethernet connectivity.
References
Hope you enjoyed the article. Please comment below or send us an email to info@protonest.co, if you face any issues when implementing.
Contact us for any consultations or projects related to IoT and embedded systems.
Email: info@protonest.co
Protonest for more details.
Protonest specializes in transforming IoT ideas into reality. We offer prototyping services from concept to completion. Our commitment ensures that your visionary IoT concepts become tangible, innovative, and advanced prototypes.
Our Website: https://www.protonest.co/
Cheers!