ESP8266 SENDING WRONG DATA

This project displays the serial communication between esp8266 esp-01 and arduino uno. I uploaded the following code to esp8266

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

void loop() {
if(Serial.available())
{
if(Serial.read()=='1')
Serial.write(5);
}
}

the connections are:-

Arduino ESP8266
Rx-------------->Rx
TX--------------->TX
Gnd,RESET-------------->Gnd,GP0
3.3v--------------->CHPD,VCC

The code I uploaded to arduino:-

#include <SoftwareSerial.h>
SoftwareSerial esp(4,3);
void setup() {
Serial.begin(9600);
esp.begin(9600);
}
void loop() {
esp.write(1);
Serial.println(esp.read());
delay(1000);
}

The Connections are:-

Arduino ESP82666
D4--------->RX
D3--------->TX
GND-------->GND
3.3v--------->Vcc,CHPD

The Expected output was 5 but in the serial monitor I am getting -1 as output.

RX to TX

and you want
Serial.print(5);

and
if (Serial.available()) {
in esp8266 sketch

Serial.print() is there in the code of the arduino because it is connected to the serial monitor. Esp8266 is not connected directly to the serial monitor so why should i put Serial.print() in the esp8266 code?

santam:
Serial.print() is there in the code of the arduino because it is connected to the serial monitor. Esp8266 is not connected directly to the serial monitor so why should i put Serial.print() in the esp8266 code?

you have write(), not print(). write(5) will print a character with ascii code 5. it is a not visible control character. print(5) will print 5 as text.

santam:
This project displays the serial communication between esp8266 esp-01 and arduino uno. I uploaded the following code to esp8266

the connections are:-

Arduino ESP8266
Rx-------------->Rx
TX--------------->TX
Gnd,RESET-------------->Gnd,GP0
3.3v--------------->CHPD,VCC

For serial connections you connect the receiver port of one to the transmitter of the other, your connections should look like
the connections are:-

Arduino ESP8266
Tx-------------->Rx
RX--------------->TX
Gnd,RESET-------------->Gnd,GP0
3.3v--------------->CHPD,VCC