Custom component (A02YYUW) ESP Home

Tags: #<Tag:0x00007f77fdcd1eb0> #<Tag:0x00007f77fdcd1d20>

I have a water proof ultrasonic sensor (A02YYUW) which i would like to add to a project built in esphome.

It outputs via UART and there is a sample code in the data sheet.

is it possible to integrate this code directly as a custom component in esp home or am I barking up the wrong tree :roll_eyes:

Any help is appreciated.

/*
  *@File  : DFRobot_Distance_A02.ino 
  *@Brief : This example use A02YYUW ultrasonic sensor to measure distance
  *         With initialization completed, We can get distance value 
  *@Copyright [DFRobot](http://www.dfrobot.com),2016         
  *           GUN Lesser General Pulic License
  *@version V1.0           
  *@data  2019-8-28
*/

#include <SoftwareSerial.h>

SoftwareSerial mySerial(11,10); // RX, TX
unsigned char data[4]={};
float distance;

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

void loop()
{
    do{
     for(int i=0;i<4;i++)
     {
       data[i]=mySerial.read();
     }
  }while(mySerial.read()==0xff);

  mySerial.flush();

  if(data[0]==0xff)
    {
      int sum;
      sum=(data[0]+data[1]+data[2])&0x00FF;
      if(sum==data[3])
      {
        distance=(data[1]<<8)+data[2];
        if(distance>30)
          {
           Serial.print("distance=");
           Serial.print(distance/10);
           Serial.println("cm");
          }else 
             {
               Serial.println("Below the lower limit");
             }
      }else Serial.println("ERROR");
     }
     delay(100);
}
1 Like

Have you read this:

Thanks Tom,

I have had a read through the docs and some examples on esphome.io

the below is what i have come up with so far. I have left the names as is from the exaamples to try and eliminate any naming errors on my behalf.

So far this is giving me an output on the esphome logger of:

nothing is attached to the tx and rx

  • [10:01:59][E][uart:229]: Reading from UART timed out at byte 0!

This is as I would expect with nothing connected.

When the Tx of the distance sensor is connected to the Rx pin (D2 in this case) on the 8266 the logger ceases to function immediately after initiating. If i disconnect the pin the logger immediately resumes.

Is this a problem with the logger not being able to function with an external UART connected or is my code off track?

This is the yaml in esphome.

Copy to clipboard

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

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

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:


uart:
  id: uart_bus
  tx_pin: D1
  rx_pin: D2
  stop_bits: 1
  baud_rate: 9600
  
custom_component:
- lambda: |-
    auto my_custom = new MyCustomComponent(id(uart_bus));
    return {my_custom};

This is my_custom_component.h

Copy to clipboard

#include "esphome.h"

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

	unsigned char data[4] = {};
	float distance;

	void setup() override {
		
	}
	void loop() override {
	
		String line = readString();
		

		if (data[0] == 0xff)
		{
			int sum;
			sum = (data[0] + data[1] + data[2]) & 0x00FF;
			if (sum == data[3])
			{
				distance = (data[1] << 8) + data[2];
				if (distance > 30)
				{
					Serial.print("distance=");
					Serial.print(distance / 10);
					Serial.println("cm");
				}
				else
				{
					Serial.println("Below the lower limit");
				}
			}
			else Serial.println("ERROR");
		}
		delay(100);

		
	}
};

You’re calling readString() but never using the result. It’s probably blocking waiting for an end of line, which doesn’t occur in your protocol, and that’s why it seems like nothing happens. You need to copy the first part of the original code too I think, the part that populates the data array.

2 Likes

Hi @chocolatejoe , i am also facing identical trouble here. What device do you use for ESPHome?
I use wemos D1 mini with only 1 UART available and having same problem when nothing is attached to UART.

  • Reading from UART timed out at byte 0!

But when attach my sensor, ESPHome device will be disconnected. Do you figure something out? Thanks

Hi @hmoffatt, I am facing identical problem like @chocolatejoe. Can i ask your help.

I used Wemos D1 Mini with UART on GPIO1 and 3. I also read the documentation here and made my_custom_component.h same with the documentation as follow

#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
  }
};

Here is the configuration yaml file for my ESPHome device

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:

# Enable Home Assistant API
api:

ota:

When nothing is attached to GPIO1 and 3, it will show error like this

I used Arduino with serial logic level of 3.3V, the same voltage of Wemos D1 logic level, to simply send string from this arduino to Wemos D1 mini. Here is my arduino code. When i attach arduino serial to Wemos D1 mini serial, the connection is cut off. My Wemos D1 mini is not connected to home assistant server anymore. The status become offline.

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

void loop() {
//  Serial.write(45); // send a byte with the value 45

  Serial.write("hello\r\n");  //send the string "hello" and return the length of the string.
}

Can you help me. what is happening here? Thanks.

You need to customise the loop() code. I would start by just keeping the loop that reads characters and printing each one out as it’s received. Then you will know is it timing out on the readString(), or the parseInt() or what.

You definitely don’t want the parseInt, because you’re not sending any ints…