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??