Serial.readString() not reading

I'm trying to communicate between an Arduino Uno and an ESP8266 wifi chip via serial connection. Currently I'm having the ESP print the line "Serial Connection established" to Serial and having the Arduino read this via SoftwareSerial and print it to it's own Serial connection for me to see.

This does not work when using SoftwareSerial.readString(); I get no output. When using SoftwareSerial.read() I get the following string of numbers which I don't think I'm supposed to get?
83 101 114 105 97 108 32 67 111 110 110 101 99 116 105 111 110 32 101 115 116 97 98 108 105 115 104 101 100 13 10

Any idea what's going wrong? Not entirely sure what I should be getting from SoftwareSerial.read() either so any resources on how to interpret that would be appreciated.

My code is:

On the ESP8266:

void setup() {
  Serial.begin(115200);
}

void loop() {
  Serial.println("Serial Connection established");
  delay(1000);
}

On the Arduino:

#include <SoftwareSerial.h>

SoftwareSerial espSerial(12, 13); // RX, TX

void setup() {
  Serial.begin(9600);
  espSerial.begin(115200);
}

void loop() {
  if (espSerial.available()) {
    Serial.println(espSerial.readString());
  }
}

Have you tried converting those values to ASCII?
(I only ask, because you didn't make it clear what you expected)
Hint: Serial.read returns an int, so that's the type that .print prints

Convert those values to ASCII :thinking:

Sketch:

int x[] = {
  83, 101, 114, 105, 97, 108, 32, 67, 111, 110, 110, 101, 
  99, 116, 105, 111, 110, 32, 101, 115, 116, 97, 98, 108, 
  105, 115, 104, 101, 100, 13, 10 };

void setup() 
{
  Serial.begin(115200);
  for( int i=0; i<sizeof(x)/sizeof(int); i++)
  {
    Serial.print(char(x[i]));
  }
}

void loop() {}

Output:

Serial Connection established

Ok thanks that means I'm successfully transmitting my message through serial :smile: .

Unfortunately it doesn't help with getting readString() to work. I could do everything one char at a time I suppose but it seems a bit easier to work in Strings.

Plenty of material on simple serial handling available.

...until it blows up in your face, and you wish you'd done it with simple C arrays in the first place.

1 Like

an arduino is unable to reliably receive characters with software-serial at a baudrate of 115200
reduce to a baudrate to 19200 and try again

If you want to use Strings use the SafeString-library
The SafeString-library has even functions for non-blocking receiving included

https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/docs/html/index.html

best regards Stefan

1 Like

I recommend this tutorial: Serial Input Basics - updated

Here's an example of serialStr. Non blocking, hands you back a complete c string when its done to any function you name. Should work with software serial.

-jim lee

1 Like

Thanks! This was super helpful

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.