Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 884 of 93612. IoT Connectivity and SecurityFree lesson

Managing CoAP Observe Notifications for Telemetry

Implement the CoAP observe mechanism to allow clients to receive asynchronous updates from servers without constant polling requests.

In this lesson, you will learn how to implement the CoAP Observe mechanism to enable efficient, push-based telemetry updates, replacing power-hungry polling cycles.

Moving Beyond Polling

In our previous modules, we established that constant polling—where a client repeatedly requests the same resource—wastes CPU cycles and bandwidth, which is prohibitive for battery-constrained embedded devices. The CoAP Observe mechanism (RFC 7641) flips this relationship. Instead of the client asking "What is the current temperature?", the client registers its interest once, and the server pushes updates only when the state changes.

The Observation Pattern

To implement this, the client sends a standard GET request to the resource, but adds an Observe option (value 0, register) to the CoAP header. The server acknowledges the request and adds the client to an "observer list" for that specific resource.

When the telemetry data (e.g., a sensor reading) changes, the server sends a new CoAP response packet to all clients on that list. Each response includes a sequence number to ensure the client can detect if packets arrived out of order.

Conceptual Implementation

On the server side, you maintain a linked list of client endpoints. When your sensor interrupt fires, you trigger the notification:

// Pseudo-logic for CoAP Observe trigger
void on_sensor_threshold_met(float new_value) {
    for (list_node_t *client = observer_list; client != NULL; client = client->next) {
        coap_pdu_t *pdu = coap_new_pdu();
        coap_add_option(pdu, COAP_OPTION_OBSERVE, sequence_number++);
        coap_add_data(pdu, format_payload(new_value));
        coap_send(client->endpoint, pdu);
    }
}

Managing State Consistency

Because CoAP runs over UDP, notifications can be lost. To maintain reliability, you must occasionally perform an "active" GET request or use CoAP Confirmable (CON) messages for critical updates. If a client stops responding to these CON messages after a threshold, the server must remove it from the observer list to reclaim memory—an essential task for the static memory allocation strategies we discussed in earlier lessons.

Key Takeaway: The CoAP Observe mechanism transforms telemetry from a client-initiated pull model to an event-driven push model, significantly reducing network traffic and power consumption.