Layer: L7 Application ยท Build flags: PC_ENABLE_HTTP_CLIENT (optional PC_ENABLE_TLS + PC_ENABLE_HTTP_CLIENT_TLS for https://)
What this example teaches
So far the device has been a server; here it is a client. http_get(url, &r) performs a blocking GET over raw lwIP (with client-side mbedTLS for https://), for webhooks, telemetry push, or REST calls out to a remote service. The host is resolved via DNS (or used directly if it is a dotted-quad IP) and the response status and body land in a fixed static buffer (no heap).
One blocking call, result in a static struct:
HttpClientResult r;
int status = http_get(URL, &r);
if (status >= 0) {
Serial.printf("HTTP %d, %u body bytes:\n", r.status, (unsigned)r.body_len);
Serial.write(r.body, r.body_len);
}
The return value is negative on a transport-level failure; on success r.status holds the HTTP status code and r.body/r.body_len the (bounded) body.
TLS trust. https:// is encrypt-only by default - the device has no trust store, so the peer is unauthenticated. To authenticate the server, install a CA trust anchor with http_client_set_ca(pem, len) (verifies chain + hostname) and/or a SHA-256 certificate pin with http_client_set_pin(hash32); call once before issuing requests, and a verification failure aborts the request.
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--project-option="build_flags=-DPC_ENABLE_HTTP_CLIENT=1" \
--lib="." examples/L7-Application/HttpClient/HttpClient.ino
For https://, add -DPC_ENABLE_TLS=1 -DPC_ENABLE_HTTP_CLIENT_TLS=1. The sketch fetches URL once at boot and prints the result to Serial @ 115200.
Annotated source
The complete sketch (HttpClient.ino), reproduced verbatim with added explanatory comments:
#define PC_ENABLE_HTTP_CLIENT 1
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
static const char *URL = "http://example.com/";
void setup()
{
Serial.begin(115200);
Serial.print("Connecting to WiFi");
{
delay(250);
Serial.print('.');
}
Serial.printf("IP: %u.%u.%u.%u\n", (unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF),
(unsigned)((ip >> 16) & 0xFF), (unsigned)((ip >> 24) & 0xFF));
HttpClientResult r;
int status = http_get(URL, &r);
if (status < 0)
{
Serial.printf("request failed (error %d)\n", status);
}
else
{
Serial.printf("HTTP %d, %u body bytes:\n", r.status, (unsigned)r.body_len);
Serial.write(r.body, r.body_len);
Serial.println();
}
}
void loop()
{
delay(1000);
}
Zero-heap outbound HTTP(S) client over raw lwIP (PC_ENABLE_HTTP_CLIENT).
bool init_wifi_physical(const char *, const char *)
Connect to a WiFi access point.
uint32_t pc_net_egress_ip(void)
IPv4 (network byte order) of the current egress interface, or 0 if none.
bool wifi_ready()
True if the WiFi station link is up (associated + an IP is assigned).
Layer 1 (Physical) - link bring-up and live egress-interface reporting.
Layer 7 (Application) - public HTTP routing API.