Home / References / ESP32 Library / HTTPClient
Description
The setTimeout
method in the ESP32 HTTPClient Library is used to set the timeout duration (in milliseconds) for HTTP operations, such as connecting to a server or waiting for a response. This method helps manage how long the ESP32 will wait before aborting an HTTP request if no response is received, making it a critical tool for ensuring robust IoT applications.
Please note the differences between setTimeout and setConnectTimeout in the ESP32 HTTPClient Library:
Purpose
- setTimeout: Sets the overall timeout for the HTTP request operations after a connection is established, including waiting for the server to respond and reading the response data.
- setConnectTimeout: Sets the timeout specifically for establishing the initial connection to the server (i.e., the time allowed for the TCP handshake or SSL negotiation to complete).
Scope
- setTimeout: Applies to the entire HTTP transaction once connected, such as delays in receiving headers or body data from the server.
- setConnectTimeout: Applies only to the connection phase, determining how long the client waits to establish a connection before giving up.
Syntax and Usage
The setTimeout
method is straightforward and has a single way of usage. Below is how to use it:
Using the method with an argument:
http.setTimeout(timeout);
This is the only way to use setTimeout
. You provide a timeout value in milliseconds as an argument to specify how long the HTTP client should wait for a response before timing out.
Argument(s)
- timeout: An unsigned 16-bit integer (
uint16_t
) representing the timeout duration in milliseconds. This value determines how long the HTTP client will wait for a response before considering the operation timed out. For example, setting it to 5000 means a 5-second timeout.
Return Value
The setTimeout
method does not return a value. It is a void function that simply configures the timeout property of the HTTPClient object for subsequent HTTP operations.
Example Codes
Making an HTTP GET Request with a Custom Timeout
This example shows how to use setTimeout
to set a 5-second timeout for an HTTP GET request to www.example.com
. The ESP32 connects to a Wi-Fi network and attempts to fetch data, with the timeout ensuring the request doesn’t hang indefinitely if the server is unresponsive.
/*
* Author: Avant Maker
* Date: February 21, 2025
* Version: 1.0
* Description: This example code demonstrates how to
* use the ESP32 HTTPClient Library's setTimeout to set
* a 5-second timeout for an HTTP GET request to www.example.com.
* The ESP32 connects to a Wi-Fi network and attempts to
* fetch data, with the timeout ensuring the request doesn't
* hang indefinitely if the server is unresponsive.
*
* 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";
const char* password = "YOUR_PASSWORD";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin("http://www.example.com");
http.setTimeout(5000); // Set timeout to 5 seconds
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println("Response: " + payload);
} else {
Serial.println("Error on HTTP request: " + String(httpCode));
}
http.end();
}
delay(10000); // Wait 10 seconds before next request
}
ESP32 Library Index
- ESP32 WiFi Library
- ESP32 WiFiClient Library
- ESP32 WiFiClientSecure Library
- ESP32 WebServer Library
- ESP32 HTTPClient Library
- Connection
- Request Methods
- Request Config
- Response
- Cookie
- 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!