Serial receives 3 commands instead of 1

Hi, I’m trying to use an HC-06 module but I’m having a weird behaviour.

Standard version:

char serialValue;

void setup() {
  Serial.begin(9600);
  while(!Serial){}
}

void loop() {
  if (Serial.available() > 0) {
    serialValue = Serial.read();
    if (serialValue == 'H') {
      Serial.println("ON");
    } else if (serialValue == 'L') {
      Serial.println("OFF");
    } else {
      Serial.println("NOT VALID");
    }
  }
}

SoftwareSerial version:

#include <SoftwareSerial.h>

#define BT_RX 10
#define BT_TX 11
SoftwareSerial bluetooth(BT_RX, BT_TX);

char serialValue;

void setup() {
  Serial.begin(9600);
  while(!Serial){}
  bluetooth.begin(9600);
}

void loop() {
  if (bluetooth.available() > 0) {
    serialValue = bluetooth.read();
    if (serialValue == 'H') {
      Serial.println("ON");
    } else if (serialValue == 'L') {
      Serial.println("OFF");
    } else {
      Serial.println("NOT VALID");
    }
  }
}

Using both codes, I get the same results.

When I send a command from my mobile, the Serial Monitor shows this (i.e. sending ‘H’):

ON
NOT VALID
NOT VALID

If I send any other character than ‘H’ or ‘L’, the Serial Monitor shows this:

NOT VALID
NOT VALID
NOT VALID

And when I disconnect from the mobile it shows ‘NOT VALID’ 19 times.

What I’m I missing here?

carriage return, line feed.

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

...R

Yes! I was using a Android Bluetooth Terminal that I didn't knew it was sending the CRLF after each command. Now I did my own Android application and works perfect. Thanks a lot!

Now the last mistery is what is being send when I disconnect from the socket and the stream that end up being a 'NOT VALID' 19 times.

Android code

private void closeBluetooth() {
    try {
        if (bluetoothSocket != null) {
            bluetoothSocket.close();
        }
        if (outputStream != null) {
            outputStream.close();
        }
    } catch (IOException e) {
        Log.i("Bluetooth", e.getMessage(), e);
    }
}

This is the debugged with the 'bluetooth.available()' value each time (it is always the same).

0 NOT VALID
1 NOT VALID
2 NOT VALID
4 NOT VALID
5 NOT VALID
6 NOT VALID
8 NOT VALID
11 NOT VALID
10 NOT VALID
9 NOT VALID
8 NOT VALID
7 NOT VALID
6 NOT VALID
5 NOT VALID
4 NOT VALID
3 NOT VALID
2 NOT VALID
1 NOT VALID
0 NOT VALID

Again thanks a lot for the CR/LF part!