ESP32 HTTPClient Library – connected

Home / References / ESP32 Library / HTTPClient

Description

The connected() method in the ESP32 HTTPClient Library is used to check if the HTTP client is currently connected to the server. This method is particularly useful for managing connection states in IoT projects, ensuring that your ESP32 remains in sync with the server during HTTP operations. It provides a simple way to verify the connection status before attempting to send or receive data.


Syntax and Usage

The connected() method is straightforward and has only one usage pattern. Below is the syntax and how to use it:

bool HTTPClient::connected()
  • Basic Usage (No Arguments): Call this method on an initialized HTTPClient object to determine if the underlying network client (e.g., WiFiClient) is still connected to the server. It does not require any arguments and returns a boolean value indicating the connection status.

Argument(s)

This method does not require any arguments. It simply checks the internal state of the HTTP client’s connection to the server.


Return Value

The connected() method returns a bool value:

  • true: Indicates that the HTTP client is currently connected to the server.
  • false: Indicates that the HTTP client is not connected to the server (e.g., the connection was never established, has been closed, or an error occurred).

Example Codes

Below is an example demonstrating the use of the connected() method. This example connects to www.example.com, performs an HTTP GET request, and checks the connection status at various points.

Example: Checking Connection Status During an HTTP Request

This example initializes an HTTPClient, connects to www.example.com, and uses connected() to monitor the connection state before and after making a GET request. Ensure your ESP32 is connected to a Wi-Fi network before running this code.

/*
 * Author: Avant Maker
 * Date: February 21, 2025
 * Version: 1.0
 * Description: This example code demonstrates how to 
 * use the ESP32 HTTPClient Library's connected method to 
 * monitor the connection state before and after making 
 * a GET request. Ensure your ESP32 is connected to a 
 * Wi-Fi network before running this code.
 *
 * 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>
#include <HTTPClient.h>

const char* ssid = "your-SSID";       // Replace with your Wi-Fi SSID
const char* password = "your-PASSWORD"; // Replace with your Wi-Fi password

void setup() {
    Serial.begin(115200);
    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.println("Connecting to WiFi...");
    }
    Serial.println("Connected to WiFi");

    HTTPClient http;

    Serial.println("[HTTP] Beginning connection...");
    http.begin("http://www.example.com/"); // Connect to example.com

    // Check connection status before GET request
    if (http.connected()) {
        Serial.println("[HTTP] Client is connected to the server");
    } else {
        Serial.println("[HTTP] Client is not connected yet");
    }

    int httpCode = http.GET(); // Perform GET request

    if (httpCode > 0) {
        Serial.println("[HTTP] GET request successful, code: " + String(httpCode));
        String payload = http.getString();
        Serial.println("Response: " + payload);
    } else {
        Serial.println("[HTTP] GET request failed, error: " + String(httpCode));
    }

    // Check connection status after GET request
    if (http.connected()) {
        Serial.println("[HTTP] Client is still connected to the server after GET request");
    } else {
        Serial.println("[HTTP] Client is no longer connected");
    }

    http.end(); // Close the connection
}

void loop() {

}
error: Content is protected !!