Table of Contents:
Introduction
This document guides you on integrating Dragino -NB and -CB series devices with ThingsBoard. For this guide, we use ThingsBoard Cloud, which is one of the ThingsBoard versions that allows you to try it for free.
Add New Device
In the left navigation, click Entities and then click Devices.
On the ‘Devices’ page, click on the ‘+’ button, and then click on the ‘Add new device’ from the dropdown menu.
Data Converters
Uplink
In the left navigation, click ‘Integrations center’, and then click ‘Data converters’.
On the ‘Data converters’ page, click on the ‘+’ button, and then click on the ‘Create new converter’ from the dropdown menu.
The ‘Add data converter’ window will appear. Name it ‘UDP Uplink Converter NB/CB’ and select the Type as ‘Uplink’.
Click on the ‘JavaScript’ button. Now copy and paste the following script to the ‘Decoder function’ section:
// decode payload to string
var payloadStr = decodeToString(payload);
// decode payload to JSON
var objdata = {};
var obj1 = {};
var data = decodeToJson(payload);
var deviceName = data.IMEI;
delete data.IMEI;
var modelname = "Dragino " + data.Model;
//var mod = data.mod
delete data.Model;
//delete data.mod
var timestamp = new Date().getTime();
for (var key in data) {
if (Number(key)) {
obj1[key] = data[key];
obj1[key][obj1[key].length - 1] = Number(new Date(
obj1[key][obj1[key].length - 1]));
}
//Alec submitted25/02/25
//turn old key into new
else if (key === "Reading") {
objdata["reading"] = data[key];
} else if (key === "work mode") {
objdata["work_mode"] = data[key];
} else if (key === "hum") {
objdata["humidity"] = data[key];
}else if (key === "hum2") {
objdata["humidity2"] = data[key];
} else if (key === "hum3") {
objdata["humidity3"] = data[key];
} else if (key === "tem") {
objdata["temperature"] = data[key];
} else if (key === "tem2") {
objdata["temperature2"] = data[key];
} else if (key === "tem3") {
objdata["temperature3"] = data[key];
} else if (key === "DS18B20_Temp") {
objdata["temperature_pro"] = data[key];
} else if (key === "ds18b20_temperature") {
objdata["temperature_pro"] = data[key];
} else if (key === "DS18B20_temperature_pro") {
objdata["temperature_pro"] = data[key];
} else if (key === "tdc send flag") {
objdata["tdc_send_flag"] = data[key];
} else if (key === "trigger mode") {
objdata["trigger_mode"] = data[key];
} else if (key === "soil dielectric constant") {
objdata["soil_dielectric_constant"] = data[key];
} else if (key === "door open num") {
objdata["door_open_num"] = data[key];
} else if (key === "door duration") {
objdata["door_duration"] = data[key];
} else if (key === "count time") {
objdata["count_time"] = data[key];
} else if (key === "last open time2") {
objdata["last_open_time2"] = data[key];
} else if (key === "last open time3") {
objdata["last_open_time3"] = data[key];
}
//Alec submitted25/02/25
else {
objdata[key] = data[key]
}
}
var listdata = [{
"ts": timestamp,
"values": objdata
}]
for (var key1 in obj1) {
if (modelname == "Dragino RS485-NB") {
listdata.push({
"ts": obj1[key1][obj1[key1].length - 1],
"values": {
"Payload": obj1[key1][0],
}
})
} else {
listdata.push({
"ts": obj1[key1][obj1[key1].length - 1],
"values": {
"values": obj1[key1]
},
})
}
}
var result = {
deviceName: deviceName,
deviceType: modelname,
attributes: {
model: modelname,
//customerName: "NB-CB",
//groupName: "NB-CB",
//integrationName: metadata['integrationName']
},
telemetry: listdata
}
function decodeToString(payload) {
return String.fromCharCode.apply(String, payload);
}
function decodeToJson(payload) {
// covert payload to string.
var str = decodeToString(payload);
// parse string to JSON
var data = JSON.parse(str);
return data;
}
return result;
Click on the ‘Add’ button.
The uplink data converter is added to ThingsBoard and appears on the ‘Data Converters’ page
Downlink
On the ‘Data converters’ page, click on the ‘+’ button, and then click on the ‘Create new converter’ from the dropdown menu.
The ‘Add data converter’ window will appear. Name it ‘UDP Downlink Converter NB/CB’ and select the Type as ‘Downlink’.
Click on the ‘JavaScript’ button. Now copy and paste the following script to the ‘Encoder function’ section:
// 将16进制字符串两个字符转换为一个字节
var bytes = hexString.match(/.{2}/g);
// 对每个字节进行解析,并转换为对应的字符
var binaryString = bytes.map(function(byte) {
return String.fromCharCode(parseInt(byte, 16));
}).join('');
// 使用btoa进行base64编码
return btoa(binaryString);
}
// Result object with encoded downlink payload
var result = {
// downlink data content type: JSON, TEXT or BINARY (base64 format)
contentType: "BINARY",
// downlink data
data:hexToBase64(metadata.shared_value)
// Optional metadata object presented in key/value format
//metadata: {}
};
return result;
Click on the Add button.
Add Integration
In the left navigation, click ‘Integrations center’, and then click ‘Integrations’.
On the ‘Integrations’ page, click on the “+” button.
The ‘Add integration’ window appears.
In the Add Integration window, configure the following settings:
Basic settings:
- Integration type: UDP
- Name: UDP Integration NB/CB
Click Next button.