bluetooth terminal adds two characters

//#define serial


String readStr = "";
char buf;
int flag = 0;
int ledState = 0;
int red = 6;

#ifndef serial
 #include <SoftwareSerial.h>
 SoftwareSerial BTSerial(3, 2);
#endif

void setup() {
  // put your setup code here, to run once
  
#ifndef serial
 BTSerial.begin(9600);
 BTSerial.println("Commands:   'led on'    'led off'");
#else
 Serial.begin(9600);
 while(!Serial);
 Serial.println("Commands:   'led on'    'led off'");
#endif

pinMode(red, OUTPUT);
}

void loop() {
  
  readSerial();

  if ((readStr != "led on" && readStr != "led off" && readStr != "") && (flag == 0)) {
    #ifdef serial 
     Serial.println("invalid command:");
     Serial.println(readStr);
    #else
     BTSerial.println("invalid command:");
     BTSerial.println(readStr);
    #endif
     readStr = "";
     flag = 1;
  }

  if ((readStr == "led on" || ledState == 1) && (flag == 0)) {
    flag = 1;
    ledState = 1;
    readStr = "";
    #ifdef serial
     Serial.println("brightness (soft - normal - hard)");
     while(Serial.available() <= 0) { }
    #else
     BTSerial.println("brightness (soft - normal - hard)");
     while(BTSerial.available() <= 0) { } 
    #endif
    readSerial();
    if (readStr == "soft") {
      analogWrite(red, 25);
      #ifdef serial
       Serial.println("brightness is soft");
      #else
       BTSerial.println("brightness is soft");
      #endif
      readStr = "";
    }
    if (readStr == "normal") {
      analogWrite(red, 127);
      #ifdef serial
       Serial.println("brightness is normal");
      #else
       BTSerial.println("brightness is normal");
      #endif
      readStr = "";
    }
    if (readStr == "hard") {
      analogWrite(red, 255);
      #ifdef serial
       Serial.println("brightness is hard");
      #else
       BTSerial.println("brightness is hard");
      #endif
      readStr = "";
    }
  }

  if (readStr == "led off" && flag == 0) {
    flag = 1;
    ledState = 0;
    #ifdef serial
     Serial.println("led is off");
    #else
     BTSerial.println("led is off");
    #endif
    readStr = "";
    digitalWrite(red, LOW);
  }
}

void readSerial () {

#ifndef serial  
  if (BTSerial.available() > 0) {
   while (BTSerial.available() > 0) {
     delay(2);
     buf = BTSerial.read();
     readStr += buf;    
   }
   flag = 0;
   BTSerial.println(readStr.length());
  } 
#else
  if (Serial.available() > 0) {
   while (Serial.available() > 0) {
     delay(2);
     buf = Serial.read();
     readStr += buf;
   }
   flag = 0;
   Serial.println(readStr.length());
  }
#endif
 }

what this does is it waits for an input on the bluetooth terminal and stores it in a string.

I use the android app: "Serial Bluetooth Terminal"

But here comes the problem. If i used the code on the Serial monitor, it works just fine. But on the app, it keeps saying "invalid command". I added some code to see how many characters there are in the string and I found out there are always two characters added.
I connected it to a led display and get weird symbols at the end of the word.

Does anybody know how to fix this?

Thanks in advance.

println adds a cr/lf to the string.

But if i use the Serial monitor (#define serial) everything works fine and this piece of code is exactly the same (apart from "Serial" instead of "BTSerial").

And is cr/If the null-terninator (im not sure what it is)?

println adds a cr/lf (0x0A and 0x0D) plus a zero terminator.

If you want only the zero terminator, use print instead.

buf = Serial.read();
readStr += buf;
if (readStr == "soft")

When you use the String object with code like this, the bluetooth serial terminal app needs to use the same terminators that you used when developing the Arduino code. Or to reverse the thinking, the development of the sketch should use the same terminators as the Bluetooth terminal app.

There should be a settings screen in the Bluetooth Terminal App where you can set the line endings for send and receive.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

Now I get it. Thanks!