Connecting an ESP32 to Wi‑Fi and a REST API: Power‑vs‑Latency Trade‑offs
Learn how to connect an ESP32 to Wi‑Fi, send data to a REST API, and balance power consumption against latency using deep sleep. A step‑by‑step guide with code, trade‑offs, and practical checks.
22 Jun 2026, 22:21 UTC

Concrete Problem
You’re building a battery‑powered sensor node that needs to send periodic data to a cloud service. The ESP32’s built‑in Wi‑Fi seems perfect, but you’re unsure how to reliably connect, send an HTTP request, and manage power without sacrificing responsiveness.
Thesis
Using the Arduino core for ESP32, you can connect to Wi‑Fi with WiFi.begin(ssid, pwd), perform simple REST calls with WiFiClient or ArduinoHttpClient, and control power via deep sleep. The key trade‑off is that saving power by sleeping the radio introduces a few hundred milliseconds of reconnection latency each time you wake.
Step 1 – Initialize Wi‑Fi in Station Mode
The default mode is Station (STA). If you need an AP or AP+STA, call WiFi.mode(WIFI_AP_STA) before WiFi.begin(). The WiFi.begin() call blocks until the device obtains an IP address, so for non‑blocking code you should loop on WiFi.isConnected() and WiFi.status().
void connectWiFi(const char* ssid, const char* pwd) {
WiFi.mode(WIFI_STA); // default is STA, but explicit is safer
WiFi.begin(ssid, pwd);
Serial.print("Connecting to "); Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected");
Serial.print("IP: "); Serial.println(WiFi.localIP());
}
Run this in setup() and verify the Serial Monitor prints the IP address. The sketch should be uploaded with the latest ESP32 core (≥2.0.0) to avoid DNS bugs.
Step 2 – Send a REST API Request
For a lightweight HTTP client, WiFiClient is sufficient. The ArduinoHttpClient library adds a higher‑level API but requires additional headers. Below is a minimal GET to https://httpbin.org/get that demonstrates the flow.
#include <WiFi.h>
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
void setup() {
Serial.begin(115200);
connectWiFi(ssid, password);
sendGet();
}
void sendGet() {
WiFiClient client;
const char* host = "httpbin.org";
const char* path = "/get";
if (!client.connect(host, 80)) {
Serial.println("Connection failed");
return;
}
client.print("GET "); client.print(path); client.print(" HTTP/1.1\r\n");
client.print("Host: "); client.println(host);
client.println("Connection: close\r\n");
while (client.connected() || client.available()) {
if (client.available()) {
String line = client.readStringUntil('\n');
Serial.println(line);
}
}
client.stop();
}
After uploading, the Serial Monitor should show a 200 OK response and the JSON payload. If the device prints “Connection failed”, check that the Wi‑Fi credentials are correct and that the core is up‑to‑date.
Step 3 – Power‑Saving with Deep Sleep
The ESP32 can enter deep sleep, turning off most peripherals except the RTC and a few wake‑up sources. To wake after a timer, use esp_sleep_enable_timer_wakeup() and esp_deep_sleep_start(). When the device wakes, the Wi‑Fi radio is off, so you must re‑initialize it.
#include <esp_sleep.h>
void loop() {
// Send data
sendGet();
// Prepare deep sleep for 10 seconds
const uint64_t uS_TO_S_FACTOR = 1000000; // conversion factor for micro seconds to seconds
const uint64_t TIME_TO_SLEEP = 10; // seconds
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Going to deep sleep");
delay(100); // ensure serial flush
esp_deep_sleep_start();
}
When the device wakes, setup() runs again, so connectWiFi() will re‑establish the connection. The reconnection time is typically 200–400 ms on a clean network, but can be longer if the AP is busy or uses WPA3.
Trade‑off: Power vs. Latency
In active mode, the ESP32 draws ~80–120 mA. Entering light sleep reduces this to <10 mA but still keeps the Wi‑Fi radio powered, allowing near‑instant reconnection. Deep sleep saves more energy but incurs the reconnection latency described above.
Decision matrix:
| Mode | Power | Latency |
|---|---|---|
| Active | ~100 mA | 0 ms |
| Light Sleep | <10 mA | 10–20 ms |
| Deep Sleep | <1 mA (standby) | 200–400 ms |
If your application can tolerate a few hundred milliseconds of delay between wake and data upload, deep sleep is the most battery‑efficient. For real‑time monitoring, light sleep or active mode may be preferable.
Actionable Checklist
- Install the latest ESP32 Arduino core (≥2.0.0) via the Board Manager.
- Upload the sketch and confirm the Serial Monitor prints a successful IP assignment.
- Verify the HTTP GET returns a 200 OK response.
- Enable deep sleep and observe the reconnection delay in the Serial output.
- Measure current draw with a multimeter or shunt resistor to confirm the expected power profile.
- If you need WPA3 or advanced security, check that the firmware is up‑to‑date; older cores may lack support.
- For production, consider adding a watchdog or OTA update routine to handle firmware rollbacks if the Wi‑Fi stack fails.
By following this pattern, you’ll have a reliable, power‑efficient ESP32 sensor node that can push data to any REST API.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.