Skip to content

Commit 518117b

Browse files
marekmaskarinechubmartin
authored andcommitted
applications: clime: Fix LRW codec for IAQ
Signed-off-by: Marek Maškarinec <[email protected]>
1 parent 107adce commit 518117b

File tree

2 files changed

+57
-22
lines changed

2 files changed

+57
-22
lines changed

applications/clime/codec/cs-decoder.js

+51-17
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@ var buffer;
33

44
// Uncomment for ChirpStack:
55
function decodeUplink(input) {
6-
76
// Uncomment for The Things Network:
87
// function Decoder(port, bytes) {
98

10-
buffer = bytes;
9+
buffer = input.bytes;
1110

1211
var data = {};
1312

1413
var header = u8();
1514

1615
if (header & 0x80) {
17-
var header_higher = u8() << 8;
18-
header = header_higher + header;
16+
header = header | (u8() << 8);
1917
}
2018

2119
if ((header & 0x01) !== 0) {
@@ -48,7 +46,16 @@ function decodeUplink(input) {
4846
}
4947
}
5048

51-
if ((header & 0x04) !== 0) {
49+
if ((header & 0x04) !== 0 && (header & 0x08) == 0) {
50+
data.therm_temperature = s16();
51+
if (data.therm_temperature === 0x7fff) {
52+
data.therm_temperature = null;
53+
} else {
54+
data.therm_temperature /= 100;
55+
}
56+
}
57+
58+
if ((header & 0x08) !== 0) {
5259
data.internal_temperature = s16();
5360
if (data.internal_temperature === 0x7fff) {
5461
data.internal_temperature = null;
@@ -91,11 +98,11 @@ function decodeUplink(input) {
9198
data.illuminance = data.illuminance / 100;
9299
}
93100

94-
data.presure = u16();
95-
if (data.presure === 0xffff) {
96-
data.presure = null;
101+
data.pressure = u16();
102+
if (data.pressure === 0xffff) {
103+
data.pressure = null;
97104
} else {
98-
data.presure = data.presure / 100;
105+
data.pressure = data.pressure / 100;
99106
}
100107
}
101108

@@ -182,7 +189,33 @@ function decodeUplink(input) {
182189
}
183190
}
184191

185-
return data;
192+
if ((header & 0x0200) !== 0) {
193+
data.soil_sensors = [];
194+
195+
let count = u8();
196+
197+
for (let i = 0; i < count; i++) {
198+
let sensor = {};
199+
200+
let temperature = s16();
201+
if (temperature === 0x7fff) {
202+
sensor.temperature = null;
203+
} else {
204+
sensor.temperature = temperature / 100;
205+
}
206+
207+
let moisture = u16();
208+
if (moisture === 0xffff) {
209+
sensor.moisture = null;
210+
} else {
211+
sensor.moisture = moisture;
212+
}
213+
214+
data.soil_sensors.push(sensor);
215+
}
216+
}
217+
218+
return { data: data };
186219
}
187220

188221
function s8() {
@@ -205,7 +238,7 @@ function u8() {
205238

206239
function s16() {
207240
var value = buffer.slice(cursor);
208-
value = value[0] | value[1] << 8;
241+
value = value[0] | (value[1] << 8);
209242
if ((value & (1 << 15)) > 0) {
210243
value = (~value & 0xffff) + 1;
211244
value = -value;
@@ -216,21 +249,21 @@ function s16() {
216249

217250
function u16() {
218251
var value = buffer.slice(cursor);
219-
value = value[0] | value[1] << 8;
252+
value = value[0] | (value[1] << 8);
220253
cursor = cursor + 2;
221254
return value;
222255
}
223256

224257
function u32() {
225258
var value = buffer.slice(cursor);
226-
value = value[0] | value[1] << 8 | value[2] << 16 | value[3] << 24;
259+
value = value[0] | (value[1] << 8) | (value[2] << 16) | (value[3] << 24);
227260
cursor = cursor + 4;
228261
return value;
229262
}
230263

231264
function s32() {
232265
var value = buffer.slice(cursor);
233-
value = value[0] | value[1] << 8 | value[2] << 16 | value[3] << 24;
266+
value = value[0] | (value[1] << 8) | (value[2] << 16) | (value[3] << 24);
234267
if ((value & (1 << 31)) > 0) {
235268
value = (~value & 0xffffffff) + 1;
236269
value = -value;
@@ -240,7 +273,8 @@ function s32() {
240273
}
241274

242275
if (false) {
243-
var hex = "00000020026609ff7fff97260200e712691298b700000000000000000000000000000000000000000000"
244-
var buf = Buffer.from(hex, 'hex')
245-
console.log(JSON.stringify(Decode(1, buf, 0)));
276+
var hex = "af01070eda0d23022e094f096d0d2887c4e46400145c0000";
277+
var buf = Buffer.from(hex, "hex");
278+
console.log(decodeUplink({ bytes: buf }));
246279
}
280+

applications/clime/src/app_send.c

+6-5
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ static int compose_lrw(struct ctr_buf *buf)
7575
}
7676

7777
/* Flag RTD_THERM */
78-
if (IS_ENABLED(FEATURE_HARDWARE_CHESTER_RTD_A) || IS_ENABLED(FEATURE_HARDWARE_CHESTER_RTD_B)) {
78+
if (IS_ENABLED(FEATURE_HARDWARE_CHESTER_RTD_A) ||
79+
IS_ENABLED(FEATURE_HARDWARE_CHESTER_RTD_B)) {
7980
header |= BIT(6);
8081
}
8182

@@ -163,7 +164,7 @@ static int compose_lrw(struct ctr_buf *buf)
163164
if (isnan(iaqdata->sensors.last_co2_conc)) {
164165
ret |= ctr_buf_append_u16_le(buf, BIT_MASK(16));
165166
} else {
166-
uint16_t val = iaqdata->sensors.last_co2_conc * 100.f;
167+
uint16_t val = iaqdata->sensors.last_co2_conc;
167168
ret |= ctr_buf_append_u16_le(buf, val);
168169
}
169170

@@ -175,10 +176,10 @@ static int compose_lrw(struct ctr_buf *buf)
175176
}
176177

177178
if (isnan(iaqdata->sensors.last_pressure)) {
178-
ret |= ctr_buf_append_u16_le(buf, BIT_MASK(16));
179+
ret |= ctr_buf_append_u32_le(buf, BIT_MASK(16));
179180
} else {
180-
uint16_t val = iaqdata->sensors.last_pressure * 100.f;
181-
ret |= ctr_buf_append_u16_le(buf, val);
181+
uint32_t val = iaqdata->sensors.last_pressure;
182+
ret |= ctr_buf_append_u32_le(buf, val);
182183
}
183184
}
184185
#endif /* defined(FEATURE_HARDWARE_CHESTER_S1) */

0 commit comments

Comments
 (0)