ESP32 BASICS · LESSON 11
Making HTTP Requests from the ESP32
Fetching data from the internet and parsing a small JSON reply on a microcontroller.
With Wi-Fi joined, the ESP32 can act as a client and pull data from a server the same way a browser does — handy for fetching a value, checking the time, or logging a reading to a cloud service.
http_get.ino
#include
#include
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin("http://example.local/data");
int code = http.GET();
if (code == 200) {
String body = http.getString();
Serial.println(body);
}
http.end();
}
delay(10000);
}
Parsing the reply
Many APIs reply in JSON. A small JSON library lets you pull individual fields out of that text without writing your own parser — worth adding once a reply has more than one or two values you care about.
KEY IDEA
Always call http.end() once you're done with a request — skipping it leaks memory over many requests and will eventually crash a long-running sketch.