Hi all.
I need help. I am trying to send data via UART from an ESP32 to an arduino mega. I am using the Serial2 pins (16 & 17) on both modules. I am using a level shifter too. The wiring is correct, i.e. ESP32 TX goes to Mega RX.
The problem that I am facing is that the arduino receives some data but that data is very random. When I disconnect the EP32, the arduino continues to display this random data until I reset it, then it shows that it is no longer receiving data, upon reconnecting the ESP32 the arduino picks up (the random data) again. Below is the code I use for the sender (ESP32):
[code]
#define RXp2 16
#define TXp2 17
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial2.begin(115200, SERIAL_8N1, RXp2, TXp2);
}
void loop() {
for (int i =0; i<301; i++)
{
Serial2.println(i);
//Serial.println(Serial2.readString());
Serial.print("sent packet ");Serial.println(i);
delay(500);
}
}
[/code]
Below is the code I use for the Receiver (Mega):
#define RXp2 17
#define TXp2 16
String incomingByte;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial2.begin(115200);
}
void loop() {
if (Serial2.available())
{
incomingByte=Serial2.read();
Serial.print("Received: ");
Serial.println(incomingByte);
delay(500);
}
else
{
Serial.println("Nothing in the buffer... still waiting to receive...");
delay (5000);
}
}
@UKHeliBob Thank you much! your help is appreciated.Great that you point out that there is no need for the delay on the RX code. I did it subconciously. I have updated the code to include the received value as characters. I am now receiving everything, however in a format that I still have to work on to be able to discriminate the data. Here is a paste of how 6 numbers (268-273) have been received, I unfortunately have troubles with uploading a snapshot of the Serial monitor:
Nothing in the buffer... still waiting to receive...
Received: 2
Received: 6
Received: 8
Received:
Received:
Received: 2
Received: 6
Received: 9
Received:
Received:
Received: 2
Received: 7
Received: 0
Received:
Received:
Received: 2
Received: 7
Received: 1
Received:
Received:
Received: 2
Received: 7
Received: 2
Received:
Received:
Received: 2
Received: 7
Received: 3
Received:
I suspect this has to do with how the received data is stored in the buffer. I am yet to go and read more on that so that i can build structures that can collate this data to use it as numbers.
A huge thank you to @UKHeliBob and @horace for their contributions, I am able to end data from the ESP32 to the MEGA via UART. The final sample code that works is here:
ESP UART SENDER
#define RXp2 16
#define TXp2 17
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial2.begin(115200, SERIAL_8N1, RXp2, TXp2);
}
void loop() {
for (int i =0; i<301; i++)
{
Serial2.println(i);
//Serial.println(Serial2.readString());
Serial.print("sent packet ");Serial.println(i);
delay(500);
}
}
MEGA UART RECEIVER
#define RXp2 17
#define TXp2 16
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial2.begin(115200);
}
void loop() {
char data[100]={0};
if (Serial2.available())
{
Serial2.readBytesUntil('\n',data,100);
Serial.print("Received: ");
Serial.println(data);
}
}