forked from letscontrolit/ESPEasyPluginPlayground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_P215_Atlas_EZO_EC.ino
364 lines (305 loc) · 14.7 KB
/
_P215_Atlas_EZO_EC.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//########################################################################
//################## Plugin 214 : Atlas Scientific EZO EC sensor ########
//########################################################################
// datasheet at https://www.atlas-scientific.com/_files/_datasheets/_circuit/EC_EZO_datasheet.pdf
// works only in i2c mode
#define PLUGIN_215
#define PLUGIN_ID_215 215
#define PLUGIN_NAME_215 "Environment - Atlas Scientific EC EZO"
#define PLUGIN_VALUENAME1_215 "EC"
boolean Plugin_215_init = false;
boolean Plugin_215(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_215;
Device[deviceCount].Type = DEVICE_TYPE_I2C;
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_215);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_215));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
#define ATLASEZO_EC_I2C_NB_OPTIONS 3
byte I2Cchoice = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
int optionValues[ATLASEZO_EC_I2C_NB_OPTIONS] = { 0x64, 0x65, 0x66 };
addFormSelectorI2C(F("Plugin_215_i2c"), ATLASEZO_EC_I2C_NB_OPTIONS, optionValues, I2Cchoice);
addFormSubHeader(F("General"));
char sensordata[32];
bool status;
status = _P215_send_I2C_command(Settings.TaskDevicePluginConfig[event->TaskIndex][0],"i",sensordata);
if (status) {
String boardInfo(sensordata);
addHtml(F("<TR><TD>Board type : </TD><TD>"));
int pos1 = boardInfo.indexOf(',');
int pos2 = boardInfo.lastIndexOf(',');
addHtml(boardInfo.substring(pos1+1,pos2));
if (boardInfo.substring(pos1+1,pos2) != "EC"){
addHtml(F("<span style='color:red'> WARNING : Board type should be 'EC', check your i2c Address ? </span>"));
}
addHtml(F("</TD></TR><TR><TD>Board version :</TD><TD>"));
addHtml(boardInfo.substring(pos2+1));
addHtml(F("</TD></TR>\n"));
addHtml(F("<input type='hidden' name='Plugin_215_sensorVersion' value='"));
addHtml(boardInfo.substring(pos2+1));
addHtml(F("'>"));
status = _P215_send_I2C_command(Settings.TaskDevicePluginConfig[event->TaskIndex][0],"K,?",sensordata);
boardInfo = sensordata;
addHtml(F("<TR><TD>Sensor type : </TD><TD>K="));
pos2 = boardInfo.lastIndexOf(',');
addHtml(boardInfo.substring(pos2+1));
addHtml(F("</TD></TR>\n"));
} else {
addHtml(F("<span style='color:red;'>Unable to send command to device</span>"));
success = false;
break;
}
addFormCheckBox(F("Status LED"), F("Plugin_215_status_led"), Settings.TaskDevicePluginConfig[event->TaskIndex][1]);
addFormSubHeader(F("Calibration"));
addRowLabel(F("<strong>Dry calibration</strong>"));
addFormCheckBox(F("Enable"),F("Plugin_215_enable_cal_dry"), false);
addHtml(F("\n<script type='text/javascript'>document.getElementById(\"Plugin_215_enable_cal_dry\").onclick=function() {document.getElementById(\"Plugin_215_enable_cal_single\").checked = false;document.getElementById(\"Plugin_215_enable_cal_L\").checked = false;;document.getElementById(\"Plugin_215_enable_cal_H\").checked = false;};</script>"));
addFormNote(F("Dry calibration must always be done first!"));
addRowLabel(F("<strong>Single point calibration</strong> "));
addFormCheckBox(F("Enable"),F("Plugin_215_enable_cal_single"), false);
addHtml(F("\n<script type='text/javascript'>document.getElementById(\"Plugin_215_enable_cal_single\").onclick=function() {document.getElementById(\"Plugin_215_enable_cal_dry\").checked = false;document.getElementById(\"Plugin_215_enable_cal_L\").checked = false;;document.getElementById(\"Plugin_215_enable_cal_H\").checked = false;};</script>"));
addFormNumericBox(F("Ref EC"),F("Plugin_215_ref_cal_single"), Settings.TaskDevicePluginConfig[event->TaskIndex][2]);
addUnit("µS");
addRowLabel(F("<strong>Low calibration</strong>"));
addFormCheckBox(F("enable"),F("Plugin_215_enable_cal_L' onClick='document.getElementById(\"Plugin_215_enable_cal_dry\").checked = false;document.getElementById(\"Plugin_215_enable_cal_single\").checked = false;;document.getElementById(\"Plugin_215_enable_cal_H\").checked = false;"), false);
addHtml(F("\n<script type='text/javascript'>document.getElementById(\"Plugin_215_enable_cal_L\").onclick=function() {document.getElementById(\"Plugin_215_enable_cal_dry\").checked = false;document.getElementById(\"Plugin_215_enable_cal_single\").checked = false;;document.getElementById(\"Plugin_215_enable_cal_H\").checked = false;};</script>"));
addFormNumericBox(F("Ref EC"),F("Plugin_215_ref_cal_L"), Settings.TaskDevicePluginConfig[event->TaskIndex][3]);
addUnit("µS");
addRowLabel(F("<strong>High calibration</strong>"));
addFormCheckBox(F("Enable"),F("Plugin_215_enable_cal_H"), false);
addHtml(F("\n<script type='text/javascript'>document.getElementById(\"Plugin_215_enable_cal_H\").onclick=function() {document.getElementById(\"Plugin_215_enable_cal_dry\").checked = false;document.getElementById(\"Plugin_215_enable_cal_single\").checked = false;;document.getElementById(\"Plugin_215_enable_cal_L\").checked = false;};</script>"));
addFormNumericBox(F("Ref EC"),F("Plugin_215_ref_cal_H"), Settings.TaskDevicePluginConfig[event->TaskIndex][4]);
addUnit("µS");
status = _P215_send_I2C_command(Settings.TaskDevicePluginConfig[event->TaskIndex][0], "Cal,?",sensordata);
if (status){
switch (sensordata[5]) {
case '0' :
addFormNote(F("<span style='color:red'>Calibration needed</span>"));
break;
case '1' :
addFormNote(F("<span style='color:green'>Single point calibration ok</span>"));
break;
case '2' :
addFormNote(F("<span style='color:green'>Two points calibration ok</span>"));
break;
}
}
addFormSubHeader(F("Temperature compensation"));
char deviceTemperatureTemplate[40];
LoadCustomTaskSettings(event->TaskIndex, (byte*)&deviceTemperatureTemplate, sizeof(deviceTemperatureTemplate));
addFormTextBox(F("Temperature "), F("Plugin_215_temperature_template"), deviceTemperatureTemplate, sizeof(deviceTemperatureTemplate));
addFormNote(F("You can use a formulae (and idealy refer to a temp sensor). "));
float value;
char strValue[5];
addHtml(F("<div class='note'>"));
if (Calculate(deviceTemperatureTemplate,&value) == CALCULATE_OK ){
addHtml(F("Actual value : "));
dtostrf(value,5,2,strValue);
addHtml(strValue);
} else {
addHtml(F("(It seems I can't parse your formulae)"));
}
addHtml(F("</div>"));
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = getFormItemInt(F("Plugin_215_i2c"));
Settings.TaskDevicePluginConfigFloat[event->TaskIndex][0] = getFormItemFloat(F("Plugin_215_sensorVersion"));
char sensordata[32];
if (isFormItemChecked(F("Plugin_215_status_led"))) {
_P215_send_I2C_command(Settings.TaskDevicePluginConfig[event->TaskIndex][0],"L,1",sensordata);
} else {
_P215_send_I2C_command(Settings.TaskDevicePluginConfig[event->TaskIndex][0],"L,0",sensordata);
}
Settings.TaskDevicePluginConfig[event->TaskIndex][1] = isFormItemChecked(F("Plugin_215_status_led"));
Settings.TaskDevicePluginConfig[event->TaskIndex][2] = getFormItemInt(F("Plugin_215_ref_cal_single"));
Settings.TaskDevicePluginConfig[event->TaskIndex][3] = getFormItemInt(F("Plugin_215_ref_cal_L"));
Settings.TaskDevicePluginConfig[event->TaskIndex][4] = getFormItemInt(F("Plugin_215_ref_cal_H"));
String cmd ("Cal,");
bool triggerCalibrate = false;
if (isFormItemChecked("Plugin_215_enable_cal_dry")) {
cmd += "dry";
triggerCalibrate = true;
} else if (isFormItemChecked("Plugin_215_enable_cal_single")){
cmd += Settings.TaskDevicePluginConfig[event->TaskIndex][2];
triggerCalibrate = true;
} else if (isFormItemChecked("Plugin_215_enable_cal_L")){
cmd += "low,";
cmd += Settings.TaskDevicePluginConfig[event->TaskIndex][3];
triggerCalibrate = true;
} else if (isFormItemChecked("Plugin_215_enable_cal_H")){
cmd += "high,";
cmd += Settings.TaskDevicePluginConfig[event->TaskIndex][4];
triggerCalibrate = true;
}
if (triggerCalibrate){
char sensordata[32];
_P215_send_I2C_command(Settings.TaskDevicePluginConfig[event->TaskIndex][0],cmd.c_str(),sensordata);
}
char deviceTemperatureTemplate[40];
String tmpString = WebServer.arg(F("Plugin_215_temperature_template"));
strncpy(deviceTemperatureTemplate, tmpString.c_str(), sizeof(deviceTemperatureTemplate)-1);
deviceTemperatureTemplate[sizeof(deviceTemperatureTemplate)-1]=0; //be sure that our string ends with a \0
SaveCustomTaskSettings(event->TaskIndex, (byte*)&deviceTemperatureTemplate, sizeof(deviceTemperatureTemplate));
Plugin_215_init = false;
success = true;
break;
}
case PLUGIN_INIT:
{
Plugin_215_init = true;
}
case PLUGIN_READ:
{
char sensordata[32];
bool status;
//first set the temperature of reading
char deviceTemperatureTemplate[40];
LoadCustomTaskSettings(event->TaskIndex, (byte*)&deviceTemperatureTemplate, sizeof(deviceTemperatureTemplate));
String setTemperature("T,");
float temperatureReading;
if (Calculate(deviceTemperatureTemplate,&temperatureReading) == CALCULATE_OK ){
setTemperature += temperatureReading;
} else {
success = false;
break;
}
status = _P215_send_I2C_command(Settings.TaskDevicePluginConfig[event->TaskIndex][0],setTemperature.c_str(),sensordata);
//ok, now we can read the EC value
status = _P215_send_I2C_command(Settings.TaskDevicePluginConfig[event->TaskIndex][0],"r",sensordata);
if (status){
String sensorString(sensordata);
UserVar[event->BaseVarIndex] = sensorString.toFloat();
}
else {
UserVar[event->BaseVarIndex] = -1;
}
//go to sleep
//status = _P215_send_I2C_command(Settings.TaskDevicePluginConfig[event->TaskIndex][0],"Sleep",sensordata);
success = true;
break;
}
case PLUGIN_WRITE:
{
//TODO : do something more usefull ...
String tmpString = string;
int argIndex = tmpString.indexOf(',');
if (argIndex)
tmpString = tmpString.substring(0, argIndex);
if (tmpString.equalsIgnoreCase(F("ATLASCMD")))
{
success = true;
argIndex = string.lastIndexOf(',');
tmpString = string.substring(argIndex + 1);
if (tmpString.equalsIgnoreCase(F("CalMid"))){
String log("Asking for Mid calibration ");
addLog(LOG_LEVEL_INFO, log);
}
else if (tmpString.equalsIgnoreCase(F("CalLow"))){
String log("Asking for Low calibration ");
addLog(LOG_LEVEL_INFO, log);
}
else if (tmpString.equalsIgnoreCase(F("CalHigh"))){
String log("Asking for High calibration ");
addLog(LOG_LEVEL_INFO, log);
}
}
break;
}
}
return success;
}
// Call this function with two char arrays, one containing the command
// The other containing an allocatted char array for answer
// Returns true on success, false otherwise
bool _P215_send_I2C_command(uint8_t I2Caddress,const char * cmd, char* sensordata) {
uint16_t sensor_bytes_received = 0;
byte error;
byte i2c_response_code = 0;
byte in_char = 0;
Serial.println(cmd);
Wire.beginTransmission(I2Caddress);
Wire.write(cmd);
error = Wire.endTransmission();
if (error != 0) {
return false;
}
//don't read answer if we want to go to sleep
if (strncmp(cmd,"Sleep",5) == 0) {
return true;
}
i2c_response_code = 254;
while (i2c_response_code == 254) { // in case the cammand takes longer to process, we keep looping here until we get a success or an error
if (
( (cmd[0] == 'r' || cmd[0] == 'R') && cmd[1] == '\0' )
||
( ( strncmp(cmd,"cal",3) || strncmp(cmd,"Cal",3) ) && !strncmp(cmd,"Cal,?",5) )
)
{
delay(600);
}
else {
delay(300);
}
Wire.requestFrom(I2Caddress, (uint8_t) 32); //call the circuit and request 32 bytes (this is more then we need).
i2c_response_code = Wire.read(); //read response code
while (Wire.available()) { //read response
in_char = Wire.read();
if (in_char == 0) { //if we receive a null caracter, we're done
while (Wire.available()) { //purge the data line if needed
Wire.read();
}
break; //exit the while loop.
}
else {
sensordata[sensor_bytes_received] = in_char; //load this byte into our array.
sensor_bytes_received++;
}
}
sensordata[sensor_bytes_received] = '\0';
switch (i2c_response_code) {
case 1:
Serial.print( F("< success, answer = "));
Serial.println(sensordata);
break;
case 2:
Serial.println( F("< command failed"));
return false;
case 254:
Serial.println( F("< command pending"));
break;
case 255:
Serial.println( F("< no data"));
return false;
}
}
Serial.println(sensordata);
return true;
}