Home / References / ESP32 Library / WiFiClientSecure
Description
The available
method in the WiFiClientSecure
library checks if there are any bytes available to read from the secure connection. It is used after establishing a TLS connection to determine if the server has sent data that can be processed, making it essential for reading responses in IoT applications.
Syntax and Usage
The available
method has a single, straightforward usage without arguments. Below is the syntax with an example:
Checking Available Data
client.available()
Returns the number of bytes available to read from the secure connection, typically used in a loop to process incoming 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. It operates on the current state of the WiFiClientSecure
object, checking the buffer for incoming data from the secure connection.
Return Value
The available
method returns an int
value:
- Positive integer: The number of bytes available to read from the connection.
- 0: No data is available to read, indicating either the buffer is empty or the connection has no new data.
Example Codes
Below is an example code demonstrating the usage of the available
method, adapted from the reference code provided.
Example: Reading Server Response with available
This example connects to www.howsmyssl.com
securely, sends an HTTPS GET request, and uses available
to check for and read the server’s response, printing it to the Serial Monitor.
ATTENTION: The Root CA certificate has an expiration date. If the code fails to execute, it may be due to the Root CA certificate has expired. Try obtaining the latest Root CA certificate and replacing the expired one in this code. For detailed instructions on acquiring a website’s Root CA certificate, check out the link below:
How to Acquire the Root CA Certificate
/*
* Author: Avant Maker
* Date: February 24, 2025
* Version: 1.0
*
* Description: Below is an example code demonstrating how
* to use the ESP32 WiFiClientSecure Library's available method.
*
* ATTENTION: The Root CA certificate has an expiration date.
* If this code fails to execute, acquire the most up-to-date
* Root CA certificate.
* To find out more details about how to acquire the Root CA
* certificate of www.howsmyssl.com, please check out this link below:
*
* https://avantmaker.com/references/esp32-arduino-core-index/esp32-wificlientsecure-library/how-to-acquire-the-root-ca-certificate/
*
* 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 <NetworkClientSecure.h>
// WiFi credentials
const char* wifiName = "Your-WiFi-SSID"; // Your WiFi SSID
const char* wifiPass = "Your-WiFi-Password"; // Your WiFi password
// Server details
const char* targetServer = "www.howsmyssl.com"; // Target server URL
// Root CA certificate for www.howsmyssl.com
const char* rootCACert = R"literal(
-----BEGIN CERTIFICATE-----
MIIFBTCCAu2gAwIBAgIQS6hSk/eaL6JzBkuoBI110DANBgkqhkiG9w0BAQsFADBP
MQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJuZXQgU2VjdXJpdHkgUmVzZWFy
Y2hhG3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBYMTAeFw0yNDAzMTMwMDAwMDBa
Fw0yNzAzMTIyMzU5NTlaMDMxCzAJBgNVBAYTAlVTMRYwFAYDVQQKEw1MZXQncyBF
bmNyeXB0MQwwCgYDVQQDEwNSMTAwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK
AoIBAQDPV+XmxFQS7bRH/sknWHZGUCiMHT6I3wWd1bUYKb3dtVq/+vbOo76vACFL
YlpaPAEvxVgD9on/jhFD68G14BQHlo9vH9fnuoE5CXVlt8KvGFs3Jijno/QHK20a
/6tYvJWuQP/py1fEtVt/eA0YYbwX51TGu0mRzW4Y0YCF7qZlNrx06rxQTOr8IfM4
FpOUurDTazgGzRYSespSdcitdrLCnF2YRVxvYXvGLe48E1KGAdlX5jgc3421H5KR
mudKHMxFqHJV8LDmowfs/acbZp4/SItxhHFYyTr6717yW0QrPHTnj7JHwQdqzZq3
DZb3EoEmUVQK7GH29/Xi8orIlQ2NAgMBAAGjgfgwgfUwDgYDVR0PAQH/BAQDAgGG
MB0GA1UdJQQWMBQGCCsGAQUFBwMCBggrBgEFBQcDATASBgNVHRMBAf8ECDAGAQH/
AgEAMB0GA1UdDgQWBBS7vMNHpeS8qcbDpHIMEI2iNeHI6DAfBgNVHSMEGDAWgBR5
tFnme7bl5AFzgAiIyBpY9umbbjAyBggrBgEFBQcBAQQmMCQwIgYIKwYBBQUHMAKG
Fmh0dHA6Ly94MS5pLmxlbmNyLm9yZy8wEwYDVR0gBAwwCjAIBgZngQwBAgEwJwYD
VR0fBCAwHjAcoBqgGIYWaHR0cDovL3gxLmMubGVuY3Iub3JnLzANBgkqhkiG9w0B
AQsFAAOCAgEAkrHnQTfreZ2B5s3iJeE6IOmQRJWjgVzPw139vaBw1bGWKCIL0vIo
zwzn1OZDjCQiHcFCktEJr59L9MhwTyAWsVrdAfYf+B9haxQnsHKNY67u4s5Lzzfd
u6PUzeetUK29v+PsPmI2cJkxp+iN3epi4hKu9ZzUPSwMqtCceb7qPVxEbpYxY1p9
1n5PJKBLBX9eb9LU6l8zSxPWV7bK3lG4XaMJgnT9x3ies7msFtpKK5bDtotij/l0
GaKeA97pb5uwD9KgWvaFXMIEt8jVTjLEvwRdvCn294GPDF08U8lAkIv7tghluaQh
1QnlE4SEN4LOECj8dsIGJXpGUk3aU3KkJz9icKy+aUgA+2cP21uh6NcDIS3XyfaZ
QjmDQ993ChII8SXWupQZVBiIpcWO4RqZk3lr7Bz5MUCwzDIA359e57SSq5CCkY0N
4B6Vulk7LktfwrdGNVI5BsC9qqxSwSKgRJeZ9wygIaehbHFHFhcBaMDKpiZlBHyz
rsnnlFXCb5s8HKn5LsUgGvB24L7sGNZP2CX7dhHov+YhD+jozLW2p9W4959Bz2Ei
RmqDtmiXLnzqTpXbI+suyCsohKRg6Un0RC47+cpiVwHiXZAW+cn8eiNIjqbVgXLx
KPpdzvvtTnOPlC7SQZSYmdunr3Bf9b77AiC/ZidstK36dRILKz7OA54=
-----END CERTIFICATE-----
)literal";
NetworkClientSecure secureClient;
void setup() {
Serial.begin(115200);
delay(200);
WiFi.begin(wifiName, wifiPass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
secureClient.setCACert(rootCACert);
if (!secureClient.connect(targetServer, 443)) {
Serial.println("Connection failed. Check code description for solution.");
return;
}
Serial.println("Connected to server!");
secureClient.println("GET /a/check HTTP/1.1");
secureClient.println("Host: www.howsmyssl.com");
secureClient.println("Connection: close");
secureClient.println();
// Wait for server response
while (secureClient.connected()) {
String line = secureClient.readStringUntil('\n');
if (line == "\r") {
Serial.println("Headers received");
break;
}
}
// Check and read available data
while (secureClient.available()) {
char c = secureClient.read();
Serial.print(c);
}
secureClient.stop();
Serial.println("\nConnection closed");
}
void loop() {
}
ESP32 Library Index
- ESP32 WiFi Library
- ESP32 WiFiClient Library
- ESP32 HTTPClient Library
- ESP32 WebServer Library
- ESP32 WiFiClientSecure Library
- Connection
- Data Sending
- Data Receiving
- 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!