forked from letscontrolit/ESPEasyPluginPlayground
/
_P222_Atlas_EZO_ORP.ino
297 lines (243 loc) · 9.49 KB
/
_P222_Atlas_EZO_ORP.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
//########################################################################
//################## Plugin 222 : Atlas Scientific EZO ORP sensor ########
//########################################################################
// datasheet at https://www.atlas-scientific.com/_files/_datasheets/_circuit/ORP_EZO_datasheet.pdf
// works only in i2c mode
#define PLUGIN_222
#define PLUGIN_ID_222 222
#define PLUGIN_NAME_222 "Environment - Atlas Scientific ORP EZO [TESTING]"
#define PLUGIN_VALUENAME1_222 "ORP"
boolean Plugin_222_init = false;
boolean Plugin_222(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_222;
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_222);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_222));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
#define _P222_ATLASEZO_I2C_NB_OPTIONS 4
byte I2Cchoice = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
int optionValues[_P222_ATLASEZO_I2C_NB_OPTIONS] = { 0x62, 0x63, 0x64, 0x65 };
addFormSelectorI2C(F("plugin_222_i2c"), _P222_ATLASEZO_I2C_NB_OPTIONS, optionValues, I2Cchoice);
addFormSubHeader(F("General"));
char sensordata[32];
bool status;
status = _P222_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) != "ORP"){
addHtml(F("<span style='color:red'> WARNING : Board type should be 'ORP', 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_222_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_222_status_led"), Settings.TaskDevicePluginConfig[event->TaskIndex][1]);
addFormSubHeader(F("Calibration"));
int nb_calibration_points = -1;
status = _P222_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>ORP Calibration</strong>"));
addFormNumericBox(F("Ref ORP"),F("Plugin_222_ref_cal_M' step='1"),Settings.TaskDevicePluginConfigFloat[event->TaskIndex][1],0,1500);
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_222_enable_cal_M"), false);
if (nb_calibration_points > 1){
char sensordata[32];
char cmd[8] = "Slope,?";
bool status;
status = _P222_send_I2C_command(Settings.TaskDevicePluginConfig[event->TaskIndex][0],cmd,sensordata);
if (status){
String slopeAnswer("Answer to 'Slope' command : ");
slopeAnswer += sensordata;
addFormNote(slopeAnswer);
}
}
addHtml(F("</div>"));
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = getFormItemInt(F("plugin_222_i2c"));
Settings.TaskDevicePluginConfigFloat[event->TaskIndex][0] = getFormItemFloat(F("plugin_222_sensorVersion"));
char sensordata[32];
if (isFormItemChecked(F("Plugin_222_status_led"))) {
_P222_send_I2C_command(Settings.TaskDevicePluginConfig[event->TaskIndex][0],"L,1",sensordata);
} else {
_P222_send_I2C_command(Settings.TaskDevicePluginConfig[event->TaskIndex][0],"L,0",sensordata);
}
Settings.TaskDevicePluginConfig[event->TaskIndex][1] = isFormItemChecked(F("Plugin_222_status_led"));
Settings.TaskDevicePluginConfigFloat[event->TaskIndex][1] = getFormItemFloat(F("Plugin_222_ref_cal_M"));
String cmd ("Cal,");
bool triggerCalibrate = false;
if (isFormItemChecked("Plugin_222_enable_cal_M")) {
cmd += Settings.TaskDevicePluginConfigFloat[event->TaskIndex][1];
triggerCalibrate = true;
}
if (triggerCalibrate){
char sensordata[32];
_P222_send_I2C_command(Settings.TaskDevicePluginConfig[event->TaskIndex][0],cmd.c_str(),sensordata);
}
Plugin_222_init = false;
success = true;
break;
}
case PLUGIN_INIT:
{
Plugin_222_init = true;
}
case PLUGIN_READ:
{
char sensordata[32];
bool status;
//ok, now we can read the ORP value
status = _P222_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 = _P222_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 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 _P222_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) {
Serial.println(error);
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;
}