2015-12-08 44 views
2

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

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

它易于使用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); 

} 
+0

这个解决方案不为me.by GSM库工作的示例代码我无法发送messge.why?我将sim900的Rx连接到Arduino nano的tx,将sim900的tx连接到Arduino Nano的rx。 – Sadeq

+0

我非常关注这个解决方案。我认为这个例子只适用于Arduino GSM Shield,但我没有它。我有一个Sim900模块。 – Sadeq

+0

只需连接用于软件串行连接到SIm900 rx tx的引脚即可。 – dmSherazi

相关问题