2016-05-25 82 views
0

從標題中可以清楚看到,我正在使用連接到GSM屏蔽SIM900的Arduino Mega 2560和TFT Nextion HMI屏幕進行消息發送和接收項目。以下代碼正在工作,我可以使用Arduino IDE軟件中的串行監視器發送和接收消息。下一步是將接收到的短消息打印到屏幕上。我正在使用AT命令來接收和發送GSM消息。問題是,如何才能讀取消息,以便我可以輕鬆地存儲在任何字符串變量中並使用TFT Nextion樣式的打印代碼打印到TFT屏幕?如何使用Arduino IDE將GSM SIM900收到的SMS打印到TFT Nextion屏幕

這是代碼:

#include <SoftwareSerial.h> 
#include <Nextion.h> 

SoftwareSerial mySerial(10, 11); // GSM Rx pin is connected to pin 10 and Tx pin is connected to pin 11 
SoftwareSerial nextion(53, 52); // Nextion TX to pin 2 and RX to pin 3 of Arduino 

Nextion myNextion(nextion, 9600); //a Nextion object named myNextion using the nextion serial port @ 9600bps 

void setup() 
{ 
    mySerial.begin(9600); // Setting the baud rate of GSM Module 
    Serial.begin(9600); // Setting the baud rate of Serial Monitor (Arduino) 
    delay(100); 
    myNextion.init();  // Setting the baud rate of TFT Screen (LCD) 
} 


void loop() 
{ 
    if (Serial.available()>0) 
    switch(Serial.read()) 
    { 
    case 's': 
     SendMessage(); 
     break; 
    case 'r': 
     RecieveMessage(); 
     break; 
    } 

if (mySerial.available()>0) 
    Serial.write(mySerial.read()); 
} 


void SendMessage() 
{ 
    mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode 
    delay(1000); // Delay of 1000 milli seconds or 1 second 
    mySerial.println("AT+CMGS=\"+947779328453\"\r"); // Replace x with mobile number 
    delay(1000); 
    mySerial.println("I am SMS from GSM Module");// The SMS text you want to send 
    delay(100); 
    mySerial.println((char)26);// ASCII code of CTRL+Z 
    delay(1000); 
} 


void RecieveMessage() 
{ 
    mySerial.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS 
    delay(1000); 
} 
+0

不要垃圾標籤! Arduino不是C! – Olaf

回答

0

正如你所說的,與調制解調器溝通,你必須使用一套AT命令。對於第一次嘗試,我建議您只需將調制解調器連接到串行端口並手動運行AT命令來體驗它們。

這是第一個文件我在谷歌找到,搜索CMGR:

http://www.zeeman.de/wp-content/uploads/2007/09/ubinetics-at-command-set.pdf

你也可能需要設置PIN碼,這裏是從心裏是怎麼做到這一點:

AT+CPIN?  <-- request pin status 
AT+CPIN=1234 <-- Enter the pin 

然後當它工作,你知道使用的命令集,你可以開始編程!

根據供應商的不同,可能會提供一些特定的AT命令,但爲了便於攜帶,我建議您堅持使用標準!

對不起,我現在沒有任何調制解調器,所以我不能提供一個完整的例子...

+0

對不起,我沒有向下滾動你的整個代碼,但最後CMGR/CMGL似乎要走的路:) – morandg