Need help comparing strings with a special character in them

Hi all, so I haven't been able to find a way to get this working after a lot of searching, I'm using an HM-18 bluetooth module and some example code to read data from an hm-10.

Essentially, to get this to do what I want, I need to compare strings that have a special character in them, because the module broadcasts OK+CONN when it connects, and OK+LOST when it disconnects. I need to be able to ignore these two messages but also would like to do something with these two updates.

So, basically

If the module output is OK+CONN do x
if the module output is OK+LOST do y
if the module output is neither do z

It seems like the + character is what's causing all my issues.

#include "Keyboard.h"
#include <SoftwareSerial.h>
#include <Wire.h>

SoftwareSerial HM10(2, 3); // RX = 2, TX = 3
char appData;  
String inData = "";
String conok = "OK+CONN";
int rawdata;
void setup()
{
  Serial.begin(9600);
  Serial.println("HM10 serial started at 9600");
  HM10.begin(9600); // set HM10 serial at 9600 baud rate
  Keyboard.begin();

}

void loop(){
  HM10.listen();  // listen the HM10 port
  while (HM10.available() > 0) {   // if HM10 sends something then read
    appData = HM10.read();
    inData = String(appData);  // save the data in string format

  }
}








Where is the code where you are making the comparison?

What do you get when you print the inData String?

It may be that you are receiving invisible white space characters like line feed and/or carriage return.

What issues might those be? Your code doesn't seem to do much.

Do you have any code where you try anything at all using String comparison and if statements?

What you ask is straightforward. There is nothing special about '+' that would keep you from receiving it, making it part of a group of characters to be handled together (String or array of chars, small s C strings) and

And comparing it to other such entities that might or might not have '+'.

There really are very few special characters at all. The plus sign isn't one of them.

I'd help with the String thing, but I don't use them.

a7

Everything I tried to do a comparison failed to work, I've tried so many variances using strcmp and other methods that it's all a bit of a mess.

The example that was in the original example for the bluetooth module I found used this.

if ( inData == "F") {
    Serial.println("LED OFF");
    digitalWrite(13, LOW); // switch OFF LED
    delay(500);
  }

I've tried changing it to the following, however I get nothing from it. My code doesn't do much because I'm adding bluetooth into something else, so I just want to get this up and running, even just with serial outputs before merging it.

When I tried just the demo changing an LED on and off, it did work.

void loop(){
  HM10.listen();  // listen the HM10 port
  while (HM10.available() > 0) {   // if HM10 sends something then read
    appData = HM10.read();
    inData = String(appData);  // save the data in string format
    Serial.print(inData);
    if (inData == "OK+CONN") {
    Serial.println("Connected");
  }
  }
}

I also tried with equals, so

void loop(){
  HM10.listen();  // listen the HM10 port
  while (HM10.available() > 0) {   // if HM10 sends something then read
    appData = HM10.read();
    inData = String(appData);  // save the data in string format
    Serial.print(inData);
    if (inData.equals("OK+CONN")){
    Serial.println("Connected");
  }
  }
}

but that doesn't work

No extra spaces or carriage returns from what I can tell annoyingly, I also tried getting the printing "HM10.read" directly, however that returns a string of numbers that also does not work.

OK+CONN reads out as 7975436778781310
and OK+LOST reads out as 7975437679831310

However, if I use Serial.printlm(HM10.read());

It outputs the following for OK+CONN

79 43 79 78 10

each with a carriage return between them, and OK+LOST is similar.

Instead of read() use readString().

The numbers are ASCII character codes. 13 is carriage return (\r) and 10 is line feed (\n). "OK+CONN" is not equal to "OK+COMM\r\n"

The serial input basics tutorial may be of interest. It receives the serial data into a string (null terminated character array). You cannot use == with strings, you need to use the strcmp() function.

If you use Strings (String class objects) use the trim() function to remove any white space characters from the String.

1 Like

Try this:

 while (HM10.available() > 0)     // if HM10 sends something then read
   {
      appData = HM10.readString();
      appData.trim();
      Serial.print(appData);
      if (appData.equals("OK+CONN"))
      {
         Serial.println("Connected");
      }

What prints for Serial.print(appData); ?

1 Like

Ok, flippin awesome! That's working perfectly now.

#include "Keyboard.h"
#include <SoftwareSerial.h>
#include <Wire.h>

SoftwareSerial HM10(2, 3); // RX = 2, TX = 3
char appData;  
String inData = "";
String conok = "OK+CONN";
int rawdata;
void setup()
{
  Serial.begin(9600);
  Serial.println("HM10 serial started at 9600");
  HM10.begin(9600); // set HM10 serial at 9600 baud rate
  Keyboard.begin();

}

void loop(){
  HM10.listen();  // listen the HM10 port
   while (HM10.available() > 0)     // if HM10 sends something then read
   {
      inData = HM10.readString();
      inData.trim();
      //Serial.print(inData);
      if (inData.equals("OK+CONN")){
         Serial.println("Connected");
      } else if (inData.equals("OK+LOST")){
        Serial.println("Disconnected");
      } else {
        Serial.println(inData);
    }
  }
}

Yay! :grinning:

1 Like

Your mistake was reading a single character, turning it into a single-character String, and comparing it to a multi-character string. There is no way a single character will equal a string of multiple characters.

Using .readString() works because it returns a String of multiple characters, unlike .read() which only returns one character.

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