I have a GSM / GPRS Shield connected to an Arduino Duemilanove, and i have much trouble communicating to it reliably.
I set the Shield so that it takes commands via SoftwareSerial and then spits out any response to that Command via regular Serial - this has improved my issues a fair bit. Whats happening is that i send a command to the GSM Shield, and i get a reply of unknown length back. Or often i don't. The below process is fairly consistent and demonstrates my issue with an example:
1. Reset Arduino & open Serial Console
2. Send a new SMS to the GSM shield via Mobilephone
3. Run check_sms command to list new SMS
4. The output in the console is either not existing or cut off somewhere.
5. Run check_sms command to list new SMS again
6. The output is, in 95% of cases, what it should be.
7. Send another new SMS via Mobilephone
8. Rinse & repeat. Output is wrong the first time, it's nearly always correct the second or third time.
So why is it that i need to run a command twice? I played around with different methods, timings and delays for days now, and just cant get it to work reliably. When i started out using pure SoftwareSerial to communicate back and forth with the GSM Shield it was even worse, and get a lot of garbled output. Now the issue is that i don't reliably get all the output, or sometimes even no output.
I would be most grateful for someone to tell me just what the right way(TM) is to communicate reliably with my GSM Shield, or what the problems are in my Code or with my Timing. How to solidly read Data from the Device so that i can nearly guaranteed get the full output? I guess it all has to do with how fast the Shield processes incoming commands and how long it takes to send back a reply.
Thanks a lot.
#include <SoftwareSerial.h>
SoftwareSerial cell(2,3);
char ch;
String command = "";
void setup(){
Serial.begin(9600);
cell.begin(9600);
Serial.println("Starting ATWIN Communication...");
}
void loop(){
if(Serial.available()){
command = get_serial_contents();
}
if(command == "check_sms")
check_sms();
command = "";
}
void check_sms(){
Serial.println("Checking SMS");
cell.println("AT+CMGL=\"ALL\"");
delay(100);
while(!Serial.available() >0){};
if(Serial.available()>0){
while(Serial.available()>0){
delay(1);
ch = Serial.read();
Serial.print(ch);
}
}
}
String get_serial_contents(){
Serial.println("Getting Serial data");
char character;
String contents;
while(Serial.available()){
delay(10);
character = Serial.read();
contents.concat(character);
}
Serial.flush();
return contents;
}