Layer: L7 Application ยท Build flags: PC_ENABLE_HTTP_CLIENT, PC_ENABLE_WEBHOOK
What this example teaches
A webhook pushes an event from the device to some other service: Slack, Discord, IFTTT, or your own API. This builds a small JSON payload and POSTs it through the outbound HTTP client (HttpClient), with a helper for the IFTTT Maker value1/2/3 shape. It fires once at boot.
Build a payload, then POST it:
char body[128];
pc_ifttt_payload("boot", "esp32", nullptr, body, sizeof(body));
int status = pc_webhook_post(WEBHOOK_URL, body);
pc_ifttt_payload() formats the three IFTTT values into JSON; pc_webhook_post() sends it. There is also a one-shot pc_ifttt_trigger(event, key, v1, v2, v3) that builds the Maker URL for you.
Where it fires matters. The POST is blocking, so the example fires it from loop() (guarded by a fired flag), not from a request handler - a blocking outbound call inside a handler would stall the worker serving this device's own server.
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--project-option="build_flags=-DPC_ENABLE_HTTP_CLIENT=1 -DPC_ENABLE_WEBHOOK=1" \
--lib="." examples/L7-Application/Webhook/Webhook.ino
# receive the POST on a host while the device boots:
nc -l 8080 # set WEBHOOK_URL to http://<this-host>:8080/hook
Annotated source
The complete sketch (Webhook.ino), reproduced verbatim with added explanatory comments:
#define PC_ENABLE_HTTP_CLIENT 1
#define PC_ENABLE_WEBHOOK 1
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
static const char *WEBHOOK_URL = "http://192.168.1.10:8080/hook";
void setup()
{
delay(250);
Serial.print("IP: ");
Serial.printf("IP: %u.%u.%u.%u\n", (unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF),
(unsigned)((ip >> 16) & 0xFF), (unsigned)((ip >> 24) & 0xFF));
}
void loop()
{
static bool fired = false;
{
fired = true;
char body[128];
pc_ifttt_payload("boot", "esp32", nullptr, body, sizeof(body));
int status = pc_webhook_post(WEBHOOK_URL, body);
Serial.printf("[webhook] POST -> status %d\n", status);
}
}
Single-port HTTP server with deterministic, zero-allocation execution.
int32_t begin(const WebServerConfig *cfg=nullptr)
Initialize all connection slots and open all registered listeners.
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.
Outbound webhooks / IFTTT (PC_ENABLE_WEBHOOK).