-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttn_payload_formatter_function.js
85 lines (69 loc) · 2.29 KB
/
ttn_payload_formatter_function.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
function decodeUplink(input) {
var buf_temp = new ArrayBuffer(4);
var view_temp = new DataView(buf_temp);
const temp_bytes = input.bytes.slice(0, 4);
var buf_pres = new ArrayBuffer(4);
var view_pres = new DataView(buf_pres);
const pres_bytes = input.bytes.slice(4, 8);
var buf_hum = new ArrayBuffer(4);
var view_hum = new DataView(buf_hum);
const hum_bytes = input.bytes.slice(8, 12);
var buf_pm10 = new ArrayBuffer(8);
var view_pm10 = new DataView(buf_pm10);
const pm10_bytes = input.bytes.slice(12, 20);
var buf_pm25 = new ArrayBuffer(8);
var view_pm25 = new DataView(buf_pm25);
const pm25_bytes = input.bytes.slice(20, 28);
temp_bytes.forEach(function(b, i) {
view_temp.setUint8(i, b);
});
pres_bytes.forEach(function(b, i) {
view_pres.setUint8(i, b);
});
hum_bytes.forEach(function(b, i) {
view_hum.setUint8(i, b);
});
pm10_bytes.forEach(function(b, i) {
view_pm10.setUint8(i, b);
});
pm25_bytes.forEach(function(b, i) {
view_pm25.setUint8(i, b);
});
//var temp = view_temp.getFloat64(0);
var temp = view_temp.getInt32(0);
var pres = view_pres.getInt32(0);
var hum = view_hum.getInt32(0);
var pm10 = view_pm10.getFloat64(0);
var pm25 = view_pm25.getFloat64(0);
//Варна е на 80м надм. височина
const above_sea_level = 80;
var slp = pres + ((pres * 9.80665 * above_sea_level) / (287 * (273 + temp + (above_sea_level / 400))));
var p25 = Math.round(pm10 * 10) / 10;
var p10 = Math.round(pm25 * 10) / 10;
pm10 = p10.toString();
pm25 = p25.toString();
return {
data: {
id: 1,
data: [{
name: 'bme280',
type: "temp_sensor",
data: {
temperature: temp,
actual_pressure: pres,
humidity: hum,
sea_level_pressure: Math.round(slp)
}
},
{
name: "dust",
type: "dust_sensor",
data: {
pm10: pm10,
pm25: pm25
}
}
]
}
}
}