### Hello Server 2 Example Source: https://github.com/khoih-prog/webserver_esp32_w5500/blob/main/README.md Similar to the Hello Server example, this demonstrates a basic web server setup. It might differ in specific configurations or response content, suitable for simple text-based responses. ```cpp #include #include // Define network parameters #define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT #define ETH_PHY_TYPE ETH_PHY_IP101 #define ETH_PHY_ADDR 1 #define ETH_MDC_PIN 23 #define ETH_MDIO_PIN 18 #define ETH_TYPE ETH_MODULE_W5500 #define ETH_POWER_PIN -1 WebServer server(80); void setup() { Serial.begin(115200); // Initialize Ethernet connection ETH.begin(ETH_ADDR, ETH_DNS, ETH_GATEWAY, ETH_MASK, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE, ETH_PHY_ADDR, ETH_PHY_TYPE); // Define web server route for the root path server.on("/", []() { server.send(200, "text/plain", "Hello World!"); }); // Start the server server.begin(); Serial.println("WebServer started."); } void loop() { server.handleClient(); delay(2); } ``` -------------------------------- ### Basic Web Server Example Source: https://github.com/khoih-prog/webserver_esp32_w5500/blob/main/README.md A fundamental example of setting up a web server. This code initializes the Ethernet connection and starts a server that can respond to basic HTTP requests. ```cpp #include #include // Define network parameters #define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT #define ETH_PHY_TYPE ETH_PHY_IP101 #define ETH_PHY_ADDR 1 #define ETH_MDC_PIN 23 #define ETH_MDIO_PIN 18 #define ETH_TYPE ETH_MODULE_W5500 #define ETH_POWER_PIN -1 WebServer server(80); void setup() { Serial.begin(115200); // Initialize Ethernet connection ETH.begin(ETH_ADDR, ETH_DNS, ETH_GATEWAY, ETH_MASK, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE, ETH_PHY_ADDR, ETH_PHY_TYPE); // Define a route for the root path server.on("/", []() { server.send(200, "text/plain", "Hello from ESP32 WebServer!"); }); // Start the server server.begin(); Serial.println("WebServer started."); } void loop() { server.handleClient(); delay(2); } ``` -------------------------------- ### WebServer - Basic HTTP Server Setup Source: https://context7.com/khoih-prog/webserver_esp32_w5500/llms.txt Instantiate a WebServer object, register URI handlers, start the server, and handle incoming client requests in the main loop. ```APIDOC ## WebServer — Basic HTTP Server ### Description Instantiate `WebServer` on a port, register URI handlers, call `begin()`, and dispatch in the main loop with `handleClient()`. The API is identical to the standard Arduino ESP32 WebServer. ### Setup Example ```cpp #include WebServer server(80); void handleRoot() { int sec = millis() / 1000; char html[256]; snprintf(html, sizeof(html), "

Hello from ESP32+W5500

Uptime: %d s

", sec); server.send(200, "text/html", html); } void handleNotFound() { server.send(404, "text/plain", "Not found: " + server.uri()); } void setup() { ESP32_W5500_onEvent(); ETH.begin(MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST); ESP32_W5500_waitForConnect(); server.on("/", handleRoot); server.on("/inline", []() { server.send(200, "text/plain", "Inline lambda handler"); }); server.onNotFound(handleNotFound); server.begin(); Serial.println("Server started at: http://" + ETH.localIP().toString()); } void loop() { server.handleClient(); } // GET / → 200 HTML page // GET /inline → 200 "Inline lambda handler" // GET /missing → 404 "Not found: /missing" ``` ``` -------------------------------- ### Basic HTTP Server Setup Source: https://context7.com/khoih-prog/webserver_esp32_w5500/llms.txt Instantiates a WebServer on a specified port, registers URI handlers, starts the server, and dispatches client requests in the main loop. The API is identical to the standard Arduino ESP32 WebServer. ```cpp #include WebServer server(80); void handleRoot() { int sec = millis() / 1000; char html[256]; snprintf(html, sizeof(html), "

Hello from ESP32+W5500

Uptime: %d s

", sec); server.send(200, "text/html", html); } void handleNotFound() { server.send(404, "text/plain", "Not found: " + server.uri()); } void setup() { ESP32_W5500_onEvent(); ETH.begin(MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST); ESP32_W5500_waitForConnect(); server.on("/", handleRoot); server.on("/inline", []() { server.send(200, "text/plain", "Inline lambda handler"); }); server.onNotFound(handleNotFound); server.begin(); Serial.println("Server started at: http://" + ETH.localIP().toString()); } void loop() { server.handleClient(); } // GET / → 200 HTML page // GET /inline → 200 "Inline lambda handler" // GET /missing → 404 "Not found: /missing" ``` -------------------------------- ### Hello Server Example Source: https://github.com/khoih-prog/webserver_esp32_w5500/blob/main/README.md A basic example demonstrating how to set up a simple web server that responds with plain text. This is useful for initial testing and understanding basic server operations. ```cpp #include #include // Define network parameters #define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT #define ETH_PHY_TYPE ETH_PHY_IP101 #define ETH_PHY_ADDR 1 #define ETH_MDC_PIN 23 #define ETH_MDIO_PIN 18 #define ETH_TYPE ETH_MODULE_W5500 #define ETH_POWER_PIN -1 WebServer server(80); void setup() { Serial.begin(115200); // Initialize Ethernet connection ETH.begin(ETH_ADDR, ETH_DNS, ETH_GATEWAY, ETH_MASK, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE, ETH_PHY_ADDR, ETH_PHY_TYPE); // Define web server route for the root path server.on("/", []() { server.send(200, "text/plain", "Hello from ESP32 WebServer!"); }); // Start the server server.begin(); Serial.println("WebServer started."); } void loop() { server.handleClient(); delay(2); } ``` -------------------------------- ### Start Web Server Source: https://github.com/khoih-prog/webserver_esp32_w5500/blob/main/README.md Initializes and starts the web server to listen for incoming connections. ```cpp void begin(); ``` -------------------------------- ### Web Client Example Source: https://github.com/khoih-prog/webserver_esp32_w5500/blob/main/README.md This example demonstrates how to use the ESP32 as a web client to fetch data from a remote server. It's useful for integrating with external APIs or web services. ```cpp #include #include #include // Define network parameters #define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT #define ETH_PHY_TYPE ETH_PHY_IP101 #define ETH_PHY_ADDR 1 #define ETH_MDC_PIN 23 #define ETH_MDIO_PIN 18 #define ETH_TYPE ETH_MODULE_W5500 #define ETH_POWER_PIN -1 WebServer server(80); void setup() { Serial.begin(115200); // Initialize Ethernet connection ETH.begin(ETH_ADDR, ETH_DNS, ETH_GATEWAY, ETH_MASK, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE, ETH_PHY_ADDR, ETH_PHY_TYPE); // Define a route to trigger the web client request server.on("/fetch", []() { HTTPClient http; http.begin("http://example.com"); // URL to fetch int httpCode = http.GET(); // Send HTTP GET request String payload = ""; if (httpCode > 0) { payload = http.getString(); // Get response payload Serial.println("HTTP Response code: " + String(httpCode)); Serial.println(payload); } else { Serial.println("Error on HTTP request"); } http.end(); // Close connection server.send(200, "text/plain", "Fetched data: " + payload); }); // Start the web server server.begin(); Serial.println("WebServer started."); } void loop() { server.handleClient(); delay(2); } ``` -------------------------------- ### MQTT Client Authentication Example Source: https://github.com/khoih-prog/webserver_esp32_w5500/blob/main/README.md This example demonstrates how to configure an MQTT client with authentication credentials. It's useful for connecting to MQTT brokers that require a username and password for secure communication. ```cpp #include #include #include // Define network parameters #define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT #define ETH_PHY_TYPE ETH_PHY_IP101 #define ETH_PHY_ADDR 1 #define ETH_MDC_PIN 23 #define ETH_MDIO_PIN 18 #define ETH_TYPE ETH_MODULE_W5500 #define ETH_POWER_PIN -1 // MQTT Broker details #define MQTT_HOST "your_mqtt_broker_address" #define MQTT_PORT 1883 #define MQTT_USER "your_mqtt_username" #define MQTT_PASS "your_mqtt_password" WebServer server(80); WiFiClient ethClient; PubSubClient client(ethClient); void setup() { Serial.begin(115200); // Initialize Ethernet connection ETH.begin(ETH_ADDR, ETH_DNS, ETH_GATEWAY, ETH_MASK, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE, ETH_PHY_ADDR, ETH_PHY_TYPE); // Set MQTT server details and connect client.setServer(MQTT_HOST, MQTT_PORT); client.setCallback(callback); // Start the web server server.on("/", []() { server.send(200, "text/plain", "Hello from ESP32 MQTT Client!"); }); server.begin(); Serial.println("WebServer started."); } void loop() { if (!client.connected()) { reconnect(); } client.loop(); server.handleClient(); delay(2); } void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); for (unsigned int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); } void reconnect() { while (!client.connected()) { Serial.print("Attempting MQTT connection..."); if (client.connect("ESP32Client", MQTT_USER, MQTT_PASS)) { Serial.println("connected"); client.subscribe("esp32/test"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); delay(5000); } } } ``` -------------------------------- ### MQTT Client Basic Example Source: https://github.com/khoih-prog/webserver_esp32_w5500/blob/main/README.md This example demonstrates basic MQTT client functionality, including connecting to a broker and publishing/subscribing to topics. It's suitable for simple IoT messaging scenarios. ```cpp #include #include #include // Define network parameters #define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT #define ETH_PHY_TYPE ETH_PHY_IP101 #define ETH_PHY_ADDR 1 #define ETH_MDC_PIN 23 #define ETH_MDIO_PIN 18 #define ETH_TYPE ETH_MODULE_W5500 #define ETH_POWER_PIN -1 // MQTT Broker details #define MQTT_HOST "your_mqtt_broker_address" #define MQTT_PORT 1883 WebServer server(80); WiFiClient ethClient; PubSubClient client(ethClient); void setup() { Serial.begin(115200); // Initialize Ethernet connection ETH.begin(ETH_ADDR, ETH_DNS, ETH_GATEWAY, ETH_MASK, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE, ETH_PHY_ADDR, ETH_PHY_TYPE); // Set MQTT server details and connect client.setServer(MQTT_HOST, MQTT_PORT); client.setCallback(callback); // Start the web server server.on("/", []() { server.send(200, "text/plain", "Hello from ESP32 MQTT Client!"); }); server.begin(); Serial.println("WebServer started."); } void loop() { if (!client.connected()) { reconnect(); } client.loop(); server.handleClient(); delay(2); } void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); for (unsigned int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); } void reconnect() { while (!client.connected()) { Serial.print("Attempting MQTT connection..."); if (client.connect("ESP32Client")) { Serial.println("connected"); client.subscribe("esp32/test"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); delay(5000); } } } ``` -------------------------------- ### POST Server Example Source: https://github.com/khoih-prog/webserver_esp32_w5500/blob/main/README.md This example demonstrates how to handle HTTP POST requests. It's useful for receiving data submitted from HTML forms or other clients. The server can then process the received data. ```cpp #include #include // Define network parameters #define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT #define ETH_PHY_TYPE ETH_PHY_IP101 #define ETH_PHY_ADDR 1 #define ETH_MDC_PIN 23 #define ETH_MDIO_PIN 18 #define ETH_TYPE ETH_MODULE_W5500 #define ETH_POWER_PIN -1 WebServer server(80); void setup() { Serial.begin(115200); // Initialize Ethernet connection ETH.begin(ETH_ADDR, ETH_DNS, ETH_GATEWAY, ETH_MASK, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE, ETH_PHY_ADDR, ETH_PHY_TYPE); // Handle POST requests to the root path server.on("/", HTTP_POST, []() { String message = "Received POST request. Arguments: "; if (server.hasArg("plain")) { message += "\n" + server.arg("plain"); } else { for (uint8_t i = 0; i < server.args(); i++) { message += "\n " + server.argName(i) + ": " + server.arg(i); } } server.send(200, "text/plain", message); }); // Start the server server.begin(); Serial.println("WebServer started."); } void loop() { server.handleClient(); delay(2); } ``` -------------------------------- ### HTTP Basic Authentication Example Source: https://github.com/khoih-prog/webserver_esp32_w5500/blob/main/README.md This example shows how to implement basic HTTP authentication for your web server. It requires a username and password to access protected resources. Ensure you handle credentials securely. ```cpp #include #include // Define network parameters #define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT #define ETH_PHY_TYPE ETH_PHY_IP101 #define ETH_PHY_ADDR 1 #define ETH_MDC_PIN 23 #define ETH_MDIO_PIN 18 #define ETH_TYPE ETH_MODULE_W5500 #define ETH_POWER_PIN -1 WebServer server(80); // Define username and password for authentication const char* user = "esp32"; const char* password = "12345"; void setup() { Serial.begin(115200); // Initialize Ethernet connection ETH.begin(ETH_ADDR, ETH_DNS, ETH_GATEWAY, ETH_MASK, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE, ETH_PHY_ADDR, ETH_PHY_TYPE); // Route for the root path with authentication server.on("/", HTTP_GET, []() { if (!server.authenticate(user, password)) { return server.requestAuthentication(); } server.send(200, "text/plain", "Hello authenticated user!"); }); // Start the server server.begin(); Serial.println("WebServer started."); } void loop() { server.handleClient(); delay(2); } ``` -------------------------------- ### ESP32 W5500 WebClient Example Output Source: https://github.com/khoih-prog/webserver_esp32_w5500/blob/main/README.md Terminal output from the WebClient example running on an ESP32_DEV with an ESP32_W5500 module. Shows network connection details and HTTP response. ```text Start WebClient on ESP32_DEV with ESP32_W5500 WebServer_ESP32_W5500 v1.5.3 for core v2.0.0+ [EWS] Default SPI pinout: [EWS] SPI_HOST: 2 [EWS] MOSI: 23 [EWS] MISO: 19 [EWS] SCK: 18 [EWS] CS: 5 [EWS] INT: 4 [EWS] SPI Clock (MHz): 25 [EWS] ========================= ETH Started ETH Connected ETH MAC: B4:E6:2D:E9:68:AC, IPv4: 192.168.2.78 FULL_DUPLEX, 100Mbps Starting connection to server... Connected to server HTTP/1.1 200 OK Date: Wed, 11 Jan 2023 19:19:39 GMT Content-Type: text/plain Content-Length: 2263 Connection: close x-amz-id-2: Tibw9r4/epZQzFM/rmO3cvTdq1DH4jSKocwQ9aMHx4RlkSotUFEGantuPNlKQKGqoHtdqwpFKw0= x-amz-request-id: SD2XWDHW10J2H90M Last-Modified: Wed, 23 Feb 2022 14:56:42 GMT ETag: "667cf48afcc12c38c8c1637947a04224" CF-Cache-Status: DYNAMIC Report-To: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=HDCMzIyDC7oeBtywXTEb50R8fKz1H%2B7cB18KeeJVTdmf6NrFvvbxfSPcInISlUQCNqHTVbYQp4rVNVXnwo8qvTMn8bP92ozfI53aScTnOJvPaBYaK4vOt%2FZxKfHsj3o%3D"}],"group":"cf-nel","max_age":604800} NEL: {"success_fraction":0,"report_to":"cf-nel","max_age":604800} Server: cloudflare CF-RAY: 78800158694b1839-EWR alt-svc: h3=":443"; ma=86400, h3-29=":443"; ma=86400 `:;;;,` .:;;:. .;;;;;;;;;;;` :;;;;;;;;;;: TM `;;;;;;;;;;;;;;;` :;;;;;;;;;;;;;;; :;;;;;;;;;;;;;;;;;; `;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;; .;;;;;;;;;;;;;;;;;;;; ;;;;;;;;:` `;;;;;;;;; ,;;;;;;;;.` .;;;;;;;; .;;;;;;, :;;;;;;; .;;;;;;; ;;;;;;; ;;;;;; ;;;;;;; ;;;;;;, ;;;;;;. ,;;;;; ;;;;;;.;;;;;;` ;;;;;; ;;;;;. ;;;;;;;;;;;` ``` ;;;;;` ;;;;; ;;;;;;;;;, ;;; .;;;;; `;;;;: `;;;;;;;; ;;; ;;;;; ,;;;;` `,,,,,,,, ;;;;;;; .,,;;;,,, ;;;;; :;;;;` .;;;;;;;; ;;;;;, :;;;;;;;; ;;;;; :;;;;` .;;;;;;;; `;;;;;; :;;;;;;;; ;;;;; .;;;;. ;;;;;;;. ;;; ;;;;; ;;;;; ;;;;;;;;; ;;; ;;;;; ;;;;; .;;;;;;;;;; ;;; ;;;;;, ;;;;;; `;;;;;;;;;;;; ;;;;; `;;;;;, .;;;;;; ;;;;;;; ;;;;;; ;;;;;;: :;;;;;;. ;;;;;;; ;;;;;; ;;;;;;;` .;;;;;;;, ;;;;;;;; ;;;;;;;: ;;;;;;;;;;:,:;;;;;;;;;: ;;;;;;;;;;:,;;;;;;;;;; `;;;;;;;;;;;;;;;;;;;. ;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;; :;;;;;;;;;;;;;;;;: ,;;;;;;;;;;;;;, ;;;;;;;;;;;;;; .;;;;;;;;;` ,;;;;;;;;: ;;; ;;;;;` ;;;;: .;; ;; ,;;;;;, ;;. `;, ;;;; ;;; ;;:;;; ;;;;;; .;; ;; ,;;;;;: ;;; `;, ;;;:;; ,;:; ;; ;; ;; ;; .;; ;; ,;, ;;;,`;, ;; ;; ;; ;: ;; ;; ;; ;; .;; ;; ,;, ;;;;`;, ;; ;;. ;: ;; ;;;;;: ;; ;; .;; ;; ,;, ;;`;;;, ;; ;;` ,;;;;; ;;`;; ;; ;; .;; ;; ,;, ;; ;;;, ;; ;; ;; ,;, ;; .;; ;;;;;: ;;;;;: ,;;;;;: ;; ;;, ;;;;;; ;; ;; ;; ;;` ;;;;. `;;;: ,;;;;;, ;; ;;, ;;;; Disconnecting from server... ``` -------------------------------- ### Advanced Web Server Example Source: https://github.com/khoih-prog/webserver_esp32_w5500/blob/main/README.md Demonstrates advanced features of the WebServer library, including handling different HTTP requests and serving dynamic content. This example is suitable for complex web interfaces. ```cpp #include #include // Define network parameters #define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT #define ETH_PHY_TYPE ETH_PHY_IP101 #define ETH_PHY_ADDR 1 #define ETH_MDC_PIN 23 #define ETH_MDIO_PIN 18 #define ETH_TYPE ETH_MODULE_W5500 #define ETH_POWER_PIN -1 // Use static IP address // #define STATIC_IP_ADDRESS #ifdef STATIC_IP_ADDRESS static uint8_t ETH_ADDR[4] = {192, 168, 1, 177}; static uint8_t ETH_MASK[4] = {255, 255, 255, 0}; static uint8_t ETH_DNS[4] = {192, 168, 1, 1}; #endif WebServer server(80); void setup() { Serial.begin(115200); // Initialize Ethernet connection ETH.begin(ETH_ADDR, ETH_DNS, ETH_GATEWAY, ETH_MASK, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE, ETH_PHY_ADDR, ETH_PHY_TYPE); #ifdef STATIC_IP_ADDRESS ETH.config(ETH_ADDR, ETH_DNS, ETH_GATEWAY, ETH_MASK); #endif // Print network info Serial.print("ETH MAC: "); Serial.println(ETH.macAddress()); Serial.print("ETH IPv4: "); Serial.println(ETH.localIP()); // Define web server routes server.on("/", []() { server.send(200, "text/plain", "Hello from ESP32 WebServer!"); }); server.on("/inline", []() { server.send(200, "text/plain", "This is an inline response."); }); server.on("/json", []() { server.send(200, "application/json", "{\"message\": \"Hello JSON!\"}"); }); server.on("/html", []() { String html = "ESP32 WebServer

Hello HTML!

This is an HTML response.

"; server.send(200, "text/html", html); }); server.onNotFound([]() { server.send(404, "text/plain", "Not found"); }); // Start the server server.begin(); Serial.println("WebServer started."); } void loop() { server.handleClient(); delay(2); } ``` -------------------------------- ### ETH.begin() Source: https://context7.com/khoih-prog/webserver_esp32_w5500/llms.txt Initializes the W5500 over SPI and starts the LwIP Ethernet driver. It can use the ESP32's built-in MAC address or a custom one, and uses DHCP by default. ```APIDOC ## ETH.begin() Initializes the W5500 over SPI and starts the LwIP Ethernet driver. Uses the ESP32's built-in hardware MAC address by default; an optional custom MAC can be supplied. DHCP is used unless `ETH.config()` is called beforehand. ```cpp // Signature: // bool begin(int MISO, int MOSI, int SCLK, int CS, int INT, // int SPICLOCK_MHZ = 25, int SPIHOST = SPI3_HOST, // uint8_t *W5500_Mac = W5500_Default_Mac); #include byte customMac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 }; void setup() { ESP32_W5500_onEvent(); // DHCP with built-in ESP32 MAC (recommended) ETH.begin(MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST); // — OR — DHCP with a custom MAC address // ETH.begin(MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, customMac); ESP32_W5500_waitForConnect(); // Returns true on success; ETH.localIP() is valid after waitForConnect() } ``` ``` -------------------------------- ### Bug Report Example Source: https://github.com/khoih-prog/webserver_esp32_w5500/blob/main/CONTRIBUTING.md An example of the information to include when submitting a bug report, covering IDE version, board type, core version, OS, and reproduction steps. ```text Arduino IDE version: 1.8.19 ESP32_DEV board ESP32 core v2.0.6 OS: Ubuntu 20.04 LTS Linux xy-Inspiron-3593 5.15.0-57-generic #63~20.04.1-Ubuntu SMP Wed Nov 30 13:40:16 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux Context: I encountered a crash while using this library Steps to reproduce: 1. ... 2. ... 3. ... 4. ... ``` -------------------------------- ### UDP NTP Client Example Source: https://github.com/khoih-prog/webserver_esp32_w5500/blob/main/README.md This example demonstrates how to use the ESP32 to get the current time from an NTP (Network Time Protocol) server using UDP. It requires network connectivity and a valid NTP server address. ```cpp #include #include #include // Define network parameters #define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT #define ETH_PHY_TYPE ETH_PHY_IP101 #define ETH_PHY_ADDR 1 #define ETH_MDC_PIN 23 #define ETH_MDIO_PIN 18 #define ETH_TYPE ETH_MODULE_W5500 #define ETH_POWER_PIN -1 WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP, "pool.ntp.org", 3600); // Offset in seconds for your timezone void setup() { Serial.begin(115200); // Initialize Ethernet connection ETH.begin(ETH_ADDR, ETH_DNS, ETH_GATEWAY, ETH_MASK, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE, ETH_PHY_ADDR, ETH_PHY_TYPE); // Start NTP client timeClient.begin(); } void loop() { while (!timeClient.update()) { timeClient.forceUpdate(); } Serial.print("Current Time: "); Serial.println(timeClient.getFormattedTime()); delay(5000); } ``` -------------------------------- ### MQTT ThingStream Example Source: https://github.com/khoih-prog/webserver_esp32_w5500/blob/main/README.md This example integrates MQTT with ThingStream, likely for data visualization or device management. It demonstrates sending data to ThingStream via MQTT. Requires ThingStream account and configuration. ```cpp #include #include #include // Define network parameters #define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT #define ETH_PHY_TYPE ETH_PHY_IP101 #define ETH_PHY_ADDR 1 #define ETH_MDC_PIN 23 #define ETH_MDIO_PIN 18 #define ETH_TYPE ETH_MODULE_W5500 #define ETH_POWER_PIN -1 // ThingStream details #define TS_HOST "api.thingstream.io" #define TS_PORT 1883 #define TS_USER "your_thingstream_username" #define TS_PASS "your_thingstream_password" #define TS_DEVICE_ID "your_device_id" WebServer server(80); WiFiClient ethClient; PubSubClient client(ethClient); void setup() { Serial.begin(115200); // Initialize Ethernet connection ETH.begin(ETH_ADDR, ETH_DNS, ETH_GATEWAY, ETH_MASK, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE, ETH_PHY_ADDR, ETH_PHY_TYPE); // Set MQTT server details and connect client.setServer(TS_HOST, TS_PORT); client.setCallback(callback); // Start the web server server.on("/", []() { server.send(200, "text/plain", "Hello from ESP32 ThingStream Client!"); }); server.begin(); Serial.println("WebServer started."); } void loop() { if (!client.connected()) { reconnect(); } client.loop(); server.handleClient(); // Publish data to ThingStream every 10 seconds static unsigned long lastPublish = 0; if (millis() - lastPublish > 10000) { lastPublish = millis(); publishData(); } delay(2); } void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); for (unsigned int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); } void reconnect() { while (!client.connected()) { Serial.print("Attempting MQTT connection..."); if (client.connect(TS_DEVICE_ID, TS_USER, TS_PASS)) { Serial.println("connected"); client.subscribe(String("devices/" + String(TS_DEVICE_ID) + "/commands").c_str()); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); delay(5000); } } } void publishData() { String data = "{\"value\": " + String(random(100)) + "}"; String topic = "devices/" + String(TS_DEVICE_ID) + "/data"; client.publish(topic.c_str(), data.c_str()); Serial.println("Published data to ThingStream."); } ``` -------------------------------- ### UDP Send Receive Example Source: https://github.com/khoih-prog/webserver_esp32_w5500/blob/main/README.md This example demonstrates sending and receiving UDP packets. It's useful for network communication where connectionless data transfer is required, such as in some IoT or control applications. ```cpp #include #include // Define network parameters #define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT #define ETH_PHY_TYPE ETH_PHY_IP101 #define ETH_PHY_ADDR 1 #define ETH_MDC_PIN 23 #define ETH_MDIO_PIN 18 #define ETH_TYPE ETH_MODULE_W5500 #define ETH_POWER_PIN -1 WiFiUDP udp; unsigned int localPort = 2390; // local port to listen on void setup() { Serial.begin(115200); // Initialize Ethernet connection ETH.begin(ETH_ADDR, ETH_DNS, ETH_GATEWAY, ETH_MASK, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE, ETH_PHY_ADDR, ETH_PHY_TYPE); // Start listening for UDP packets if (udp.begin(localPort)) { Serial.print("Listening on port "); Serial.println(localPort); } } void loop() { // Check if there are any UDP packets available if (udp.parsePacket() > 0) { // Read the packet char packetBuffer[255]; int len = udp.read(packetBuffer, 255); // Null-terminate the buffer packetBuffer[len] = '\0'; Serial.print("Received packet: "); Serial.println(packetBuffer); // Send a response back to the sender udp.beginPacket(udp.remoteIP(), udp.remotePort()); udp.print("Acknowledged: "); udp.print(packetBuffer); udp.endPacket(); } delay(10); } ``` -------------------------------- ### ESP32 W5500 UDPSendReceive Example Output Source: https://github.com/khoih-prog/webserver_esp32_w5500/blob/main/README.md Terminal output from the UDPSendReceive example running on an ESP32_DEV with an ESP32_W5500 module. Shows network connection and UDP packet reception details. ```text Start UDPSendReceive on ESP32_DEV with ESP32_W5500 WebServer_ESP32_W5500 v1.5.3 for core v2.0.0+ [EWS] Default SPI pinout: [EWS] SPI_HOST: 2 [EWS] MOSI: 23 [EWS] MISO: 19 [EWS] SCK: 18 [EWS] CS: 5 [EWS] INT: 4 [EWS] SPI Clock (MHz): 25 [EWS] ========================= ETH Started ETH Connected ETH MAC: B4:E6:2D:E9:68:AC, IPv4: 192.168.2.78 FULL_DUPLEX, 100Mbps Starting connection to server... Listening on port 2390 UDP Packet received, size 48 From 132.163.97.3, port 123 Seconds since Jan 1 1900 = 3882452857 Unix time = 1673464057 The UTC time is 19:07:37 ``` -------------------------------- ### Simple Authentication Example Source: https://github.com/khoih-prog/webserver_esp32_w5500/blob/main/README.md Provides a straightforward implementation of web server authentication. This is suitable for basic security needs where a username and password are required to access the server's resources. ```cpp #include #include // Define network parameters #define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT #define ETH_PHY_TYPE ETH_PHY_IP101 #define ETH_PHY_ADDR 1 #define ETH_MDC_PIN 23 #define ETH_MDIO_PIN 18 #define ETH_TYPE ETH_MODULE_W5500 #define ETH_POWER_PIN -1 WebServer server(80); // Define username and password for authentication const char* user = "admin"; const char* password = "password"; void setup() { Serial.begin(115200); // Initialize Ethernet connection ETH.begin(ETH_ADDR, ETH_DNS, ETH_GATEWAY, ETH_MASK, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE, ETH_PHY_ADDR, ETH_PHY_TYPE); // Route for the root path with authentication server.on("/", HTTP_GET, []() { if (!server.authenticate(user, password)) { return server.requestAuthentication(); } server.send(200, "text/plain", "Welcome, authenticated user!"); }); // Start the server server.begin(); Serial.println("WebServer started."); } void loop() { server.handleClient(); delay(2); } ``` -------------------------------- ### Web Client Repeating Example Source: https://github.com/khoih-prog/webserver_esp32_w5500/blob/main/README.md This example extends the web client functionality by making repeated requests to a remote server at regular intervals. It's useful for applications that need to continuously poll or fetch updated data. ```cpp #include #include #include // Define network parameters #define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT #define ETH_PHY_TYPE ETH_PHY_IP101 #define ETH_PHY_ADDR 1 #define ETH_MDC_PIN 23 #define ETH_MDIO_PIN 18 #define ETH_TYPE ETH_MODULE_W5500 #define ETH_POWER_PIN -1 WebServer server(80); void setup() { Serial.begin(115200); // Initialize Ethernet connection ETH.begin(ETH_ADDR, ETH_DNS, ETH_GATEWAY, ETH_MASK, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE, ETH_PHY_ADDR, ETH_PHY_TYPE); // Define a route to trigger the web client request server.on("/fetch", []() { HTTPClient http; http.begin("http://example.com"); // URL to fetch int httpCode = http.GET(); // Send HTTP GET request String payload = ""; if (httpCode > 0) { payload = http.getString(); // Get response payload Serial.println("HTTP Response code: " + String(httpCode)); Serial.println(payload); } else { Serial.println("Error on HTTP request"); } http.end(); // Close connection server.send(200, "text/plain", "Fetched data: " + payload); }); // Start the web server server.begin(); Serial.println("WebServer started."); } void loop() { server.handleClient(); // Make a request every 5 seconds static unsigned long lastRequest = 0; if (millis() - lastRequest > 5000) { lastRequest = millis(); HTTPClient http; http.begin("http://example.com"); int httpCode = http.GET(); if (httpCode > 0) { Serial.println("Periodic fetch successful. Code: " + String(httpCode)); } else { Serial.println("Periodic fetch failed. Code: " + String(httpCode)); } http.end(); } delay(2); } ``` -------------------------------- ### Include WebServer_ESP32_W5500.h in Main File Only Source: https://github.com/khoih-prog/webserver_esp32_w5500/blob/main/README.md Include this header file in only one .h, .cpp, or .ino file (typically the main setup file) to prevent 'Multiple Definitions' linker errors. This is the standard include for the library. ```cpp // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error #include "WebServer_ESP32_W5500.h" //https://github.com/khoih-prog/WebServer_ESP32_W5500 ``` -------------------------------- ### Include WebServer_ESP32_W5500.hpp for Multi-File Projects Source: https://github.com/khoih-prog/webserver_esp32_w5500/blob/main/README.md Include this header in multiple files to avoid 'Multiple Definitions' linker errors in multi-file projects. Ensure it's not included in the main setup file. ```cpp // Can be included as many times as necessary, without `Multiple Definitions` Linker Error #include "WebServer_ESP32_W5500.hpp" //https://github.com/khoih-prog/WebServer_ESP32_W5500 ``` -------------------------------- ### Multi-File Project Example Source: https://github.com/khoih-prog/webserver_esp32_w5500/blob/main/README.md This entry likely refers to a project structure involving multiple files, common in larger Arduino or PlatformIO projects. It suggests how to organize code across different files for better modularity. ```cpp // This is a placeholder for a multi-file project example. // Actual code would be distributed across multiple .h and .cpp files. // Example structure: // - main.ino: Main sketch file // - config.h: Configuration settings // - web_handlers.cpp: Web server request handlers // - network.cpp: Network initialization and management // main.ino (example snippet) #include "config.h" #include "web_handlers.h" #include #include WebServer server(80); void setup() { Serial.begin(115200); // Initialize Ethernet ETH.begin(...); // Setup web server routes setupWebRoutes(server); server.begin(); } void loop() { server.handleClient(); } // web_handlers.h (example snippet) #pragma once #include void setupWebRoutes(WebServer &server); // web_handlers.cpp (example snippet) #include "web_handlers.h" void setupWebRoutes(WebServer &server) { server.on("/", []() { server.send(200, "text/plain", "Hello from multi-file project!"); }); // Add more routes here } ``` -------------------------------- ### MQTTClient_Auth Debug Output Source: https://github.com/khoih-prog/webserver_esp32_w5500/blob/main/README.md Terminal output for the MQTTClient_Auth example on an ESP32 with ESP32_W5500. Shows authenticated MQTT connection and message exchange. ```cpp Start MQTTClient_Auth on ESP32_DEV with ESP32_W5500 WebServer_ESP32_W5500 v1.5.3 for core v2.0.0+ [EWS] Default SPI pinout: [EWS] SPI_HOST: 2 [EWS] MOSI: 23 [EWS] MISO: 19 [EWS] SCK: 18 [EWS] CS: 5 [EWS] INT: 4 [EWS] SPI Clock (MHz): 25 [EWS] ========================= ETH Started ETH Connected ETH MAC: B4:E6:2D:E9:68:AC, IPv4: 192.168.2.78 FULL_DUPLEX, 100Mbps Attempting MQTT connection to broker.emqx.io...connected Message Send : MQTT_Pub => Hello from MQTTClient_Auth on ESP32_DEV with ESP32_W5500 Message arrived [MQTT_Pub] Hello from MQTTClient_Auth on ESP32_DEV with ESP32_W5500 Message Send : MQTT_Pub => Hello from MQTTClient_Auth on ESP32_DEV with ESP32_W5500 Message arrived [MQTT_Pub] Hello from MQTTClient_Auth on ESP32_DEV with ESP32_W5500 ``` -------------------------------- ### MQTTClient_Basic Debug Output Source: https://github.com/khoih-prog/webserver_esp32_w5500/blob/main/README.md Terminal output for the MQTTClient_Basic example on an ESP32 with ESP32_W5500. Illustrates basic MQTT publishing and subscribing. ```cpp Start MQTTClient_Basic on ESP32_DEV with ESP32_W5500 WebServer_ESP32_W5500 v1.5.3 for core v2.0.0+ [EWS] Default SPI pinout: [EWS] SPI_HOST: 2 [EWS] MOSI: 23 [EWS] MISO: 19 [EWS] SCK: 18 [EWS] CS: 5 [EWS] INT: 4 [EWS] SPI Clock (MHz): 25 [EWS] ========================= ETH Started ETH Connected ETH MAC: B4:E6:2D:E9:68:AC, IPv4: 192.168.2.78 FULL_DUPLEX, 100Mbps Message Send : MQTT_Pub => Hello from MQTTClient_Basic on ESP32_DEV with ESP32_W5500 Message arrived [MQTT_Pub] Hello from MQTTClient_Basic on ESP32_DEV with ESP32_W5500 Message Send : MQTT_Pub => Hello from MQTTClient_Basic on ESP32_DEV with ESP32_W5500 Message Send : MQTT_Pub => Hello from MQTTClient_Basic on ESP32_DEV with ESP32_W5500 Message arrived [MQTT_Pub] Hello from MQTTClient_Basic on ESP32_DEV with ESP32_W5500 ``` -------------------------------- ### Server Control Methods Source: https://github.com/khoih-prog/webserver_esp32_w5500/blob/main/README.md Methods to start, handle client requests, and stop the web server. ```APIDOC ## Server Control ### Description Methods to manage the lifecycle and operation of the web server. ### Methods * `void begin()`: Starts the web server. * `void handleClient()`: Handles incoming client requests. * `void close()`: Disables the server. * `void stop()`: Disables the server (same functionality as `close()`). ``` -------------------------------- ### AdvancedWebServer Debug Output Source: https://github.com/khoih-prog/webserver_esp32_w5500/blob/main/README.md Debug output from the AdvancedWebServer example running on an ESP32 with an ESP32_W5500. Shows network initialization and HTTP server status. ```cpp Start AdvancedWebServer on ESP32_DEV with ESP32_W5500 WebServer_ESP32_W5500 v1.5.3 for core v2.0.0+ [EWS] Default SPI pinout: [EWS] SPI_HOST: 2 [EWS] MOSI: 23 [EWS] MISO: 19 [EWS] SCK: 18 [EWS] CS: 5 [EWS] INT: 4 [EWS] SPI Clock (MHz): 25 [EWS] ========================= ETH Started ETH Connected ETH MAC: B4:E6:2D:E9:68:AC, IPv4: 192.168.2.78 FULL_DUPLEX, 100Mbps HTTP EthernetWebServer is @ IP : 192.168.2.78 .......... .......... .......... ..... ```