Checking if a the serial monitor contains a String.

Hello everyone. Arduino beginner here.

I got the basic’s down but when it get’s to the more advanced stuff, i get stuck, cause i never studied code,
so i don’t really know how it work’s or why it sometimes does not. Everything i know about it is from the arduino tutorial’s I’ve been watching.

Anyway, here is the problem I’m facing:

I hooked up a GSM module up to my arduino. And it work’s fine, i can make calls, send / receive messages and so on.

However i can’t make the arduino do something when it receives a certain sms.

And that mainly comes from the fact that i don’t know how to make the arduino understand when it received a certain String.

Meaning:

Using this code that i found on the internet:

#include <SoftwareSerial.h>

//Create software serial object to communicate with SIM800L
SoftwareSerial mySerial(7, 8); //SIM800L Tx & Rx is connected to Arduino #7 & #8

void setup()
{
  //Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
  Serial.begin(9600);
  
  //Begin serial communication with Arduino and SIM800L
  mySerial.begin(9600);

  Serial.println("Initializing..."); 
  delay(1000);

  mySerial.println("AT"); //Once the handshake test is successful, it will back to OK
  updateSerial();
  
  mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
  updateSerial();
  mySerial.println("AT+CNMI=1,2,0,0,0"); // Decides how newly arrived SMS messages should be handled
  updateSerial();
}

void loop()
{
  updateSerial();
}

void updateSerial()
{
  delay(500);
  while (Serial.available()) 
  {
    mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
  }
  while(mySerial.available()) 
  {
    Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
  }
}

when the GSM module receives an SMS that says “ON”.
The serial monitor basically writes it out like this:

+CMT: “+zzxxxxxxxxxx”,"",“20/04/16,20:25:43+08”
ON

The +zzxxxxxxxxxx is my number, but thats irelevant.

Question is, how do i make the arduino understand that it recieved the word “ON” based on that Serial.read.

Or to rephrase my question, how do i make the arduino check to see if the Serial.read contains the word
“ON”.

Also I am not looking for a piece of code that i can copy>paste into my sketch, i want to understand the code. So if you have an idea, please post the code with //comment’s so i can understand what it actually does.

Thank you for all your replies in advance ! :slight_smile:

That program is completely unsuitable for what you want to do because all it does is pass characters from the GSM to the Serial Monitor. By the time the character gets to the Serial Monitor it is lost forever to the Arduino.

In order to do something when a message is received you first have to save the message on the Arduino.

Have a look at the second example in Serial Input Basics. If you change it so it is reading from mySerial it should save any messages into the array receivedChars. You can then check for a match with code like this

if (strcmp(receivedChars, "ON") == 0) {
   // there was a match
}

...R

Well, i tried what you said. I set my serial monitor to newline, and implemented example 2 into the original code.

The code now looks like this:

#include <SoftwareSerial.h>

//Create software serial object to communicate with SIM800L
SoftwareSerial mySerial(7, 8); //SIM800L Tx & Rx is connected to Arduino #7 & #8

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup()
{
  //Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
  Serial.begin(9600);
  
  //Begin serial communication with Arduino and SIM800L
  mySerial.begin(9600);

  Serial.println("Initializing..."); 
  delay(1000);

  mySerial.println("AT"); //Once the handshake test is successful, it will back to OK
  
  mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
  
  mySerial.println("AT+CNMI=1,2,0,0,0"); // Decides how newly arrived SMS messages should be handled
  
}

void loop(){
   recvWithEndMarker();
    showNewData();
    if (strcmp(receivedChars, "ON") == 0) {
   Serial.println ("if statement worked");
}
  }


void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
   
    while (mySerial.available() > 0 && newData == false) {
        rc = mySerial.read();

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

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        newData = false;
    }
}

So the if statement in the void loop still wont work…

When i ran this code and sent “ON” to the gsm module, all i got was this on the serial monitor:

Initializing…
This just in … AT
AT+CMGF=1
AT+CNMI=1,2,0,0,0

This just in … OK

This just in …

This just in … OK

This just in …

This just in … OK

This just in …

This just in … +CMT: “+zzxxxxxxxxxx”,"","20/04
This just in … ON

Have i wrote something wrong? :confused:

I have found this on the internet.

And it works. The if statements react to “ON” or “OFF”.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(7,8);  // (Rx,Tx  > Tx,Rx) 

char incomingByte; 
String inputString;


void setup() 
{
     
      Serial.begin(9600);
      mySerial.begin(9600); 

     while(!mySerial.available()){
        mySerial.println("AT");
        delay(1000); 
        Serial.println("Connecting...");
        }
      Serial.println("Connected!");  
      mySerial.println("AT+CMGF=1");  //Set SMS to Text Mode 
      delay(1000);  
      mySerial.println("AT+CNMI=1,2,0,0,0");  //Procedure to handle newly arrived messages(command name in text: new message indications to TE) 
      delay(1000);
      mySerial.println("AT+CMGL=\"REC UNREAD\""); // Read Unread Messages
     }

void loop()
{  
  if(mySerial.available()){
      delay(100);

      // Serial Buffer
      while(mySerial.available()){
        incomingByte = mySerial.read();
        inputString += incomingByte; 
        }

        delay(10);      

        Serial.println(inputString);
        inputString.toUpperCase(); // Uppercase the Received Message

        //turn RELAY ON or OFF
        if (inputString.indexOf("ON") > -1){
          Serial.println ("if statement WORKED");
          
          }
         if (inputString.indexOf("OFF") > -1){
          
          }          

        delay(50);

        //Delete Messages & Save Memory
        if (inputString.indexOf("OK") == -1){
        mySerial.println("AT+CMGDA=\"DEL ALL\"");

        delay(1000);}

        inputString = "";
  }
}

Now, i understand that he made a variable called “inputString” and that’s what he check’s in the if statement’s.

However i dont understand what’s the “char incomingByte” for, and why is

incomingByte = mySerial.read();;
inputString += incomingByte ???

incomingByte = mySerial.read();

assigns one character from the input buffer to a temporary variable, 'incomingByte'.

inputString += incomingByte;

appends the value of that temporary variable to the end of the String object 'inputString'.

Hmm, could you maybe clarify that a bit…

Why does he do that?

Couldn’t he just say:

inputString = mySerial.read();

and then

if (inputString.indexOf ("ON")>-1){}

what character does he store and why does he store it?

and why does he append it to the inputString variable?

Sorry if this is asking for too much info, i would just like to understand the code a bit better, not just copy/paste it :confused:

Xaxina: Have i wrote something wrong? :/

A little bit.

This code

void loop(){
   recvWithEndMarker();
    showNewData();
    if (strcmp(receivedChars, "ON") == 0) {
        Serial.println ("if statement worked");
    }
}

checks receivedChars much too often. You should only check it if there is a new message - like this

void loop(){
   recvWithEndMarker();
   if (newData == true) {
        if (strcmp(receivedChars, "ON") == 0) {
             Serial.println ("if statement worked");
        }
    }
    showNewData();
}

Note that I have put it ahead of showNewData() because that function changes newData to false - ready for the next message

...R