So as it clear from the title, I am working on message sending and receiving project using arduino Mega 2560 connected to GSM shield SIM900 and a TFT Nextion HMI Screen. The following code is working and I am able to send and receive message using the serial monitor in Arduino IDE software. The next step is to print the received sms message to the screen. I am using AT command for receiving and sending messages with GSM. The problem is, how can I only read the message so that I can easily stored in any string variable and printed to the TFT screen using TFT Nextion style print code??
This is the code:
#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);
}