-
Notifications
You must be signed in to change notification settings - Fork 0
/
GravityTDSAnalog.cpp
199 lines (173 loc) · 6.27 KB
/
GravityTDSAnalog.cpp
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
//==========MODIFICATION==========//
/*
This is a modification of TDS Sensor Meter suitable for ESP32 and the usage of external ADC
modification is mainly in the read Analog and removing analog input pin (just input analog voltage value)
*/
/***************************************************
DFRobot Gravity: Analog TDS Sensor/Meter
<https://www.dfrobot.com/wiki/index.php/Gravity:_Analog_TDS_Sensor_/_Meter_For_Arduino_SKU:_SEN0244>
***************************************************
This sample code shows how to read the tds value and calibrate it with the standard buffer solution.
707ppm(1413us/cm)@25^c standard buffer solution is recommended.
Created 2018-1-3
By Jason <[email protected]@dfrobot.com>
GNU Lesser General Public License.
See <http://www.gnu.org/licenses/> for details.
All above must be included in any redistribution.
****************************************************/
#include <EEPROM.h>
#include "GravityTDSAnalog.h"
#define EEPROM_write(address, p) {int i = 0; byte *pp = (byte*)&(p);for(; i < sizeof(p); i++) EEPROM.write(address+i, pp[i]);}
#define EEPROM_read(address, p) {int i = 0; byte *pp = (byte*)&(p);for(; i < sizeof(p); i++) pp[i]=EEPROM.read(address+i);}
GravityTDSAnalog::GravityTDSAnalog()
{
this->temperature = 25.0;
this->kValueAddress = 8;
this->kValue = 1.0;
this->voltage = 0.0;
}
GravityTDSAnalog::~GravityTDSAnalog(){}
void GravityTDSAnalog::setVoltage(float voltage){
this->voltage = voltage;
}
void GravityTDSAnalog::setTemperature(float temp){
this->temperature = temp;
}
void GravityTDSAnalog::setKvalueAddress(int address){
this->kValueAddress = address;
}
void GravityTDSAnalog::begin(){
readKValues();
}
float GravityTDSAnalog::getKvalue(){
return this -> kValue;
}
void GravityTDSAnalog::update(){
this->ecValue=(133.42*this->voltage*this->voltage*this->voltage - 255.86*this->voltage*this->voltage + 857.39*this->voltage)*this->kValue;
this->ecValue25 = this->ecValue / (1.0+0.02*(this->temperature-25.0)); //temperature compensation
this->tdsValue = ecValue25 * TdsFactor;
if(cmdSerialDataAvailable() > 0)
{
ecCalibration(cmdParse()); // if received serial cmd from the serial monitor, enter into the calibration mode
}
}
float GravityTDSAnalog::getTdsValue()
{
return tdsValue;
}
float GravityTDSAnalog::getEcValue()
{
return ecValue25;
}
void GravityTDSAnalog::readKValues()
{
EEPROM_read(this->kValueAddress, this->kValue);
if(EEPROM.read(this->kValueAddress)==0xFF && EEPROM.read(this->kValueAddress+1)==0xFF && EEPROM.read(this->kValueAddress+2)==0xFF && EEPROM.read(this->kValueAddress+3)==0xFF)
{
this->kValue = 1.0; // default value: K = 1.0
EEPROM_write(this->kValueAddress, this->kValue);
}
}
boolean GravityTDSAnalog::cmdSerialDataAvailable()
{
char cmdReceivedChar;
static unsigned long cmdReceivedTimeOut = millis();
while (Serial.available()>0)
{
if (millis() - cmdReceivedTimeOut > 500U)
{
cmdReceivedBufferIndex = 0;
memset(cmdReceivedBuffer,0,(ReceivedBufferLength+1));
}
cmdReceivedTimeOut = millis();
cmdReceivedChar = Serial.read();
if (cmdReceivedChar == '\n' || cmdReceivedBufferIndex==ReceivedBufferLength){
cmdReceivedBufferIndex = 0;
strupr(cmdReceivedBuffer);
return true;
}else{
cmdReceivedBuffer[cmdReceivedBufferIndex] = cmdReceivedChar;
cmdReceivedBufferIndex++;
}
}
return false;
}
byte GravityTDSAnalog::cmdParse()
{
byte modeIndex = 0;
if(strstr(cmdReceivedBuffer, "ENTER") != NULL)
modeIndex = 1;
else if(strstr(cmdReceivedBuffer, "EXIT") != NULL)
modeIndex = 3;
else if(strstr(cmdReceivedBuffer, "CAL:") != NULL)
modeIndex = 2;
return modeIndex;
}
void GravityTDSAnalog::ecCalibration(byte mode)
{
char *cmdReceivedBufferPtr;
static boolean ecCalibrationFinish = 0;
static boolean enterCalibrationFlag = 0;
float KValueTemp,rawECsolution;
switch(mode)
{
case 0:
if(enterCalibrationFlag)
Serial.println(F("Command Error"));
break;
case 1:
enterCalibrationFlag = 1;
ecCalibrationFinish = 0;
Serial.println();
Serial.println(F(">>>Enter Calibration Mode<<<"));
Serial.println(F(">>>Please put the probe into the standard buffer solution<<<"));
Serial.println();
break;
case 2:
cmdReceivedBufferPtr=strstr(cmdReceivedBuffer, "CAL:");
cmdReceivedBufferPtr+=strlen("CAL:");
rawECsolution = strtod(cmdReceivedBufferPtr,NULL)/(float)(TdsFactor);
rawECsolution = rawECsolution*(1.0+0.02*(temperature-25.0));
if(enterCalibrationFlag)
{
// Serial.print("rawECsolution:");
// Serial.print(rawECsolution);
// Serial.print(" ecvalue:");
// Serial.println(ecValue);
KValueTemp = rawECsolution/(133.42*voltage*voltage*voltage - 255.86*voltage*voltage + 857.39*voltage); //calibrate in the buffer solution, such as 707ppm(1413us/cm)@25^c
if((rawECsolution>0) && (rawECsolution<2000) && (KValueTemp>0.25) && (KValueTemp<4.0))
{
Serial.println();
Serial.print(F(">>>Confrim Successful,K:"));
Serial.print(KValueTemp);
Serial.println(F(", Send EXIT to Save and Exit<<<"));
kValue = KValueTemp;
ecCalibrationFinish = 1;
}
else{
Serial.println();
Serial.println(F(">>>Confirm Failed,Try Again<<<"));
Serial.println();
ecCalibrationFinish = 0;
}
}
break;
case 3:
if(enterCalibrationFlag)
{
Serial.println();
if(ecCalibrationFinish)
{
EEPROM_write(kValueAddress, kValue);
EEPROM.commit();
Serial.print(F(">>>Calibration Successful,K Value Saved"));
}
else Serial.print(F(">>>Calibration Failed"));
Serial.println(F(",Exit Calibration Mode<<<"));
Serial.println();
ecCalibrationFinish = 0;
enterCalibrationFlag = 0;
}
break;
}
}