Layer: L7 Application ยท Build flags: PC_ENABLE_COAP
What this example teaches
CoAP (RFC 7252) is a compact request/response protocol for constrained devices, running over UDP. This stands up a CoAP server on UDP/5683 alongside the HTTP server, exposing a few resources dispatched by Uri-Path. It is callback-driven over the transport-layer UDP service (no per-loop servicing) and every buffer is static.
Build a resource table, then bind. Each resource has a path, an allowed-method mask, and a handler:
pc_coap_server_reset();
pc_coap_server_add_resource("/info", CoapMethodMask::COAP_ALLOW_GET, coap_info);
pc_coap_server_add_resource("/led", CoapMethodMask::COAP_ALLOW_GET | CoapMethodMask::COAP_ALLOW_PUT,
coap_led);
pc_coap_server_add_resource("/hello", CoapMethodMask::COAP_ALLOW_GET, coap_hello);
pc_coap_server_begin(5683);
The handler shape. A handler fills a CoapResponse from the CoapRequest: write into resp->payload (bounded by resp->payload_cap), set payload_len, the content_format, and the response code. The LED handler shows reading the method and the request payload:
static void coap_led(const CoapRequest *req, CoapResponse *resp) {
if (req->method == CoapMethod::COAP_PUT) {
g_led_state = (req->payload_len && req->payload[0] != '0') ? 1 : 0;
digitalWrite(LED_BUILTIN, g_led_state ? HIGH : LOW);
resp->code = (uint8_t)CoapResponseCode::COAP_RSP_CHANGED; resp->payload_len = 0; return;
}
resp->payload[0] = g_led_state ? '1' : '0';
resp->payload_len = 1; resp->content_format = CoapContentFormat::COAP_CF_TEXT; resp->code = (uint8_t)CoapResponseCode::COAP_RSP_CONTENT;
}
CON requests get a piggybacked ACK; NON requests get a NON response - handled by the library. For server-push and large transfers, see CoapObserve and CoapBlock.
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--project-option="build_flags=-DPC_ENABLE_COAP=1" \
--lib="." examples/L7-Application/CoAP/CoAP.ino
coap-client -m get coap://<ip>/info
coap-client -m put -e 1 coap://<ip>/led
coap-client -m get coap://<ip>/hello
Annotated source
The complete sketch (CoAP.ino), reproduced verbatim with added explanatory comments:
#define PC_ENABLE_COAP 1
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
#ifndef LED_BUILTIN
#define LED_BUILTIN 2
#endif
static int g_led_state = 0;
static void coap_info(const CoapRequest *req, CoapResponse *resp)
{
(void)req;
int n = snprintf((char *)resp->payload, resp->payload_cap, "{\"uptime_ms\":%lu,\"free_heap\":%u}",
(unsigned long)millis(), (unsigned)ESP.getFreeHeap());
if (n < 0)
n = 0;
resp->payload_len = (size_t)n;
resp->content_format = CoapContentFormat::COAP_CF_JSON;
resp->code = (uint8_t)CoapResponseCode::COAP_RSP_CONTENT;
}
static void coap_led(const CoapRequest *req, CoapResponse *resp)
{
if (req->method == CoapMethod::COAP_PUT)
{
g_led_state = (req->payload_len && req->payload[0] != '0') ? 1 : 0;
digitalWrite(LED_BUILTIN, g_led_state ? HIGH : LOW);
resp->code = (uint8_t)CoapResponseCode::COAP_RSP_CHANGED;
resp->payload_len = 0;
return;
}
resp->payload[0] = g_led_state ? '1' : '0';
resp->payload_len = 1;
resp->content_format = CoapContentFormat::COAP_CF_TEXT;
resp->code = (uint8_t)CoapResponseCode::COAP_RSP_CONTENT;
}
static void coap_hello(const CoapRequest *req, CoapResponse *resp)
{
(void)req;
static const char msg[] = "hello from a deterministic CoAP server";
size_t n = sizeof(msg) - 1;
if (n > resp->payload_cap)
n = resp->payload_cap;
memcpy(resp->payload, msg, n);
resp->payload_len = n;
resp->content_format = CoapContentFormat::COAP_CF_TEXT;
resp->code = (uint8_t)CoapResponseCode::COAP_RSP_CONTENT;
}
void setup()
{
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
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));
pc_coap_server_reset();
pc_coap_server_add_resource("/info", CoapMethodMask::COAP_ALLOW_GET, coap_info);
pc_coap_server_add_resource("/led", CoapMethodMask::COAP_ALLOW_GET | CoapMethodMask::COAP_ALLOW_PUT,
coap_led);
pc_coap_server_add_resource("/hello", CoapMethodMask::COAP_ALLOW_GET, coap_hello);
pc_coap_server_begin(5683);
Serial.println("CoAP server listening on UDP/5683 (try: coap-client -m get coap://<ip>/info)");
int32_t result = server.
begin(80);
if (result < 0)
Serial.printf("begin() failed (error %d)\n", result);
}
void loop()
{
}
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.
void handle()
Drive the server - call every Arduino loop() iteration.
Zero-heap CoAP server (RFC 7252): message codec + a fixed resource table.
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.