I have an Arduino UNO connected to ESP32 through voltage divider with TX0 from Uno to RX2 to ESP32. My goal is to send data from my Uno to my ESP32 through HardwareSerial. Here's my code for the Uno:
long randNumber;
String myString;
void setup() {
Serial.begin(9600);
}
void loop() {
randNumber = random(40);
myString = String(randNumber);
Serial.println(myString);
delay(2000);
}
Here I generate a random number, cast it to string and then send it serial through Serial.println(myString);
My ESP32 code:
#define RXD2 16
#define TXD2 -1
HardwareSerial Serial2(1); // Use UART channel 1
void setup() {
Serial.begin(9600);
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
}
void loop() {
if(Serial2.available() >= 1){
Serial.println("ESP32");
Serial.print(Serial2.read());
}
else {
Serial.print("Not sending");
}
delay(2000);
}
After launching Uno and the ESP32 after a second or two, I got the following output:
UNO ESP32
7 ESP32
9 51ESP32
33 56ESP32
18 13ESP32
10 10ESP32
32 49ESP32
24 54ESP32
38 13ESP32
3 10ESP32
As you may notice, the numbers are not the same. Even if I unplug the Uno from electricity, the ESP32 still prints numbers and that should not be the case because it should print "Not sending". What am I doing wrong?