I'm trying to send data from esp32 to Arduino mega and get it displayed on the serial monitor. Does anyone know what mistake am i doing from the code below:
I'm connecting pin 17/TX of esp32 to pin 19/RX1 of Arduino mega and pin16/RX of esp32 to pin 18/TX1 of arduino mega.
The problem is that only "Message Received: " gets printed and randomly blank square characters gets printed after it. I've checked the baud rate on serial monitor it's set to 9600, so I believe the issue is something else, tried other TX and RX pins of mega but the issue is still the same.
Do you have the ground connected between the Mega and the ESP32?
Also, be careful, the Tx output of the Mega will produce a 5V signal, the ESP32 operates at 3.3V.
yes they share common ground. I'm able to transfer data from Mega to esp32 (but at 1500s delay@9600 baudrate and minimum 1000s delay@115200 baudrate), so I think data transfer and wiring is fine, but my objective is to send data from ESP32 to Mega, so the problem is something with my programming I guess.
If you use serial.readString without adjusting the timeout you need this delay(1000) because the default timeout of function readString() is 1000 milliseconds.
This means using readString() seems to be easy but has it's own difficulties that are hidden under the hood.
I recommend that you learn the things explained in the serial input basics
to be able to do fast and reliable serial receiving
and put a logic level-shifter between ESP32 and Mega 2560
So now I just have to read data in different format to remove the delay. Actually the data which I'll receive from transmitter esp will be joystick inputs i.e x and y axis which I have reduced to 0-255 bits and a switch, which just gives 0 or 1 digital signal, so basically the final data i need in mega is in integer format for further processing.
If it is a single byte you could use Serial.read() which does exactly that reading a single byte.
If it is more than a single byte I recommend that you use sending your data with a startmarker-byte and an endmarker-byte
For example "<" as startmarker and ">" as endmarker
and then use the serial receive with start/endmarker-function like shown in the serial input basiscs tutorial linked above
Thanks for the update. So I followed the link above and tried to implement it the same way but I'm not gertting any output on Mega side. Here's the code for both sides:
//ESP32 SIDE CODE
#define RXp2 16
#define TXp2 17
int xAxis = 120;
int yAxis = 160;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial2.begin(115200, SERIAL_8N1, RXp2, TXp2);
}
void loop() {
String message = "<" + String(xAxis) + "," + String(yAxis) + ">";
Serial2.println(message);
Serial.println(message);
//Serial2.println("123");
delay(1500);
}
On ESP32 side, the output is "<120,160>" (without quotes). Which is as expected, so now I think there's something wrong with the Mega side code. Could you please check and see if you find anything? Another possibility could be that I'm sending the data incorrectly.
Main objective: I want to receive joystick <x-axis, y-axis> data in Mega and use it to control an RC car.
if you fill 1digit-numbers with two leading zeros 001,002,003..009
and
2digit-numbers with one zero 010..011.012....099
The position of the comma is always the same.
example-data
123,987
So receivedChars[0]..receivedChars[2] contains "123"
and
So receivedChars[4]..receivedChars[6] contains "987"
you store these characters in a new variable and then use function atoi()
to convert them to integers
myXChars[0] = receivedChars[0];
myXChars[1] = receivedChars[1];
myXChars[2] = receivedChars[2];
myXChars[3] = 0; // VERY IMPORTANT zero-terminating of the c_string
myYChars[0] = receivedChars[4];
myYChars[1] = receivedChars[5];
myYChars[2] = receivedChars[6];
myYChars[3] = 0; // VERY IMPORTANT zero-terminating of the c_string
myXInt = atoi(myXChars);
myYint = atoi(myYChars);
on three bytes a for-loop has the same amount of typing
for (byte i = 0; i < 3; i++
myYChars[i] = receivedChars[i + 4];
myYChars[3] = 0;
got it working. I used pointers instead, but it does the same job. I have one more question. I have already written and tested the pid control part of the code( speed control of dc motor), and it is working fine. So I used to declare a fixed setpoint (rpm) in the setup function and used to give starting pwm of 50% but now I want to set the setpoint = xValue of joystick which is received. I tried to do this inside loop function but it doesnt work...motor starts at 127pwm(50%) and quickly comes to a stop then I directly substitued "xValue" inplace of "setpoint" directly inside the PIDcalculation function, but it didn't work.
This is my current problem so should I post this querry as a new topic or is it okay to continue in this thread? If it's okay then does anyone have any idea of what I should do, I'm pretty sure it's a programming mistake.
could be both. Continuiing as your thread-title is very unspecific.
or
starting a new thread with a really precise and specific title.
In the new thread post a link to this thread.
In any case post your complete sketch instead of some words.