Char Data Type issues

Dear all
I am rather new in the C++ Arduino world but managed to make my project mostly work. However I have some issues with the char datatype as I could not figure out how to convert the data to make my "if query" work.
Setup: My Android App sends characters via Bluetooth to my ESP32 (and alternatively Arduino UNO) and I receive this commands as a set of characters in my code named receivedChars

I can perfectly display the variable receivedChars using Serial function on both my Terminal and the 7735 TFT Display. However I do not know how I can perform a query with it as there seems a datatype mismatch or a kind of:

if (receivedChars == ("1152a1a"))
** {**
** } **

It does compile but I get the warning:
comparison with string literal results in unspecified behaviour [-Waddress]
** if (receivedChars == ("1152a1a"))**

As I understand I cannot compare receivedChars with my Characters "1152a1a". But how can I achieve this?

Any help or suggestion would be much appreciated.

Kind regards

Pls find the code below:

#include <Arduino.h>
#include <BluetoothSerial.h>

#include <Adafruit_GFX.h>    // include Adafruit graphics library
#include <Adafruit_ST7735.h> // include Adafruit ST7735 TFT library
#include <Adafruit_I2CDevice.h>

// ST7735 TFT module connections
#define TFT_RST 26 // TFT RST pin is connected to
#define TFT_CS 5   // TFT CS  pin is connected to
#define TFT_DC 17  // TFT DC  pin is connected to

Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
BluetoothSerial Serial_BT;

const byte numChars = 8;      // Input function
char receivedChars[numChars]; // Input function
boolean newData = false; // Input function

char *destination[] = {"St. Moritz", "Celerina", "Samedan", "Spinas", "Preda", "Berg\201n", "Filisur", "Thusis", "Reichenau-T.", "Chur"};


void callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
{
  if (event == ESP_SPP_SRV_OPEN_EVT)
  {
    Serial.println("Client Connected");
  }

  if (event == ESP_SPP_CLOSE_EVT)
  {
    Serial.println("Client disconnected");
  }
}

void recvWithStartEndMarkers()
{
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  while (Serial_BT.available() > 0 && newData == false)
  {
    rc = Serial_BT.read();

    if (recvInProgress == true)
    {
      if (rc != endMarker)
      {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars)
        {
          ndx = numChars - 1;
        }
      }
      else
      {
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }
    else if (rc == startMarker)
    {
      recvInProgress = true;
    }
  }
}

void showNewData()
{
  if (newData == true)
  {
    Serial.print("Characters received:");
    Serial.println(receivedChars);
    newData = false;
  }
}

void setup()
{

  Serial.begin(115200);

  Serial_BT.register_callback(callback);

  if (!Serial_BT.begin("Bluetooth ESP32"))
  {
    Serial.println("An error occurred initializing Bluetooth");
  }
  else
  {
    Serial.println("Bluetooth initialized");
  }

  tft.initR(INITR_BLACKTAB);    // initialize a ST7735S chip, black tab
  tft.fillScreen(ST7735_WHITE); // fill screen with black color
  tft.setRotation(-1);
  //tft.drawFastHLine(0, 44,  tft.width(), ST7735_ORANGE);    // draw horizontal blue line at position (0, 44)
  tft.setTextColor(ST7735_ORANGE, ST7735_WHITE); // set text color to white and black background
  tft.setTextWrap(false);                        // Don't wrap text to next line
  tft.setTextSize(1);                            // text size = 1

  tft.setCursor(4, 27); // move cursor to position (4, 27) pixel
  tft.printf(destination[5]);
  
}

void loop()
{

  recvWithStartEndMarkers(); // Input function
  showNewData();             // Input function

  if (receivedChars == ("1152a1a"))
  {
    Serial.println("Display Case 1");
    tft.fillScreen(ST7735_WHITE);

    tft.setCursor(20, 25); // move cursor to position (, ) pixel
    tft.setTextSize(1);
    tft.setTextColor(ST7735_ORANGE, ST7735_WHITE);
    tft.printf("Display Case 1");
    delay(3000);
  }

  if (receivedChars == ("1152a2"))
  {
    Serial.println("Display Case 2");
    tft.fillScreen(ST7735_WHITE);
    tft.setCursor(20, 25); // move cursor to position (, ) pixel
    tft.setTextSize(1);
    tft.setTextColor(ST7735_ORANGE, ST7735_WHITE);
    tft.printf("Display Case 2");
    delay(3000);
  }
}

After receiving/storing 8 charcaters into the receivedChars[numChars] array, insert null-byte (0x00 = '\0') as the last element into the said array and then perform the following codes if applicable:

bool n = strcmp(receivedChars, "1152a1a");  //strcmp = string compare 
if( n == 0)
{
     //received string has matched with "1152a1a"
}

@OP
char receivedChars[numChars];
Here you declare a char arrays.
So receivedChars become a pointer to the first element in your array.
So you cannot do:
receivedChars == “11521a1” in your if statement.
Thanks

great, thank you both very much.

still having a very much related question, how can I achive that the routine mentioned by GolamMostafa is only performed if a new string receivedChars has arrived. In other words something like.

Thanks a lot

//Variable definition:
receivedChars[numChars];
char previous_receivedChars[numChars]=receivedChars; //not working

void setup
{
previous_receivedChars=(receivedChars); //initialization of arrays to equal value
}

void loop(
{
 //compare string (Serial input) not equal to previous sting (previous serial input -> input changed
 bool n = strcmp(receivedChars, previous_receivedChar); 
 if( n != 0)
 
  {
   bool n = strcmp(receivedChars, "1152a1a");  //strcmp = string compare
   if( n == 0)

      {
      //strings are equal, do something
      }

   }
}

@OP
Recommend to use the string type, more convenient for what you want to achieve.
You can store the incoming data into a string.
String class has already some built in methods you can use.
For more information, look on Arduino IDE samples under string

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