從標題中可以清楚看到,我正在使用連接到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);
}
不要垃圾標籤! Arduino不是C! – Olaf