ESP32 WiFiClient Library – connected

Home / References / ESP32 Library / WiFiClientSecure

Description

The connected method in the WiFiClient library checks whether a TCP connection to a server remains active. It’s a simple yet powerful way to monitor the state of your client connection, returning a boolean value that indicates if the ESP32 is still linked to the server or if data is still available to read. This method is especially useful for managing network tasks efficiently.


Syntax and Usage

The connected method has a single, straightforward usage with no arguments. Here’s how to use it:

Basic Usage:

client.connected()

This checks if the WiFiClient object is still connected to the server. It’s typically used in loops to process incoming data or to confirm the connection status after an operation.

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. Call it directly on your WiFiClient object to get the connection status.


Return Value

The connected method returns a boolean value:

  • true: The connection is active, or there is still data available to read from the server.
  • false: The connection is closed, and no data remains in the buffer.

Example Codes

Below is an example demonstrating the use of the connected method. Since it has only one usage, a single example is provided. This code connects to a server, sends a request, and uses connected to manage the connection.

Example: Checking Connection Status with connected

This example connects to www.httpbin.org on port 80, sends an HTTP GET request, and uses connected to read the server’s response until the connection closes. Upload this code to your ESP32, replace the Wi-Fi credentials, and open the Serial Monitor at 115200 baud to see the output.

/*
 * Author: Avant Maker
 * Date: February 24, 2025
 * Version: 1.0
 * Description: This example demonstrates how to use 
 * ESP32 WiFiClient Library's connected method to
 * check the status of ESP32's connection to the 
 * webserver.
 *
 * 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();

        // Read response while connected
        while (client.connected()) {
            if (client.available()) {
                String line = client.readStringUntil('\n');
                Serial.println(line);
            }
        }
        Serial.println("Connection closed by server");
        client.stop(); // Clean up
    } else {
        Serial.println("Connection failed");
    }
}

void loop() {
    // Nothing to do here after connection is closed
}

ESP32 Library Index

ESP32 Arduino Core Library


FAQ

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!

error: Content is protected !!