Distance Sensor A02YYUW

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
Wolvie
New user
Posts: 4
Joined: 01 Jun 2020, 20:55

Distance Sensor A02YYUW

#1 Post by Wolvie » 01 Jun 2020, 21:14

Hi all, :D

I'm new, so please forgive me the probably bad question. :roll:

I would like to use this Sensor (https://wiki.dfrobot.com/A02YYUW%20Wate ... 1#target_0) (waterresistant and short minimal range) with ESP Easy on a WEMOS D1 Mini.

Since I have not found a similar sensor in the list of supported devices, is there an other possibility to connect this sensor?

Thanks

Wolvie
New user
Posts: 4
Joined: 01 Jun 2020, 20:55

Re: Distance Sensor A02YYUW

#2 Post by Wolvie » 03 Jun 2020, 19:42

Nobody can help me?

No one got experience with this sensor?

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Distance Sensor A02YYUW

#3 Post by TD-er » 03 Jun 2020, 20:48

I haven't seen that sensor before, but it doesn't look like a very complicated one to support in ESPEasy.

Right now there is no plugin supporting that sensor.

Wolvie
New user
Posts: 4
Joined: 01 Jun 2020, 20:55

Re: Distance Sensor A02YYUW

#4 Post by Wolvie » 05 Jun 2020, 06:52

Thanks for the reply,

but what can I do now?

Since I'm new to this...

Wait until somebody create a plugin...
or
do it myself... but how? :?:

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Distance Sensor A02YYUW

#5 Post by TD-er » 05 Jun 2020, 11:10

Reading of that sensor is quite simple.

So you may want to start by looking into a very simple plugin like this one: https://github.com/letscontrolit/ESPEas ... 8_Dust.ino
It is about the most simple plugin I could find.

Only you need to read from the serial port.
So first you need to add code to read it from Serial as described on the dfRobot site.
Then later on we can make it more generic by using the ESPEasySerial wrapper, so it can work on any serial port for ESP8266 or ESP32.

For a first attempt, you can use the reading code listed on the dfRobot site and run it in the PLUGIN_READ case of the switch statement.

Wolvie
New user
Posts: 4
Joined: 01 Jun 2020, 20:55

Re: Distance Sensor A02YYUW

#6 Post by Wolvie » 06 Jun 2020, 09:31

Great!

Let's see if I get it...

I'm learning with every project and new idea... :D

Thanks!

Zork123
New user
Posts: 1
Joined: 16 Oct 2020, 15:19

Re: Distance Sensor A02YYUW

#7 Post by Zork123 » 16 Oct 2020, 15:27

Hey Wolvie, did you have any success with the A02YYUW and wemos.
Because I also have a wemos and A02YYUW over here and would love to play with it.
Looking forward to your reply.

seb82
Normal user
Posts: 62
Joined: 05 Sep 2018, 10:56

Re: Distance Sensor A02YYUW

#8 Post by seb82 » 18 Aug 2021, 09:54

With inspiration from this this library, I managed to write a plugin for this device that seems to work fine.

All in all, this device appears to be quite good with a good range and accuracy.

Below is the code :

Code: Select all

#include "_Plugin_Helper.h"
#ifdef USES_P251

//#######################################################################################################
//#################################### Plugin 251: A02YYUW ##############################################
//#######################################################################################################

#define PLUGIN_251
#define PLUGIN_ID_251 251
#define PLUGIN_NAME_251 "Distance - A02YYUW"
#define PLUGIN_VALUENAME1_251 "Distance"

#define MIN_DISTANCE 30
#define MAX_DISTANCE 4500
#define SERIAL_BAUD_RATE 9600
#define SERIAL_HEAD_DATA 0xff
#define SERIAL_AVAILABLE_CHECK_DELAY 50
#define SERIAL_AVAILABLE_CHECK_CICLES 10 // SERIAL_AVAILABLE_CHECK_DELAY * SERIAL_AVAILABLE_CHECK_CICLES = 500ms

enum {
    DistanceSensor_A02YYUW_MEASSUREMENT_STATUS_OK = 0,
    DistanceSensor_A02YYUW_MEASSUREMENT_STATUS_ERROR_CHECK_SUM = -1,
    DistanceSensor_A02YYUW_MEASSUREMENT_STATUS_ERROR_MAX_LIMIT = -2,
    DistanceSensor_A02YYUW_MEASSUREMENT_STATUS_ERROR_MIN_LIMIT = -3,
    DistanceSensor_A02YYUW_MEASSUREMENT_STATUS_ERROR_SERIAL = -4
};

#include <ESPeasySerial.h>
ESPeasySerial *P251_Serial = nullptr;

boolean Plugin_251_init = false;

boolean Plugin_251(uint8_t function, struct EventStruct *event, String& string)
{
  boolean success = false;

  switch (function)
  {
    case PLUGIN_DEVICE_ADD:
      {
        Device[++deviceCount].Number = PLUGIN_ID_251;
        Device[deviceCount].Type = DEVICE_TYPE_SERIAL;
        Device[deviceCount].VType = Sensor_VType::SENSOR_TYPE_SINGLE;
        Device[deviceCount].Ports = 0;
        Device[deviceCount].PullUpOption = false;
        Device[deviceCount].InverseLogicOption = false;
        Device[deviceCount].FormulaOption = true;
        Device[deviceCount].ValueCount = 1;
        Device[deviceCount].SendDataOption = true;
        Device[deviceCount].TimerOption = true;
        Device[deviceCount].GlobalSyncOption = true;
        break;
      }

    case PLUGIN_GET_DEVICENAME:
      {
        string = F(PLUGIN_NAME_251);
        break;
      }

    case PLUGIN_GET_DEVICEVALUENAMES:
      {
        strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_251));
        break;
      }

    case PLUGIN_GET_DEVICEGPIONAMES:
      {
        event->String1 = formatGpioName_output(F("RX PIN"));
        event->String2 = formatGpioName_output(F("TX PIN"));
        break;
      }

    case PLUGIN_INIT:
      {
        int rxPin = CONFIG_PIN1;
        int txPin = CONFIG_PIN2;
        const ESPEasySerialPort port = static_cast<ESPEasySerialPort>(CONFIG_PORT);
		if (P251_Serial != nullptr) {
			delete P251_Serial;
			P251_Serial = nullptr;
        }
		P251_Serial = new ESPeasySerial(port, rxPin, txPin);
        if (P251_Serial == nullptr) {
          break;
        }
        P251_Serial->begin(9600);
        P251_Serial->flush();
        addLog(LOG_LEVEL_INFO, F("A02YYUW: Initialization OK"));
        Plugin_251_init = true;
        success = true;
        break;
      }

    case PLUGIN_READ:
      {
		unsigned char data[4] = {};
		int i = 0;
		unsigned int meassuredDistance = MIN_DISTANCE;
		int meassurementStatus;
		P251_Serial->flush();
		while (!P251_Serial->available() && i < SERIAL_AVAILABLE_CHECK_CICLES) {
			i++;
			delay(SERIAL_AVAILABLE_CHECK_DELAY);
		}
		i = 0;
		while(P251_Serial->available() && i < 4) {
			data[i] = P251_Serial->read();
			i++;
			if (data[0] != SERIAL_HEAD_DATA) {
			  i = 0;
			}
		}
		if (i != 4) {
			meassurementStatus = DistanceSensor_A02YYUW_MEASSUREMENT_STATUS_ERROR_SERIAL;
		} else {
			if (!(((data[0] + data[1] + data[2])& 0x00FF) == data[3])) {
				meassurementStatus = DistanceSensor_A02YYUW_MEASSUREMENT_STATUS_ERROR_CHECK_SUM;
			} else {
				meassuredDistance = ((data[1] << 8) + data[2]);
				if (meassuredDistance < MIN_DISTANCE) {
					meassurementStatus = DistanceSensor_A02YYUW_MEASSUREMENT_STATUS_ERROR_MIN_LIMIT;
				}
				else if (meassuredDistance > MAX_DISTANCE) {
					meassurementStatus = DistanceSensor_A02YYUW_MEASSUREMENT_STATUS_ERROR_MAX_LIMIT;
				} else {
				meassurementStatus = DistanceSensor_A02YYUW_MEASSUREMENT_STATUS_OK;
				}
			}
		}
		if (meassurementStatus == DistanceSensor_A02YYUW_MEASSUREMENT_STATUS_OK) {
			UserVar[event->BaseVarIndex] = (float)meassuredDistance;
			if (loglevelActiveFor(LOG_LEVEL_INFO)) {
			  String log = F("A02YYUW: Distance value = ");
			  log += meassuredDistance;
			  addLog(LOG_LEVEL_INFO, log);
			}
			success = true;
		} else {
			if (loglevelActiveFor(LOG_LEVEL_INFO)) {
				String log = F("A02YYUW: Error status = ");
				log += meassurementStatus;
				addLog(LOG_LEVEL_INFO, log);
			}
			success = false;
		}
        break;
      }
  }
  return success;
}
#endif // USES_P251
Sébastien - espRFLinkMQTT gateway RFLink MQTT on esp

Zodiac69
Normal user
Posts: 85
Joined: 13 Jun 2016, 17:20

Re: Distance Sensor A02YYUW

#9 Post by Zodiac69 » 15 May 2022, 12:47

Hi seb82

Would you mind sharing the bin file for the A02YYUW?

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Distance Sensor A02YYUW

#10 Post by Ath » 15 May 2022, 16:29

I have reserved Plugin ID P134 in the mega branch of ESPEasy repository for this plugin, and will soon start moving this code to the regular ESPEasy builds.

it will take some effort, as it isn't yet multi-instance compatible, and has some delay-related issues that probably need to be addressed.
/Ton (PayPal.me)

Zodiac69
Normal user
Posts: 85
Joined: 13 Jun 2016, 17:20

Re: Distance Sensor A02YYUW

#11 Post by Zodiac69 » 17 May 2022, 22:13

Thank you Ath

I am trying to do a build with A02YYUW included, but i have not done a custom build for some time now, a few years, so i need to get into Platformio as i have not used it before.

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Distance Sensor A02YYUW

#12 Post by Ath » 17 May 2022, 22:25

Then this will help in setting stuff up: https://espeasy.readthedocs.io/en/lates ... ormIO.html
Especially the Starters guide, just scroll a bit down that page :D
/Ton (PayPal.me)

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Distance Sensor A02YYUW

#13 Post by Ath » 01 Aug 2022, 15:50

I've adjusted the above code to current ESPEasy coding standards, and adapted to multi-instance use, so multiple sensors can be connected to a single ESPEasy device, only limited by the number of serial ports (each device requires its own serial port)

This is the PR #4168, and I'd need some help in testing, as I don't own this sensor. It is in the Collection A builds (previous name Test A, that has been changed very recently).
/Ton (PayPal.me)

User avatar
iron
Normal user
Posts: 221
Joined: 24 Sep 2016, 08:37
Location: Greece
Contact:

Re: Distance Sensor A02YYUW

#14 Post by iron » 03 Aug 2022, 15:19

Ath wrote: 01 Aug 2022, 15:50 ... and adapted to multi-instance use, so multiple sensors can be connected to a single ESPEasy device, only limited by the number of serial ports (each device requires its own serial port)...
Meaning 2 instances on 8266 and 3 instances on EPS32, correct ?
-D

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Distance Sensor A02YYUW

#15 Post by Ath » 03 Aug 2022, 15:41

iron wrote: 03 Aug 2022, 15:19 Meaning 2 instances on 8266 and 3 instances on EPS32, correct ?
Yes, and probably more on builds that have the I2C serial support for SC16IS752 based boards included, like by default on ESP32. ;)
/Ton (PayPal.me)

trendkill
Normal user
Posts: 26
Joined: 22 Feb 2022, 09:04

Re: Distance Sensor A02YYUW

#16 Post by trendkill » 31 Aug 2022, 13:14

Hi! I just flashed the ESP_Easy_mega_20220809_collection_A_ESP8266_4M1M.bin file into my Wemos D1 Mini, and I can't find this device in the list. I have two pieces of dfrobot A02YYUW which I would like to use with this single ESP8266 board. Which bin file could I use? Thank you very much.
Ath wrote: 01 Aug 2022, 15:50 I've adjusted the above code to current ESPEasy coding standards, and adapted to multi-instance use, so multiple sensors can be connected to a single ESPEasy device, only limited by the number of serial ports (each device requires its own serial port)

This is the PR #4168, and I'd need some help in testing, as I don't own this sensor. It is in the Collection A builds (previous name Test A, that has been changed very recently).

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Distance Sensor A02YYUW

#17 Post by Ath » 31 Aug 2022, 13:25

trendkill wrote: 31 Aug 2022, 13:14 Hi! I just flashed the ESP_Easy_mega_20220809_collection_A_ESP8266_4M1M.bin file into my Wemos D1 Mini, and I can't find this device in the list.
This plugin is not merged into the regular release yet.
You will have to download a build from the PR, latest build is here, that does include that plugin. You will need to be logged in to Github to be able to download the Binaries zip file that contains a zip file for each build, select the one that has collection_A_ESP8266_4M1M in the name, and you should be good to go.
/Ton (PayPal.me)

trendkill
Normal user
Posts: 26
Joined: 22 Feb 2022, 09:04

Re: Distance Sensor A02YYUW

#18 Post by trendkill » 31 Aug 2022, 14:12

Thank you, got it!
Ath wrote: 31 Aug 2022, 13:25
trendkill wrote: 31 Aug 2022, 13:14 Hi! I just flashed the ESP_Easy_mega_20220809_collection_A_ESP8266_4M1M.bin file into my Wemos D1 Mini, and I can't find this device in the list.
This plugin is not merged into the regular release yet.
You will have to download a build from the PR, latest build is here, that does include that plugin. You will need to be logged in to Github to be able to download the Binaries zip file that contains a zip file for each build, select the one that has collection_A_ESP8266_4M1M in the name, and you should be good to go.

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Distance Sensor A02YYUW

#19 Post by Ath » 31 Aug 2022, 15:46

trendkill wrote: 31 Aug 2022, 14:12 Thank you, got it!
Any feedback is appreciated. You can also, instead of here, write that as a comment in Github, just choose what feels most appropriate to you. ;)
/Ton (PayPal.me)

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Distance Sensor A02YYUW

#20 Post by TD-er » 31 Aug 2022, 15:51

Ath wrote: 31 Aug 2022, 15:46
trendkill wrote: 31 Aug 2022, 14:12 Thank you, got it!
Any feedback is appreciated. You can also, instead of here, write that as a comment in Github, just choose what feels most appropriate to you. ;)
May I propose to use RFC1149?
https://en.wikipedia.org/wiki/IP_over_Avian_Carriers

trendkill
Normal user
Posts: 26
Joined: 22 Feb 2022, 09:04

Re: Distance Sensor A02YYUW

#21 Post by trendkill » 31 Aug 2022, 22:19

Ath wrote: 31 Aug 2022, 15:46
trendkill wrote: 31 Aug 2022, 14:12 Thank you, got it!
Any feedback is appreciated. You can also, instead of here, write that as a comment in Github, just choose what feels most appropriate to you. ;)
I just finished soldering them together. Both sensor works! At least they send the data, I didn't measure yet how accurate they are. I didn't use any pullup resitors or anything else, just wired directly to the Wemos D1 Mini board like this:

SENSOR1
Port: SoftwareSerial

Red wire: VCC 5V
Black wire: GND
White wire (TX): D1 (GPIO5)
Yellow wire (RX): not connected

SENSOR2
Port: HW Serial0 swap

Red wire: VCC 5V
Black wire: GND
White wire (TX): D7 (GPIO13)
Yellow wire (RX): D8 (GPIO15)

I used shielded 4x0.22 alarm cable, shield is connected to GND. Both cable is about 3m long, the signal seems stable.

Thanks for your great work!

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Distance Sensor A02YYUW

#22 Post by Ath » 31 Aug 2022, 22:29

trendkill wrote: 31 Aug 2022, 22:19 ... At least they send the data, I didn't measure yet how accurate they are.
The accuracy isn't determined by the plugin, that's just providing the data that is received ;)
trendkill wrote: 31 Aug 2022, 22:19 I didn't use any pullup resitors or anything else, just wired directly to the Wemos D1 Mini board like this:
Usually, serial connections don't use pull-up resistors, so that should be fine.
Not sure if the sensors will work (reliably) with 3.3V, not all ESP boards handle 5V on the GPIO's nicely (they could produce magic smoke...), but most D1 mini clones seem to handle it fine.
/Ton (PayPal.me)

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Distance Sensor A02YYUW

#23 Post by TD-er » 31 Aug 2022, 23:14

Still... if the TX from the ESP is only 3V3 and the device expects a slightly higher level then it also may work "flaky"

What may be useful however is to add a 500 Ohm resistor (or 470 Ohm, as the actual resistance value isn't that critical) in series with the TX line and/or RX.
This may prevent "ringing" in the signal where a pulse may "bounce" over and over on the cable between device and ESP causing noise.
Such a resistor in series will dampen these ringing effects.

Do this if you experience strange behavior and/or using relatively long cables.

trendkill
Normal user
Posts: 26
Joined: 22 Feb 2022, 09:04

Re: Distance Sensor A02YYUW

#24 Post by trendkill » 01 Sep 2022, 15:53

TD-er wrote: 31 Aug 2022, 23:14 Still... if the TX from the ESP is only 3V3 and the device expects a slightly higher level then it also may work "flaky"

What may be useful however is to add a 500 Ohm resistor (or 470 Ohm, as the actual resistance value isn't that critical) in series with the TX line and/or RX.
This may prevent "ringing" in the signal where a pulse may "bounce" over and over on the cable between device and ESP causing noise.
Such a resistor in series will dampen these ringing effects.

Do this if you experience strange behavior and/or using relatively long cables.
Thanks, noted. They are both in a water tank now, measuring the water level. When the water surface is flat, the measurment seems rock stable. I'll be smarter after a week of operation.

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Distance Sensor A02YYUW

#25 Post by TD-er » 01 Sep 2022, 17:23

Maybe you measure reflections?
A ripple on water may tilt the surface causing a longer path before it returns.
Perhaps taking the shortest sampled distance of a few measurements may give a more stable value?

trendkill
Normal user
Posts: 26
Joined: 22 Feb 2022, 09:04

Re: Distance Sensor A02YYUW

#26 Post by trendkill » 01 Sep 2022, 19:49

Yes, when I load the tank with fresh water, the surface ripples. Therefore the measurments are jumping, but only +- 20-40mm. Whis is correct, the size of the wave. Let's see in the coming days, if there will be any weird stuff.
TD-er wrote: 01 Sep 2022, 17:23 Maybe you measure reflections?
A ripple on water may tilt the surface causing a longer path before it returns.
Perhaps taking the shortest sampled distance of a few measurements may give a more stable value?

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Distance Sensor A02YYUW

#27 Post by TD-er » 01 Sep 2022, 21:36

I can't imagine it is "only 20 - 40 mm"
Since the surface is tilted, you will reflect your signal in a different direction, which may bounce off the wall of the container, back to the ripple and back to your sensor.
So it will be causing some "echo" or "reverb" regardless whether you're measuring distance using ultrasonic sensor or laser.

trendkill
Normal user
Posts: 26
Joined: 22 Feb 2022, 09:04

Re: Distance Sensor A02YYUW

#28 Post by trendkill » 12 Sep 2022, 23:09

Using it since then, rock solid, very accurate. You right, when the water ripples, it's more than 20-40 mm, my bad. But not crazy numbers. I measure the water level every hour, except when filling up the tank, therefore I have no false measurement.
TD-er wrote: 01 Sep 2022, 21:36 I can't imagine it is "only 20 - 40 mm"
Since the surface is tilted, you will reflect your signal in a different direction, which may bounce off the wall of the container, back to the ripple and back to your sensor.
So it will be causing some "echo" or "reverb" regardless whether you're measuring distance using ultrasonic sensor or laser.

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Distance Sensor A02YYUW

#29 Post by TD-er » 12 Sep 2022, 23:21

Great!
Thanks for reporting back.
I just merged the PR into the main branch, so it will be included in the next build.

Post Reply

Who is online

Users browsing this forum: No registered users and 31 guests