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);
}
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
press Ctrl-T for autoformatting your code
do a rightclick with the mouse and choose "copy for forum"
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.
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.
As for the numbers in leu of the data the you expected. Try reversing the two wires that you are using for the Serial communications. I have this working correctly using the following:
All baud rates are set to 9600
SoftwareSerial esp(6, 7); // on the Arduino Mega 2560
Pin 6 wire from the Mega 2560 is on RX of the ESP8266
Pin 7 wire from the Mega 2560 is on TX of the ESP8266