Home / References / ESP32 Library / WiFiClientSecure
Description
The stop
method in the WiFiClient
library closes an active TCP connection established by a WiFiClient
object. It’s essential for freeing up resources and ensuring your ESP32 can handle multiple connections efficiently. Use this method when you’ve completed communication with a server or need to terminate a connection cleanly.
Syntax and Usage
The stop
method is straightforward and has only one way of being used, requiring no arguments. Here’s how to call it:
Basic Usage:
client.stop()
This closes the current connection, releasing any associated network resources. It’s typically called after you’ve finished sending or receiving data.
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)
This method does not require any arguments. Simply call it on your WiFiClient
object to terminate the connection.
Return Value
The stop
method does not return a value (i.e., its return type is void
). It performs the action of closing the connection without providing feedback through a return value. To check if the connection is closed, you can use the connected()
method afterward.
Example Codes
Below is an example demonstrating the use of the stop
method. Since there’s only one way to use it, a single example is provided. This code connects to a server, sends a request, and then closes the connection using stop
.
Example: Using stop to Close a Connection
This example connects to www.httpbin.org
on port 80, sends an HTTP GET request, reads the response, and then uses stop
to close the connection. Upload this code to your ESP32, replace the Wi-Fi credentials, and open the Serial Monitor at 115200 baud to observe the process.
/*
* Author: Avant Maker
* Date: February 24, 2025
* Version: 1.0
* Description: This example demonstrates how to use
* ESP32 WiFiClient Library's stop method to close
* the connection of ESP32 to a web server.
*
* License: MIT
*
* 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/references/esp32-arduino-core-index/
*
* 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>
const char* ssid = "your-SSID"; // Replace with your Wi-Fi SSID
const char* password = "your-PASSWORD"; // Replace with your Wi-Fi password
const char* host = "www.httpbin.org";
const int port = 80;
WiFiClient client;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi");
if (client.connect(host, port)) {
Serial.println("Connected to server");
client.println("GET /get HTTP/1.1");
client.println("Host: www.httpbin.org");
client.println("Connection: close");
client.println();
// Wait for response
while (client.connected()) {
if (client.available()) {
String line = client.readStringUntil('\n');
Serial.println(line);
}
}
client.stop(); // Close the connection
Serial.println("Connection closed with stop()");
} else {
Serial.println("Connection failed");
}
}
void loop() {
// Nothing to do here after stopping the connection
}
ESP32 Library Index
- ESP32 WiFi Library
- ESP32 HTTPClient Library
- ESP32 WiFiClientSecure Library
- ESP32 WebServer Library
- ESP32 WiFiClient Library
- Connection
- Send Data
- Receive Data
- Config
- Status
- 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!