Arduino send a data to ESP8266-01

These codes are used to send a data from ESP8266-01 to Arduino.

void setup() {
  // put your setup code here, to run once:
  Serial.begin( 115200 );
  Serial1.begin( 115200 );
  Serial.println( "<MEGA is Ready>" );
}

void loop() {
  // put your main code here, to run repeatedly:
  // RECEIVER
  if( Serial1.available() ){
    Serial.println( "Received: " + Serial1.readString() ); // RECEIVE FROM ESP8266
  }

  // SENDER 
  if( Serial.available() ){
    Serial1.write( 'P' ); // send to ESP8266
  }
}
void setup() {
  // put your setup code here, to run once:
  Serial.begin( 115200 );
  Serial.println( "<ESP8266 is Ready>" );
}

void loop() {
  delay( 1000 );
  Serial.write( "[MEGA] Hey!" ) ;
}

It works...


But I don't know how to get or receive a data from Arduino to ESP-01.

It works...

How do you know it works? The ESP8266 code never reads anything.

PaulS:
How do you know it works?

When the ArduinoMega receive something.. It will display the message "[MEGA] Hey" every 1 sec.

PaulS:
The ESP8266 code never reads anything.

So it means the ESP-01 can't read the Serial Data from Arduino?

When the ArduinoMega receive something.. It will display the message "[MEGA] Hey" every 1 sec.

The second sentence in your reply is correct but where is the code that checks that the Mega has received something ?

UKHeliBob:
The second sentence in your reply is correct but where is the code that checks that the Mega has received something ?

Using this code to find out if there is data that has arrived or not

void setup() {
  // put your setup code here, to run once:
  Serial.begin( 115200 );
  Serial1.begin( 115200 );
  Serial.println( "<MEGA is Ready>" );
}

void loop() {
  // put your main code here, to run repeatedly:
  // RECEIVER
  if( Serial1.available() ){
    Serial.println( "Received: " + Serial1.readString() ); // RECEIVE FROM ESP8266
  }

  // SENDER 
  if( Serial.available() ){
    Serial1.write( 'P' ); // send to ESP8266
  }
}

When the ArduinoMega receive something.. It will display the message "[MEGA] Hey" every 1 sec.

The ESP8266 code sends "[MEGA] Hey" once a second. It does that regardless of whether or not it has received anything, and without knowing whether the Mega is receiving something from the PC.

So, that is not an answer to the question.

So it means the ESP-01 can't read the Serial Data from Arduino?

No, that is not true. The ESP CAN read serial data from the Arduino.

No, that is not true. The ESP CAN read serial data from the Arduino.

How?, can you give me a sample code?

NELMAN:
How?, can you give me a sample code?

If you send data to the ESP using Serial.print(), doesn't it seem obvious that you'd use Serial.available() and Serial.read() (or the software serial equivalents) to determine if there is anything to read, and to read it?