Can't send data to ESP8266 via arduino

I have an ESP8266-01 and I want to send sensor data from Arduino to ESP8266 via serial communication but the data I receive is not correct, I tried changing baud rates but no luck, here is the code

Code for ESP8266:


#include <SoftwareSerial.h>

void setup() {
// put your setup code here, to run once:
Serial.begin(38400);

}

void loop() {
// put your main code here, to run repeatedly:
while(Serial.available()) {
Serial.println("yes");
Serial.println(Serial.read());
}
delay(5);
}


Code for Arduino


#include <SoftwareSerial.h>

SoftwareSerial esp(1, 0);
String str;
void setup(){
Serial.begin(38400);
esp.begin(38400);
delay(2000);
}

void loop() {
// put your main code here, to run repeatedly:
str = String("Hi there");
esp.println(str);
esp.println("hi there");
delay(1000);

}


Here is the serial monitor:

Serial monitor showing numbers instead of "hi there":
(https://i.stack.imgur.com/mVd11.png)
The wiring connections are correct.

You cannot use pins 0 and 1 for software serial on arduino since that is where the hardware serial is.

Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

On Arduino-Unos software-serial works only reliable up to 19200 baud.
38400 is a bit fast for software-serial.
And you have to use other pins than IO-pin 0 and 1 because these are the pins that were used by serial (the standard serial interface which is connected to your USB-cable)

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

Have you checked the return type of Serial.read()? I think it's probably a 'byte'. So you're reading exactly one byte, then print 'yes', read a byte again, etc. In the meantime, your Arduino is probably experiencing timing issues since it can't keep up with printing and reading soft-serial at the same time.

Anyway, as always when people try to knit together an Arduino and an ESP board, my question is: what limitation on the ESP8266 do you think you have that justifies keeping an Arduino in the system as well? Not sure what kind of Arduino you're using, but often people even use a Nano, UNO or something else that's completely overpowered by the ESP (even the old 8266!) without realizing they're bogged down by the complexity of having an inferior microcontroller in the circuit for no good reason at all.

Have you taken into account that most Arduinos work at 5V whilst the ES8266 works at 3.3V ?

In addition, do the two systems have a common GND connection ?

Many people struggle with trying to link the ESP8266-01 to an Arduino.

Is the Arduino doing something which can not be done on a Node MCU or Wemos D1 esp8266 development board. They have USB connectors and can run with the Arduino IDE. They are not expensive.

The ESP32 development board is another good choice to replace the Arduino/8266-01 combination.