乐闻世界logo
搜索文章和话题

KURA : how to change the MQTT messages format

1个答案

1

Because MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol primarily designed for low-bandwidth, high-latency, or unreliable network environments between devices and servers, the format of MQTT messages is fixed, consisting of a fixed header, an optional variable header, and a payload.

Modifying Message Content

If you are referring to modifying the content of the message (i.e., the Payload part), this typically depends on the specific application and the message format used. For example, if JSON is employed to encapsulate data, modifying the message content simply involves adjusting the JSON structure. Suppose the original message content is:

json
{ "temperature": 22, "humidity": 45 }

If we need to add a new data field representing wind speed, the modified JSON might appear as:

json
{ "temperature": 22, "humidity": 45, "windspeed": 7.5 }

Changing Messages Using Kura

If you are asking how to change the format of MQTT messages on the Kura platform, Kura offers multiple approaches to process and transform data. For instance, you can utilize the Wires component in Kura to graphically handle data streams and modify MQTT message structures within it. Specifically, you can add a Data Mapper component, which enables data transformation using JavaScript or simple Java code. Within this component, you can write scripts to alter the existing JSON structure or completely redefine the data format.

Example

Suppose we are using Kura to connect to a temperature and humidity sensor and send data via MQTT. We can change the data format in Kura through the following steps:

  1. Add Data Source: First, configure the sensor data source to ensure accurate data reading.
  2. Use Wires Component: In the Kura Wires view, add a Data Mapper component.
  3. Write Transformation Logic: In the Data Mapper component, write appropriate JavaScript or Java code to modify the data structure. As illustrated previously, include the wind speed field.
  4. Publish to MQTT: Set up another component to publish the modified data to the MQTT server. By doing this, we can flexibly adjust MQTT message content before transmission to accommodate various application scenarios or requirements of the data receiving end.
2024年8月21日 01:28 回复

你的答案