I need some help with ESPHome, uart port and a Windsonic anemonemeter

Hi
I’m trying to get a custom_component to work on ESPHome,
I will try to get an instrument that sends data in NMEA 0183 (it’s not a GPS) via rs232 ttl adapter to uart on a esp32.

I’m a bit stuck, I’m new to this platform.

So far I have done this:
From my YAML file:

uart:
  id: wind
  tx_pin: GPIO1
  rx_pin: GPIO3
  baud_rate: 19200
  
custom_component:
- lambda: |-
    auto Windsonic = new Windsonic(id(wind));
    return {Windsonic->retning, Windsonic->hastighet}; 

From my Windsonic.h file

#include "esphome.h"

class Windsonic : public Component, public UARTDevice {
 public:
  Windsonic(UARTComponent *parent) : UARTDevice(parent) {}
  float retning = 0;
  float hastighet = 0;
  
  void setup() override {
    // nothing to do here
  }
  void loop() override {
    // Use Arduino API to read data, for example
    String line = readStringUntil('\n');
    retning = parse_float(line.substring(8, 10));
	hastighet = parse_float(line.substring(15, 19));
    
    }
    // etc
  
 // void update() override {
//	retning->publish_state(retning);
//	hastighet->publish_state(hastighet);
//  }
};

From my compile log:

src/main.cpp:113:3: warning: multi-line comment [-Wcomment]
   //   lambda: !lambda "auto Windsonic = new Windsonic(id(wind));\nreturn {Windsonic->retning,\
   ^
In file included from src/main.cpp:14:0:
src/windsonic.h: In member function 'virtual void Windsonic::loop()':
src/windsonic.h:15:41: error: invalid initialization of reference of type 'const string& {aka const std::__cxx11::basic_string<char>&}' from expression of type 'String'
     retning = parse_float(line.substring(8, 10));
                                         ^
In file included from src/esphome/core/application.h:8:0,
                 from src/esphome/components/api/api_connection.h:4,
                 from src/esphome.h:2,
                 from src/main.cpp:3:
src/esphome/core/helpers.h:44:17: note: in passing argument 1 of 'esphome::optional<float> esphome::parse_float(const string&)'
 optional<float> parse_float(const std::string &str);
                 ^
In file included from src/main.cpp:14:0:
src/windsonic.h:16:40: error: invalid initialization of reference of type 'const string& {aka const std::__cxx11::basic_string<char>&}' from expression of type 'String'
  hastighet = parse_float(line.substring(15, 19));
                                        ^
In file included from src/esphome/core/application.h:8:0,
                 from src/esphome/components/api/api_connection.h:4,
                 from src/esphome.h:2,
                 from src/main.cpp:3:
src/esphome/core/helpers.h:44:17: note: in passing argument 1 of 'esphome::optional<float> esphome::parse_float(const string&)'
 optional<float> parse_float(const std::string &str);
                 ^
src/main.cpp: In lambda function:
src/main.cpp:121:28: error: expected type-specifier before 'Windsonic'
       auto Windsonic = new Windsonic(wind);
                            ^
src/main.cpp:122:55: error: could not convert '{<expression error>, <expression error>}' from '<brace-enclosed initializer list>' to 'std::vector<esphome::Component*>'
       return {Windsonic->retning, Windsonic->hastighet};  
                                                       ^
*** [/data/vind/.pioenvs/vind/src/main.cpp.o] Error 1

I need som advice on how to get this right.

You need to post your code properly. See here (point 11 but read the whole thing):

Hi. I also look for identical solution for this problem. Do you figure something out? Thanks

I’m not found a solution to this yet, I’m trying to learn a bit more about the ESPHOme platform and howto set it up to read the NMEA sentence and get out 2 varaibles and return them to Home assistant.

I have the hardware part sortet. I have made another system based on Python, RPI and a SQL server earlier.

1 Like

So far I have my ESPHome showing up in integrations, but with no variables. It should be 2. In the ESPHome log the data and the variabel ar showing up correctly.

How to connect the ESPHome to integrations correctly ?
It’s made as an custom sensor.

So you made another system, that’s a good news.

I browse this forum, but the solution always refer to this link. I tried to read and undestand it but it takes time because i don’t have solid programming skill.

Aside from this link, i am not able to find any step-by-step tutorial on how to do it. Is no one ever did it before? Why can’t i find any documentation.

I am using Wemos D1 mini which only has 1 serial, it is connected on GPIO1 and GPIO3. Everytime i power-up Wemos D1 while the serial connected to my arduino, Home Assistant server doesn’t detecct Wemos D1. The status is always offline;

But when i unplug the serial pin connected to the arduino, status of Wemos D1 changed to online. I tried to change another pin, but the problem still happens. How come?

What is it connected to an arduino for?

@nickrout I used the arduino just to try to send some random string because the sensor is on shipping right now. It took so long to arrive, so i tried to experiment with arduino instead, just to make sure this serial could work. So by the time my sensor arrive, i could just easily plug it in to ESPHome. I code it just like this to send hello

void setup() {
Serial.begin(9600);
}

void loop() {
Serial.write(“hello\r\n”); //send the string “hello”
}

Is it possible that somehow the serial port in my WEMOS D1 Mini by default has been used by ESPHome firmware. so i can’t use it again to read serial sent from arduino? It always happen when i plug serial from arduino to Wemos D1 mini serial.

I used GPIO1 and GPIO3 for serial in Wemos D1 mini.

uart:
id: uart_bus
tx_pin: GPIO1
rx_pin: GPIO3
baud_rate: 9600

You have to sett baudrate for the logger in the YAML file for this project to 0 for to use this settup.

Hi @jorgenn, do you mean to set the baudrate like this?

# Enable logging
logger:
 baud_rate: 0

I tried it, but still i cant’ get the serial sent from arduino to Wemos. Anway the log from Wemos shows this error:

Here is my configuration yaml file for Wemos D1 Mini:

esphome:
  name: test_serial
  platform: ESP8266
  board: d1_mini
  includes:
    - my_custom_component.h

uart:
  id: uart_bus
  tx_pin: GPIO1
  rx_pin: GPIO3
  baud_rate: 9600

custom_component:
- lambda: |-
    auto my_custom = new MyCustomComponent(id(uart_bus));
    return {my_custom};

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

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

captive_portal:

# Enable logging
logger:
   baud_rate: 0

# Enable Home Assistant API
api:

ota:

When nothing is attached to Wemos Serial, i will have this error

[15:21:57][E][uart:229]: Reading from UART timed out at byte 0!
[15:21:57][E][uart:229]: Reading from UART timed out at byte 0!
[15:21:57][E][uart:229]: Reading from UART timed out at byte 0!
[15:21:58][E][uart:229]: Reading from UART timed out at byte 0!
[15:21:58][E][uart:229]: Reading from UART timed out at byte 0!

So far I have ended up with this
YAML

esphome:
  name: vind
  platform: ESP32
  board: esp-wrover-kit
  includes:
    - windsonic.h
wifi:
  ssid: "dark-side"
  password: ""

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

captive_portal:

# Enable logging
logger:
  baud_rate: 0
# Enable Home Assistant API
api:
  password: ""

ota:
  password: ""

uart:
  id: wind
  tx_pin: GPIO1
  rx_pin: GPIO3
  baud_rate: 19200
  
sensor:
- platform: custom
  lambda: |-
    auto my_Windsonic = new Windsonic(id(wind));
    App.register_component(my_Windsonic);
    return {my_Windsonic->retning_sensor, my_Windsonic->hastighet_sensor};     
    
  sensors:
  - name: "Vindretning"
    unit_of_measurement: °Grader
    accuracy_decimals: 3
  - name: "Vindhastighet"
    unit_of_measurement: ms
    accuracy_decimals: 3
  
  

And I have this my_sensor.h

#include "esphome.h"

class Windsonic : public Component, public Sensor, public UARTDevice {
 public:
  Windsonic(UARTComponent *parent) : UARTDevice(parent) {}
  Sensor *hastighet_sensor = new Sensor();
  Sensor *retning_sensor = new Sensor();
  float retning;
  float hastighet;
  
  void setup() override {
    // nothing to do here
  }
  void loop() override {
    // Use Arduino API to read data, for example
    String line = readStringUntil('\n');
    String r = line.substring(7, 10);
	String s = line.substring(14, 19);
	retning = r.toFloat();
	hastighet = s.toFloat();
    retning_sensor->publish_state(retning);
    hastighet_sensor->publish_state(hastighet);
	retning = 0;
	hastighet = 0;
	r = "";
	s = "";
  }
  
};

And This shows up in integrations in HA except for the actuall sensors, there is what i have to sort out nest to get this to work

I am getting confused now. Here is my_custom_component.h. I just copy-paste it from here.

#include "esphome.h"

class MyCustomComponent : public Component, public UARTDevice {
 public:
  MyCustomComponent(UARTComponent *parent) : UARTDevice(parent) {}

  void setup() override {
    // nothing to do here
  }
  void loop() override {
    // Use Arduino API to read data, for example
    String line = readString();
    int i = parseInt();
    while (available()) {
      char c = read();
    }
    // etc
  }
};

I think if it only to read serial like this from my arduino, it won’t need any adjustment. Am i mistaken?
My serial from arduino also uses 3.3v logic level, so i think the problem is not over there.

void setup() {
Serial.begin(9600);
}

void loop() {
Serial.write(“hello\r\n”); //send the string “hello”
}

Are you trying to read serial data from a sensor that uses rs232 or a other serial connection ?

Don’t post photos of log files.

How long did you wait for the log to start up after the point where esphome says it hasn’t connected after 1 second? Hint, it might take longer.

Can you ping the esp device?

Does it appear on your router, or by searching with fing?

In short, it’s what i’m trying to do:

Several resistive sensor that require analog reading -> arduino to read the analog value and pass that value to Wemos D1 via serial -> Wemos D1 receive the value via serial and send it to home assistant.

I think you have to use the arduino to read the analog signals and translate them into values you can publish via the serial bus to the ESP, there you have to setup and routine that reads line by line incomming values, Then publish this to HA

I am so sorry sir @nickrout, i’m just a newbie trying to learn. Please i beg you don’t be so harsh on me.

Let me explain what happened.The device works fine as long as i don’t connect GPIO1 and GPIO3 on Wemos D1 to UART pin of arduino. I can ping the ESP device, it appears on my router, even by searching with ‘fing’. Wemos easily connect after less than 5 seconds. But it shows error ‘Reading from UART timed out at byte 0!’, even i already set logger baud rate to 0 . Here is the no-picture log

INFO Reading configuration /config/esphome/test_serial.yaml...
INFO Starting log output from test_serial.local using esphome API
INFO Connecting to test_serial.local:6053 (192.168.42.35)
INFO Successfully connected to test_serial.local
[16:23:12][I][app:100]: ESPHome version 1.14.3 compiled on Apr  9 2020, 15:15:41
[16:23:12][C][wifi:415]: WiFi:
[16:23:12][C][wifi:283]:   SSID: [redacted]
[16:23:12][C][wifi:284]:   IP Address: 192.168.42.35
[16:23:12][C][wifi:286]:   BSSID: [redacted]
[16:23:12][C][wifi:287]:   Hostname: 'test_serial'
[16:23:12][C][wifi:291]:   Signal strength: -65 dB ▂▄▆█
[16:23:12][C][wifi:295]:   Channel: 11
[16:23:12][C][wifi:296]:   Subnet: 255.255.255.0
[16:23:12][C][wifi:297]:   Gateway: 192.168.42.129
[16:23:12][C][wifi:298]:   DNS1: 192.168.42.129
[16:23:12][C][wifi:299]:   DNS2: (IP unset)
[16:23:13][E][uart:229]: Reading from UART timed out at byte 0!
[16:23:13][E][uart:229]: Reading from UART timed out at byte 0!
[16:23:13][E][uart:229]: Reading from UART timed out at byte 0!
[16:23:13][E][uart:229]: Reading from UART timed out at byte 0!

But something different if i connect GPIO and GPIO3 on Wemos D1 to UART pin of arduino. Here’s the no-picture log

INFO Reading configuration /config/esphome/test_serial.yaml...
INFO Starting log output from test_serial.local using esphome API
INFO Connecting to test_serial.local:6053 (192.168.42.35)
WARNING Initial connection failed. The ESP might not be connected to WiFi yet (Timeout while waiting for message response!). Re-Trying in 1 seconds
INFO Connecting to test_serial.local:6053 (192.168.42.35)
WARNING Initial connection failed. The ESP might not be connected to WiFi yet (Timeout while waiting for message response!). Re-Trying in 1 seconds
INFO Connecting to test_serial.local:6053 (192.168.42.35)
WARNING Initial connection failed. The ESP might not be connected to WiFi yet (Timeout while waiting for message response!). Re-Trying in 2 seconds
INFO Connecting to test_serial.local:6053 (192.168.42.35)
WARNING Initial connection failed. The ESP might not be connected to WiFi yet (Timeout while waiting for message response!). Re-Trying in 3 seconds

I’ve been waiting for more than1 hour but that error still happens. Because it say’s Wemos is not connected, i absolutely can’t ping the ESP device, it doesn’t appears on my router, even by searching with ‘fing’.

Do you have any suggestion how to handle it? Or do you have any hints available for this newbie? Thanks before

Thanks for the advice, but since the sensor hasn’t arrived yet, i can’t do much. I think the corona virus cause so much trouble in shipping. Then, i am just experimenting to send string hello from arduino and check at Wemos, whether the string received or no.

I dont get what you mean. How can i setup and routine that reads if just by simply send “hello” string doesn’t work at all. Is my_custom_component.h wrong? Thanks.

You have the electrical connection done right, ie TX is connectetd to RX on the other device ?