-
Notifications
You must be signed in to change notification settings - Fork 1
/
xsns_20_novasds.ino
executable file
·142 lines (119 loc) · 3.74 KB
/
xsns_20_novasds.ino
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
xsns_20_novasds.ino - Nova SDS011/SDS021 particle concentration sensor support for Sonoff-Tasmota
*/
#ifdef USE_NOVA_SDS
/*********************************************************************************************\
* Nova Fitness SDS011 (and possibly SDS021) particle concentration sensor
* For background information see http://aqicn.org/sensor/sds011/
*
* Hardware Serial will be selected if GPIO3 = [SDS0X01]
\*********************************************************************************************/
#include <TasmotaSerial.h>
TasmotaSerial *NovaSdsSerial;
uint8_t novasds_type = 1;
uint8_t novasds_valid = 0;
struct sds011data {
uint16_t pm100;
uint16_t pm25;
} novasds_data;
bool NovaSdsReadData()
{
if (! NovaSdsSerial->available()) return false;
while ((NovaSdsSerial->peek() != 0xAA) && NovaSdsSerial->available()) {
NovaSdsSerial->read();
}
byte d[8] = { 0 };
NovaSdsSerial->read(); // skip 0xAA
NovaSdsSerial->readBytes(d, 8);
NovaSdsSerial->flush();
AddLogSerial(LOG_LEVEL_DEBUG_MORE, d, 8);
if (d[7] == ((d[1] + d[2] + d[3] + d[4] + d[5] + d[6]) & 0xFF)) {
novasds_data.pm25 = (d[1] + 256 * d[2]);
novasds_data.pm100 = (d[3] + 256 * d[4]);
} else {
AddLog_P(LOG_LEVEL_DEBUG, PSTR("SDS: " D_CHECKSUM_FAILURE));
return false;
}
novasds_valid = 10;
return true;
}
/*********************************************************************************************/
void NovaSdsSecond() // Every second
{
if (NovaSdsReadData()) {
novasds_valid = 10;
} else {
if (novasds_valid) {
novasds_valid--;
}
}
}
/*********************************************************************************************/
void NovaSdsInit()
{
novasds_type = 0;
if (pin[GPIO_SDS0X1] < 99) {
NovaSdsSerial = new TasmotaSerial(pin[GPIO_SDS0X1], -1, 1);
if (NovaSdsSerial->begin(9600)) {
if (NovaSdsSerial->hardwareSerial()) { ClaimSerial(); }
novasds_type = 1;
}
}
}
#ifdef USE_WEBSERVER
const char HTTP_SDS0X1_SNS[] PROGMEM = "%s"
"{s}SDS0X1 " D_ENVIRONMENTAL_CONCENTRATION " 2.5 " D_UNIT_MICROMETER "{m}%s " D_UNIT_MICROGRAM_PER_CUBIC_METER "{e}"
"{s}SDS0X1 " D_ENVIRONMENTAL_CONCENTRATION " 10 " D_UNIT_MICROMETER "{m}%s " D_UNIT_MICROGRAM_PER_CUBIC_METER "{e}"; // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
#endif // USE_WEBSERVER
void NovaSdsShow(boolean json)
{
if (novasds_valid) {
char pm10[10];
char pm2_5[10];
float pm10f = (float)(novasds_data.pm100) / 10.0f;
float pm2_5f = (float)(novasds_data.pm25) / 10.0f;
dtostrfd(pm10f, 1, pm10);
dtostrfd(pm2_5f, 1, pm2_5);
if (json) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"SDS0X1\":{\"PM2.5\":%s,\"PM10\":%s}"), mqtt_data, pm2_5, pm10);
#ifdef USE_DOMOTICZ
if (0 == tele_period) {
DomoticzSensor(DZ_VOLTAGE, pm2_5); // PM2.5
DomoticzSensor(DZ_CURRENT, pm10); // PM10
}
#endif // USE_DOMOTICZ
#ifdef USE_WEBSERVER
} else {
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SDS0X1_SNS, mqtt_data, pm2_5, pm10);
#endif // USE_WEBSERVER
}
}
}
/*********************************************************************************************\
* Interface
\*********************************************************************************************/
#define XSNS_20
boolean Xsns20(byte function)
{
boolean result = false;
if (novasds_type) {
switch (function) {
case FUNC_INIT:
NovaSdsInit();
break;
case FUNC_EVERY_SECOND:
NovaSdsSecond();
break;
case FUNC_JSON_APPEND:
NovaSdsShow(1);
break;
#ifdef USE_WEBSERVER
case FUNC_WEB_APPEND:
NovaSdsShow(0);
break;
#endif // USE_WEBSERVER
}
}
return result;
}
#endif // USE_NOVA_SDS