Code for arduino using A02YYW to read on internet

Hi, I wish to read the level of the water tanks on my property remotely, I was able to do on site via my router using a using an HC-SR04. However, water damage to the sensor prompted me to replace the sensor with an A02YYUW . I have used the sketch you supplied on your web page and it works ok.
The problem is I cannot get the sensor to work on an IP address! It will work on the serial monitor ok, but I cannot get it to print to my PC. I am a novice at this coding business however, despite my age I am determined to get it going. Are you able to suggest any ideas or at least point me in the right direction. I have included the code I wrote initially (with some help from online research) for the HC-SR04
Many Thanks

[//Measurement of distance to water level in Shed Tanks

#include <SPI.h>

//MAC Address
byte mac [] = {0xDE,0xAD,0xBE,0xEF,0xFE,0xED};
IPAddress ip(192, 168, 0, 209  );
EthernetServer server(80);

//Define connections to the sensor
int pinRX = 6;
int pinTX = 5;

//Array to store incoming serial data
unsigned char data_buffer [4] = {0};

// Integer to store distance
int distance = 0;

// Variable to hold checksum
unsigned char CS;

// Object to represent software serial port
SoftwareSerial mySerial(pinRX, pinTX);

void setup() {
  // Set up serial monitor
  Ethernet.begin (mac);
  server.begin ();
  server.println ("Distance to water level");
  Serial.print ("Server started on: ");
  Serial.println (Ethernet.localIP () );
  //Set up software serial port


void loop() {
  // Run if data available
  if (mySerial.available() > 0) {

    // Check for packet header character 0xff
    if ( == 0xff) {

      //Insert header into array
      data_buffer[0] = 0xff;

      // Read remaining 3 characters of data and insert into array
      for (int i = 1; i < 4; i++) {
        data_buffer[i] =;

        //Compute checksum
        CS = data_buffer[0] + data_buffer[1] + data_buffer[2];

        //If checksum is valid compose distance from data

        if (data_buffer[3] == CS) {
          distance = (data_buffer[1] << 8) + data_buffer[2];

          //Print to serial monitor
          Serial.print("distance: ");
          Serial.println(" mm");
         // Listen for incoming clients
         EthernetClient client = server.available ();
         if (client)
         while (client. connected () );

delay (1000);

You haven’t indicated what hardware you are using - is this an Arduino of some form with an ethernet shield, or is it an ESP (eg, ESP32 or ESP8266) or something else?

Are you getting the correct IP printed at the console? If so, what exactly happens when you connect to that IP? The message should indicate that the address was located.

Are you getting the initial message at the client (“Distance to water level”)? If so then you all you need is to add the line in the loop to actually send the data:

server.println (distance);

(immediately before the delay). If you aren’t getting the initial message from the setup then you don’t have a client/server connection. Adding debug prompts to console throughout the code would help in tracking down that problem.


I could be wrong, but the HC-SR04 and A02YYUW outputs are very different.

Send a 10us pulse on the trigger pin and time how long before the echo pin goes high. From that you can calculate the distance.

Outputs a serial data stream on the TX pin related to the distance measured as two bytes. The RX pin level determines the response time and the stability of the data.

The code you have shown is similar to the code from the DFRobot wiki page.
With the attempted addition of an Ethernet client.

The IP connection is a little confusing to me. You don’t seem to be sending anything to the Ethernet server. I note @Jeff105671 has replied while I was writing with the same. I agree with his advice.


1 Like

Hi Mick,

Welcome to the forum! I think James is on the money here with:

The Arduino example suggests a server.write(dataToSend) when a client connects. Right now, your code looks to be waiting for a client, then waiting while doing nothing for as long as it is connected, rather than sending a response.

Keen to see how you go once you try out a write()!

Hi James, Hi Jeff. It seems the replies I sent last week did not get through.
I have had some success with the code following your suggestions and can now print to the client IP address. You were correct in your advice stating I was not sending anything to the server. I have cut and pasted some parts of the earlier sketch to the one using the A02YYUW device. I can now read the water level in my tanks. I have put it on to the web where I can monitor things when I’m not at home. Many thanks to you all for taking the time to assist me.