user69580: I'm trying to send a string from an ESP32 to an Arduino. I'm using a level shifter, where the Uno is now the Mega (since I couldn't get the Uno to work).

RX0 is now RX1, connected to UART2 of ESP32.
// Master sender ESP32

#include <HardwareSerial.h>
 
void setup() {
  // Serial.begin(115200);
  Serial2.begin(9600, SERIAL_8N1, 16, 17);
  delay(100);
}
    
void loop() {
  String shape = "1,2,3";
  Serial2.println(shape);
  delay(500);
}


//Receiver Mega

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial1.begin(9600);
  Serial.begin(19200);
  delay(100);
}

void loop() {
  if (Serial1.available()) {
    String received = "";
    received = Serial1.readString();
    Serial.println(received);
  }
}

Is there something in either sketch that should be changed?

user69580: readStringUntil('\n')
//Receiver Mega

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial1.begin(9600);
  Serial.begin(19200);
  delay(100);
}

void loop() {
  if (Serial1.available()) {
    String received = "";
    received = Serial1.readStringUntil('\n');
    Serial.println(received);
  }
}

It's about how Serial.readString() works: it reads from the serial port forever in this case. It stops reading if the serial interface is given a time-out. There are two possibilities:

use readStringUntil() on the receiver
call mySerial.setTimeout(300); (from setup()) to set a 300ms (for instance, as long as it's significantly less than 1000) time out on the receiver — it defaults to one second!


69580: <p>I'm trying to send a string from an ESP32 to an Arduino. I'm using a level shifter, where the Uno is now the Mega (since I couldn't get the Uno to work).</p>
<p><a href="https://i.stack.imgur.com/OhNYP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OhNYP.png" alt="enter image description here" /></a></p>
<p>RX0 is now RX1, connected to UART2 of ESP32.</p>
<pre class="lang-cpp prettyprint-override"><code>// Master sender ESP32

#include &lt;HardwareSerial.h&gt;
 
void setup() {
  // Serial.begin(115200);
  Serial2.begin(9600, SERIAL_8N1, 16, 17);
  delay(100);
}
    
void loop() {
  String shape = &quot;1,2,3&quot;;
  Serial2.println(shape);
  delay(500);
}
</code></pre>
<hr />
<pre class="lang-cpp prettyprint-override"><code>//Receiver Mega

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial1.begin(9600);
  Serial.begin(19200);
  delay(100);
}

void loop() {
  if (Serial1.available()) {
    String received = &quot;&quot;;
    received = Serial1.readString();
    Serial.println(received);
  }
}
</code></pre>
<p>Is there something in either sketch that should be changed?</p>
 69580: <p>readString<strong>Until</strong>('\n')</p>
<pre><code>//Receiver Mega

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial1.begin(9600);
  Serial.begin(19200);
  delay(100);
}

void loop() {
  if (Serial1.available()) {
    String received = &quot;&quot;;
    received = Serial1.readStringUntil('\n');
    Serial.println(received);
  }
}
</code></pre>
<p>It's about how <code>Serial.readString()</code> works: it reads from the serial port forever in this case. It stops reading if the serial interface is given a time-out. There are two possibilities:</p>
<ul>
<li>use <code>readStringUntil()</code> on the receiver</li>
<li>call <code>mySerial.setTimeout(300);</code> (from <code>setup()</code>) to set a 300ms (for instance, as long as it's significantly less than 1000) time out on the receiver — it defaults to one second!</li>
</ul>
