2

我使用 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);  
}

那行得通,但是我如何从邮件中检索发件人号码?

4

1 回答 1

1

它易于使用arduino IDE 附带的GSM 库。

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

}
于 2015-12-09T10:08:04.230 回答