2017-04-03 39 views
1

我目前在「物聯網」領域下研究我的迷你項目。我選擇使用GSM模塊設計無線通知板。如何從嵌入式C中的GSM調制解調器讀取消息?

我將項目分爲兩個模塊。首先,完美完成的Arduino-LED板接口。

二,GSM-Arduino接口。基本上,信息/短信將從手機發送到GSM模塊,然後我們必須使用Arduino從GSM模塊讀取該信息。我在這裏面臨一個問題。該消息正被髮送到GSM調制解調器,但我無法閱讀。我嘗試寫不同的代碼,但它不工作。該消息未被顯示。

這是我試過的代碼片段。

`#include SoftwareSerial.h 

SoftwareSerial SIM900A(2,3);// RX | TX 

// Connect the SIM900A TX to Arduino pin 2 RX 

// Connect the SIM900A RX to Arduino pin 3 TX. 


void setup() 
{ 

     SIM900A.begin(9600); // Setting the baud rate of GSM Module 

     Serial.begin(9600); // Setting the baud rate of Serial Monitor(Arduino) 

     Serial.println ("SIM900A Ready"); 

     delay(100); 

     Serial.println (" Press s to send and r to recieve "); 

} 

void loop() 
{ 

    if (Serial.available()>0) 

    switch(Serial.read()) 

    { 

     case 's': SendMessage(); 
       break; 

     case 'r': RecieveMessage(); 
       break; 

     } 

    if (SIM900A.available()>0) 

     Serial.write(SIM900A.read()); 

} 


void SendMessage() 
{ 

    SIM900A.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode 

    delay(1000); // Delay of 1000 milli seconds or 1 second 

    Serial.println ("Set SMS Number"); 

    SIM900A.println("AT+CMGS=\"+91xxxxxxxxxx\"\r"); //Replace with your mobileno. 

    delay(1000); 

    Serial.println ("Set SMS Content"); 

    SIM900A.println("Hello, I am SIM900A GSM Module");// The SMS text you want to send 

    delay(100); 

    Serial.println ("Finish"); 

    SIM900A.println((char)26);// ASCII code of CTRL+Z 

    delay(1000); 

    Serial.println (" ->SMS Sent"); 
} 


void RecieveMessage() 
{ 

    Serial.println ("SIM900A Receiving SMS"); 

    delay (1000); 

    SIM900A.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS 

    delay(1000); 

    Serial.write (" ->Unread SMS Recieved"); 

}` 
+0

我們不知道你的硬件是否工作。我們不能幫助硬件調試。你在範圍上看到什麼? – ThingyWotsit

+0

硬件工作正常!當我們在GSM模塊中插入SIM卡並測試它是否從移動電話發送時接收到任何消息時,它將完全正常工作。如何閱讀接收消息是我面臨問題的地方。我無法閱讀GSM的消息。 –

+0

'測試它是否接收到任何消息' - 如果您無法接收Arduino接口上的任何數據,如何測試它? – ThingyWotsit

回答

0

您可能必須使用命令來設置首選短信存儲到SIM卡:

SIM900A.print("AT+CPMS=\"SM\"\r"); 

此外,移動這個命令設置():

SIM900A.print("AT+CMGF=1\r"); 

最後,注我如何使用SIM900A.print()而不是SIM900A.println(),並在每個命令後發送'\ r'或0x0d。 println()發送一個「\ n \ r」並導致某些調制解調器出現問題。

+0

您可能還想驗證調制解調器的波特率是9600還是別的。大多數調制解調器讓你配置這個,所以它可能已經改變爲其他值 –