Hi guys,
the codes below is about receiving sms from phone via the sim5320e gsm module to control the motor.
i tried upload the code uno its working well. however, when i uploaded into mega the code could not be fully executed. i had tried using the communication pin on mega, but its still not working well. Pls help me out.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 10); //tx (9), rx (10)
void setup()
{
Serial.begin(115200); // Setting the baud rate of Serial Monitor (Arduino)
mySerial.begin(115200); // Setting the baud rate of GSM Module
delay(100);
mySerial.print("AT+CMGF=1\r"); // set SMS mode to text
delay(100);
mySerial.print("AT+CNMI=1,2,0,0,0\r");
// blurt out contents of new SMS upon receipt to the GSM shield's serial out
delay(100);
} // end void
//Variable to hold last line of serial output from sim5320e
char currentLine[500] = "";
int currentLineIndex = 0;
//Boolean to be set to true if message notificaion was found and next
//line of serial output is the actual SMS message content
bool nextLineIsMessage = false;
void loop()
{
//If a character comes in from the cellular module.
if(mySerial.available()>0)
{
char lastCharRead = mySerial.read();
Serial.print(lastCharRead);
//Read each character from serial output until \r or \n is reached (which denotes end of line)
if(lastCharRead == '\r' || lastCharRead == '\n')
{
String lastLine = String(currentLine);
//If last line read +CMT, New SMS Message Indications was received.
//Hence, next line is the message content.
if(lastLine.startsWith("+CMT:"))
{
Serial.println(lastLine);
nextLineIsMessage = true;
} // end if
else if (lastLine.length() > 0)
{
if(nextLineIsMessage)
{
Serial.println(lastLine);
//Read message content and set status according to SMS content
if(lastLine.indexOf("1") >= 0)
{
Serial.println("on motor");
}
else if(lastLine.indexOf("2") >= 0)
{
Serial.println("off motor");
}
nextLineIsMessage = false;
} // end if next line
} // end of else if (lastLine.length() > 0)
//Clear char array for next line of read
for( int i = 0; i < sizeof(currentLine); ++i ) {
currentLine = (char)0;
- }*
- currentLineIndex = 0;*
- }*
- else*
- {*
- currentLine[currentLineIndex++] = lastCharRead;*
- } // end else*
- } // end of //If a character comes in from the cellular module.*
}