ProsecCoAP 🥂
 
Loading...
Searching...
No Matches
serverWithObserve.ino File Reference

A CoAP server with Observe functionality. More...

#include <Ethernet.h>
#include <EthernetUdp.h>
#include <ProsecCoAP.h>

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}
 

Detailed Description

A CoAP server with Observe functionality.

Author: Pasquale Lafiosca (2026)

Testing with libcoap

To test this example with the coap-client tool from libcoap:

coap-client-notls -p 5683 -v 9 -s 60 coap://192.168.0.1/time

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.

Macro Definition Documentation

◆ DEVICE_ID

#define DEVICE_ID   1

◆ ENABLE_SERIAL_DEBUG

#define ENABLE_SERIAL_DEBUG   1

◆ SERIAL_BEGIN

#define SERIAL_BEGIN ( baud)
Value:
Serial.begin(baud)

◆ SERIAL_PRINT

#define SERIAL_PRINT ( ...)
Value:
Serial.print(__VA_ARGS__)

◆ SERIAL_PRINTLN

#define SERIAL_PRINTLN ( ...)
Value:
Serial.println(__VA_ARGS__)

◆ SERIAL_WHILE_WAIT

#define SERIAL_WHILE_WAIT
Value:
while (!Serial) \
{ \
delay(10); \
}

◆ SERIAL_WRITE

#define SERIAL_WRITE ( ...)
Value:
Serial.write(__VA_ARGS__)

Function Documentation

◆ dns()

IPAddress dns ( 0 ,
0 ,
0 ,
0  )

◆ gateway()

IPAddress gateway ( 0 ,
0 ,
0 ,
0  )

◆ getCurrentTimeMessage()

Coap::Message getCurrentTimeMessage ( )

Helper to build the time message.

◆ ip()

IPAddress ip ( 192 ,
168 ,
0 ,
DEVICE_ID  )

◆ loop()

void loop ( )

◆ sendNotification()

void sendNotification ( )

◆ setup()

void setup ( )

◆ subnet()

IPAddress subnet ( 255 ,
255 ,
255 ,
0  )

◆ timeCallback()

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().

Variable Documentation

◆ coapNode

Coap::Node coapNode(Udp) ( Udp )

◆ mac

byte mac[] = {0xBE, 0xEF, 0xBE, 0xEF, 0x00, DEVICE_ID}

◆ myObservers

Coap::ObserverRegistry<5> myObservers

◆ Udp

EthernetUDP Udp