跳至主要內容

星纵 LoRaWAN EM500 CO2 传感器上报数据解析

约 550 字大约 2 分钟

星纵 LoRaWAN EM500 CO2 传感器上报数据解析

客户的星纵 LoRaWAN 子设备 EM500 CO2 传感器已经接受到来自网关转发的原始数据,这个示例介绍如何对数据进行解码。

关于如何将网关设备接收的数据转发到子设备,请阅读:

创建属性上报预处理规则

对子设备或所属设备类型创建消息规则,这里使用属性上报预处理规则,如下图:

选择正确的设备来源,如下图:

添加操作,选择预处理函数,如下图:

这里根据设备厂家提供的协议,我们编写了解码函数。

函数内容如下,可以直接复制使用。

module.exports = function (report_attributes) {
    /**
     * report_attributes:   上报的属性对象,同时作为函数返回值。函数中可更新属性对象。
     */

    if (report_attributes.data !== undefined) {

        // 利用内置函数,将 LoRaWAN 设备上报的 base64 格式的 data 转为二进制数据。
        var buffer = Cloud.Utils.base64ToBuffer(report_attributes.data);
        // 转成 hex 格式,仅做调试记录
        report_attributes.data_hex = buffer.toString('hex');
        
        // 根据设备协议解码
        let i = 0;
        if (buffer[i] == 0x01 && buffer[i+1] == 0x75) {
            report_attributes.battery = buffer.readInt8(i+2);
            i += 3;
        }
        if (buffer[i] == 0x03 && buffer[i+1] == 0x67) {
            report_attributes.temperature = buffer.readInt16LE(i+2) / 10;
            i += 4;
        }
        if (buffer[i] == 0x04 && buffer[i+1] == 0x68) {
            report_attributes.humidity = buffer.readInt8(i+2) / 2;
            i += 3;
        }
        if (buffer[i] == 0x05 && buffer[i+1] == 0x7d) {
            report_attributes.co2 = buffer.readUInt16LE(i+2);
            i += 4;
        }
        if (buffer[i] == 0x06 && buffer[i+1] == 0x73) {
            report_attributes.pressure = buffer.readInt16LE(i+2) / 10;
            i += 4;
        }
    }
    
    return report_attributes;
}

建好的规则是这样的,如下图:

数据解析成功

规则创建后自动生效,可以看到设备调试日志中,每条属性上报消息在经过规则处理后,自动追加了新的属性。

在设备概览中可以看到解码后的属性值,如下图:

详细了解 ThingsCloud 消息规则,请浏览 消息规则

更多功能