Code for PH Sensor DFRobot

I have not implemented this, but calibration mode seems to be implemented as a service.

In other words it should show up as a service in home assistant.

Hello

how to enable calibration mode? I can’t find it in home assistant

In developer tools, services, do you see any services with the name starting esphome. ?

Hi guys,

I have a question for pH probe users from DFRobots or any other general Chinese probe. Do you see stable pH values with those probe?
I am trying to automate my hydroponic system, ad found unstable behaviour. The values drop in time in matters of days. Values are stable but they are dropping down. For example from 22-16 hours the probe is in calibration solution pH7 and drops 0.5 pH in that period.

I found out, that my old probe doesn’t measure anymore. pH4 or pH7 calibration solution, it shows 1.7V no matter what. Than I swap for a new probe and measurements start to give me some range to work with. Do you experience same behavior?

After some research, I found out, that those probe are not great for continuous measurement, so I ordered a bit different one with PTFE membrane, to test those out. But I am curious if somebody else notice it.

That could well be your nutrients @Pulcik , or the plant releasing certain acids into the reservoir. We notice that specifically during peak flowering. pH will adjust (slightly) with temperature too, but you can try just leaving it in straight water solution for ~24 hours as a baseline to ascertain the accuracy of your particular probe.

This was testing setup, just solution in a bucket. Those numbers are unnatural, as the water slowly turns itself to strong acid according to the probe. But in reality, it was just a water.
I found out, it was the probe, as I use non industrial one and not suited for continuous submerge. So I suspect, that the inner solution in probe start to shift its pH and thus affected the measurement.

I have this kit:

Unexpected maker feathers2 esp32.

I have this code:

esphome:
  includes:
    - custom_dfrobot_ph_sensor.h
  libraries:
    - eeprom
    - onewire
    - DallasTemperature
    - file:///config/esphome/custom/DFRobot_ESP_PH


  name: bassenginfo



esp32:
  board: um_feathers2
  framework:
    type: arduino

    

# Enable logging
logger:

api:



ota:
  password: "***********************************"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Bassenginfo Fallback Hotspot"
    password: "****************"

captive_portal:

sensor:
  - platform: custom
    lambda: |-
      auto my_sensor = new CustomDFRobotPhSensor();
      App.register_component(my_sensor);
      return {my_sensor};
  
    sensors:
      name: "DFRobot Ph Sensor"
      accuracy_decimals: 2
      unit_of_measurement: " "
    


I get this error:

hi! i managed to use a esp32 with adc and your custom component without issues, great work! was painless to setup and easy to use.

just a quick question, the calibration just works in the console via serial but the text is not pushed if the log is over wireless, is there a way to push it in the log also? or in the console of the webui of the esp32?

Hi! I tried your code and instructions, and I got the following error. I am using the DFRobot Ph Sensor v1.1 directly connected to pin 35 of my ESP32. Hopefully the SEN0161 model will work with the same code.
Here’s my config:

esphome:
  name: aquarium
  platform: ESP32
  board: esp32doit-devkit-v1
  includes:
    - custom_dfrobot_ph_sensor.h
  libraries:
    - eeprom
    - onewire
    - DallasTemperature
    - file:///config/esphome/custom/DFRobot_ESP_PH

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  fast_connect: true
  manual_ip:
    # Set this to the IP of the ESP
    static_ip: 192.168.2.150
    # Set this to the IP address of the router. Often ends with .1
    gateway: 192.168.2.1
    # The subnet of the network. 255.255.255.0 works for most home networks.
    subnet: 255.255.255.0
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Aquarium Fallback Hotspot"
    password: "qFLqMfXVUmEv"

captive_portal:

logger:

api:
ota:

# MQTT Home Assistant
#mqtt:
#  broker: 192.168.1.36
#  username: !secret mqtt_user
#  password: !secret mqtt_pass
#  discovery: true
#  discovery_retain: true

############################################
# Dallas Temperature Sensor configuration
dallas:
  - pin: 32
    update_interval: 20s
    id: dallas_bus_01
############################################
sensor:
# Dallas Temperature Sensor
  - platform: dallas
    dallas_id: dallas_bus_01
    address: 0x05011939EB518F28
    id: aquarium_temperature
    name: "Aquarium Temperature"
    accuracy_decimals: 1
# DFRobots Ph Sensor
  - platform: custom
    lambda: |-
      auto my_sensor = new CustomDFRobotPhSensor();
      App.register_component(my_sensor);
      return {my_sensor};
    sensors:
      name: "DFRobot Ph Sensor"
      accuracy_decimals: 2
      unit_of_measurement: " "

UPDATE: I discovered the fix to my “Please upgrade to the PlatformIO Core 6” error was to uninstall the ESPHome Add-on and reinstall it. Don’t worry… your configs will still be there when you reinstall even though it tells you that you’ll lose your ESPHome configuration.
BUT now I’m still running into this error:

Compiling /data/aquarium/.pioenvs/aquarium/src/main.cpp.o
/config/esphome/aquarium.yaml: In lambda function:
/config/esphome/aquarium.yaml:62:50: error: no matching function for call to 'CustomDFRobotPhSensor::CustomDFRobotPhSensor()'
       auto my_sensor = new CustomDFRobotPhSensor();
                                                  ^
In file included from src/main.cpp:27:
src/custom_dfrobot_ph_sensor.h:36:3: note: candidate: 'CustomDFRobotPhSensor::CustomDFRobotPhSensor(uint32_t)'
   CustomDFRobotPhSensor(uint32_t update_interval) : PollingComponent(update_interval) {}
   ^~~~~~~~~~~~~~~~~~~~~
src/custom_dfrobot_ph_sensor.h:36:3: note:   candidate expects 1 argument, 0 provided
src/custom_dfrobot_ph_sensor.h:33:7: note: candidate: 'CustomDFRobotPhSensor::CustomDFRobotPhSensor(const CustomDFRobotPhSensor&)'
 class CustomDFRobotPhSensor : public PollingComponent, public Sensor, public CustomAPIDevice {
       ^~~~~~~~~~~~~~~~~~~~~
src/custom_dfrobot_ph_sensor.h:33:7: note:   candidate expects 1 argument, 0 provided
src/custom_dfrobot_ph_sensor.h:33:7: note: candidate: 'CustomDFRobotPhSensor::CustomDFRobotPhSensor(CustomDFRobotPhSensor&&)'
src/custom_dfrobot_ph_sensor.h:33:7: note:   candidate expects 1 argument, 0 provided
/config/esphome/aquarium.yaml:64:24: error: could not convert '{my_sensor}' from '<brace-enclosed initializer list>' to 'std::vector<esphome::sensor::Sensor*>'
       return {my_sensor};
                        ^
*** [/data/aquarium/.pioenvs/aquarium/src/main.cpp.o] Error 1
========================= [FAILED] Took 282.74 seconds =========================
========== [ERROR] /config/esphome/aquarium.yaml ==========

Hello,

I’ve read all messages requesting helps and I have the same problem here.

My setup is the gravity kit v2 directly connected to pin 36 of an esp32, and no temperature sensor.

In this setup if should use:

And this is my code:

esphome:
  name: "phmeter2"
  friendly_name: "phmeter2"
  includes:
    - custom_dfrobot_ph_sensor.h
  libraries:
    - eeprom
    - onewire
    - DallasTemperature
    - file:///config/custom/DFRobot_ESP_PH

esp32:
  board: nodemcu-32s

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xxxx"

ota:
  password: "xxxxx"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "phMeter Fallback Hotspot"
    password: "xxxxxx"

captive_portal:

sensor:
  - platform: custom
    lambda: |-
      auto my_sensor = new CustomDFRobotPhSensor(30000);
      App.register_component(my_sensor);
      return {my_sensor->ph_sensor, my_sensor->temperature_sensor, my_sensor->voltage_sensor};

    sensors:
      - name: "pH Sensor"
        accuracy_decimals: 2
        unit_of_measurement: "pH"

      - name: "Water Temperature Sensor"
        accuracy_decimals: 2
        unit_of_measurement: "°C"

      - name: "pH Sensor Voltage"
        accuracy_decimals: 2
        unit_of_measurement: "v"

The error is similar to others:

hmeter-jeff.yaml: In lambda function:
phmeter-jeff.yaml:42:26: error: 'class CustomDFRobotPhSensor' has no member named 'ph_sensor'; did you mean 'Sensor'?
phmeter-jeff.yaml:42:48: error: 'class CustomDFRobotPhSensor' has no member named 'temperature_sensor'
phmeter-jeff.yaml:42:79: error: 'class CustomDFRobotPhSensor' has no member named 'voltage_sensor'
phmeter-jeff.yaml:42:93: error: could not convert '{<expression error>, <expression error>, <expression error>}' from '<brace-enclosed initializer list>' to 'std::vector<esphome::sensor::Sensor*>'

Is someone is runing well with a similar setup and could help us ?

Thanks.

Ok for my setup, the code in lambda is:

lambda: |-
      auto my_sensor = new CustomDFRobotPhSensor(30000);
      App.register_component(my_sensor);
      return {my_sensor};

The function CustomDFRobotPhSensor is returning just one value:

publish_state(phValue);

Now I’m trying to understand how to run the calibration process (enterph,calph,exitph).

Any help please ?

I now have an ads1115 that I could use for my setup. So I’ll modify to use the code with ADC.

But where could I find the wiring to connect the dfrobot card to ads1115 and to connect the ads1115 to esp32 please ?

ADS115 is I2C wiring. And the board has GND and 4 analogue inputs.

Yes I have the card in front of me. I’ve found a doc tha describes the wiring for SDA/SCL.

ADS 1115

SDA → Pin 21
SCL → Pin 22
GND → Pin GND
VCC → Pin 3V3
A0 → Pin data from the robotshop card

Robotshop card

Red → 3V3
Black → GND
Blue → A0 on ADS115 card

Hope it is right …

And for calibration, there are 3 services in home assitant. each one sends a commande “enterph”, “calph” and “exitph” to make the calibrating process.

eg: <yourdevicename>_enter_ph / <yourdevicename>_exit_ph / <yourdevicename>_calibrate_ph

Actually the solution used in the small caps of the sensor has a ph between 6.0 (not have calibrated yet). I don’t know if my wiring is good or not.

hey @nkgilley

Sorry to “ping you” but you might solve this way faster than I do :slight_smile:
I would like to get the temperature from a sensor that is already in HomeAssistant. How can I adapt your code to read from that sensor?

TIA

Hello;

Can’t seem to get your integration working properly.

I have the input on A3 on the adc, where do I define that?

Also I have the temp sensor on 15 where do I define that?

Thanks for the help.

EDIT:

I just moved my temp sensor onto 2 on the esp32 and moved the output of the PH sensor onto the A0 to test. I’m just not sure where they should go.

EDIT2: My problem is the temp sensor reads -127

Here’s my code:

esphome:
  name: krisproxy
  friendly_name: KrisProxy
  includes:
    - custom_dfrobot_ph_sensor_adc.h
  libraries:
    - eeprom
    - onewire
    - DallasTemperature
    - Wire
    - SPI
    - adafruit/Adafruit BusIO
    - adafruit/Adafruit ADS1X15
    - file:///config/esphome/custom/DFRobot_ESP_PH_WITH_ADC

esp32:
  board: esp32dev
  framework:
#    type: esp-idf
    type: arduino
    version: recommended

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "***"

ota:
  password: "**"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

switch:
  - platform: restart
    name: "Restart Device"

sensor:

  - platform: custom
    lambda: |-
      auto my_sensor = new CustomDFRobotPhSensor(30000);
      App.register_component(my_sensor);
      return {my_sensor->ph_sensor, my_sensor->temperature_sensor, my_sensor->voltage_sensor};
  
    sensors:
    - name: "pH Sensor"
      accuracy_decimals: 2
      unit_of_measurement: " "

    - name: "Water Temperature Sensor"
      accuracy_decimals: 2
      unit_of_measurement: "°C"

    - name: "pH Sensor Voltage"
      accuracy_decimals: 2
      unit_of_measurement: "V"

did you get an answer to this, trying to use the temperature but it is stuck on -196 F although I have the dallas sensor outputting 23.9c just not sure how to get the initial code for dallas to read into the ph code if that makes sense

Gave up on it after much struggle.

Found two spots in the code where it says onewire bus (X) with a number in the X, changed it to the pin i have the dallas on the esp32 and now it shows proper temp. one in the customer dfrobot ph sensor adc.h file and the other was in the read_ph.info

I am sure you have figured it out by now but to help others, I ended up in the same place where the pH sensor’s reading fluctuated a lot. For me, the problem was the second decimal digit which was going up and down constantly.
Fortunately, I am using the sensor to check the pH of an Aquarium so I was/am happy to reduce the accuracy of the sensor to one decimal digit and that did the trick. You can achieve this by an extra attribute in your EspHome config:

- platform: adc
  pin: A0
  id: ph
  name: "Tank's-pH"
  accuracy_decimals: 1   # This property did the trick
  update_interval: 60s
  unit_of_measurement: pH
  filters:
    - median:
        window_size: 7
        send_every: 4
        send_first_at: 3
    # Measured voltage -> Actual pH (buffer solution)
    - calibrate_linear:
       - 0.569 -> 7.1
       - 0.719 -> 4.0

Hope this helps.