Reading sensor values from serial

Hello community,

I have been trying to get a project to work for days, but I think it is beyond my understanding. I am trying to combine this hydroponics system with Home Assistant via ESPhome. To clarify - I am a total novice in coding (i’m a biochemist), and I try to learn as I go. I have successfully modified the Arduino code in the project and got it working. It sends the sensor values to serial (I can see them in the arduino serial monitor). Now comes the next part - reading those values on my ESP32 running esphome (it also controls a few relays). So far I have physically connected the two controlrers (Rx -tx, tx-rx and gnd). Not getting the values is where I am stuck. I have tried everything I could think of. My first plan was to do this: Custom UART Device — ESPHome, but as it is being removed, I tried to get the external component to work (locally). No luck. I have tried using ChatGTP to help, but after 8 hours of trying a lot of different codes for yaml, cpp and h file - no luck.
So I humbly ask the members of the community, if you could donate some of your time to help me get this thing working? I don’t understand C++ at all, so blindly copy pasting stuff is not helping. I want to see the sensor readings in HA (in graphs or something) for starters.

Thank you

Which sensors?

You may need to summarise the project a bit more for us.

Are you using all of these?

pH Sensor, EC sensor or TDS meter, DS18b20 one-wire digital temperature sensor, TOF10120 Laser distance Rangefinder, and an Oled display Module.

Sharing a bit more of what you’ve tried might also help us understand where you’re at…

Is there a reason you want to keep the Arduino? Are the sensors 5V logic levels maybe?

Otherwise, have you considered replacing the Arduino with an ESP32 and thus cut out one level of complication. You could then do everything with ESPHome, or alternatively run the Arduino code on an ESP32 and add a wifi and MQTT library.

That was one of the first projects I did after converting from Arduino hardware to ESP - it wasn’t too difficult…

First of all, thank you for taking the time to reply.
I am using TOF10120 for distance measuring, DS18b20 for temperature, a TDS meter, and a HW-828 pH meter. And yes, it also uses an Oled display, but that doesn’t matter much. I just added it as it was already planned into the PCB design. This is the code I run on arduino nano: Arduino code - Pastebin.com
I did think of moving all the sensors to the ESP32, but that would have required programing them into the ESPhome code and I assumed it would be more difficult than just running them on the Arduino with the existing code. Especially since I have no idea what I am doing :smiley:
So my thought was that it would be less complicated to write something that just gets the text values via serial and displays them in HA. But somehow, it became complicated.
As for .h and .cpp files - I tried to implement a custom component, using chatGPT to write the code, but eventually it always ran into some compilation errors. Either circular reference or missing sensor. I did not keep track of the various iterations, but I can put up the last attempt code.
I have been thinking and to be honest, do I really need to actually add anything as a sensor? There should be a way just to use the text information? I may have confused the AI, so it let me on the wrong path to begin with.

Part in the Yaml file: yaml - Pastebin.com
custom_sensor.cpp: custom_sensor.cpp - Pastebin.com
custom_sensor.h: custom_sensor.h - Pastebin.com

All the files are in the esphome directory on my HA.

So this is where I am at. I have a waking arduino, that sends data over serial and no way to add it to HA.
Anything else I can provide?

The sensors are not connected at the moment.


Anyone? this is driving me crazy :smiley:

Do you have some samples of the uart messages?

Have you tried just seeing what the ESPHome UART debugger shows up once connected? You should just need the Tx on the Arduino connected to an Rx on the ESP I think? Maybe GNDs too.

You could look into this technique to read uart without a custom component. I’ve used it a few times.

Are the Arduino serial pins 5v or 3.3V?

1 Like

Since I have managed to figure it out with the help of a few people on Discord, I thought it would be useful to post the solution here, in case someone wants to do the same.
One of the users pointed me to his github of custom components.

I had to modify the arduino code to output the values as comma separated values, but with that and the custom component I was able to implement it into the esphome.

This is the relavant part in the ESPhome config:

#Arduino part

external_components:
  - source:
      type: git
      url: https://github.com/ssieb/esphome_components
    components: [ serial_csv ]

uart:
  id: uart_bus
  rx_pin: 3
  tx_pin: 1
  baud_rate: 9600

sensor:
  - platform: serial_csv
    uart_id: uart_bus   # optional
    sensors:
      - index: 0
        name: pH
        accuracy_decimals: 2
        device_class: "ph"
        unit_of_measurement: "pH"
      - index: 1
        name: TDS
        accuracy_decimals: 0
        device_class: volatile_organic_compounds_parts
        unit_of_measurement: "ppm" 
      - index: 2
        name: EC
        accuracy_decimals: 2
        device_class: energy
        unit_of_measurement: "S/m"
      - index: 3
        name: Temp
        accuracy_decimals: 2
        unit_of_measurement: "°C"
        device_class: "temperature"
      - index: 4
        name: TOF
        accuracy_decimals: 0
        device_class: "distance"
        unit_of_measurement: "mm"

I still haven’t figured out how to reduce the time between the sensor checks, but more is better than less :smiley:

The resulting Home Assistant panel looks like this:

I want to thank the community for the suggestions. Once all the project is complete, I will upload more pictures (if I do not forget).

2 Likes

Nice. I’m guessing the component processes all incoming data? So it won’t accept update_interval?

You can look at throttle and delta filters if you want to reduce data sent to HA.