String comparison with answer from HM10


I'm stuck on a really annoying problem.

In my program, I am able to compare a data read from the HM10 when it is myself who emits this one from other device.
Using this method:

String data = HM10.readStringUntil('\n');
 if (data.equals("test"))  { //Works

However in this case I am trying to compare a data coming from the HM10 when it is itself which transmits it. (Following at the command "AT + NOTI?")
The HM10 sends me either "OK + CONN" or "OK + LOST". (if you need more information on this command in HM-10 DataSheet)
Using the same method and although the data is all strings and that these are similar visually on my serial.
The equals() method does not work.

Do you have any idea why this is not working?
Thank you very much in advance for your help.

My code below and my serial next:

            while (HM10.available()){   
                      String data = HM10.readStringUntil('\n');
                      Serial.println("Reception data: "+data);
                          if (data.equals("OK+LOST"))
                              Serial.print("All IS OK");

ps: The other lines in the serial are correct and correspond to other part of the code which has no influence on my problem.

You should avoid the usage of the String class. You could try with "data.equals(String("OK+LOST"))". If the HM10 module uses CRLF (written as "\r\n") as terminator, then the received string will have a '\r' at the end which may be removed with data.trim().

1 Like

This Code works -->"data.equals(String("OK+LOST\r"))"
Thanks you Danois90


The code below dont works..

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