reading strings after printed in serial

Hello to everyone I use this library to connect to internet MQTT & Node-red Server with Sim800l

https://github.com/elementzonline/SIM800_MQTT

I created a switch in Node-red UI-dashboard to turn on or off the lights , so each time you turn on the switch ,"LightsOn" string is printed via serial .

I want to read the strings that are printed via mySerial.println(); and comparing them and then turn on or off the GPIO pins of Arduino Uno

This is the code that called when i change switch in ui-dashboard and prints the message of subscribed topic

void GSM_MQTT::OnMessage(char *Topic, int TopicLength, char *Message, int MessageLength)
{
  /*
    This function is called whenever a message received from subscribed topics
    put your subscription publish codes here.
  */
    
  /*
     Topic        :Name of the topic from which message is coming
     TopicLength  :Number of characters in topic name
     Message      :The containing array
     MessageLength:Number of characters in message
  */


  mySerial.println(TopicLength);
  mySerial.println(Topic);
  mySerial.println(MessageLength);
 mySerial.println(Message);


}

But i tried a lot of the tutorials about reading strings , but they are for when you enter the values manually not automatic. ( i tried a lot of them without any result.)

What do you think about this?

behzad_a: What do you think about this?

You need to post the complete program and tell us in detail what it actually does and what you want it to do that is different. It will make it much easier to focus on the parts you need help with rather than wasting time on things that you can do.

...R

behzad_a: I want to read the strings and comparing them and then turn on or off the GPIO pins of Arduino Uno

Read by what? The board that sent them? Another board?

Your post seems to be missing more details than it gives. I won't guess what you're trying to do.

Robin2: You need to post the complete program and tell us in detail what it actually does and what you want it to do that is different. It will make it much easier to focus on the parts you need help with rather than wasting time on things that you can do.

...R

Thnaks for your reply

When i run program mySerial.begin(9600); started and it connetcs to the server and subscribes to "lamps" topic

On the server side i change the switch and by each swith i receive a string in the serial by a void method

void GSM_MQTT::OnMessage(char *Topic, int TopicLength, char *Message, int MessageLength)
{
 /*
   This function is called whenever a message received from subscribed topics
   put your subscription publish codes here.
 */
  

mySerial.println(Message);

//here i want to read messages that are printed via mySerial and compare them with a string and based on them turn on or off GPIO pins of Arduino 


//i tried this but it doesn't work .i think it's for when the characters are typed by the user and are in the buffer

 While(Serial.available()) {

String a= Serial.readString();// read the incoming data as string

if(a == "LightsOn"){
digitalWrite(6,HIGH)
}

}

}

A common test method for specific content in Strings received from a client is below. There are others, but this seems close to your situation.

if(readString.indexOf('?') >=0)

behzad_a:
//i tried this but it doesn’t work .i think it’s for when the characters are typed by the user and are in the buffer

 While(Serial.available()) {

String a= Serial.readString();// read the incoming data as string

if(a == “LightsOn”){
digitalWrite(6,HIGH)
}

Just 1st, make serial fast if possible. The serial output buffer will clear faster, you can print more w/o overfill.

Next, stop using String with Arduino or be prepared to see RAM problems once you write more than small sketches.
String variables behave badly for small RAM computing.

When you use this:
String a= Serial.readString();// read the incoming data as string

your sketch stays on that command until all the text has arrived. Nothing else runs, forget reading a button or anything like that… 9600 baud serial chars arrive in Arduino time like 2 hours apart in your time and during that time, nothing else can be done.

If you have text commands coming in you can read them 1 char at a time and try to match them with words in a list of words.

// command words as char array strings -- you can use the indexes to get the letters
char lightsBlink[] = "LightsBlink";
char lightsOff[] = "LightsOff";
char lightsOn[] = "LightsOn";

// this makes an array of pointers to those strings, pointer is just the address to the 1st char. 
char *commands[] = { lightsBlink, lightsOff, LightsOn );
byte numberOfWords = 3;

char readChar; // a 1 char buffer
byte wordNumber = 0;  // which word you are trying to match.
byte letterNumber = 0;   // how many letters have been matched in the current word.

… and down in the sketch… I am out of time on making this all code…

You read a serial char and try to match it with the current word and letterNumber

if ( readChar == command[ wordNumber ][ letterNumber ] ) {

If it matches then add 1 to letterNumber and if it doesn’t then consider that if letterNumber is > 0, you try to match that many chars of the current wordNumber against the next word (unless there is no next word, then you have a No Match Error) and then compare readChar to the next letter until you read whatever delimiter you put between commands at the send end, usually a space or comma or newline ( ‘\n’ ) and if wordNumber is still < numberOfWords and letterNumber is the length of the word at wordNumber ( readChar will be a delimiter while commands[ wordNumber ][ letterNumber ] == 0, the string terminator/end )…
… then You Have A Match in the time it took to read the incoming serial.

Otherwise at least read the serial char by char and stick them into a char array string then compare that to your command strings and -still- at least you’re not blocking the rest of the sketch.

char readCommand[ 20 ]; // make big enough to hold the longest command + 1 for the terminating char == 0 (not char '0');

.......

    readChar = Serial.read();

    if ( readChar > 0 )
    {
      readCommand[ letterNumber++ ] = readChar;  // the ++ at the end adds 1 -after- the value of letterNumber is used.
                                                                                 // if the ++ is at the start it adds 1 then uses that as the value.
   }
   else // buffer is full, now use strcmp() from the string.h library to see if the buffered input matches any command word.

This way, when the last serial char arrives your sketch only starts trying to match any of your commands.

My way is harder to code but runs faster and tends to be smaller. I expect that you will go the buffer then compare route first as that is pretty standard.

If you want to learn more about blocking and how not to do it and ways to read serial w/o blocking then try the first 2 Nick Gammon tutorials (very well done, quite complete) listed in my signature space below. I am not Nick, I am GFS.