Home / References / ESP32 Library / WebServer Library
Description
The begin()
method is a fundamental part of the ESP32 WebServer Library, used to initialize and start the web server on your ESP32. This method prepares the server to listen for incoming HTTP requests on a specified port, enabling your ESP32 to serve web pages, handle client requests, and interact with connected devices. Whether you’re building a simple control interface or a complex IoT application, begin()
is your starting point for web server functionality.
Syntax and Usage
The begin()
method can be called in two ways, depending on whether you need to specify a custom port. Below are the available usages:
- Without Arguments: Starts the web server on the default port (80), which is the standard port for HTTP communication.
- With Port Argument: Allows you to specify a custom port number for the server to listen on, providing flexibility for advanced setups.
Here’s how you can use it in your code:
WebServer server(); // Create a WebServer object
server.begin(); // Start the server on the default port 80
// OR
WebServer server(); // Create a WebServer object
server.begin(8080); // Start the server on port 8080
For practical applications and examples of this method, please consult the “Example Code” section on this page. This section provides comprehensive guidance to help you better understand and apply the method effectively.
Argument(s)
The begin()
method can be used with or without an argument. When an argument is provided, it customizes the port on which the server operates.
- port (uint16_t): An optional 16-bit unsigned integer specifying the port number the server should listen on. Common values range from 1 to 65535, though port 80 is the default for HTTP. If omitted, the server uses the port defined during the
WebServer
object instantiation (typically 80).
Return Value
The begin()
method does not return a value. Its purpose is to initialize the server and start listening for client connections. After calling begin()
, you can use server.handleClient()
in your loop to process incoming requests.
Example Codes
Example 1: Starting a WebServer on Default Port
This code demonstrates how to initialize and start an ESP32 web server using the begin()
method of the WebServer library. The begin()
method starts the server on the default port (80), enabling the ESP32 to respond to HTTP requests, in this case, with a simple text message “AvantMaker.com ESP32 Web Server Example” .
To use this code, replace “your-SSID” and “your-PASSWORD” with your Wi-Fi credentials. After uploading to your ESP32, open a web browser and enter the ESP32’s IP address, which is displayed in the serial monitor. You should see the message “Hello from ESP32!” displayed in your browser.
/*
* Author: Avant Maker
* Date: February 7, 2025
* Version: 1.0
* License: MIT
*
* Description:
* This example demonstrates how to use ESP32 WebServer Library's
* begin() method to set up a basic web server on port 80,
* responding with a simple "Hello from ESP32!" message when accessed.
*
* Code Source:
* This example code is sourced from the Comprehensive Guide
* to the ESP32 Arduino Core Library, accessible on AvantMaker.com.
* For additional code examples and in-depth documentation related to
* the ESP32 Arduino Core Library, please visit:
*
* https://avantmaker.com/home/all-about-esp32-arduino-core-library/
*
* AvantMaker.com, your premier destination for all things
* DIY, AI, IoT, Smart Home, and STEM projects. We are dedicated
* to empowering makers, learners, and enthusiasts with
* the resources they need to bring their innovative ideas to life.
*/
#include <WiFi.h>
#include <WebServer.h>
const char* ssid = "your-SSID"; // Replace with your Wi-Fi SSID
const char* password = "your-PASSWORD"; // Replace with your Wi-Fi password
WebServer server(80); // Create a server on port 80
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
// Print the IP address
Serial.print("Connected to WiFi. IP address: ");
Serial.println(WiFi.localIP());
// Define the root URL handler
server.on("/", handleRoot);
// Start the server (using the default port defined during initialization)
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient(); // Handle client requests
}
void handleRoot() {
server.send(200, "text/plain", "AvantMaker.com ESP32 Web Server Example!");
}
Example 2: Starting a WebServer on a Custom Port
This example demonstrates how to use the ESP32 WebServer Library’s begin()
method to create a web server on a custom port (8080). By specifying port 8080, the ESP32 will listen for incoming HTTP requests on this non-default port, allowing for flexible server configurations.
To use this code, replace “your-SSID” and “your-PASSWORD” with your Wi-Fi credentials. After uploading to your ESP32, open a web browser and navigate to the ESP32’s IP address followed by “:8080” (e.g., 192.168.1.100:8080). The IP address and port will be printed in the serial monitor.
/*
* Author: Avant Maker
* Date: February 7, 2025
* Version: 1.0
* License: MIT
*
* Description:
* This example demonstrates how to use ESP32 WebServer Library's
* begin() method to create a WebServer on a custom port (8080).
*
* Code Source:
* This example code is sourced from the Comprehensive Guide
* to the ESP32 Arduino Core Library, accessible on AvantMaker.com.
* For additional code examples and in-depth documentation related to
* the ESP32 Arduino Core Library, please visit:
*
* https://avantmaker.com/home/all-about-esp32-arduino-core-library/
*
* AvantMaker.com, your premier destination for all things
* DIY, AI, IoT, Smart Home, and STEM projects. We are dedicated
* to empowering makers, learners, and enthusiasts with
* the resources they need to bring their innovative ideas to life.
*/
#include <WiFi.h>
#include <WebServer.h>
const char* ssid = "your-SSID"; // Replace with your Wi-Fi SSID
const char* password = "your-PASSWORD"; // Replace with your Wi-Fi password
WebServer server; // Create a server without specifying a port
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
// Print the IP address
Serial.print("Connected to WiFi. IP address: ");
Serial.println(WiFi.localIP());
// Define the root URL handler
server.on("/", handleRoot);
// Start the server on port 8080
server.begin(8080);
Serial.println("HTTP server started on port 8080");
Serial.println("You can access the server at http://" + WiFi.localIP().toString() + ":8080/");
}
void loop() {
server.handleClient(); // Handle client requests
}
void handleRoot() {
server.send(200, "text/html", "<h1>AvantMaker.com ESP32 Web Server Example</h1><p>Custom port example from AvantMaker.com</p>");
}
Example 3: Running Multiple WebServers on Different Ports
This code demonstrates how to use the ESP32 WebServer Library’s begin()
method to start two separate web servers on different ports. This allows the ESP32 to serve different web pages simultaneously. One server runs on the default port 80, and the other runs on port 8080.
To use this code, replace “your-SSID” and “your-PASSWORD” with your Wi-Fi credentials. After uploading the code to your ESP32, open the Serial Monitor to see the assigned IP address. Then, open a web browser and navigate to the ESP32’s IP address. To access the second server, append “:8080” to the IP address in the browser’s address bar.
/*
* Author: Avant Maker
* Date: February 7, 2025
* Version: 1.0
* License: MIT
*
* Description:
* This example demonstrates how to run
* two separate WebServers on different ports. This allows
* the ESP32 to serve different content on different
* ports simultaneously.
*
* Code Source:
* This example code is sourced from the Comprehensive Guide
* to the ESP32 Arduino Core Library, accessible on AvantMaker.com.
* For additional code examples and in-depth documentation related to
* the ESP32 Arduino Core Library, please visit:
*
* https://avantmaker.com/home/all-about-esp32-arduino-core-library/
*
* AvantMaker.com, your premier destination for all things
* DIY, AI, IoT, Smart Home, and STEM projects. We are dedicated
* to empowering makers, learners, and enthusiasts with
* the resources they need to bring their innovative ideas to life.
*/
#include <WiFi.h>
#include <WebServer.h>
const char* ssid = "your-SSID"; // Replace with your Wi-Fi SSID
const char* password = "your-PASSWORD"; // Replace with your Wi-Fi password
WebServer server1; // Create first server
WebServer server2; // Create second server
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
// Print the IP address
Serial.print("Connected to WiFi. IP address: ");
Serial.println(WiFi.localIP());
// Define handlers for server1
server1.on("/", []() {
server1.send(200, "text/html", "<h1>Server 1</h1><p>This is the primary server on port 80.</p>");
});
// Define handlers for server2
server2.on("/", []() {
server2.send(200, "text/html", "<h1>Server 2</h1><p>This is the secondary server on port 8080.</p>");
});
// Start both servers
server1.begin(); // Start server1 on port 80 (default)
server2.begin(8080); // Start server2 on port 8080
Serial.println("HTTP servers started");
Serial.println("Server 1: http://" + WiFi.localIP().toString() + "/");
Serial.println("Server 2: http://" + WiFi.localIP().toString() + ":8080/");
}
void loop() {
// Handle client requests for both servers
server1.handleClient();
server2.handleClient();
}
ESP32 Library Index
- ESP32 WiFi Library
- ESP32 WiFiClient Library
- ESP32 HTTPClient Library
- ESP32 WiFiClientSecure Library
- ESP32 AsyncUDP Librarry
- ESP32 WebServer Library
- Server Operation
- Client Hnadling
- Routing and Handlers
- Authentication
- Request Information
- Request Header Management
- Response Information
- Server Configuration
- Which ESP32 Boards are Recommended for Learners
- How to Copy Codes from AvantMaker.com
- What is SPIFFS and how to upload files to it?
- What is LIttleFS and how to upload files to it?
Ready to experiment and explore more about ESP32? Visit our website’s All About ESP32 Resources Hub, packed with tutorials, guides, and tools to inspire your maker journey. Experiment, explore, and elevate your skills with everything you need to master this powerful microcontroller platform!