Hello there,
I'm using UART between ESP32 and ARDUINO UNO and I'm trying to communicate as fast as possible between them.
Here's the idea:
ESP32 sends a float requesting a value to UNO.
UNO receives this value, and through a switch...case , returns the value asked (in a float) to ESP32.
However, this is taking 2 seconds to complete, which is A LOT OF TIME. With 38400 baud rate, it should be doing this pretty fast. What's happening?
HERE'S THE ESP32 CODE:
#include <HardwareSerial.h>
float send_float;
unsigned long myTimer;
float rec_float;
void setup(){
Serial2.begin(38400,SERIAL_8N1,22,23);
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop(){
send_float = 3.00;
Serial2.print(send_float);
myTimer = millis();
while(!Serial2.available()){
if(millis()-myTimer > 3000){
Serial.println("TIMEOUT REACHED");
break;
}
}
rec_float = Serial2.readString().toFloat();
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); //blink LED to identify new message
Serial.println(rec_float); //print received float on MONITOR
}
HERE'S ARDUINO UNO CODE:
#define RX 5
#define TX 7
float soilH = 79.00;
float soilT = 18.32;
float airT = 19.29;
void setup() {
Serial.begin(38400);
}
void loop() {
if(Serial.available()){
float recieved = Serial.parseFloat();
int my_switch = recieved;
switch(my_switch){
case 1:
Serial.print(soilH);
break;
case 2:
Serial.print(soilT);
break;
case 3:
Serial.print(airT);
break;
default: //command not found
Serial.print(404);
break;
}
}
}
Any clue?
Thank you in advanced!