Reading Serial data from arduino to esphome

Home Assistant automation projects, questions, etc. go here.
User avatar
LEDG
Site Admin
Reactions:
Posts: 1599
Joined: Sun Jun 04, 2017 8:15 pm

Morbidbystander wrote:
Thu Nov 19, 2020 9:46 pm
I tried and failed. Possibly because I was trying to smash the code with the Ruuvi sensor that is steaming temp and humidity. Idk, it said failed during the up load with a arrow pointing at the "s" in scale after new.

auto hx711scale = new scaleSensor(id(

And the final } was also tagged as an error. But I did get the spacing correct. Yaml kicks my butt. For some reason I can understand c++ way better.
can you post your code? both .h and yaml
Want to Support the Site?

Use this Amazon referral link and any purchase you make within 24 hrs will earn LEDgardener a commission at no cost to you!
Morbidbystander
LED Enthusiast
LED Enthusiast
Reactions:
Posts: 40
Joined: Wed Sep 16, 2020 4:12 am

After completing the .h file I still get some errors.

In file included from src/main.cpp:24:0:
src/uart_com.h:23:5: error: expected unqualified-id before 'return'
return -1;
^
src/uart_com.h:24:3: error: expected declaration before '}' token
}
^
*** [/data/ruuvi/.pioenvs/ruuvi/src/main.cpp.o] Error 1

When I tried to copy paste all the spacing was lost in the preview. How are you attaching files with the correct spacing? The h and the yaml will not upload in the add files section.

Is this the best method to get Ph data from a arduino to Home Assistant?
User avatar
LEDG
Site Admin
Reactions:
Posts: 1599
Joined: Sun Jun 04, 2017 8:15 pm

[quote=Morbidbystander post_id=24164 time=1605835363 user_id=2945]
After completing the .h file I still get some errors.

In file included from src/main.cpp:24:0:
src/uart_com.h:23:5: error: expected unqualified-id before 'return'
return -1;
^
src/uart_com.h:24:3: error: expected declaration before '}' token
}
^
*** [/data/ruuvi/.pioenvs/ruuvi/src/main.cpp.o] Error 1

When I tried to copy paste all the spacing was lost in the preview. How are you attaching files with the correct spacing? The h and the yaml will not upload in the add files section.

Is this the best method to get Ph data from a arduino to Home Assistant?
[/quote]

At the top of the text editor when you're making a post you'll see all the normal bold, italics etc. and you'll also see a button that looks like this </>

Hit that button, then paste your code in like this

[code]YOUR CODE HERE[/code]

What pH sensor are you using again? If it's Atlas, there's already a custom component available for it which should make life easier for you.
Want to Support the Site?

Use this Amazon referral link and any purchase you make within 24 hrs will earn LEDgardener a commission at no cost to you!
Morbidbystander
LED Enthusiast
LED Enthusiast
Reactions:
Posts: 40
Joined: Wed Sep 16, 2020 4:12 am

Thank you kindly. This is the yaml.

Code: Select all

esphome:
  name: ruuvi
  platform: ESP32
  board: esp-wrover-kit
  includes: uart_com.h

wifi:
  ssid: "!secret mynetwork"
  password: "!secret mypassword"

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

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

uart:
  id: uart_bus
  tx_pin: GPIO17
  rx_pin: GPIO16
  baud_rate: 9600

esp32_ble_tracker:

sensor:
- platform: ruuvitag
  mac_address: FF:04:1D:7E:14:EC
  humidity:
    name: "Ruuvi 2x2 Humidity"
  temperature:
    name: "Ruuvi 2x2 Temperature"
  battery_voltage:
    name: "Ruuvi 2x2 Battery Voltage"
  tx_power:
    name: "Ruuvi 2x2 TX Power"
    
- platform: custom
  lambda: |-
    auto EZO = new scaleSensor(id(uart_bus));
    App.register_component(EZO);
    return {EZO};
  sensors:
    name: "Ph Sensor"
    unit_of_measurement: Ph
    accuracy_decimals: 2
Then the .h file.

Code: Select all

#include "esphome.h"

class scaleSensor : public PollingComponent, public UARTDevice, public sensor::Sensor{
 public:
  scaleSensor(UARTComponent *parent) : PollingComponent(1000), UARTDevice(parent) {}
  float weight = 0;
  
  void setup() override {
    // nothing to do here
  }
  
  void loop() override {
    while (available()) {
      String line = readStringUntil('\n');
      weight = line.toFloat();
    }
  }
  
  void update() {
    publish_state(weight);
  }
};
    return -1;
  }

  void loop() override {
    const int max_line_length = 80;
    static char buffer[max_line_length];
    if (available() && readline(read(), buffer, max_line_length) > 0) {
      publish_state(buffer);
    }
  }
};
I am running the Atlas Ph sensor with I2C on a Mega. I ended up using a cheap pump, a relay and an I2C LCD in the grow to display current Ph. My esp32 is only running a Ruuvi sensor at the moment and reporting to HA. I need more yaml experience to pimp that esp.
User avatar
LEDG
Site Admin
Reactions:
Posts: 1599
Joined: Sun Jun 04, 2017 8:15 pm

yaml looks fine, the .h file is a mix between my 2 versions though.

Replace the contents with this code:

Code: Select all

#include "esphome.h"

class scaleSensor : public PollingComponent, public UARTDevice, public sensor::Sensor{
 public:
  scaleSensor(UARTComponent *parent) : PollingComponent(1000), UARTDevice(parent) {}
  float weight = 0;
  
  void setup() override {
    // nothing to do here
  }
  
  void loop() override {
    while (available()) {
      String line = readStringUntil('\n');
      weight = line.toFloat();
    }
  }
  
  void update() {
    publish_state(weight);
  }
};
Once it works, you can replace all the verbiage about "scale" with something about pH.
Want to Support the Site?

Use this Amazon referral link and any purchase you make within 24 hrs will earn LEDgardener a commission at no cost to you!
Morbidbystander
LED Enthusiast
LED Enthusiast
Reactions:
Posts: 40
Joined: Wed Sep 16, 2020 4:12 am

You have excellent eyes my friend. I guess my dyslexic brain combined the 2. I ran into some very big problems however. Every time I hooked up my Arduino TX to the ESP32 RX the ESP dropped off wifi. I ran the TX pins back to TX0 in yaml same issue. I built a voltage divider thinking maybe the off brand ESP could need 3.3v...nope. Then I remembered your post about a "text sensor" and I realized my bus contained text for the LCD display.

At this point I went back into the yaml and changed everything for a text sensor and it fired right up. However there is no graphing with a text sensor. It basically just documents change of state on the serial monitor.
User avatar
LEDG
Site Admin
Reactions:
Posts: 1599
Joined: Sun Jun 04, 2017 8:15 pm

Hmmm.

Here is a thread containing the code for the Atlas ESPhome integration somebody built... this looks promising:

viewtopic.php?f=35&t=5771

Looks like it runs directly off the ESP though and I’m not sure if that works for you.
Want to Support the Site?

Use this Amazon referral link and any purchase you make within 24 hrs will earn LEDgardener a commission at no cost to you!
Morbidbystander
LED Enthusiast
LED Enthusiast
Reactions:
Posts: 40
Joined: Wed Sep 16, 2020 4:12 am

I will definitely play around with that. There's many options at this point. I forgot to say I tested an arduino uno just communicating an integer and the uart com worked with the sensor sketch; Uploaded fine to HA complete with a gauge and graph. So I could scrub my ph controller program of serial print text and that would work as well. Probably end up testing both. :lol:

But for now I'm happy with mobile reporting ph, temp and humidity.
Morbidbystander
LED Enthusiast
LED Enthusiast
Reactions:
Posts: 40
Joined: Wed Sep 16, 2020 4:12 am

I played, I crashed, bricked, wiped and restored. :roll:

Long story short, I2C needs a master and a slave to communicate. The arduino master and the esp master got in a fight.

Anyway, I ended up removing all the text from my arduino ph controller code and the custom sensor code works great. But again, I found myself stuck in the trying to upload, failure loop because I had TX and RX connected during upload. Last time I make that mistake.

One more interesting thing. After 24 hours of HA getting a Ph text sensor status every second, the sensor entity history would crash if you clicked it. :shock:
User avatar
LEDG
Site Admin
Reactions:
Posts: 1599
Joined: Sun Jun 04, 2017 8:15 pm

Good findings here. The serial thing burned me a few times at first too. I think it’d be worth implementing something like a time_throttle filter for your pH sensor to reduce the readings recorded: https://www.home-assistant.io/integrations/filter

So you’d create this new filter sensor and choose how often you want it to update its value, then exclude the actual pH sensor that’s puking data every second from your history and recording, and instead include the filtered version. This would just be to prevent your history from being overwhelmed and you’d still use your actual sensor for automations, real-time monitoring, etc.
Want to Support the Site?

Use this Amazon referral link and any purchase you make within 24 hrs will earn LEDgardener a commission at no cost to you!
Post Reply