forked from letscontrolit/ESPEasyPluginPlayground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_P214_Atlas_EZO_pH.ino
373 lines (312 loc) · 14.1 KB
/
_P214_Atlas_EZO_pH.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
365
366
367
368
369
370
371
372
373
//########################################################################
//################## Plugin 214 : Atlas Scientific EZO Ph sensor ########
//########################################################################
// datasheet at https://www.atlas-scientific.com/_files/_datasheets/_circuit/pH_EZO_datasheet.pdf
// works only in i2c mode
#define PLUGIN_214
#define PLUGIN_ID_214 214
#define PLUGIN_NAME_214 "Environment - Atlas Scientific pH EZO"
#define PLUGIN_VALUENAME1_214 "pH"
boolean Plugin_214_init = false;
boolean Plugin_214(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_214;
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_214);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_214));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
#define _P214_ATLASEZO_I2C_NB_OPTIONS 4
byte I2Cchoice = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
int optionValues[_P214_ATLASEZO_I2C_NB_OPTIONS] = { 0x63, 0x64, 0x65, 0x66 };
addFormSelectorI2C(F("plugin_214_i2c"), _P214_ATLASEZO_I2C_NB_OPTIONS, optionValues, I2Cchoice);
addFormSubHeader(F("General"));
char sensordata[32];
bool status;
status = _P214_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) != "pH"){
addHtml(F("<span style='color:red'> WARNING : Board type should be 'pH', check your i2c Address ? </span>"));
}
addHtml(F("</TD></TR><TR><TD>Board version :</TD><TD>"));
addHtml(boardInfo.substring(pos2+1));
addHtml(F("</TD></TR>"));
addHtml(F("<input type='hidden' name='plugin_214_sensorVersion' value='"));
addHtml(boardInfo.substring(pos2+1));
addHtml(F("'>"));
} else {
addHtml(F("<span style='color:red;'>Unable to send command to device</span>"));
success = false;
break;
}
addFormCheckBox(F("Status LED"),F("Plugin_214_status_led"), Settings.TaskDevicePluginConfig[event->TaskIndex][1]);
addFormSubHeader(F("Calibration"));
int nb_calibration_points = -1;
status = _P214_send_I2C_command(Settings.TaskDevicePluginConfig[event->TaskIndex][0], "Cal,?",sensordata);
if (status){
if (strncmp(sensordata,"?Cal,",5)){
char tmp[2];
tmp[0] = sensordata[5];
tmp[1] = '\0',
nb_calibration_points = atoi(tmp);
}
}
addRowLabel(F("<strong>Middle</strong>"));
addFormNumericBox(F("Ref Ph"),F("Plugin_214_ref_cal_M' step='0.01"),Settings.TaskDevicePluginConfigFloat[event->TaskIndex][1],1,14);
if (nb_calibration_points > 0) {
addHtml(F(" <span style='color:green;'>OK</span>"));
} else {
addHtml(F(" <span style='color:red;'>Not yet calibrated</span>"));
}
addFormCheckBox(F("Enable"),F("Plugin_214_enable_cal_M"), false);
addHtml(F("\n<script type='text/javascript'>document.getElementById(\"Plugin_214_enable_cal_M\").onclick = function(){document.getElementById(\"Plugin_214_enable_cal_L\").checked = false;document.getElementById(\"Plugin_214_enable_cal_H\").checked = false;};</script>\n"));
addRowLabel(F("<strong>Low</strong>"));
addFormNumericBox(F("Ref Ph"),F("Plugin_214_ref_cal_L' step='0.01"), Settings.TaskDevicePluginConfigFloat[event->TaskIndex][2],1,14);
if (nb_calibration_points > 1) {
addHtml(F(" <span style='color:green;'>OK</span>"));
} else {
addHtml(F(" <span style='color:red;'>Not yet calibrated</span>"));
}
addFormCheckBox(F("Enable"),F("Plugin_214_enable_cal_L"), false);
addHtml(F("\n<script type='text/javascript'>document.getElementById(\"Plugin_214_enable_cal_L\").onclick = function(){document.getElementById(\"Plugin_214_enable_cal_M\").checked = false;document.getElementById(\"Plugin_214_enable_cal_H\").checked = false;};</script>\n"));
addHtml(F("<TR><TD><strong>High</strong></TD>"));
addFormNumericBox(F("Ref Ph"),F("Plugin_214_ref_cal_H' step='0.01"), Settings.TaskDevicePluginConfigFloat[event->TaskIndex][3],1,14);
if (nb_calibration_points > 2) {
addHtml(F(" <span style='color:green;'>OK</span>"));
} else {
addHtml(F(" <span style='color:orange;'>Not yet calibrated</span>"));
}
addFormCheckBox(F("Enable"),F("Plugin_214_enable_cal_H"), false);
addHtml(F("\n<script type='text/javascript'>document.getElementById(\"Plugin_214_enable_cal_H\").onclick = function(){document.getElementById(\"Plugin_214_enable_cal_L\").checked = false;document.getElementById(\"Plugin_214_enable_cal_M\").checked = false;};</script>\n"));
if (nb_calibration_points > 1){
char sensordata[32];
char cmd[8] = "Slope,?";
bool status;
status = _P214_send_I2C_command(Settings.TaskDevicePluginConfig[event->TaskIndex][0],cmd,sensordata);
if (status){
String slopeAnswer("Answer to 'Slope' command : ");
slopeAnswer += sensordata;
addFormNote(slopeAnswer);
}
}
addFormSubHeader(F("Temperature compensation"));
char deviceTemperatureTemplate[40];
LoadCustomTaskSettings(event->TaskIndex, (byte*)&deviceTemperatureTemplate, sizeof(deviceTemperatureTemplate));
addFormTextBox(F("Temperature "), F("Plugin_214_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_214_i2c"));
Settings.TaskDevicePluginConfigFloat[event->TaskIndex][0] = getFormItemFloat(F("plugin_214_sensorVersion"));
char sensordata[32];
if (isFormItemChecked(F("Plugin_214_status_led"))) {
_P214_send_I2C_command(Settings.TaskDevicePluginConfig[event->TaskIndex][0],"L,1",sensordata);
} else {
_P214_send_I2C_command(Settings.TaskDevicePluginConfig[event->TaskIndex][0],"L,0",sensordata);
}
Settings.TaskDevicePluginConfig[event->TaskIndex][1] = isFormItemChecked(F("Plugin_214_status_led"));
Settings.TaskDevicePluginConfigFloat[event->TaskIndex][1] = getFormItemFloat(F("Plugin_214_ref_cal_M"));
Settings.TaskDevicePluginConfigFloat[event->TaskIndex][2] = getFormItemFloat(F("Plugin_214_ref_cal_L"));
Settings.TaskDevicePluginConfigFloat[event->TaskIndex][3] = getFormItemFloat(F("Plugin_214_ref_cal_H"));
String cmd ("Cal,");
bool triggerCalibrate = false;
if (isFormItemChecked("Plugin_214_enable_cal_M")) {
cmd += "mid,";
cmd += Settings.TaskDevicePluginConfigFloat[event->TaskIndex][1];
triggerCalibrate = true;
} else if (isFormItemChecked("Plugin_214_enable_cal_L")){
cmd += "low,";
cmd += Settings.TaskDevicePluginConfigFloat[event->TaskIndex][2];
triggerCalibrate = true;
} else if (isFormItemChecked("Plugin_214_enable_cal_H")){
cmd += "high,";
cmd += Settings.TaskDevicePluginConfigFloat[event->TaskIndex][3];
triggerCalibrate = true;
}
if (triggerCalibrate){
char sensordata[32];
_P214_send_I2C_command(Settings.TaskDevicePluginConfig[event->TaskIndex][0],cmd.c_str(),sensordata);
}
char deviceTemperatureTemplate[40];
String tmpString = WebServer.arg(F("Plugin_214_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_214_init = false;
success = true;
break;
}
case PLUGIN_INIT:
{
Plugin_214_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 = _P214_send_I2C_command(Settings.TaskDevicePluginConfig[event->TaskIndex][0],setTemperature.c_str(),sensordata);
//ok, now we can read the pH value
status = _P214_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 = _P214_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 _P214_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(900);
}
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;
}