Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using CoAP with Node-RED for Low-Power Devices #2422

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added src/blog/2024/08/images/coap-in-node.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/blog/2024/08/images/coap-request-node.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/blog/2024/08/images/coap-with-node-red.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
103 changes: 103 additions & 0 deletions src/blog/2024/08/using-coap-with-node-red.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
---
title: Using CoAP with Node-RED for Low-Power Devices
subtitle: A Practical Guide to Integrating CoAP with Node-RED for Efficient IoT Communication
description: Learn how to use CoAP with Node-RED to optimize communication for low-power, resource-constrained devices.
date: 2024-08-15
authors: ["sumit-shinde"]
image:
tags:
- post
- nodered
- coap iot
- coap
- CoAP vs HTTPS vs MQTT
- low power device
---

Have you ever wondered how to make communication between IoT devices more efficient, especially when dealing with limited resources? In this blog, we’ll explore CoAP (Constrained Application Protocol), a protocol designed for low-power, resource-constrained environments. We’ll compare CoAP with HTTPS and MQTT, and show you how to integrate CoAP with Node-RED for seamless data communication.

<!--more-->

## What is CoAP and why it matters?

CoAP (Constrained Application Protocol) is a specialized protocol designed for the Internet of Things (IoT) and other environments where resources are limited. Unlike HTTP and MQTT, which relies on the heavier TCP (Transmission Control Protocol), CoAP uses UDP (User Datagram Protocol). This allows CoAP to offer lower overhead and reduced latency, making it much more efficient for devices with limited power and bandwidth.

**Why does this matter?** Take a battery-powered Raspberry Pi used for monitoring environmental conditions such as temperature and humidity. With CoAP, the Raspberry Pi can send and receive data more efficiently, conserving battery life and operating effectively even in low-bandwidth areas. By minimizing the energy required for communication, CoAP helps extend the device’s operational life and ensures reliable data transmission, which is essential for maintaining accurate and continuous monitoring in remote or resource-constrained settings.

## Comparing Resource Consumption of HTTPS, MQTT, and CoAP for Temperature Data Transmission

To understand how different protocols perform in real-world scenarios, let’s compare HTTPS, MQTT, and CoAP. We'll look at how each protocol handles the task of sending temperature data to a server. For this comparison, we'll assume the following typical setup:

- Payload: Each message contains 50 bytes of temperature data.
- Frequency: The system sends 100 messages per second.

**Note**: The values presented are approximate and calculated based on typical characteristics of each protocol under common conditions. Actual values may vary depending on specific implementations and network environments.

| **Protocol** | **Payload (bytes)** | **Message Size Overhead (bytes)** | **Total Data Sent per Message (bytes)** | **Bandwidth Usage Calculation** | **Bandwidth Usage (kbps)** | **Latency (ms)** | **CPU Usage (%)** | **Memory Usage (MB)** |
|--------------|----------------------|----------------------------------|----------------------------------------|-------------------------------------------------------------------------|----------------------------|------------------|-------------------|-----------------------|
| **HTTPS** | 50 | 500 | 550 ≈ 600 | (550 bytes/message × 100 messages/second × 8 bits/byte) / 1000 | 440 | 100 ≈ 110 | 10% ≈ 50% | 100 ≈ 120 |
| **MQTT** | 50 | 100 | 150 ≈ 200 | (150 bytes/message × 100 messages/second × 8 bits/byte) / 1000 | 120 | 50 ≈ 60 | 5% ≈ 30% | 50 ≈ 60 |
| **CoAP** | 50 | 20 | 70 ≈ 80 | (70 bytes/message × 100 messages/second × 8 bits/byte) / 1000 | 56 | 10 ≈ 15 | 2% ≈ 20% | 20 ≈ 25 |

## Using CoAP with Node-RED

In this section, we'll explore how to use CoAP with Node-RED to enable efficient communication between devices. I'll walk you through an example where a Raspberry Pi reads temperature data from a sensor, and a Windows machine requests this data using CoAP.

### Prerequisites

- **Node-RED**: Ensure that the Node-RED is installed and running on both of your hardware devices: one acting as the server and the other as the client machine.

- **node-red-contrib-coap**: Install the `node-red-contrib-coap` package in your Node-RED environment. This package provides the CoAP nodes needed for integration.

For detailed instructions on setting up the Node-RED on your various hardware devices, refer to [Setting up Node-RED on Different Hardware](/node-red/hardware/), this section of provides the steps to easily setup Node-RED using [FlowFuse Device agent](\/product/device-agent/).

### Setting up the CoAP Server

Setting up a CoAP server in Node-RED is similar to setting up an HTTP server. CoAP uses a request-response model similar to HTTP with methods like GET, POST, DELETE, and PUT, making it straightforward if you're familiar with HTTP servers in Node-RED.

If you are not fimilier with how to set up HTTP servers in Node-RED, refer to [Creating REST APIs with Node-RED](/node-red/integration-technologies/rest/).

!["Screenshot showing the settings of the coap-in node"](./images/coap-in-node.png "Screenshot showing the settings of the coap-in node"){data-zoomable}
_Screenshot showing the settings of the coap-in node_

1. Drag the `coap-in` node onto the canvas.
2. Double-click on the node and click on the "+" next to the "Server" field. Provide a name for the configuration, set the port to the default `5683` (the standard port for CoAP), and check the "Use UDP6 Agent" box if you want to enable IPv6 support.
3. Click "Add" to save the server configuration.
4. Select the GET method.
5. Enter the URL endpoint for the CoAP server, which specifies the path where the CoAP server will listen for incoming requests.
6. Click "Done" to save the configuration.
7. Drag the `change` node onto the canvas and set `msg.payload` to `flow.temp` (for the example purpose storing the temperature value in this context variable).
8. Drag the `coap-response` node onto the canvas and set the status to 200 and correct content format according your data.
9. Connect the `coap-in` node's output to the input of the `change` node and the output of the `change` node to the input of the `coap-response` node.

### Requesting Data from the Server

!["Screenshot showing settings of coap-request node"](./images/coap-request-node.png "Screenshot showing settings of coap-request node"){data-zoomable}
_Screenshot showing settings of coap-request node_

1. Drag the `inject` node onto the canvas.
2. Drag the `coap-request` node onto the canvas.
3. Double-click on the `coap-request` node, set the URL to `coap://<ip-address>/sensor/temperature`, replace `<ip-address>` with the IP address of your device running the CoAP server.
4. Select the method to `GET`.
5. Enable the `Confirmable` option to ensure reliability.
6. Drag the `debug` node onto the canvas that will print the output.
7. Connect the output of the `inject` node to the input of the `coap-request` node. and the output of the `coap-request` node to the input of the `debug` node.

Deploy the flow by clicking the deploy button in the top-right corner. To test the flow, click the inject button to retrieve data, or set an interval in the inject node to retrieve data automatically at specified time intervals.

!["Image showing Node-RED instances, one running as a CoAP server and the other as a client requesting data"](./images/coap-with-node-red.gif "Image showing Node-RED instances, one running as a CoAP server and the other as a client requesting data"){data-zoomable}
_Image showing Node-RED instances, one running as a CoAP server and the other as a client requesting data_

### When Not to Use CoAP

CoAP is designed to be efficient for constrained environments and IoT devices, but it’s important to understand when it might not be the best choice.

- **For Devices Requiring Strong Security**: CoAP, by default, does not offer as much built-in security as HTTPS. If your IoT devices need to handle sensitive data or require strong encryption to ensure privacy and security, you might need to opt for protocols like HTTPS that provide more robust security features.

- **For Devices with Complex Communication Needs**: CoAP is great for simple, efficient communication, but if your devices need to engage in complex interactions or require advanced session management, you might find that protocols with more extensive capabilities, such as HTTP, are better suited to your needs.

- **For Devices Operating in High-Bandwidth Environments**: CoAP is optimized for scenarios where network bandwidth is limited. If your devices are in an environment where there is ample bandwidth and they need to transfer large amounts of data or handle high data rates, other protocols like MQTT or HTTP might offer better performance due to their ability to manage larger payloads and more intricate data exchanges.

## Conclusion

CoAP is ideal for IoT devices with limited resources, offering efficient communication through its lightweight, UDP-based protocol. While HTTPS and MQTT are suited for more complex or secure environments, CoAP’s simplicity makes it perfect for conserving power and bandwidth in constrained settings. By integrating CoAP with Node-RED, you ensure effective and reliable data transmission for your low-power devices.
Loading