1
votes

I got this shield below which fits great with my Arduino Uno. Moreover, I have managed to upload code to the shield with a serial adapter and send/receive UDP messages. As the picture shows this shield goes right on top of the Arduino UNO.

My ESP8266 Shield

The problem is that the communication between Arduino and the Shield is very slow.

For example, I use the code below to Arduino to write something using Serial (115200).

void loop() {
  writeString("Hello!");
  delay(1000);
}

Then I use a simple code to the ESP8266 shield to read the data from Arduino and send it via UDP (writeString is just a simple converter).

Udp.beginPacket(ip, localUdpPort);
writeString(Serial.readString());
Udp.endPacket();

void writeString(String stringData) {
  for (int i = 0; i < stringData.length(); i++) {
    Serial.write(stringData[i]);
    // Push each char 1 by 1 on each loop pass
  }
}

It works fine, the "Hello!" string is read from the ESP8266 shield and sent using UDP. The problem is that if I put anything below 1,000 ms of delay in the Arduino, the ESP shield does not read anything, which is strange considering that the shield is on the top of the Arduino with no restrictions between serial communication.

1
Regarding this writeString(), you might be interested to know about Serial.print().per1234

1 Answers

2
votes

From https://www.arduino.cc/en/Serial/ReadString:

Serial.readString() reads characters from the serial buffer into a string. The function terminates if it times out (see setTimeout()).

From https://www.arduino.cc/en/Serial/SetTimeout:

It defaults to 1000 milliseconds.

So because you use this Serial.readString() thing with the default timeout there will always be a delay of 1000 ms before the string is received.

If you insist on messing with String, there is a similar function: Serial.readStringUntil():

The function terminates if the terminator character is detected or it times out

So you have it detect the terminating character of the string:

Serial.readStringUntil('\n');

In this case the timeout is merely a safeguard to keep the code from hanging if for some reason the terminating character should not arrive but this should never happen if things are working correctly so there are no unnecessary delays.

The trouble with that is your current code doesn't send a terminator but that's easy enough to fix.

I recommend that you consider using the more efficient and safer string (char array) rather than String. There is an equivalent function: Serial.readBytesUntil().