Last active
July 19, 2019 09:43
-
-
Save MkLHX/321b72c5a502aa0b7c3c64bbeb6e8ff0 to your computer and use it in GitHub Desktop.
Read Serial Monitor orders to launch EC or PH calibration process (Arduino IDE version)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "DFRobot_ESP_EC.h" //https://github.com/GreenPonik/DFRobot_ESP_EC_BY_GREENPONIK.git | |
#include "DFRobot_ESP_PH_WITH_ADC.h" //https://github.com/GreenPonik/DFRobot_ESP_PH_WITH_ADC_BY_GREENPONIK.git | |
#include "Adafruit_ADS1015.h" //https://github.com/GreenPonik/Adafruit_ADS1X15.git | |
#include "OneWire.h" | |
#include "DallasTemperature.h" | |
#include "EEPROM.h" | |
#define ONE_WIRE_BUS 15 | |
OneWire oneWire(ONE_WIRE_BUS); | |
DallasTemperature sensors(&oneWire); | |
DFRobot_ESP_PH_WITH_ADC ph; | |
DFRobot_ESP_EC ec; | |
Adafruit_ADS1115 ads; | |
unsigned long intervals[] = { | |
1000U, //0 | |
2000U, //1 | |
3000U, //2 | |
5000U, //3 | |
10000U, //4 | |
15000U, //5 | |
20000U, //6 | |
25000U, //7 | |
60000U, //8 | |
1800000U, //9 | |
};//this defines the interval for each task in milliseconds | |
unsigned long last[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | |
bool calibrationIsRunning = false; | |
float lastTemperature; | |
float getWaterTemperature() | |
{ | |
sensors.requestTemperatures(); // Send the command to get temperatures | |
float temperature = sensors.getTempCByIndex(0); | |
if (temperature == 85.00 || temperature == -127.00) //take the last correct temperature value if getting 85 or -127 value | |
{ | |
temperature = lastTemperature; | |
} | |
else | |
{ | |
lastTemperature = temperature; | |
} | |
Serial.println("[getWaterTemperature]... temperature : " + String(temperature)); | |
return temperature; | |
} | |
/** | |
* Read Serial to launch PH or EC listener | |
*/ | |
int i = 0; | |
bool readSerial(char result[]) | |
{ | |
while (Serial.available() > 0) | |
{ | |
char inChar = Serial.read(); | |
if (inChar == '\n') | |
{ | |
result[i] = '\0'; | |
Serial.flush(); | |
i = 0; | |
return true; | |
} | |
if (inChar != '\r') | |
{ | |
result[i] = inChar; | |
i++; | |
} | |
delay(1); | |
} | |
return false; | |
} | |
void setup() | |
{ | |
Serial.begin(115200); | |
EEPROM.begin(64); | |
ads.setGain(GAIN_ONE); | |
ads.begin(); | |
ph.begin(10);//we store 2 value from the start address (10 & 14) | |
ec.begin(20);//we store 2 value from the start address (20 & 24) | |
} | |
/** | |
* | |
* Process to enter in calibration mode. | |
* open arduino IDE serial monitor | |
* enter PH or EC in serial monitor field | |
* then enter ENTEREC for EC calibration or ENTERPH for PH calibration | |
* wait for EC or PH stabilization | |
* when stable enter CALEC or CALPH | |
* then to store calibration coefficient enter EXITEC or EXITPH | |
* | |
*/ | |
void loop() | |
{ | |
unsigned long now = millis(); | |
if (now - last[0] >= intervals[0]) //1000ms interval | |
{ | |
last[0] = now; | |
float ecVoltage, ecValue, phVoltage, phValue, temperature = 25; | |
if (calibrationIsRunning) | |
{ | |
Serial.println(F("[main]...>>>>>> calibration is running, to exit send exitph or exitec through serial <<<<<<")); | |
//water temperature | |
temperature = getWaterTemperature(); | |
//EC | |
ecVoltage = ads.readADC_SingleEnded(0) / 10; | |
Serial.print(F("[EC Voltage]... ecVoltage: ")); | |
Serial.println(ecVoltage); | |
ecValue = ec.readEC(ecVoltage, temperature); // convert voltage to EC with temperature compensation | |
Serial.print(F("[EC Read]... EC: ")); | |
Serial.print(ecValue); | |
Serial.println(F("ms/cm")); | |
//pH | |
phVoltage = ads.readADC_SingleEnded(1) / 10; | |
Serial.print(F("[pH Voltage]... phVoltage: ")); | |
Serial.println(phVoltage); | |
phValue = ph.readPH(phVoltage, temperature); | |
Serial.print(F("[pH Read]... pH: ")); | |
Serial.println(phValue); | |
} | |
char cmd[10]; | |
if (readSerial(cmd)) | |
{ | |
strupr(cmd); | |
if (calibrationIsRunning || strstr(cmd, "PH") || strstr(cmd, "EC")) | |
{ | |
calibrationIsRunning = true; | |
Serial.println(F("[]... >>>>>calibration is now running PH and EC are both reading, if you want to stop this process enter EXITPH or EXITEC in Serial Monitor<<<<<")); | |
if (strstr(cmd, "PH")) | |
{ | |
ph.calibration(phVoltage, temperature, cmd); //PH calibration process by Serail CMD | |
} | |
if (strstr(cmd, "EC")) | |
{ | |
ec.calibration(ecVoltage, temperature, cmd); //EC calibration process by Serail CMD | |
} | |
} | |
if (strstr(cmd, "EXITPH") || strstr(cmd, "EXITEC")) | |
{ | |
calibrationIsRunning = false; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment