forked from Hypfer/esp8266-vindriktning-particle-sensor
-
Notifications
You must be signed in to change notification settings - Fork 11
/
decoder.js
47 lines (43 loc) · 1.02 KB
/
decoder.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
function Decoder(bytes, port) {
const pm25 = (bytes[0] << 8) | bytes[1];
// classify pm25 using https://www.airgradient.com/open-airgradient/blog/ikea-vindriktning-accuracy/
var us_aqi_pm25 = "NA";
switch (true) {
case pm25 < 13:
us_aqi_pm25 = "GOOD";
break;
case pm25 < 36:
us_aqi_pm25 = "MODERATE";
break;
case pm25 < 56:
us_aqi_pm25 = "UNHEALTHY SENSITIVE IND";
break;
case pm25 < 151:
us_aqi_pm25 = "UNHEALTHY";
break;
case pm25 < 251:
us_aqi_pm25 = "VERY UNHEALTHY";
break;
default:
us_aqi_pm25 = "HAZARDOUS";
break;
}
var ikea_sensor_color = "NA";
switch (true) {
case pm25 < 36:
ikea_sensor_color = "GREEN";
break;
case pm25 < 86:
ikea_sensor_color = "YELLOW";
break;
default:
ikea_sensor_color = "RED";
break;
}
var decodedPayload = {
"pm-25": pm25,
"us-aqi-pm25": us_aqi_pm25,
"ikea-sensor-color": ikea_sensor_color,
};
return Serialize(decodedPayload);
}