Home / References / ESP32 Library / WebServer Library
Description
The stop()
method of the ESP32 WebServer Library is used to stop the web server from listening for new client connections. When this method is called, the server will no longer accept incoming requests. However, any existing connections that are currently being handled will continue to run until they are either completed or explicitly closed.
Differences Between the stop()
and close()
Methods
The key difference between the ESP32 WebServer Library’s stop()
and close()
methods lies in their scope and immediacy. The stop()
method halts the server from accepting new connections but leaves existing client connections intact, allowing them to finish their tasks.
In contrast, the close()
method goes further by not only stopping the server from listening for new connections but also actively closing all current client connections managed by the server. This immediate termination frees up all associated resources, making close()
a more aggressive shutdown option suited for situations where you need to fully stop all server activity without delay.
Syntax and Usage
The stop()
method is simple and has a single usage. It is called on an active WebServer
object to halt the server’s listening process. Below is the syntax:
- Basic Usage: Stops the web server from accepting new connections while allowing existing requests to finish. No arguments are required.
Here’s a code snippet showing how to use it:
WebServer server(); // Create a WebServer object on port 80
server.begin(80); // Start the server
// ... Server runs and handles requests ...
server.stop(); // Stop the server from accepting new connections
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 stop()
method does not require any arguments. It operates directly on the WebServer
object to gracefully halt its operation, ensuring a smooth transition when shutting down.
Return Value
The stop()
method does not return a value. Its role is to stop the server from listening for new connections, allowing active clients to complete their requests before the server fully ceases operation.
Example Codes
Below are examples demonstrating the use of the stop()
method.
Example 1: Basic Server Shutdown
This example sets up a web server on port 80 that serves a “Hello from ESP32!” message. After 60 seconds, the server is stopped using stop()
, allowing any ongoing requests to finish before it stops accepting new connections. Upload this code to your ESP32, connect to its Wi-Fi network, and access the server via its IP address within the first 60 seconds. After the stop, new connections will be refused, but active ones will complete.
/*
* Author: Avant Maker
* Date: March 17, 2025
* Version: 1.0
* License: MIT
*
* Description:
* This example demonstrates how to use ESP32 WebServer Library's
* stop() method to stop a ESP32 server.
*
* 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
bool serverRunning = false;
unsigned long serverStartTime = 0;
const unsigned long serverRunDuration = 60000; // Run server for 60 seconds
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
server.begin();
serverRunning = true;
serverStartTime = millis();
Serial.println("HTTP server started");
Serial.println("Server will run for 60 seconds");
}
void loop() {
// Check if server is running
if (serverRunning) {
server.handleClient(); // Handle client requests
// Check if it's time to stop the server
if (millis() - serverStartTime > serverRunDuration) {
Serial.println("Shutting down server after 60 seconds");
server.stop(); // Stop the server and disconnect all clients
serverRunning = false;
Serial.println("Server has been stopped and all clients disconnected");
}
}
}
void handleRoot() {
server.send(200, "text/plain", "Hello from ESP32! Server is running temporarily.");
}
ESP32 Library Index
- ESP32 WiFi Library
- ESP32 WiFiClient Library
- ESP32 HTTPClient Library
- ESP32 WiFiClientSecure Library
- ESP32 WebServer Library
- Server Operation
- Client Hnadling
- Routing and Handlers
- Authentication
- Request Information
- 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!