Why not equal if(message == "on" ){} in ESP32 bluethooth

Is there any problem not working please help me. Thanks all.

#include "BluetoothSerial.h"

#define LED 2
BluetoothSerial SerialBT;
String message = "";

void setup() {
  Serial.begin(115200);
  pinMode(LED, OUTPUT);
  if(!SerialBT.begin("ESP32")){
    Serial.println("An error occurred initializing Bluetooth");
  }
  digitalWrite(LED, HIGH);
}
 
void loop() {
  if(SerialBT.available()){
    Serial.println("Enter to recieve");
    message = "";
    while(SerialBT.available()){
      char incomingChar = SerialBT.read();
      if(incomingChar != '\n'){
        message += String(incomingChar);
      }
    }
    Serial.print(message);
  }
  if(message == "on"){
    digitalWrite(LED, HIGH);
    }
  else if(message == "off"){
    digitalWrite(LED, LOW);
    }
  Serial.print("|");
  Serial.print(message);
  delay(50);
}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

1 Like

Try printing the length of message before you test its value. Is it what you expect ?

best to understand using basic Serial interface

consider using readBytesUntil() instead of appending each character

#define LED  LED_BUILTIN

char  msg [80];

void
loop (void)
{
    if (Serial.available ())  {
        int n = Serial.readBytesUntil ('\n', msg, sizeof(msg));
        msg [n] = '\0';

        if (! strcmp (msg, "on"))
            digitalWrite (LED, HIGH);
        else if (! strcmp (msg, "off"))
            digitalWrite (LED, LOW);
        else  {
            Serial.print   (" invalid - ");
            Serial.println (msg);
        }
    }
}

void setup () {
    Serial.begin (115200);
    pinMode (LED, OUTPUT);
    digitalWrite (LED, HIGH);
}
1 Like

there is \r too

1 Like

Yes. You don't understand how serial works.

You cannot assume that every byte will arrive instantaneously ... Serial.availble() may say there is nothing left to receive... because you are asking the question more often than bytes are received. Generally serial is slower than your micro-controller .. you need to handle this.

EDIT. and what @Juraj said.

1 Like

Thank you. I changed '\n' to '\r' it is working good .

Thank you forgotten \r. Thankyou.

But i printing at end it is right recieved!

 if(SerialBT.available()){
    Serial.println("Enter to recieve");
    message = "";
    while(SerialBT.available()){
      char incomingChar = SerialBT.read();
      if(incomingChar != '\n'){
        message += String(incomingChar);
      }
    }
   < Serial.print(message);/>
  }

Try to print it like this

Serial.print ('<');
Serial.print(message);
Serial.println ('>');

Everything should be on a single line.

1 Like
#include "BluetoothSerial.h"

#define LED 2
BluetoothSerial SerialBT;
String message = "";

void setup() {
  //Serial.begin(115200);
  pinMode(LED, OUTPUT);
    if(!SerialBT.begin("ESP32")){
      SerialBT.println("An error occurred initializing Bluetooth");
    }
  digitalWrite(LED, HIGH);
}

void loop() {
  if (SerialBT.available()) {
    message = "";
    while (SerialBT.available()) {
      char incomingChar = SerialBT.read();
      if (incomingChar == '\r' || incomingChar == '\n')SerialBT.read();
      else message += String(incomingChar);
    }
  }
  if (message == "on") {
    digitalWrite(LED, HIGH);
  }
  else if (message == "off") {
    digitalWrite(LED, LOW);
  }
  delay(50);
}
1 Like

You may have a look at this sketch

#include "BluetoothSerial.h"

const byte LED = 2;
const byte MaxMessageLength = 10;

BluetoothSerial SerialBT;

String message = "";

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(115200);
  if(!SerialBT.begin("ESP32")){
      Serial.println("An error occurred initializing Bluetooth");
  }
  digitalWrite(LED, HIGH);
}

void loop() {
  if (MessageReceived()) React();
}

boolean MessageReceived(){  // Returns TRUE if either \r or \n was received
  boolean complete = false; 
   while (SerialBT.available()) {  
      char incomingChar = SerialBT.read();
      if (incomingChar == '\r' || incomingChar == '\n') {
        complete = true;
      } else message += String(incomingChar);
      // If required we discard messages which are too long:
      if (message.length() > MaxMessageLength) message = "";
   }
  // If the message is not "complete" (no \r or \n received or too long)
  // it will go on collecting characters in the next loop
  return complete;
}

void React(){
  if (message == "on") {
    digitalWrite(LED, HIGH);
  }
  // no real need for "else" as the comparison(s) do not require much time
  // and all used commands exclude each other
  // This makes it easier to expand the list of messages
  // and to follow the program sequence
  if (message == "off") {
    digitalWrite(LED, LOW);
  }
  Serial.print("<");
  Serial.print(message);
  Serial.println(">");
  message = "";  // Reset the message here after evaluation, reaction and printing
}  

It uses a receiving routine which is independent from delays between single characters of a message and also discards messages which are too long thus avoiding an overflow.

In most cases in the "real world" these problems will not occur, but to take care of Murphy's Law (If something bad can happen it will happen) ... :wink:

1 Like

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