Received strange characters

I have the following simple arduino code:

#include "Arduino.h"
#include <SoftwareSerial.h>

const byte rxPin = 9;
const byte txPin = 8;
SoftwareSerial BTSerial(rxPin, txPin); // RX TX

void setup() {
 // define pin modes for tx, rx:
 pinMode(rxPin, INPUT);
 pinMode(txPin, OUTPUT);
 BTSerial.begin(9600);
 Serial.begin(9600);

 pinMode(LED_BUILTIN, OUTPUT); //LED_BUILTIN = 13
}

// message tokens
const char START_TOKEN = '?';
const char END_TOKEN = ';';
//const char DELIMIT_TOKEN = '&';
const int CHAR_TIMEOUT = 20;

bool waitingForStartToken = true;
String messageBuffer = "";
char caracter;

long lastRun = millis();
bool outputValue = false;

void loop() {

 // handle Bluetooth link
 char nextData;
 if (BTSerial.available()) {
   // check for start of message
   if (waitingForStartToken) {
     do {
       nextData = BTSerial.read();
       Serial.print("BTserial.read: ");
       Serial.println(nextData);
     } while((nextData != START_TOKEN) && BTSerial.available());
     if (nextData == START_TOKEN) {
       Serial.println("message start");
       waitingForStartToken = false;
     }
   }

   // read command
   if (!waitingForStartToken && BTSerial.available()){
     do {
       nextData = BTSerial.read();
       Serial.println(nextData);
       messageBuffer += nextData;
     } while((nextData != END_TOKEN) && BTSerial.available());

     // check if message complete
     if (nextData == END_TOKEN) {
       // remove last character
       messageBuffer = messageBuffer.substring(0, messageBuffer.length() - 1);
       Serial.println("message complete - " + messageBuffer);
       messageBuffer = "";
       waitingForStartToken = true;
     }

     // check for char timeout
     if (messageBuffer.length() > CHAR_TIMEOUT) {
       Serial.println("message data timeout - " + messageBuffer);
       messageBuffer = "";
       waitingForStartToken = true;
     }
   }
 }
}

And the MIT code is as following:
BluetoothClient.sendtext b text="?s=1;"

But the output in the Serial Monitor is this:

BTserial.read: ⸮

What am I doing wrong??

I think you should only use the end token, and for reading use
BTserial.readStringUntil(END_TOKEN);

Thanks for your answer.

Even when I use this code:

nextData = BTSerial.read();
       Serial.print("BTserial.read: ");
       Serial.println(nextData);

right under de loop, I got "BTserial.read: ⸮" I was expecting the whole string there instead of the strange character...

Dear @Bram_Werbrouck, are you sure that the BTSerialLine shall be set @9600 or to another baudrate ? i.e. 38400: some HC05's are factory set at that baudrate.
What BT shield (and what Arduino board ) or what BT interface are you using ?
And is your Serial monitor settings set to 9600 as well ?
EDIT: ...and whether you are using a HC05 (or HC06) shield, rememeber to cross the Tx and Rx wires between Arduino board and the shield... trivial but essential... :slight_smile:

1 Like

As I mentioned above...use:

BTserial.readString();

or

BTsrial.readStringUntil(END_TOKEN);

Hi,

The BT interface was apparently set on 115200. I changed it with the command AT+UART=9600,1,0 and now it works! Thank you all for the help

1 Like

Hi @Bram, glad having helped you :+1: