ESP32 WiFiClient Library – available

Home / References / ESP32 Library / WiFiClientSecure

Description

The WiFiClient.available() method checks how many bytes of data are ready to be read from a server after establishing a connection with your ESP32. It’s a key tool for handling incoming data in networked projects—like reading responses from a web server or messages from another device—making it essential for interactive IoT applications.


Syntax and Usage

The WiFiClient.available() method is simple and used in one standard way:

Basic Usage: 

client.available();

Returns the number of bytes available to read from the server.

There are no variations—this method is straightforward and always called without arguments to check the input buffer.

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. It simply reports the amount of data waiting to be read from the connected server.


Return Value

The WiFiClient.available() method returns an integer representing the number of bytes available to read from the server. If no data is available, it returns 0. This value helps you decide when to call read() to retrieve the data without blocking or missing anything.


Example Codes

Reading Server Response with available()

This example shows how to use WiFiClient.available() to check for and read data from www.httpbin.org after sending an HTTP GET request. It waits for available data and then prints the server’s response to the Serial Monitor, demonstrating real-time data handling.

/*
 * Author: Avant Maker
 * Date: February 24, 2025
 * Version: 1.0
 * Description: This example demonstrates how to use 
 * ESP32 WiFiClient Library's available method to
 * check for and read data from www.httpbin.org
 * after sending an HTTP GET request. 
 * It waits for available data and then prints the server’s
 * response to the Serial Monitor, demonstrating real-time
 * data handling.
 *
 * 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>

// Wi-Fi credentials
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("\nWiFi connected");

    if (client.connect(host, port)) {
        Serial.println("Connected to server!");
        client.print("GET /get HTTP/1.1\r\nHost: www.httpbin.org\r\nConnection: close\r\n\r\n");
        
        // Wait for data to become available
        while (client.available() == 0) {
            delay(10); // Small delay to avoid busy-waiting
            if (!client.connected()) {
                Serial.println("Connection lost");
                client.stop();
                return;
            }
        }

        // Read and print available data
        Serial.println("Server response:");
        while (client.available()) {
            char c = client.read();
            Serial.write(c);
        }
        client.stop();
    } else {
        Serial.println("Connection failed");
    }
}

void loop() {
    // Nothing here for this example
}

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 !!