How to let the arduino know if sim800 received message from specific number

hi everyone.

I'm using SIM800C and it's working great to call and receive calls.

I want to turn led ON when i receive SMS message from a particular number, until now all tutorials show you how to constantly read serial to print the message received without the possibility to detect that and do something with it.

i don't have a code to post here but to show my progress, this is the code to receive and print the message to serial.

#include <SoftwareSerial.h>

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

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
  }
}

Take a look at this

/* Code Written and tested by:
 *  Youtube- Hyper_makes(https://www.youtube.com/channel/UCrCxVyN2idCxPNOwCwK6qtQ)
 *  Github:https://github.com/Kruze17/Parsing-SMS-with-SIM800L
 */

#include <SoftwareSerial.h>

String PHONE = "";
String msg;

#define rxPin D5
#define txPin D6
SoftwareSerial sim800(rxPin, txPin);

void setup()
{
  Serial.begin(115200);
  sim800.begin(19200);
  initSIM();
}

void loop()
{
  while (sim800.available())
  {
    parseData(sim800.readString());  //Calls the parseData function to parse SMS
    if (PHONE != "")
    {
      doAction();  //Does necessary action according to SMS message
    }
  }
}

void parseData(String buff)
{
  Serial.println(buff);
  unsigned int index;
  //Remove sent "AT Command" from the response string.
  index = buff.indexOf("\r");
  buff.remove(0, index + 2);
  buff.trim();
  if (buff != "OK")
  {
    index = buff.indexOf(":");
    String cmd = buff.substring(0, index);
    cmd.trim();
    buff.remove(0, index + 2);
    //Parse necessary message from SIM800L Serial buffer string
    if (cmd == "+CMT")
    {
      //get newly arrived memory location and store it in temp
      index = buff.lastIndexOf(0x0D);                  //Looking for position of CR(Carriage Return)
      msg = buff.substring(index + 2, buff.length());  //Writes message to variable "msg"
      msg.toLowerCase();                               //Whole message gets changed to lower case
                                                       //  Serial.println(msg);
      index = buff.indexOf(0x22);                      //Looking for position of first double quotes-> "
      PHONE = buff.substring(index + 1, index + 14);   //Writes phone number to variable "PHONE"
      //  Serial.println(PHONE);
    }
  }
}

void doAction()
{
  //code to respond to text based on text or number goes here
  Serial.println();
  Serial.print("Received : ");
  Serial.println(msg);
  Serial.print("from :");
  Serial.println(PHONE);

  PHONE = "";  //Clears string
  msg = "";    //Clears message string
}

void initSIM()
{
  Serial.println(F("Initializing GSM..."));
  sim800.print("AT + CMGF = 1\r");
  delay(1000);
  sim800.print("AT + CMGD = 1, 4\r");
  delay(1000);
  sim800.print("AT + CNMI = 2, 2, 0, 0, 0\r");
  delay(1000);
  Serial.println(F("SMS initialised"));
}

As you will see from the comments, it is not my code, but I believe that it works to parse an SMS message

Note that even if it works this might not be the best way to do it

Actually i found a good solution which i will post here for a future visitors.

It record everything from the received sms including the phone number and message then i can check if the particular number is there or not.

That is what the code I posted does

I know but its a bit complicated as it includ removing some parts.

But overall the same

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