A CoAP server with Observe functionality. More...
Macros | |
| #define | ENABLE_SERIAL_DEBUG 1 |
| #define | SERIAL_BEGIN(baud) |
| #define | SERIAL_WHILE_WAIT |
| #define | SERIAL_PRINT(...) |
| #define | SERIAL_PRINTLN(...) |
| #define | SERIAL_WRITE(...) |
| #define | DEVICE_ID 1 |
Functions | |
| IPAddress | ip (192, 168, 0, DEVICE_ID) |
| IPAddress | dns (0, 0, 0, 0) |
| IPAddress | gateway (0, 0, 0, 0) |
| IPAddress | subnet (255, 255, 255, 0) |
| void | timeCallback (Coap::Message &message, IPAddress ip, uint16_t port) |
| Declaration of our callback to liked to the "time" resource. | |
| void | setup () |
| void | loop () |
| Coap::Message | getCurrentTimeMessage () |
| Helper to build the time message. | |
| void | sendNotification () |
Variables | |
| EthernetUDP | Udp |
| Coap::Node | coapNode (Udp) |
| Coap::ObserverRegistry< 5 > | myObservers |
| byte | mac [] = {0xBE, 0xEF, 0xBE, 0xEF, 0x00, DEVICE_ID} |
A CoAP server with Observe functionality.
Author: Pasquale Lafiosca (2026)
To test this example with the coap-client tool from libcoap:
You will see binary data incoming. Adjust IP address and port as needed.
Note that the option -s 60 will set the CoAP observe register option and keep the command running to receive notifications for the given amount of seconds.
Notes:
-p 5683 sets a fixed port.-T AAAA optionally sets a fixed starting token (ASCII, libcoap will use the next one).Multiple subscriptions with equal IP, port, path and token will result in the same observer.
| #define DEVICE_ID 1 |
| #define ENABLE_SERIAL_DEBUG 1 |
| #define SERIAL_BEGIN | ( | baud | ) |
| #define SERIAL_PRINT | ( | ... | ) |
| #define SERIAL_PRINTLN | ( | ... | ) |
| #define SERIAL_WHILE_WAIT |
| #define SERIAL_WRITE | ( | ... | ) |
| IPAddress dns | ( | 0 | , |
| 0 | , | ||
| 0 | , | ||
| 0 | ) |
| IPAddress gateway | ( | 0 | , |
| 0 | , | ||
| 0 | , | ||
| 0 | ) |
| Coap::Message getCurrentTimeMessage | ( | ) |
Helper to build the time message.
| IPAddress ip | ( | 192 | , |
| 168 | , | ||
| 0 | , | ||
| DEVICE_ID | ) |
| void loop | ( | ) |
| void sendNotification | ( | ) |
| void setup | ( | ) |
| IPAddress subnet | ( | 255 | , |
| 255 | , | ||
| 255 | , | ||
| 0 | ) |
| void timeCallback | ( | Coap::Message & | message, |
| IPAddress | ip, | ||
| uint16_t | port ) |
Declaration of our callback to liked to the "time" resource.
For the purpose of this example, our resource is simply millis().
| Coap::Node coapNode(Udp) | ( | Udp | ) |
| byte mac[] = {0xBE, 0xEF, 0xBE, 0xEF, 0x00, DEVICE_ID} |
| Coap::ObserverRegistry<5> myObservers |
| EthernetUDP Udp |