1
votes

I am new to Arduino and GSM shield. I have been working with SMS, and I have a problem to read the SMS's text contents from the modem.

Here is the setting for the Arduino and the GSM.

#include <SoftwareSerial.h>
SoftwareSerial sim900(7, 8);

#define BUFF_SIZE 400

char buffer[BUFF_SIZE];
bool GSMconnected = false;

//functions
bool copyResponse(int* size);
void printBuff(int size);
bool readlnbuffer(int* start, int* nextline, int* size);
bool isResponseOK(int start, int endofline);
bool checkConnection();
bool checkMessage(int index);
bool checkString(int start, int end, String command);
bool checkSMS(int start, int nextline);
bool newMessage();

void setup() {
  sim900.begin(9600);
  Serial.begin(9600);
  delay(100);
  pinMode(relayIN, OUTPUT);
  delay(100);
  while (!GSMconnected) {
    checkConnection();
    delay(10000);
  }
  int rsize;
  //text mode
  sim900.print("AT+CMGF=1\r");
  delay(1000);

  //store message in SIM card
  sim900.print("AT+CPMS=\"SM\"\r");
  delay(1000);

  sim900.print("AT+CMGD=0,4\r\n");
  delay(1000);
  copyResponse(&rsize);
  printBuff(rsize);
  /*
     0: do not show header values
     1: show the values in result codes
  */
  sim900.print("AT+CSDH=1\r");
  delay(1000);
  //receive mode +CMTI:"SM", 1
  sim900.print("AT+CNMI=2,2,0,0,0 \r");
  delay(1000);
  copyResponse(&rsize);
  printBuff(rsize);

  sim900.print("AT+CSMP=17,167,0,0\r\n");
  delay(10000);
  copyResponse(&rsize);
  printBuff(rsize);
  Serial.println("GSM is ready");

I want to check the SMS message when there is a new SMS. So I simply call the function when the GSM is available.

void loop() {
  if(sim900.available()) {
    newMessage();
  }
}

newMessage() checks the index of the new SMS stored in the SIM card.

bool newMessage() {
  int responseSize;
  if (!copyResponse(&responseSize)) {
    return false;
  }
  //+CMTI: “SM”,1
  //read a line and check if there is a SMS
  int start = 0;
  int nextline = 0;
  int index;
  while (readlnbuffer(&start, &nextline, &responseSize)) {
    String command = "+CMTI: \"SM\",";
    if (checkString(start, start + 12, command)) {
      Serial.println("SMS found");
      index = int(buffer[start + 12]);
      break;
    }
    start = nextline;
  }

  //debug
  printBuff(responseSize);
  delay(10000);
  if (checkMessage(index)) {
    //delete message
    sim900.print("AT+CMGD=");
    sim900.print(char(index));
    sim900.println("\r");
    delay(100);
    return true;
  } else {
    return false;
  }

checkMessage() reads SMS message and should print SMS text data.

bool checkMessage(int index) {
  //listing
  //sim900.print("AT+CMGL=\"REC UNREAD\"\r\n");
  //reading
  sim900.print("AT+CMGR=");
  sim900.print(char(index));
  sim900.print("\r\n");
  delay(5000);
  int responseSize;
  if (!copyResponse(&responseSize)) {
    return false;
  }
  delay(100);

  //debug, to check what is in the buffer
  printBuff(responseSize);
  //read a line and check if there is a SMS
  int start = 0;
  int nextline = 0;
  bool isSMS = false;
  while (!isSMS) {
    start = nextline;
    if (!readlnbuffer(&start, &nextline, &responseSize)) {
      Serial.println("no SMS found");
      return false;
    }
    String command = "+CMGR:";
    if (checkString(nextline, nextline + 6, command)) {
      Serial.println("SMS checking");
      isSMS = true;
    }
  }

  //debug
  if (!readlnbuffer(&start, &nextline, &responseSize)) {
    Serial.println("cannot read the sms info");
    return false;
  }
  Serial.println("SMS message: ");
  for (int i = start; i < nextline; i++) {
    //debug
    Serial.print(buffer[i]);
  }
  Serial.println("");
}

When I run the code, it looks like the response for AT+CMGR cannot be copied completely. Here is what the Serial monitor shows.

checking the network

AT+CREG?

+CREG: 0,1

OK

AT+CMGF=1

OK

AT+CPMS="SM"

+CPMS: 1,30,1,30,1,30

OK

AT+

AT+CSDH=1

OK

AT+CNMI=2,1,0,0,0

OK

AT+CSMP=17,167,0,0

OK

GSM is ready

SMS found

+CMTI: "SM",1

AT+CMGR=1

> +CMGR: "REC UNREAD","+123456789","","17/11/26,11

SMS checking

SMS message:

AT+CMGD=1

OK

I think I went wrong with copying the response in the buffer. I am also a beginner of writing code and I need some help. Can anyone advise me on why I am getting this problem?

Thank you!

1

1 Answers

0
votes

I think the problem is that you are aborting the command by using delay which you should never, never, ever do as a substitute for reading and parsing the responses from the mopdem.

For hints on removing delay usage see the linked answer (and for details on aborting command you can see V.250).