ProsecCoAP 🥂
 
Loading...
Searching...
No Matches
ProsecCoAP 🥂 - CoAP client/server library for Arduino

Constrained Application Protocol (CoAP) server/client library for Arduino IDE/PlatformIO, ESP32, ESP8266.

Build with Arduino Build with PlatformIO

Documentation is available at https://decadenza.github.io/ProsecCoAP/.

Details

This library is an implementation of CoAP protocol (RFC-7252). The aim is to provide all the compulsory functionalities, maintaining the execution lightweight and clearly documenting its API.

Please open an issue to request missing functionalities or report bugs.

How to install

Install from Arduino IDE Library Manager

  1. Open the Sketch menu in the IDE.
  2. Navigate to Include Library > Manage Libraries.
  3. Search for "ProsecCoAP" and install.

Install from PlatformIO CLI

pio pkg install --library ProsecCoAP

Manual install

  1. Download this source code branch as a zip file.
  2. In the Arduino IDE, navigate to Sketch > Include Library > Add .ZIP Library. At the top of the drop down list, select the option to "Add .ZIP Library".

Getting started

A simple server can be started as:

#include <Ethernet.h>
#include <EthernetUdp.h>
#include <ProsecCoAP.h>
// Initialise a node.
EthernetUDP Udp;
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD};
IPAddress deviceIp(192, 168, 0, 99);
// Your custom callback declaration.
void myCallback(Coap::Message &message, IPAddress ip, uint16_t port);
void setup()
{
Ethernet.begin(mac, deviceIp);
coapNode.serve("my-endpoint", myCallback);
}
void loop() {
}
// Send "42" as a response to a GET request.
void myCallback(Coap::Message &message, IPAddress ip, uint16_t port)
{
if (message.getCode() == Coap::MessageCode::GET)
{
Coap::Message response;
response.addPayload((const uint8_t *)("42"), 2, Coap::ContentFormat::TEXT_PLAIN);
coapNode.sendMessage(response, ip, port);
}
}
Main header file for the ProsecCoAP library.
A CoAP message.
Definition ProsecCoAP.h:164
MessageCode getCode() const
Get the message code.
Definition ProsecCoAP.cpp:230
ErrorCode addPayload(const uint8_t *payload, size_t length)
Add a payload to the message.
Definition ProsecCoAP.cpp:826
ErrorCode intoResponse(const Message &request, MessageCode code, MessageType type)
Convert the message into a response to the given request.
Definition ProsecCoAP.cpp:86
The CoAP node that runs on this device.
Definition ProsecCoAP.h:903
ErrorCode loop()
Perform a single iteration of the CoAP event loop.
Definition ProsecCoAP.cpp:1165
ErrorCode start()
Start the CoAP instance.
Definition ProsecCoAP.cpp:1152
ErrorCode sendMessage(const Message &message, IPAddress ip, uint16_t port)
Send a CoAP message to the specified IP address and port.
Definition ProsecCoAP.cpp:1278
ErrorCode serve(const char *path, Callback callback)
Serve a given URI path with the specified callback.
Definition ProsecCoAP.cpp:1142
void setup()
Definition client.ino:34
IPAddress deviceIp(192, 168, 0, 99)
Coap::Node coapNode(Udp)
EthernetUDP Udp
Definition client.ino:31
byte mac[]
Definition client.ino:20
void loop()
Definition client.ino:57
IPAddress ip(192, 168, 0, DEVICE_ID)

Navigate to File > Examples > ProsecCoAP in Arduino IDE or check the examples folder for more examples.

Run the examples

For the examples to compile and work correctly, please ensure to have all the necessary boards installed. The examples need standard libraries, like:

  • Ethernet by Arduino
  • WiFi by Arduino

Refer to the comments in each example for details.

How to test

Verify compile errors and warnings for multiple boards

To quickly verify a successful build process for multiple boards:

  1. Install Arduino CLI.
  2. Ensure the core for the supported boards are installed:
    arduino-cli core install arduino:avr
    arduino-cli core install esp32:esp32
    arduino-cli core install esp8266:esp8266 --additional-urls http://arduino.esp8266.com/stable/package_esp8266com_index.json
  3. Run make.

Functional tests through the examples

The examples need another CoAP node to be executed. You can, alternatively:

  • Use one device and a CoAP tool running a computer. I suggest libcoap, either by compiling it yourself or use the binaries available as Debian package libcoap3-bin. In this case, you can test with coap-client-notls and coap-server-notls.
  • Use two devices and check serial monitor of each.

Documentation

Documentation is available at: https://decadenza.github.io/ProsecCoAP/. It points to the main branch.

Building the documentation

To manually build documentation in your current working directory, run:

doxygen

The documentation will be accessible from ./html/index.html.

Credits

This library was inspired by CoAP-simple-library. Credits for the original code go to the original contributors. This project maintains the original MIT License and continues the spirit of open-source IoT development.