· Zen HuiFer · Operation Documentation  · 2 min read

MQTT Parsing Script

In this article, we detail how to write an MQTT client parsing script in the Go IoT development platform. Through specific case programs, you will learn how to handle data reported by the MQTT client and ensure that the return value contains the necessary data fields. This article also provides a detailed explanation of each field to help you better understand and apply these concepts.

In this article, we detail how to write an MQTT client parsing script in the Go IoT development platform. Through specific case programs, you will learn how to handle data reported by the MQTT client and ensure that the return value contains the necessary data fields. This article also provides a detailed explanation of each field to help you better understand and apply these concepts.

The Go IoT development platform allows users to write parsing scripts for different MQTT clients, and these scripts need to be able to execute correctly. The example program is as follows

function main(nc) {
    var dataRows = [
        { "Name": "Temperature", "Value": "23" },
        { "Name": "Humidity", "Value": "30" },
        { "Name": "A", "Value": nc },
    ];
    var result = {
        "Time":  Math.floor(Date.now() / 1000),
        "DataRows": dataRows,
        "DeviceUid": "5",
        "IdentificationCode":"5",
        "Nc": nc
    };
    return [result];
}

In the above program, the data reported by the MQTT client will be transmitted to the main function in the form of a string (nc is the parameter name). Do not adjust this content.

Regarding the return value, it must contain the data fields Time, DataRows, DeviceUid, and IdentificationCode. Their meanings are as follows:

  1. Time: Device data production time, in seconds timestamp.
  2. DataRows: Core signal data points. It is an array structure where the keys are Name and Value, Name represents the signal name, and Value represents the signal data value.
  3. DeviceUid: MQTT client ID, please correspond to the unique code of the current row of data. Please check carefully

image-20240806134823594

  1. IdentificationCode: Device identification code, if your subscription topic does not have a wildcard, please keep this data information consistent with DeviceUid. If a wildcard appears, please differentiate it from DeviceUid.

Note:

  1. DeviceUid is hardcoded in the return value. Do not generate it dynamically.
  2. IdentificationCode must be differentiated from DeviceUid in principle, i.e., the two cannot be the same.

Influxdb脚本执行器Go IoTMQTT服务器设备Influxdb脚本执行器Go IoTMQTT服务器设备上报数据监听主题执行JavaScript脚本写入数据

When writing to Influxdb, assemble according to the following data assembly rules.

  1. bucket: Project fixed configuration.

  2. Measurement: Dynamically calculated as ${protocol}_${DeviceUid}_${IdentificationCode}

func genMeasurement(dt DataRowList, protocol string) string {
	return protocol + "_" + dt.DeviceUid + "_" + dt.IdentificationCode
}
Back to Blog

Related Posts

View All Posts »
Multi-protocol Support

Multi-protocol Support

This article introduces how to use WebSocket, MQTT, TCP/IP, COAP protocols for data transmission in the Go IoT development platform, and provides relevant port configuration and Nginx configuration examples to help developers better achieve multi-protocol support.

Device Access via COAP

Device Access via COAP

This article details how to use the COAP protocol to access devices on the Go IoT development platform, including access procedures, code examples, and precautions to help developers quickly get started and achieve efficient device access.

TCP/IP Access Device

TCP/IP Access Device

This article details how to use the TCP/IP protocol to access devices in the Go IoT development platform, including access processes, authentication methods, and data transmission examples, helping developers quickly get started and achieve efficient device access.

MQTT解析脚本

MQTT解析脚本

在本文中,我们详细介绍了如何在Go IoT开发平台中编写MQTT客户端解析脚本。通过具体的案例程序,您将学习到如何处理MQTT客户端上报的数据,并确保返回值包含必要的数据字段。本文还提供了每个字段的详细解释,帮助您更好地理解和应用这些概念。