forked from enesbcs/ESPEasyPluginPlayground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_P111_SenseAir.ino
266 lines (234 loc) · 7.55 KB
/
_P111_SenseAir.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//#######################################################################################################
//############################# Plugin 111: SenseAir CO2 Sensors ########################################
//#######################################################################################################
/*
Plugin originally written by: Daniel Tedenljung info__AT__tedenljungconsulting.com
Rewritten by: Mikael Trieb mikael__AT__triebconsulting.se
This plugin reads availble values of SenseAir Co2 Sensors.
Datasheet can be found here:
S8: http://www.senseair.com/products/oem-modules/senseair-s8/
K30: http://www.senseair.com/products/oem-modules/k30/
K70/tSENSE: http://www.senseair.com/products/wall-mount/tsense/
Circuit wiring
GPIO Setting 1 -> RX
GPIO Setting 2 -> TX
Use 1kOhm in serie on datapins!
*/
#define PLUGIN_111
#define PLUGIN_ID_111 111
#define PLUGIN_NAME_111 "SenseAir"
#define PLUGIN_VALUENAME1_111 ""
boolean Plugin_111_init = false;
#include <SoftwareSerial.h>
SoftwareSerial *Plugin_111_SoftSerial;
boolean Plugin_111(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_111;
Device[deviceCount].Type = DEVICE_TYPE_DUAL;
Device[deviceCount].VType = SENSOR_TYPE_SINGLE;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = true;
Device[deviceCount].ValueCount = 1;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_111);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_111));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
byte choice = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
String options[4];
options[0] = F("Status");
options[1] = F("Carbon Dioxide");
options[2] = F("Temperature");
options[3] = F("Humidity");
int optionValues[4];
optionValues[0] = 0;
optionValues[1] = 1;
optionValues[2] = 2;
optionValues[3] = 3;
string += F("<TR><TD>Sensor:<TD><select name='plugin_111'>");
for (byte x = 0; x < 4; x++)
{
string += F("<option value='");
string += optionValues[x];
string += "'";
if (choice == optionValues[x])
string += F(" selected");
string += ">";
string += options[x];
string += F("</option>");
}
string += F("</select>");
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
String plugin1 = WebServer.arg(F("plugin_111"));
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = plugin1.toInt();
success = true;
break;
}
case PLUGIN_INIT:
{
Plugin_111_init = true;
Plugin_111_SoftSerial = new SoftwareSerial(Settings.TaskDevicePin1[event->TaskIndex],
Settings.TaskDevicePin2[event->TaskIndex]);
success = true;
break;
}
case PLUGIN_READ:
{
if (Plugin_111_init)
{
String log = F("SenseAir: ");
switch(Settings.TaskDevicePluginConfig[event->TaskIndex][0])
{
case 0:
{
int sensor_status = Plugin_111_readStatus();
UserVar[event->BaseVarIndex] = sensor_status;
log += sensor_status;
break;
}
case 1:
{
int co2 = Plugin_111_readCo2();
UserVar[event->BaseVarIndex] = co2;
log += co2;
break;
}
case 2:
{
float temperature = Plugin_111_readTemperature();
UserVar[event->BaseVarIndex] = (float)temperature;
log += (float)temperature;
break;
}
case 3:
{
float relativeHumidity = Plugin_111_readRelativeHumidity();
UserVar[event->BaseVarIndex] = (float)relativeHumidity;
log += (float)relativeHumidity;
break;
}
}
addLog(LOG_LEVEL_INFO, log);
success = true;
break;
}
break;
}
}
return success;
}
void Plugin_111_buildFrame(byte slaveAddress,
byte functionCode,
short startAddress,
short numberOfRegisters,
byte frame[8])
{
frame[0] = slaveAddress;
frame[1] = functionCode;
frame[2] = (byte)(startAddress >> 8);
frame[3] = (byte)(startAddress);
frame[4] = (byte)(numberOfRegisters >> 8);
frame[5] = (byte)(numberOfRegisters);
// CRC-calculation
byte checkSum[2] = {0};
unsigned int crc = Plugin_111_ModRTU_CRC(frame, 6, checkSum);
frame[6] = checkSum[0];
frame[7] = checkSum[1];
}
int Plugin_111_sendCommand(byte command[])
{
byte recv_buf[7] = {0xff};
byte data_buf[2] = {0xff};
long value = -1;
Plugin_111_SoftSerial->write(command, 8); //Send the byte array
delay(50);
// Read answer from sensor
int ByteCounter = 0;
while(Plugin_111_SoftSerial->available()) {
recv_buf[ByteCounter] = Plugin_111_SoftSerial->read();
ByteCounter++;
}
data_buf[0] = recv_buf[3];
data_buf[1] = recv_buf[4];
value = (data_buf[0] << 8) | (data_buf[1]);
return value;
}
int Plugin_111_readStatus(void)
{
int sensor_status = -1;
byte frame[8] = {0};
Plugin_111_buildFrame(0xFE, 0x04, 0x00, 1, frame);
sensor_status = Plugin_111_sendCommand(frame);
return sensor_status;
}
int Plugin_111_readCo2(void)
{
int co2 = 0;
byte frame[8] = {0};
Plugin_111_buildFrame(0xFE, 0x04, 0x03, 1, frame);
co2 = Plugin_111_sendCommand(frame);
return co2;
}
float Plugin_111_readTemperature(void)
{
int temperatureX100 = 0;
float temperature = 0.0;
byte frame[8] = {0};
Plugin_111_buildFrame(0xFE, 0x04, 0x04, 1, frame);
temperatureX100 = Plugin_111_sendCommand(frame);
temperature = temperatureX100/100;
return temperature;
}
float Plugin_111_readRelativeHumidity(void)
{
int rhX100 = 0;
float rh = 0.0;
byte frame[8] = {0};
Plugin_111_buildFrame(0xFE, 0x04, 0x05, 1, frame);
rhX100 = Plugin_111_sendCommand(frame);
rh = rhX100/100;
return rh;
}
// Compute the MODBUS RTU CRC
unsigned int Plugin_111_ModRTU_CRC(byte buf[], int len, byte checkSum[2])
{
unsigned int crc = 0xFFFF;
for (int pos = 0; pos < len; pos++) {
crc ^= (unsigned int)buf[pos]; // XOR byte into least sig. byte of crc
for (int i = 8; i != 0; i--) { // Loop over each bit
if ((crc & 0x0001) != 0) { // If the LSB is set
crc >>= 1; // Shift right and XOR 0xA001
crc ^= 0xA001;
}
else // Else LSB is not set
crc >>= 1; // Just shift right
}
}
// Note, this number has low and high bytes swapped, so use it accordingly (or swap bytes)
checkSum[1] = (byte)((crc >> 8) & 0xFF);
checkSum[0] = (byte)(crc & 0xFF);
return crc;
}