Two of the main factors considered during the development of Internet of Things (IoT) devices are efficiency and cloud compatibility. Efficiency, both in energy consumption and data usage, is important to reduce operational and maintenance costs - especially for cellular, battery-operated devices. Energy saving is a puzzle with many pieces, and choosing the appropriate communication protocol is an important piece. In general, IoT devices must communicate with cloud platforms that can process and analyze the data to generate useful insights, in addition to device management.
This blog provides an overview of the Constrained Application Protocol (CoAP), a communication protocol with built-in efficiencies, and it details the steps for maintaining cloud compatibility. Later in the blog, there is a sample implementation sequence using 1NCE FreeRTOS Blueprint.
Protocol selection in constrained IoT devices
CoAP is a client/server, request/response, UDP-based protocol. It includes the main web concepts, such as Uniform Resource Identifier (URI) and internet media types, and it also provides RESTful (REST) services, making it a subset of HTTP optimized for constrained IoT devices. For secure communication, CoAP can be combined with Datagram Transport Layer Security (DTLS).
Figures 1 & 2 show the communication stages required to send a test packet for MQTT and CoAP.
To use MQTT, you need to consider:
- The 3-way TCP handshake (186 bytes).
- MQTT Connect & Connect Acknowledgment (ACK) (253 bytes).
- The actual publish packet containing the payload (126 bytes).
The first 2 stages must be repeated if the connection is not kept active with periodic PINGREQ packets, which can be challenging to realize for constrained IoT devices that have a long reporting interval, during which the device usually needs to go into power saving mode. Unstable cellular network coverage can cause the client to disconnect and also require initiating a new connection.
With CoAP and the connectionless UDP protocol, the IoT device can directly send data without the need to establish a connection. In this example, we can see that the test packet needed 59 bytes, and if more reliable communication is required, the optional confirmation feature can be enabled. That adds a 46 byte ACK message from the server, requiring a total of 105 bytes.
Using a connectionless protocol and sending less data can reduce the energy consumption of IoT devices. The following figure from an experiment conducted at 1NCE shows a possible reduction of up to 47.6% when using CoAP. Further energy saving is also possible with the Energy Saver tool on the 1NCE software platform, which can be used to minimize the payload size.
Using more efficient IoT protocols while maintaining cloud compatibility
CoAP can be a suitable choice for constrained IoT devices. However, its adoption in cloud IoT platforms is still limited, which makes it challenging to design an IoT solution that is efficient and also accessible from different cloud platforms.
To solve this, 1NCE introduced the IoT Integrator (Figure 4). Using this tool, IoT devices can send messages to 1NCE endpoints through protocols like CoAP, UDP or Lightweight Machine to Machine (LwM2M). Those messages can then be forwarded to AWS IoT Core, as well as webhooks and other integrations.
1NCE FreeRTOS Blueprint
The 1NCE FreeRTOS Blueprint contains multiple demos that demonstrate how IoT devices can establish secure and efficient communication with 1NCE’s software platform using cellular connectivity.
- 1NCE SIM Card.
- B-L475E-IOT01A2 STM32 Discovery kit IoT node connected to BG96 (LTE Cat M1/Cat NB1/EGPRS modem) through X-NUCLEO-STMODA1 expansion board.
- STM32 ST-LINK utility
CoAP demo components
For secure communication, the blueprint uses 1NCE SDK integration (as shown in Figure 5) to get DTLS credentials from the 1NCE OS device authentication endpoint. The credentials can be retrieved after identifying the device by the SIM ICCID. The blueprint also contains a sample MbedTLS configuration for DTLS with PSK authentication.
To get the DTLS credentials, the application calls the
os_auth() function. This function uses
the network interface defined in the SDK to communicate with the 1NCE Device Authentication endpoint. Received
credentials can then be added to the Mbedtls configuration.
It is also required to integrate a 3rd party library that can create and process CoAP packets. The
blueprint uses the open source Lobaro CoAP library, which is integrated as shown in Figure 6. A network
interface is added between the CoAP library and the FreeRTOS Secure Socket API by defining the functions
CoAP_Recv. Those functions use a socket handle that has been
configured for DTLS communication and connected to 1NCE’s CoAP server.
To enable the CoAP demo, the definition "
CONFIG_COAP_DEMO_ENABLED" should be added to
/aws_demos/config_files/nce_demo_config.h, the demo can be configured as follows:
PUBLISH_PAYLOAD_FORMATThe test payload
COAP_PORTShould be set to 5684 for DTLS communication
CLIENT_ICCIDThe SIM ICCID
COAP_URI_QUERYThe CoAP URI-query option, used to configure the topic for MQTT Publish packets in AWS IoT core.
The user needs to set up the required cloud integration from the 1NCE portal. For example: AWS integration setup.
Running the demo
After you configure the demo and flash it to the board, the device will register with 1NCE OS and the following event will be displayed in the portal.
After that, the CoAP messages sent from the device will show on the portal as follows:
It will also be forwarded to the configured cloud integration (AWS IoT Core).
With the Energy Saver tool, the application can only send the value updates instead of sending the whole JSON object, as shown in the following example.
This can help reduce the energy consumption of the device and data usage as well. This feature can be
enabled by defining "
os_energy_save() can then be used to create the payload.
Additional demos (UDP & LwM2M)
Using CoAP can provide a considerable reduction in the energy and data usage of IoT devices. With the IoT Integrator tool from 1NCE OS, this reduction can be achieved without affecting cloud compatibility. The CoAP demo in the 1NCE FreeRTOS Blueprint provides a starting point for developers interested in testing and evaluating CoAP, and shows how CoAP can be integrated with FreeRTOS. It also includes the communication with 1NCE’s software platform for device authentication and for sending CoAP messages that can be forwarded to the configured cloud platforms.