1

My intention is to control an LED according to the content of the SMS received. For example, if the SMS is "ON", turn on the LED and if it is "OFF", turn it off. I referred many sites and got the below code. The digital pins 9 and 10 are used instead of Rx and Tx to avoid the code uploading problem.

(I have kept the different versions of AT commands commented in the code because I'm not sure which one is correct. With the current one, I'm able to get some kind of output which I've mentioned after the code.)

#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 10);

char msg;
void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);   // Setting the baud rate of GSM Module  
  delay(1000);
  //mySerial.print("AT+CMGF=1r"); // set SMS mode to text
  mySerial.print("AT+CMGF=1\r");  // set SMS mode to text

  delay(100);
  //mySerial.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
  //mySerial.print("AT+CNMI=2,2,0,0,0r");

  mySerial.print("AT+CNMI=1,2,0,0,0\r"); // AT Command to receive a live SMS
  delay(100);
  Serial.println("Ready");
}

void loop() 
{
  if (mySerial.available()>0)
  {
    msg=mySerial.read();
    Serial.print(msg);
  }
}

This is the output I'm getting..

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

OK

+CMTI: "SM",31

+CMTI: "SM",32

+CMTI: "SM",33

+CMTI: "SM",34

The issue is, I'm not able to retrieve the message content. What I'm getting is

"+CMTI: "SM",31"

"+CMTI: "SM",32"

and the number keeps on incrementing. (33, 34 and so on).

I do understand that I have to parse the message to get the exact content, but here I'm not getting the content at all. What exactly should I do to get the SMS content? Could anybody help me to solve this?

One more thing, the GSM module I'm using is a SIM900A type (from elementzonline.com). It's not a shield type, so I've made connections using wires; the Rx, Tx connections and the ground one.

(Sorry for the bad explanation, I'm a complete noob in Arduino, a hobbyist.)

Thanks in advance.

0

You should download the SIM900 command set (Google away) and read it. According to your CNMI settings, sms notifications are pushed to the Arduino not the sms content, thus the +CMTI data you're receiving. SM tells you the sms is stored on the SIM and the number that follows tells you the index of the sms in the SIM memory. With that index, you use the command AT+CMGR=<index> to get the sms content (as well as the sender number and timestamp) from the modem.

So basically, you must parse the CMTI notification, get the number at the end, and use that number to retrieve the SMS content from the modem with the CMGR command. You could also change your CNMI settings to (2,2,0,0,0) i think, if you want the content of the messages to be sent by the modem, and not the notifications alone.

In either case, the SoftwareSerial Receive buffer will most likely overflow when receiving the sms content from the modem; because you cant read() fast enough. This will make the message appear truncated. You have to increase the size of the SoftwareSerial RX buffer to prevent this. Here's a link to an answer where this is explained. Good luck.

  • Thank you so much for the answer ! I modified the code as in the link. I just tried mySerial.println("AT+CMGR=35"); and I'm getting the content of the SMS. The output is like OK AT+CMGR=35 +CMGR: "REC READ","+919******7","","16/04/06,18:38:49+22" Hello. In this, "Hello" is the actual content. But now, the issue is, the output is getting displayed infinitely. – Aparna Shaji Peter Apr 6 '16 at 13:26
  • @AparnaShajiPeter well, thats probably because you placed it in loop(). Place it in setup() if u want it to print the message only once. Structure your code to do what u want – TisteAndii Apr 6 '16 at 14:27
  • one more update, now I'm not getting the notifications. i.e., if I run the same first program, I'm not getting the +CMTI messages. checked the connections, everything is perfect. I'm getting ring as well.Not sure what's happening. :( – Aparna Shaji Peter Apr 6 '16 at 14:37
  • @AparnaShajiPeter you are using your original code right? And you havent changed the CNMI settings from (2,2,0,0,0)? And you have sent new messages while the modem is connected? Also, theres no CMGR command present? – TisteAndii Apr 6 '16 at 14:41
  • first, run the AT+CNMI command to get the index of the message, and then using that index, run the AT+CMGR command. This is the idea, right? – Aparna Shaji Peter Apr 6 '16 at 14:42
1

Instead of using At commands use the GSM library of Arduino it easier to understand and implement. the code to recieve SMS

    #include <GSM.h>

    // PIN Number for the SIM
    #define PINNUMBER "" //sim pin incase the sim is kept locked 

    // initialize the library instances
    GSM gsmAccess;
    GSM_SMS sms;

    // Array to hold the number a SMS is retreived from
    char senderNumber[20];

    void setup() {
      // initialize 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("SMS Messages Receiver");

      // connection state
      boolean notConnected = true;

      // Start GSM connection
      while (notConnected) {
        if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
          notConnected = false;
        } else {
          Serial.println("Not connected");
          delay(1000);
        }
      }

      Serial.println("GSM initialized");
      Serial.println("Waiting for messages");
    }

    void loop() {
      char c;

      // If there are any SMSs available()
      if (sms.available()) {
        Serial.println("Message received from:");

        // Get remote number
        sms.remoteNumber(senderNumber, 20);
        Serial.println(senderNumber);

        // An example of message disposal
        // Any messages starting with # should be discarded
        if (sms.peek() == '#') {
          Serial.println("Discarded SMS");
          sms.flush();
        }

        // Read message bytes and print them
        while (c = sms.read()) {
          Serial.print(c);
        }

        Serial.println("\nEND OF MESSAGE");

        // Delete message from modem memory
        sms.flush();
        Serial.println("MESSAGE DELETED");
      }

      delay(1000);

    }
  • 1
    GSM library is used with Arduino GSM Shield, right? Here, I'm using another SIM900A GSM module, not the shield.. – Aparna Shaji Peter Apr 6 '16 at 10:18
0

use cnmi 2,2,0,0,0 to get the SMS contents use cnmi 1,2,0,0,0, to get only the notification with CMTI+.... if you want to read the cont then u need to parse and check wht number u get in SM and then read that numbered msg using CMGR

  • 1
    Can you please spell this out in English and show an example? – SDsolar May 25 '17 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.