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). It aims at implementing all the compulsory functionalities of the protocol, maintaining the execution lightweight and clearly documenting its API.

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

How to install

Pre-requirements and dependencies

For the examples to compile and work correctly, please ensure to have all the necessary boards installed.

In addition, these libraries are needed:

  • Ethernet by Arduino
  • WiFi by Arduino

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.

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:161
MessageCode getCode() const
Get the message code.
Definition ProsecCoAP.cpp:231
ErrorCode addPayload(const uint8_t *payload, size_t length)
Add a payload to the message.
Definition ProsecCoAP.cpp:785
ErrorCode buildResponse(MessageCode code, Message &response) const
Build a response message based on a request message.
Definition ProsecCoAP.cpp:77
The CoAP node that runs on this device.
Definition ProsecCoAP.h:831
ErrorCode loop()
Perform a single iteration of the CoAP event loop.
Definition ProsecCoAP.cpp:1067
ErrorCode start()
Start the CoAP instance.
Definition ProsecCoAP.cpp:1054
ErrorCode sendMessage(const Message &message, IPAddress ip, uint16_t port)
Send a CoAP message to the specified IP address and port.
Definition ProsecCoAP.cpp:1168
ErrorCode serve(const char *path, Callback callback)
Serve a given URI path with the specified callback.
Definition ProsecCoAP.cpp:1049
void setup()
Definition client.ino:30
IPAddress deviceIp(192, 168, 0, 99)
Coap::Node coapNode(Udp)
EthernetUDP Udp
Definition client.ino:27
byte mac[]
Definition client.ino:19
void loop()
Definition client.ino:48
IPAddress ip(192, 168, 0, DEVICE_ID)

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

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 tested. You can, alternatively:

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

Documentation

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

Building the documentation

To manually build documentation from the main folder, run:

doxygen

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

Release process memo

A new version is released following these steps:

  1. Update library.properties as required, using a new vX.Y.Z tag.
  2. Create the corresponding vX.Y.Z tag and a new release (for GitHub and Arduino library manager). The logs for the Arduino library manager bot can be checked here.
  3. Publish the new library to PlatformIO, running the following from the main project folder:
    pio pkg publish .

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.