How to Use HTTP with ESP32 in IoT Projects
To use
HTTP with ESP32 in IoT, you connect the ESP32 to Wi-Fi, then use the HTTPClient library to send HTTP requests like GET or POST. This allows your ESP32 to communicate with web servers or APIs over the internet.Syntax
The basic steps to use HTTP with ESP32 are:
- Include the
WiFi.handHTTPClient.hlibraries. - Connect to a Wi-Fi network using
WiFi.begin(). - Create an
HTTPClientobject. - Use
http.begin(url)to specify the target URL. - Send a request like
http.GET()orhttp.POST(). - Read the response with
http.getString(). - End the connection with
http.end().
cpp
#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("Connected to WiFi"); HTTPClient http; http.begin("http://example.com"); int httpCode = http.GET(); if (httpCode > 0) { String payload = http.getString(); Serial.println(payload); } http.end(); } void loop() { // put your main code here, to run repeatedly: }
Output
...
Connected to WiFi
<html>...</html>
Example
This example connects the ESP32 to Wi-Fi and sends an HTTP GET request to a public API. It prints the response to the serial monitor.
cpp
#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("\nWiFi connected"); HTTPClient http; http.begin("http://worldtimeapi.org/api/timezone/Etc/UTC"); int httpCode = http.GET(); if (httpCode > 0) { String payload = http.getString(); Serial.println("HTTP Response Code: " + String(httpCode)); Serial.println("Response payload:"); Serial.println(payload); } else { Serial.println("Error on HTTP request"); } http.end(); } void loop() { // no repeated code needed }
Output
...
WiFi connected
HTTP Response Code: 200
Response payload:
{"abbreviation":"UTC","client_ip":"...","datetime":"2024-06-01T12:00:00.000000+00:00",...}
Common Pitfalls
Common mistakes when using HTTP with ESP32 include:
- Not waiting for Wi-Fi to connect before sending requests.
- Forgetting to call
http.end(), which can cause memory leaks. - Using HTTP URLs without checking if the server supports HTTP (some require HTTPS).
- Not handling failed HTTP requests or error codes.
cpp
/* Wrong way: Not waiting for WiFi connection */ WiFi.begin(ssid, password); HTTPClient http; http.begin("http://example.com"); int code = http.GET(); // May fail if WiFi not connected /* Right way: Wait for WiFi connection */ WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); } HTTPClient http; http.begin("http://example.com"); int code = http.GET(); http.end();
Quick Reference
Remember these key points when using HTTP with ESP32:
- Always connect to Wi-Fi before HTTP requests.
- Use
HTTPClientfor easy HTTP methods. - Call
http.end()to free resources. - Check HTTP response codes to handle errors.
- Use HTTPS if security is needed (requires extra setup).
Key Takeaways
Connect ESP32 to Wi-Fi before making HTTP requests.
Use the HTTPClient library to send GET or POST requests easily.
Always call http.end() after finishing the HTTP request to free memory.
Check the HTTP response code to handle success or failure.
Use HTTPS for secure communication, but it needs additional configuration.