Constrained Application Protocol (CoAP) server/client library for Arduino IDE/PlatformIO, ESP32, ESP8266.
Documentation is available at https://decadenza.github.io/ProsecCoAP/.
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.
For the examples to compile and work correctly, please ensure to have all the necessary boards installed.
In addition, these libraries are needed:
Ethernetby ArduinoWiFiby Arduino
- Open the Sketch menu in the IDE.
- Navigate to Include Library > Manage Libraries.
- Search for "ProsecCoAP" and install.
- Download this source code branch as a zip file.
- 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".
A simple server can be started as:
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <ProsecCoAP.h>
// Initialise a node.
EthernetUDP Udp;
Coap::Node coapNode(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);
coapNode.start();
}
void loop() {
coapNode.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;
message.buildResponse(Coap::MessageCode::CONTENT, response);
response.addPayload((const uint8_t *)("42"), 2, Coap::ContentFormat::TEXT_PLAIN);
coapNode.sendMessage(response, ip, port);
}
}Navigate to File > Examples > ProsecCoAP in Arduino IDE or check the example folder for more examples.
To quickly verify a successful build process for multiple boards:
- Install Arduino CLI.
- 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
- Run
make.
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-notlsandcoap-server-notls.
Documentation is available at: https://decadenza.github.io/ProsecCoAP/.
To manually build documentation from the main folder, run:
doxygen
The documentation will be accessible from ./html/index.html.
A new version is released following these steps:
- Update library.properties as required, using a new
vX.Y.Ztag. - Create the corresponding
vX.Y.Ztag and a new release (for GitHub and Arduino library manager). The logs for the Arduino library manager bot can be checked here. - Publish the new library to PlatformIO, running the following from the main project folder:
pio pkg publish .
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.