2

I use arduino Nano and Sim900 module. I want, when a message is received, Arduino to reply back "thanks" to the sender. My message receive function code is:

void recieveSms(){
  Serial.print("\r");
  delay(1000);
  Serial.print("AT+CMGF=1\r");        
  delay(1000);                
  Serial.print("AT+CSCS=\"GSM\"\r");
  delay(1000);                  
  Serial.print("AT+CNMI=2,1\r");// set new message remind        
  delay(1000);
  Serial.print("AT+CMGR=2\r"); // read message at position 2
  delay(1000);
  Serial.print("AT+CMGD=2\r");  // delete SMS at position 2
  delay(1000);  
}

that works, but how I can retrieve sender number from the message?

gre_gor
  • 6,669
  • 9
  • 47
  • 52
Sadeq
  • 179
  • 1
  • 3
  • 14

1 Answers1

1

Its easy to use the GSM Library that comes with arduino IDE.

The GSM library is included with Arduino IDE 1.0.4 and later.

Using this libarray use remoteNumber()

Here is the example code on the documentation page

#include <GSM.h>

// PIN Number
#define PINNUMBER ""

// initialize the library instance
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSM_SMS sms;

char remoteNumber[20];  // Holds the emitting number

void setup()
{
    // initialize serial communications
    Serial.begin(9600);

    Serial.println("SMS Messages Receiver");

    // connection state
    boolean notConnected = true;

    // Start GSM shield
    // If your SIM has PIN, pass it as a parameter of begin() in quotes
    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(remoteNumber, 20);
        Serial.println(remoteNumber);

        // This is just an example of message disposal    
        // 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);

}
dmSherazi
  • 3,743
  • 5
  • 37
  • 62
  • this solution dont work for me.by gsm library I can't send messge.why?I connected Rx of sim900 to tx of Arduino nano and tx of sim900 to rx of Arduino Nano. – Sadeq Dec 09 '15 at 12:02
  • I very search about this solution.I think this example only work with Arduino GSM Shield but I dont have It.I have an Sim900 module. – Sadeq Dec 09 '15 at 14:05
  • just connect the pins that are used for software serial to connect to your SIm900 rx tx. – dmSherazi Dec 10 '15 at 09:42
  • thanks your help,I connect sim900 rx,tx to pin 2,3 of arduino according gsmShield lib help.but dont send message. – Sadeq Dec 12 '15 at 17:03