DFRobot PH and EC to one ECP32

Home Assistant automation projects, questions, etc. go here.
Post Reply
Baylos
LED-Curious
LED-Curious
Reactions:
Posts: 22
Joined: Sat Nov 07, 2020 11:49 am

Hello,

I was searching for a sketch for DFRobot PN and EC probe to use them on one ESP32 and ADC. As I could not find a sketch, I start again (I lost the previous sketch as my PC crashed).

So far, I have combined the PH and EC sketches into one, and initial testing looks ok.

Where I am not sure is the EEPROM. May it works with EC and PH or not.

Code: Select all

#include "DFRobot_ESP_PH_WITH_ADC.h"
#include "OneWire.h"
#include "DallasTemperature.h"
#include "Adafruit_ADS1X15.h"
#include "EEPROM.h"
#include "Arduino.h"
#include "DFRobot_ESP_EC.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;

float voltagePH, voltageEC, phValue, ecValue, temperature = 25;

float readTemperature()
{
  //add your code here to get the temperature from your temperature sensor
  sensors.requestTemperatures();
  return sensors.getTempCByIndex(0);
}

void setup()
{
  Serial.begin(115200);
  EEPROM.begin(32);//needed EEPROM.begin to store calibration k in eeprom
  ec.begin(30);//by default lib store calibration k since 10 change it by set ec.begin(30); to start from 30
  ph.begin();
  sensors.begin();
  ads.setGain(GAIN_ONE);
  ads.begin();
}

void loop()
{
  static unsigned long timepoint = millis();
  if (millis() - timepoint > 1000U) //time interval: 1s
  {
    timepoint = millis();
    /**
     * index 0 for adc's pin A0
     * index 1 for adc's pin A1
     * index 2 for adc's pin A2
     * index 3 for adc's pin A3
    */
    voltageEC = ads.readADC_SingleEnded(0) / 10;
    Serial.print("voltageEC:");
    Serial.println(voltageEC, 4);
    
    voltagePH = ads.readADC_SingleEnded(1) / 10; // read the voltage
    Serial.print("voltagePH:");
    Serial.println(voltagePH, 4);

    temperature = readTemperature(); // read your temperature sensor to execute temperature compensation
    Serial.print("temperature:");
    Serial.print(temperature, 1);
    Serial.println("^C");

    phValue = ph.readPH(voltagePH, temperature); // convert voltage to pH with temperature compensation
    Serial.print("pH:");
    Serial.println(phValue, 4);

    ecValue = ec.readEC(voltageEC, temperature); // convert voltage to EC with temperature compensation
    Serial.print("EC:");
    Serial.print(ecValue, 4);
    Serial.println("ms/cm");
  }
  ph.calibration(voltagePH, temperature); // calibration process by Serail CMD
  ec.calibration(voltageEC, temperature); // calibration process by Serail CMD
}
jaysal
LED Enthusiast
LED Enthusiast
Reactions:
Posts: 75
Joined: Sat May 30, 2020 7:49 am

I have not yet implemented their EC probe and I may make the switch to Atlas-Scientific but here is what I am using which includes the added code to send the data to an MQTT broker. I guess the best way is to test it out by calibrating both the PH and EC probes, then restarting the ESP32 then put the probes right back into the calibration solution, it should read the same values prior to the the ESP32 reboot. Let us know how it goes.

Code: Select all

#include <Arduino.h>
#include "DFRobot_ESP_PH_WITH_ADC.h"
#include "OneWire.h"
#include "DallasTemperature.h"
#include "Adafruit_ADS1015.h"
#include "EEPROM.h"
#include "WiFi.h"
#include <PubSubClient.h>

const char* ssid = "xxxx";
const char* password = "xxxx";
const char* mqttServer = "xxxx";
const int mqttPort = 1883;
const char* mqttUser = "xxxx";
const char* mqttPassword = "xxxx";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

//GPIO where the DS18B20 is connected to
#define ONE_WIRE_BUS 15

//Setup a onWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

//Password onewire to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);



DFRobot_ESP_PH_WITH_ADC ph;
Adafruit_ADS1115 ads;

float voltage, phValue, temperature = 25;
char tempString[8];
float readTemperature()
{
  //add your code here to get the temperature from your temperature sensor
  sensors.requestTemperatures();
  return sensors.getTempCByIndex(0);
}

void setup()
{
  Serial.begin(115200);
  EEPROM.begin(32);//needed EEPROM.begin to store calibration k in eeprom
  ph.begin();
  sensors.begin();
  ads.setGain(GAIN_ONE);
  ads.begin();
  
//Connect to WiFi
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
 
Serial.println("Connected to the WiFi network");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
 
client.setServer(mqttServer, mqttPort);
 
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
 
if (client.connect("ESP32Client", mqttUser, mqttPassword )) {
 
Serial.println("connected");
 
} else {
 
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);

    }
  }
}
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP32Client")) {
      Serial.println("connected");
      // Subscribe
      client.subscribe("esp32/output");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
void loop()
{
  static unsigned long timepoint = millis();
  if (millis() - timepoint > 1000U) //time interval: 1s
  {
    timepoint = millis();
    /**
     * index 0 for adc's pin A0
     * index 1 for adc's pin A1
     * index 2 for adc's pin A2
     * index 3 for adc's pin A3
    */
    voltage = ads.readADC_SingleEnded(1) / 10; // read the voltage
    Serial.print("voltage:");
    Serial.println(voltage, 4);

    temperature = readTemperature(); // read your temperature sensor to execute temperature compensation
    Serial.print("temperature:");
    Serial.print(temperature, 1);
    Serial.println("^C");

    phValue = ph.readPH(voltage, temperature); // convert voltage to pH with temperature compensation
    Serial.print("pH:");
    Serial.println(phValue, 4);
  }
  client.loop();
  ph.calibration(voltage, temperature); // calibration process by Serail CMD
  dtostrf(phValue, 5, 3, tempString);
  client.publish("esp32/esp32ph", tempString);
  delay(5000);
}
Baylos
LED-Curious
LED-Curious
Reactions:
Posts: 22
Joined: Sat Nov 07, 2020 11:49 am

Hi,

Thanks, jaysal.

It took a while, and not I got it working. EC and PH probe with one ESP32.

The problem occurred that the readings of the two probes interference with each other. So I had two analog signal isolators. But one was a defect.

With the Analog Signal Isolator and attached to the PH probe, I had the problem that I cannot calibrate it. It detects the ph solution of 4 as the solution to 7. Therefore ph probe direct contacted the ADC.

Finally, the Analog Signal Isolator and attached to the EC probe works.

Now the reading looks good.

(FYI, The DFRobot EC probe you should not submerge.)

Greetings
Post Reply