#define SIM_PIN 7
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(A0);
DallasTemperature DS18B20(&oneWire);
DeviceAddress DEVICEADDRESS;
#include <EEPROM.h>
#include "DFRobot_PH_by_Murri.h"
#define PH_PIN A1
DFRobot_PH PH;
#include "DFRobot_EC_by_Murri.h"
#define EC_PIN A2
DFRobot_EC EC;
#include "DFRobot_DO_by_Murri.h"
#define DO_PIN A3
DFRobot_DO DO;
#include "GravityTDS_by_Murri.h"
#define TDS_PIN A4
GravityTDS TDS;
#include "DTH_Turbidity.h"
#define TB_PIN A5
DTH_Turbidity turbSensor(TB_PIN);
float temperature = 25.0;
void setup()
{
Serial.begin(115200);
Serial.println(" ");
DS18B20.begin();
DS18B20.setWaitForConversion(false);
DS18B20.getAddress(DEVICEADDRESS, 0); // get deviceaddress for index 0;
//DS18B20.setResolution(DEVICEADDRESS, 9); // set Resolution to 9, 10, 11, 12 bits
PH.begin();
EC.begin();
DO.begin();
TDS.setPin(TDS_PIN);
TDS.begin();
pinMode(SIM_PIN, OUTPUT);
}
void loop()
{
static bool init = true;
static unsigned long reading = millis();
if (init) {
init = false;
temperature = 25.0;
DS18B20.requestTemperaturesByIndex(0); // request Temperature for the first reading
}
if (millis() - reading > 1000) {
reading = millis();
temperature = DS18B20.getTempCByIndex(0); // get Temperature without WaitForConversion after 1000ms
DS18B20.requestTemperaturesByIndex(0); // request Temperature for the next reading
}
if (calibrate()) {
digitalWrite(SIM_PIN, LOW);
return;
}
digitalWrite(SIM_PIN, bitRead(millis(), 8));
}
byte calibrate() {
/*
terminate
read ph eeprom
erase ph eeprom
read ec eeprom
erase ec eeprom
read do eeprom
erase do eeprom
read tds eeprom
erase tds eeprom
tsensor -> read temperature from sensor
tinput:val -> set temperature to val
PH Calibration:
enterph -> enter the calibration mode
calph -> calibrate with the standard buffer solution, two buffer solutions(4.0 and 7.0) will be automaticlly recognized
exitph -> save the calibrated parameters and exit from calibration mode
EC Calibration:
enterec -> enter the PH calibration mode
calec -> calibrate with the standard buffer solution, two buffer solutions(1413us/cm and 12.88ms/cm)
exitec -> save the calibrated parameters and exit from calibration mode
TDS Calibration:
enter -> enter the calibration mode
cal:tds value -> calibrate with the known tds value(25^c). e.g.cal:707
exit -> save the parameters and exit the calibration mode
DO Calibration:
enterdo -> enter the PH calibration mode
cal1do: -> voltage & temperature (read from sensors, high temperature point)
cal2do: -> voltage & temoperture (read from sensors, low temperature point)
exitdo -> save the calibrated parameters and exit from calibration mode
cal1_v:voltage[mv] -> high temperature point
cal1_t:temperature -> high temperature point
cal2_v:voltage(mv) -> low temperature point
cal2_t:temperature -> low temperature point
TB Calibration: (turbidity)
entertb -> show values only, no calibration !!!
*/
float tsensor = temperature;
static float voltage, temperature;
static int analog;
float value;
static char buff[100], strf1[20], strf2[20], strf3[20], strf4[20], cmd[20];
unsigned long timer = millis();
static unsigned long reading = timer;
static unsigned long timeout = timer;
static unsigned long counter = timer;
int ADDRESS, BYTES;
static byte calibrate = 0;
static byte tempinput = 0;
if (tempinput == 0) temperature = tsensor;
if (timer >= timeout) calibrate = 0;
if (timer - reading > 1000) { // time interval: 1s
reading = timer;
counter = (timeout - timer) / 1000;
switch (calibrate) {
case 0:
tempinput = 0;
break;
case 1:
analog = analogRead(PH_PIN);
voltage = analog / 1024.0 * 5000;
value = PH.readPH(voltage, temperature);
dtostrf(value, 0, 2, strf1);
dtostrf(analog, 0, 0, strf2);
dtostrf(voltage, 0, 2, strf3);
dtostrf(temperature, 0, 2, strf4);
snprintf(buff, 100, "pH:%s\t analog:%s\t voltage:%s[mv]\t temperature:%s[*C]\t timeout in %lu[sec]", strf1, strf2, strf3, strf4, counter);
Serial.println(buff);
break;
case 2:
analog = analogRead(EC_PIN);
voltage = analog / 1024.0 * 5000;
value = EC.readEC(voltage, temperature);
dtostrf(value, 0, 2, strf1);
dtostrf(analog, 0, 0, strf2);
dtostrf(voltage, 0, 2, strf3);
dtostrf(temperature, 0, 2, strf4);
snprintf(buff, 100, "EC:%s[ms/cm]\t analog:%s\t voltage:%s[mv]\t temperature:%s[*C]\t timeout in %lu[sec]", strf1, strf2, strf3, strf4, counter);
Serial.println(buff);
break;
case 3:
analog = analogRead(DO_PIN);
voltage = analog / 1024.0 * 5000;
value = DO.readDO(voltage, temperature);
dtostrf(value, 0, 2, strf1);
dtostrf(analog, 0, 0, strf2);
dtostrf(voltage, 0, 2, strf3);
dtostrf(temperature, 0, 2, strf4);
snprintf(buff, 100, "DO:%s[mg/L]\t analog:%s\t voltage:%s[mv]\t temperature:%s[*C]\t timeout in %lu[sec]", strf1, strf2, strf3, strf4, counter);
Serial.println(buff);
break;
case 4:
analog = analogRead(TDS_PIN);
voltage = analog / 1024.0 * 5000;
value = TDS.readTDS(voltage, temperature);
dtostrf(value, 0, 2, strf1);
dtostrf(analog, 0, 0, strf2);
dtostrf(voltage, 0, 2, strf3);
dtostrf(temperature, 0, 2, strf4);
snprintf(buff, 100, "TDS:%s[ppm]\t analog:%s\t voltage:%s[mv]\t temperature:%s[*C]\t timeout in %lu[sec]", strf1, strf2, strf3, strf4, counter);
Serial.println(buff);
break;
case 5:
analog = analogRead(TB_PIN);
voltage = analog / 1024.0 * 5000;
value = (-1120.4 * voltage / 1e3 * voltage / 1e3) + (5742.3 * voltage / 1e3) - 4352.9;
if (voltage < 2500.0) value = 3000;
if (value < 0) value = 0;
dtostrf(value, 0, 2, strf1);
dtostrf(analog, 0, 0, strf2);
dtostrf(voltage, 0, 2, strf3);
dtostrf(temperature, 0, 2, strf4);
snprintf(buff, 100, "TB:%s[NTU]\t analog:%s\t voltage:%s[mv]\t temperature:%s[*C]\t timeout in %lu[sec]", strf1, strf2, strf3, strf4, counter);
Serial.println(buff);
break;
}
}
if (readSerial(cmd)) {
timeout = timer + 180000;
for (int i = 0; cmd[i] != '\0'; i++) {
cmd[i] = toupper((unsigned char)cmd[i]); // convert cmd input to uppercase
}
if (!strcmp(cmd, "TERMINATE")) calibrate = 0;
if (!strcmp(cmd, "ENTERPH")) calibrate = 1;
if (!strcmp(cmd, "ENTEREC")) calibrate = 2;
if (!strcmp(cmd, "ENTERDO")) calibrate = 3;
if (!strcmp(cmd, "ENTER" )) calibrate = 4;
if (!strcmp(cmd, "ENTERTB")) calibrate = 5;
if (!strcmp(cmd, "TSENSOR")) goto TINPUT_0;
if (!memcmp(cmd, "TINPUT:", 7)) goto TINPUT_1;
ADDRESS = 0x00, BYTES = 8;
if (!strcmp(cmd, "READ PH EEPROM")) goto EEPROM_R;
if (!strcmp(cmd, "ERASE PH EEPROM")) goto EEPROM_W;
ADDRESS = 0x0A, BYTES = 8;
if (!strcmp(cmd, "READ EC EEPROM")) goto EEPROM_R;
if (!strcmp(cmd, "ERASE EC EEPROM")) goto EEPROM_W;
ADDRESS = 0x14, BYTES = 4;
if (!strcmp(cmd, "READ TDS EEPROM")) goto EEPROM_R;
if (!strcmp(cmd, "ERASE TDS EEPROM")) goto EEPROM_W;
ADDRESS = 0x1A, BYTES = 6;
if (!strcmp(cmd, "READ DO EEPROM")) goto EEPROM_R;
if (!strcmp(cmd, "ERASE DO EEPROM")) goto EEPROM_W;
switch (calibrate) {
case 1:
PH.calibration(voltage, temperature, cmd);
break;
case 2:
EC.calibration(voltage, temperature, cmd);
break;
case 3:
DO.calibration(voltage, temperature, cmd);
break;
case 4:
TDS.calibration(voltage, temperature, cmd);
break;
case 5:
// no calibration for turbidity sensor
break;
}
}
return calibrate;
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
TINPUT_0:
if (calibrate) {
tempinput = 0;
Serial.println(F("read temperature from sensor"));
}
return calibrate;
TINPUT_1:
if (calibrate) {
tempinput = 1;
temperature = atof(cmd + 7);
Serial.print(F("set temperature to : "));
Serial.println(temperature);
}
return calibrate;
EEPROM_W:
for (int i = ADDRESS; i < ADDRESS + BYTES; i++) {
EEPROM.write(0 + i, 0xFF);
delay(10);
}
EEPROM_R:
Serial.println();
for (int i = ADDRESS; i < ADDRESS + BYTES; i++) {
Serial.print(EEPROM.read(i), HEX); Serial.print("\t");
delay(10);
}
Serial.println();
float FLOAT;
uint8_t UINT8_T;
uint16_t UINT16_T;
switch (ADDRESS) {
case 0x00:
Serial.println(EEPROM.get(ADDRESS + 0, FLOAT));
Serial.println(EEPROM.get(ADDRESS + 4, FLOAT));
break;
case 0x0A:
Serial.println(EEPROM.get(ADDRESS + 0, FLOAT));
Serial.println(EEPROM.get(ADDRESS + 4, FLOAT));
break;
case 0x14:
Serial.println(EEPROM.get(ADDRESS + 0, FLOAT));
break;
case 0x1A:
Serial.println(EEPROM.get(ADDRESS + 0, UINT16_T));
Serial.println(EEPROM.get(ADDRESS + 2, UINT8_T));
Serial.println(EEPROM.get(ADDRESS + 3, UINT16_T));
Serial.println(EEPROM.get(ADDRESS + 5, UINT8_T));
break;
}
return calibrate;
}
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;
}
PH
TDS
EC
DO
TB