How do I "grep" a text command result

Hi everybody,

i’m on a sketch and project for taking a regex from a command result and if the result match so the code will going to a other void to making an action.

Example : I use SIM800L with my arduino and I can send SMS, no problem, I can read the SMS, but this is the output of one SMS

When the module received an SMS it say “+CMTI: “SN”,1”
And when i send to the module “AT+CMGR=1” he read the message UNREAD like this.

+CMT: “+number”, “”, “date, time+22”
TestSms

I would like to grep only the message and not the information about the number and the time.

How can I do that ?

Here is my example of my code for now.

#include <SoftwareSerial.h>
#include <String.h>
SoftwareSerial sim800l(7, 8); // RX, TX
String grepSerial;

void setup() {          // Start of setup.
 Serial.begin(9600);    // Start of modem.
 sim800l.begin(9600);   // Initialisation of serie communication.
}

void loop(){
 if (sim800l.available())
 Serial.write(sim800l.read());

 if (Serial.available()) {
 while(Serial.available()){
 sim800l.write(Serial.read());
 }
 sim800l.println();
 if (sim800l.println() = "+CMTI: \"SM"",1")
 readsms();
 }
}

void readsms(){
  Serial.println("Receiving text message...");
  sim800l.print("AT+CMGF=1\r");
  delay(100);
  sim800l.print("AT+CMGR=1\r");
  delay(1000);
  sim800l.println();
  if (sim800l.println() = "")
}

PS : I know bash and python for making a grep but in C people say that I have to compile the regex for using it but i’m not very good on C code.

Kind regards,
MVA

See serial input basics.

so the code will going to a other void to making an action.

You probably mean "a function will be called to perform an action". "void" mean a missing return value or call argument.

hi jremington,

Thanks for your reply, I’ll read the post and see if it’s helping me.
I’ll give you some feed back soon :).

And yes I would want to say that.

If you really need regex's, you'd need a regex library. I don't have one to recommend.

If you're using Strings (the String class, with a capital S), class methods are available for searching for strings within Strings, these may be useful. See the String class reference: String() - Arduino Reference Be aware that Strings, because they make heavy use of dynamic memory allocation within the memory-constrained environment of the arduino, can easily cause memory problems.

If you're using fixed length character arrays (aka "c strings") - these are a much better match for the limited resources available on an arduino, you can use c string functions like strstr(), strtok() etc - these are standard C library functions and documentation on them is abundant throughout the internet. There are also _p versions of most of these functions which will take pointers to c strings stored in PROGMEM instead of ones in RAM. See avr libc documentation for details on these. These are obviously a bit less user-friendly than Strings.