diff --git a/README-ZH.md b/README-ZH.md index 2818ff4b..a6a4cdd0 100644 --- a/README-ZH.md +++ b/README-ZH.md @@ -457,6 +457,7 @@ Build a reliable, efficient and industry-specific Internet of Vehicles platform ## [Industrial IoT | Unified Namespace | Sparkplug](https://www.emqx.com/zh/blog/category/industrial-iot) +- [先进制造企业为何选择 EMQ 助力数字化升级](https://www.emqx.com/zh/blog/why-manufacturing-enterprises-choose-emq) ([Edit](https://github.com/emqx/blog/blob/main/zh/202502/why-manufacturing-enterprises-choose-emq.md)) - [如何在离线环境下部署 EMQX ECP](https://www.emqx.com/zh/blog/how-to-deploy-emqx-ecp-in-an-offline-environment) ([Edit](https://github.com/emqx/blog/blob/main/zh/202501/how-to-deploy-emqx-ecp-in-an-offline-environment.md)) - [高效管理物联网边缘节点:利用 EMQX ECP 实现边缘服务配置的版本管理与参数化下发](https://www.emqx.com/zh/blog/efficient-management-of-iot-edge-nodes) ([Edit](https://github.com/emqx/blog/blob/main/zh/202410/efficient-management-of-iot-edge-nodes.md)) - [OPC UA 与 OPC DA:选型指南](https://www.emqx.com/zh/blog/opc-ua-vs-opc-da) ([Edit](https://github.com/emqx/blog/blob/main/zh/202406/opc-ua-vs-opc-da.md)) diff --git a/README.md b/README.md index 5496010b..5457ab0e 100644 --- a/README.md +++ b/README.md @@ -206,6 +206,7 @@ Explore more with & via EMQ. ## [EMQX Open Source | Broker](https://www.emqx.com/en/blog/category/emqx) EMQX is the world's most scalable open-source MQTT broker with a high performance that connects 100M+ IoT devices in 1 cluster, while maintaining 1M message per second throughput and sub-millisecond latency. +- [Integrating LwM2M with MQTT Using EMQX: A Step-by-Step Guide](https://www.emqx.com/en/blog/integrating-lwm2m-with-mqtt) ([Edit](https://github.com/emqx/blog/blob/main/en/202502/integrating-lwm2m-with-mqtt.md)) - [Introducing EMQX Platform Smart Data Hub: A New Era of Intelligent MQTT Data Processing](https://www.emqx.com/en/blog/introducing-emqx-platform-smart-data-hub) ([Edit](https://github.com/emqx/blog/blob/main/en/202501/introducing-emqx-platform-smart-data-hub.md)) - [Quick Start of EMQX MQTT Cloud Service](https://www.emqx.com/en/blog/quick-start-of-emqx-mqtt-cloud-service) ([Edit](https://github.com/emqx/blog/blob/main/en/202501/quick-start-of-emqx-mqtt-cloud-service.md)) - [Efficient IoT Communication with LoRaWAN & MQTT](https://www.emqx.com/en/blog/lorawan-and-mqtt) ([Edit](https://github.com/emqx/blog/blob/main/en/202411/lorawan-and-mqtt.md)) diff --git a/en/202502/integrating-lwm2m-with-mqtt.md b/en/202502/integrating-lwm2m-with-mqtt.md new file mode 100644 index 00000000..41c79a3f --- /dev/null +++ b/en/202502/integrating-lwm2m-with-mqtt.md @@ -0,0 +1,166 @@ +## Introduction to the LwM2M Protocol + +Lightweight M2M (LwM2M) is a lightweight IoT device management protocol developed by the Open Mobile Alliance (OMA). It is based on the Constrained Application Protocol (CoAP) and designed specifically for resource-constrained IoT devices, such as sensors and smart meters. These devices are typically battery-powered and have limited CPU, memory, and network resources. + +### Protocol Stack Structure + +The LwM2M protocol stack has the following characteristics: + +1. **Application Layer:** In addition to a standard set of object resource models, it allows users to define custom object models. +2. **Transport Layer:** Typically uses CoAP over UDP, balancing lightweight communication with a certain level of reliability. +3. **Transport Security:** Utilizes the DTLS protocol with support for multiple security modes, including Pre-Shared Key, Raw Public Key, and X.509 certificates. +4. **Network Layer:** Primarily based on UDP with IPv4/6 or SMS. It can also be deployed over networks like NB-IoT, TCP, or LoRaWAN. + + ![Protocol Stack Structure](https://assets.emqx.com/images/808ffadc53b4ca2ba0af54b39868a61a.png) + +### Resource Model + +The resource model is the core of the LwM2M protocol, defining communication rules between the client and server. Clients report data to the server following this model, while the server sends read, write, execute, and other commands to the client. + +The relationship between the client, objects, and resources is shown below: + +- A client can have multiple resource types, each associated with a specific object. +- All standard objects and resources are identified by fixed IDs maintained by the OMA LwM2M Registry. + +![Resource Model](https://assets.emqx.com/images/c06be1948effd185f49ea74ef7044edc.png) + +For example, the object ID for temperature sensor devices is `3303`, defining the following resources: + +- `Resource 5700: Sensor Value` (current temperature reading) +- `Resource 5701: Sensor Units` (units of the temperature reading, e.g., Celsius or Fahrenheit) +- `Resource 5601: Min Measured Value` (minimum recorded temperature) +- `Resource 5602: Max Measured Value` (maximum recorded temperature) + +The LwM2M protocol supports various operations on objects and resources, including: + +- **Read:** Retrieve the current value of a resource. +- **Write:** Set a new value for a resource. +- **Execute:** Invoke functions defined on resources. +- **Observe/Notify:** Subscribe to resource changes and receive real-time notifications. + +### Advantages + +- **Lightweight:** Minimal network overhead, ideal for resource-constrained devices. +- **Remote Management:** Standardized resources enable firmware updates, status queries, and monitoring. +- **Flexible Resource Model:** Supports custom resource definitions for specific applications. +- **Security:** Ensures data security with DTLS-based encryption. + +### Key Use Cases + +- **Smart Cities:** Managing streetlights, traffic sensors, and environmental monitoring devices. +- **Transportation:** Vehicle tracking, fleet management, and vehicle health monitoring. +- **Industrial IoT:** Monitoring and managing factory machinery, sensors, and actuators. +- **Smart Agriculture:** Remote management of soil sensors, weather stations, and irrigation systems. +- **Smart Manufacturing:** Enhancing production line efficiency and predictive maintenance. +- **Healthcare:** Managing wearable health monitors and medical devices. + +## Integrating LwM2M Protocol with EMQX + +EMQX is a scalable and feature-rich MQTT message broker designed for IoT and real-time communication applications. In addition to full [MQTT protocol](https://www.emqx.com/en/blog/the-easiest-guide-to-getting-started-with-mqtt) support, EMQX manages non-MQTT protocols like STOMP, MQTT-SN, CoAP, and LwM2M via gateways. + +EMQX features a powerful built-in LwM2M gateway that efficiently connects various LwM2M devices. It supports UDP and DTLS connections, ensuring secure and stable device communication. With the EMQX Dashboard, users can easily manage devices. The LwM2M gateway also excels in message conversion, enabling seamless translation between LwM2M messages and JSON-formatted MQTT messages. This facilitates structured data parsing and analysis while enabling flexible device control through JSON messages. The bidirectional conversion enhances EMQX's capabilities in IoT device management and data interaction, providing strong support for complex IoT applications. + +### Enabling the LwM2M Gateway + +EMQX 5.x and later versions allow enabling the LwM2M gateway via the EMQX Dashboard. + +1. Start EMQX (e.g., version 5.8.4) with the following command and map ports 1883, 18083, and 5684 to the host machine. + + ```shell + sudo docker run -d --name emqx584 \ + -p 18083:18083 \ + -p 1883:1883 \ + -p 5783:5783/udp emqx/emqx:5.8.4 + ``` + +1. Open the EMQX Dashboard, navigate to **Management → Gateway**, and enable the LwM2M gateway: + + ![Management → Gateway](https://assets.emqx.com/images/81660ca53c39e3bb508b1bdd72739fcc.png) + +1. Keep the default settings, and the gateway will be successfully enabled upon seeing the success message: + + ![Gateways](https://assets.emqx.com/images/33f93da2301d601aa344ce93486684c1.png) + +### LwM2M Client and Message Exchange + +In this example, you can either choose to manually compile and install [wakaama](https://github.com/eclipse/wakaama) to provide LwM2M client support, or use a precompiled Docker image for testing. Here's an example using Docker: + +1. Use a Docker container to start an LwM2M client and observe its interaction via MQTTX-CLI: + + ```shell + sudo docker run -it --rm --network host emqx/mqttx-cli + ``` + +1. Subscribe to the `up/#` topic to receive messages generated by the LwM2M client: + + ```shell + mqttx sub --topic up/# + ``` + +1. Start the Wakaama command-line container using Docker: + + ```shell + sudo docker run --rm -it --network host heeejianbo/my-wakaama:1.0 + ``` + +1. Within the container, use the following command to establish a LwM2M client connection: + + ```shell + lwm2mclient -l 57830 -p 5783 -h 127.0.0.1 -4 -n testlwm2mclient + ``` + +1. When you observe the following message in the MQTTX client, it indicates that the LwM2M client has successfully logged into EMQX: + + ```shell + topic: up/register, qos: 0 + {"msgType":"register","data":{"objectList":["/1","/1/0","/2/0","/3/0","/4/0","/5/0","/6/0","/7/0","/31024","/31024/10","/31024/11","/31024/12"],"lwm2m":"1.1","lt":300,"ep":"testlwm2mclient","b":"U","alternatePath":"/"}} + topic: up/resp, qos: 0 + {"msgType":"observe","is_auto_observe":true,"data":{"reqPath":"/3/0","content":[{"path":"/3/0","value":"W3siYm4iOiIvMy8wLyIsIm4iOiIwIiwidnMiOiJPcGVuIE1vYmlsZSBBbGxpYW5jZSJ9LHsibiI6IjEiLCJ2cyI6IkxpZ2h0d2VpZ2h0IE0yTSBDbGllbnQifSx7Im4iOiIyIiwidnMiOiIzNDUwMDAxMjMifSx7Im4iOiIzIiwidnMiOiIxLjAifSx7Im4iOiI2LzAiLCJ2IjoxfSx7Im4iOiI2LzEiLCJ2Ijo1fSx7Im4iOiI3LzAiLCJ2IjozODAwfSx7Im4iOiI3LzEiLCJ2Ijo1MDAwfSx7Im4iOiI4LzAiLCJ2IjoxMjV9LHsibiI6IjgvMSIsInYiOjkwMH0seyJuIjoiOSIsInYiOjEwMH0seyJuIjoiMTAiLCJ2IjoxNX0seyJuIjoiMTEvMCIsInYiOjB9LHsibiI6IjEzIiwidiI6MzEwNDg1ODkwM30seyJuIjoiMTQiLCJ2cyI6IiswMTowMCJ9LHsibiI6IjE1IiwidnMiOiJFdXJvcGUvQmVybGluIn0seyJuIjoiMTYiLCJ2cyI6IlUifV0="}],"codeMsg":"content","code":"2.05"}} + ``` + +1. Connect another MQTTX-CLI client to interact with the LwM2M device: + + ```shell + sudo docker run -it --rm --network host emqx/mqttx-cli + ``` + +1. Send a read command to the `testlwm2mclient` created in step 3 to retrieve the device's firmware version: + + ```shell + mqttx pub --topic dn/testlwm2mclient -m '{"msgType": "read", "data": {"path": "/3/0/3"}}' + ``` + +1. You will observe that the subscribed client from step 1 receives the response, showing the firmware version as `1.0`: + + ```shell + topic: up/resp, qos: 0 + {"msgType":"read","data":{"reqPath":"/3/0/3","content":[{"value":"1.0","path":"/3/0/3"}],"codeMsg":"content","code":"2.05"}} + ``` + + At this point, a simple LwM2M connection and command exchange example has been completed. + +### Managing LwM2M Clients + +Through the EMQX Dashboard, users can manage LwM2M clients, view details, and perform operations via the **Clients** page under the respective gateway. + +![LwM2M Clients](https://assets.emqx.com/images/405528527fb19cba25f50e5c527d2a92.png) + +