使用Arduino Nano和Sim900模块通过消息回复发件人

时间:2015-12-08 20:24:09

标签: arduino sms sim900 replay

我使用arduino Nano和Sim900模块。我希望,当收到消息时,Arduino会回复"thanks"给发件人。 我的消息接收功能代码是:

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

有效,但我如何从邮件中检索发件人编号?

1 个答案:

答案 0 :(得分:1)

它易于使用arduino IDE附带的GSM Library

  

GSM库包含在Arduino IDE 1.0.4及更高版本中。

使用此libarray使用remoteNumber()

以下是文档页面上的示例代码

#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);

}