Hi,
I am totally new to arduino. I can't get the complete output while reading from serial pin RX1 on arduino.
In this particular case, my Arduino Mega 2560 is connected to SIM808 GSM Shield through PIN TX1,RX1 (18,19).
I am able to send SMS but unable to read received SMS because the string read from the shield is not complete.
Following is my code and it will be followed by my input and the output I am getting.
#define BAUD_RATE 9600
void setup() {
Serial1.begin(BAUD_RATE); // the GPRS shield baud rate
Serial.begin(BAUD_RATE); // Arduino baud rate
delay(200);
}
void loop() {
String output = "";
while (Serial.available() > 0) output = output + Serial.read(); //clear buffer;
Serial.print(output);
unsigned long inTime = millis();
while (Serial.available() == 0 && inputTimeout(inTime, 5000)) ; //wait for input
if (Serial.available() > 0) {
char option = Serial.read();
String command;
unsigned long wait;
switch (option) {
case 'a':
while (Serial.available() > 0) Serial.read(); //clear buffer;
Serial.println(F("Type your AT command:"));
while (Serial.available() == 0);//Busy wait for input
command = Serial.readString();
sendAT(command);
break;
case 't':
sendSMS("--deliberately ommitted--", "An SMS string.");
break;
case 'r':
//intended for receive sms
break;
case 'z':
sendAT(String((char)26)); //send CTRL+Z
break;
default:
break;
}
Serial.println(F("Waiting..."));
}
else{
//do something else
}
while (Serial1.available()) Serial.write(Serial1.read()); // relay everything from GSM Shield to PC via board;
}
void sendAT(String command) {
Serial1.println(command);
delay(200);
String response = "";
char responseBuffer[200];
memset(responseBuffer,'\0',200);
int i=0;
delay (200);
while (Serial1.available() > 0 && i<200) {
responseBuffer[i++]=Serial1.read();
}
delay(100);
Serial.println(responseBuffer);
char *ptr=NULL;
ptr=strtok(responseBuffer,",");
while(ptr != NULL)
{
Serial.println(ptr);
//response = response+ptr;
ptr = strtok(NULL, ","); // takes a list of delimiters
}
//response = responseBuffer;
//Serial.print("Response: " + response);
}
void sendSMS(String msisdn, String text) {
Serial.println(msisdn + ": " + text);
sendAT("AT+CMGF=1"); //Because I want to send the SMS in text mode
sendAT("AT+CMGS=\"" + msisdn + "\",145\r"); //AT+CMGS="<MSISDN>",145
delay(1000);
sendAT(text);//the content of the message
delay(1000);
sendAT(String((char)26));//the ASCII code of the ctrl+z is 26
//*/
delay(100);
}
The terminal output:
--Start of output--
<input=a>
Type your AT command: <input=AT+CPMS="SM","SM","SM">
AT+CPMS="SM","SM","SM"
AT+CPMS="SM","SM","SM"
+CPMS: 16,40,1
AT+CPMS="SM"
"SM"
"SM"
AT+CPMS="SM"
"SM"
"SM"
+CPMS: 16
40
1
Waiting...<input=a>
Type your AT command: <input=AT+CMGR=1>
AT+CMGR=1
AT+CMGR=1
+CMGR: "REC READ","53030303","","17/03/2
AT+CMGR=1
AT+CMGR=1
+CMGR: "REC READ"
"53030303"
""
"17/03/2
Waiting...
--End of output--
My problem:
-
In the case of input AT+CPMS="SM","SM","SM" , the output was incomplete +CPMS: 16,40,1
There should have been something more as the output is of the format
+CPMS: ,,,,, -
In the case of input AT+CMGR=1 , the output is also incomplete +CMGR: "REC READ","53030303","","17/03/2
It should be of the format +CMGR: ,[,],[,,,,
,,,]
Reference: http://www.cika.com/soporte/Information/GSMmodules/SIM808/SIM800_Series_AT_Command_Manual_V1.09.pdf
Can anyone help me get the complete response for the AT commands?
I have done a lot of reading but I can't figure out what's wrong here. Have I missed something?
Thanks in advance!