Read an SMS, identify the sender phone number, and answer to the sender

Hi, i'm using arduino and SIM808 V3.2 for a project.
I ve already write the sketch for chek recived SMS and if it is a word that i choose ("STATO")
send an SMS.

I would like that the answer will be send not to a pre established number.
I wish to read the senders number and answer him.
Is it possible? How?

Here is my code right now

#include <SoftwareSerial.h>

// Configure software serial port
SoftwareSerial mySerial(10, 11);

// Variable to store text message
String textMessage;


void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Serial begin ok");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  mySerial.println("AT");
}

void loop(){
  ricezione();
  delay(2000);
}  

void ricezione(){
  Serial.println ("controllo ricezione SMS");
  // AT command to set mySerial to SMS mode
  mySerial.print("AT+CMGF=1\r");
  delay(100);
  // Read the first SMS saved in the sim
  mySerial.print("AT+CMGR=1\r");
  delay(10);  
  if(mySerial.available()>0){
    textMessage = mySerial.readString();
    Serial.print(textMessage);   
    delay(10);
  }
  // check if the SMS is "STATO"
  if(textMessage.indexOf("STATO")>=0){    
    Serial.println("Invio info stato arnia");
    smsstato();
    textMessage = "";   
  }
}


void smsstato(){
  // delete the first SMS 
  mySerial.print("AT+CMGD=1\r");
  delay(100);
  mySerial.print("AT+CMGF=1\r");    
  delay(1000);
  /i wish that phone number change with the phone number of the sender of SMS
  mySerial.print("AT+CMGS=\"+39348395xxxx\"\r");     
  delay(1000);
  //The text of the message to be sent.
  mySerial.print("prova invio sms: ");  
  delay(1000);
  mySerial.write(0x1A);
  delay(1000);  
}

Thank you very much!

Hi ileto85,
I am having a similar problem withmy project. Were you able to soleve the problem? and if so, would you mind posting it here?

Thank you :slight_smile:

Hi! Yes i've solved it! Here is the two function i've used :

String CellNumtemp;
String CellNum;


// check if there are incoming SMS
void ricezione(){
  Serial.println ("controllo ricezione SMS");
  // AT command to set mySerial to SMS mode
  mySerial.print("AT+CMGF=1\r");
  delay(100);
  // Read the first SMS saved in the sim
  mySerial.print("AT+CMGR=1\r");
  delay(10);  
  if(mySerial.available()>0){
    textMessage = mySerial.readString();
    Serial.print(textMessage);  
    delay(10);
  }
  // check if the SMS is "STATO"
  if(textMessage.indexOf("STATO")>=0){    
    Serial.println("Invio info stato arnia");
    //save the phone number of the senders in a string (this works with italian region you must adapt to   yours)  
    CellNumtemp = textMessage.substring(textMessage.indexOf("+39"));
    CellNum = CellNumtemp.substring(0,13);
    smsstato();
    CellNumtemp = ""; 
    textMessage = "";   
  }
  mySerial.print("AT+CMGD=1\r");
  delay(100);
  mySerial.print("AT+CMGD=2\r");
  delay(100);   
}


// Send sms with all the information to the number stored
void smsstato(){
  // delete the first SMS 
  mySerial.print("AT+CMGD=1\r");
  delay(100);
  mySerial.print("AT+CMGF=1\r");   
  delay(1000);
  mySerial.print("AT+CMGS=\"");
  mySerial.print(CellNum);
  mySerial.print("\"\r");  
  delay(1000);
  //The text of the message to be sent.
  mySerial.print("INFO: Latitudine: ");
  mySerial.print(latitude);
  mySerial.print(", Longitudine: ");
  mySerial.print(logitude);
  mySerial.print(", Ampere: ");
  mySerial.print(consumotemp);
  delay(1000);
  mySerial.write(0x1A);
  delay(1000);
  Serial.println("sms stato"); 
}

I extract the number using 2 string.. that's the only way i was able to solve it.. I think that
there are more efficient ways..
Let me know if you found it useful and if it's all clear

ilteo85:
Hi! Yes i've solved it! Here is the two function i've used :

String CellNumtemp;

String CellNum;

// check if there are incoming SMS
void ricezione(){
  Serial.println ("controllo ricezione SMS");
  // AT command to set mySerial to SMS mode
  mySerial.print("AT+CMGF=1\r");
  delay(100);
  // Read the first SMS saved in the sim
  mySerial.print("AT+CMGR=1\r");
  delay(10); 
  if(mySerial.available()>0){
    textMessage = mySerial.readString();
    Serial.print(textMessage); 
    delay(10);
  }
  // check if the SMS is "STATO"
  if(textMessage.indexOf("STATO")>=0){   
    Serial.println("Invio info stato arnia");
    //save the phone number of the senders in a string (this works with italian region you must adapt to  yours) 
    CellNumtemp = textMessage.substring(textMessage.indexOf("+39"));
    CellNum = CellNumtemp.substring(0,13);
    smsstato();
    CellNumtemp = "";
    textMessage = ""; 
  }
  mySerial.print("AT+CMGD=1\r");
  delay(100);
  mySerial.print("AT+CMGD=2\r");
  delay(100); 
}

// Send sms with all the information to the number stored
void smsstato(){
  // delete the first SMS
  mySerial.print("AT+CMGD=1\r");
  delay(100);
  mySerial.print("AT+CMGF=1\r"); 
  delay(1000);
  mySerial.print("AT+CMGS="");
  mySerial.print(CellNum);
  mySerial.print(""\r"); 
  delay(1000);
  //The text of the message to be sent.
  mySerial.print("INFO: Latitudine: ");
  mySerial.print(latitude);
  mySerial.print(", Longitudine: ");
  mySerial.print(logitude);
  mySerial.print(", Ampere: ");
  mySerial.print(consumotemp);
  delay(1000);
  mySerial.write(0x1A);
  delay(1000);
  Serial.println("sms stato");
}





I extract the number using 2 string.. that's the only way i was able to solve it.. I think that
there are more efficient ways.. 
Let me know if you found it useful and if it's all clear

yeah it's working. thanks

Thank you for the quick response, it worked for me too, Thanks :slight_smile:

Thanks to all on this posting.

===========
Output At Serial Monitor or Putty

08:50:37.247 -> Turn ON SIM7000......
08:50:44.535 -> Turn ON !
08:50:44.535 -> Set baud rate......
08:50:49.551 -> Set baud rate:19200
08:50:49.551 -> For example, if you type AT\r\n, OK\r\n will be responsed!
08:50:49.551 -> Enter your AT command :
08:50:54.625 -> SMS Sent
08:50:55.608 -> AT+CMGF=1

08:50:55.608 -> OK
08:50:55.608 -> AT+CMGS="5552341212"

08:50:55.608 -> > INFO: Latitudine: , Lo
08:50:57.132 -> +CMGS: 22
08:50:57.132 ->
08:50:57.132 -> OK

/*

  • File : DFRobot_SIM7000_ATtest.ino
  • Power : SIM7000 needs 7-12V DC power supply
  • Brief : This example use the serial port to send AT command to control the SIM7000
  • With initialization completed, we can enter AT command to SIM7000 directly
  • Note : If you use Mega please connect PIN8 PIN10 and set PIN_RX = 10
  • The AT command must end with CR&LF
    */
    #include <DFRobot_SIM7000.h>

#define PIN_TX 7
#define PIN_RX 8
SoftwareSerial mySerial(PIN_RX,PIN_TX);
DFRobot_SIM7000 sim7000;
int count=0;
String CellNum="5552341212";
String CellNumtemp;

void setup() {
Serial.begin(115200);
sim7000.begin(mySerial);
sim7000.turnOFF();
delay(5000);

Serial.println("Turn ON SIM7000......");
if(sim7000.turnON()){ //Turn ON SIM7000
Serial.println("Turn ON !");
}

Serial.println("Set baud rate......");
if(sim7000.setBaudRate(19200)){ //Set baud rate from 115200 to 19200
Serial.println("Set baud rate:19200");
// Serial.println("AT+CMGF=1");
// Serial.println("DQ = Double Quotes");
// Serial.println("AT+CMGS=DQ5552341212DQ");
// Serial.println("From Rob Parenton SIM7000E Test Chip on Arduino Board! then Ctrl-Z");

}else{
Serial.println("Faile to set baud rate");
while(1);
}

mySerial.begin(19200);
Serial.println("For example, if you type AT\r\n, OK\r\n will be responsed!");
Serial.println("Enter your AT command :");

count=1;
// Serial.println(count);

}

void loop() {

if(count==1){

mySerial.print("AT+CMGF=1\r"); // set SMS mode to text

}

mySerial.listen();
while(mySerial.available()){
Serial.write(mySerial.read());
}

mySerial.flush();
while(Serial.available()){
mySerial.write(Serial.read());
}

delay(30);

if(count==1){
delay(1000);

// set SMS mode to text
mySerial.print("AT+CMGS="");
mySerial.print(CellNum);
mySerial.print(""\r");
delay(1000);

}

if(count==1){
delay(1000);

//The text of the message to be sent.
mySerial.print("INFO: Latitudine: ");
// mySerial.print(latitude);
mySerial.print(", Longitudine: ");
// mySerial.print(logitude);
mySerial.print(", Ampere: ");
// mySerial.print(consumotemp);
delay(1000);
mySerial.write(0x1A);
delay(1000);
Serial.println("SMS Sent");

delay(1000);

count++;
}

}