0

I constantly pass AT commands to get GSM Signal Strength My code copies the entire serial output Kindly advise how to read the latest serial output (last line)

find the output below, in which i need to assign the output from last line (21,0) to the variable "signal"

My Output:

AT 

OK
AT+CREG?
+CREG: 0,1

ok
AT+CSQ

+CSQ: 21,0

My code:

byte gsmDriverPin[3] = {
3,4,5};

char signal[10];

char inChar;
int index;
char inData[200];


void setup()
{    
//Init the driver pins for GSM function
for(int i = 0 ; i < 3; i++){
pinMode(gsmDriverPin[i],OUTPUT);
}
digitalWrite(5,HIGH);//Output GSM Timing 
delay(1500);
digitalWrite(5,LOW);  
digitalWrite(3,LOW);//Enable the GSM mode
digitalWrite(4,HIGH);//Disable the GPS mode
delay(2000);
Serial.begin(9600); //set the baud rate
delay(5000);//call ready
delay(5000);
delay(5000);
start_GSM();

}

void loop()
{  
Signal_Strength();
Serial.println("AT+CMGF=1");
delay(1000);
Serial.println("AT+CMGS=\"1234567890\"");//Change the receiver phone number
delay(1000);
Serial.println(signal);
delay(1000);
Serial.write(26);
}

void Signal_Strength(){
Serial.println("AT+CSQ");
delay(2000);
read_String();    
strtok(inData, ",");
strcpy(signal,strtok(NULL, ",")); 

}

void read_String() {
index=0;

while(Serial.available() > 0) // Don't read unless
// there you know there is data
 {
 if(index < 199) // One less than the size of the array
 {
 inChar = Serial.read(); // Read a character
 inData[index] = inChar; // Store it
 index++; // Increment where to write next
 inData[index] = '\0'; // Null terminate the string
 }
 }
 }

 void start_GSM(){
 //Configuracion GPRS Claro Argentina
Serial.println("AT");
delay(2000);
Serial.println("AT+CREG?");
delay(2000);
 }
Jay Chang
  • 1
  • 1
  • 3
  • Try resetting your collection routine each time you get a newline, and at that time see if what you have collected since the last one is interesting to parse. Or build a state machine which processes character by character as they arrive. – Chris Stratton Dec 04 '13 at 15:04
  • @chirs, could you please elaborate or give some sample codes to work on – Jay Chang Dec 05 '13 at 02:13

1 Answers1

2

First of all, you must seriously redo your AT command handling to

  • Read and parse the every single response line given back from the modem until you get a final result code. This applies for every single command line invocation, no exceptions whatsoever. See this answer for more details.
  • Never ever call delay in any code that handles AT commands. See this answer for more details about the risk of aborting the next command.

Before fixing these fundamental issues you cannot expect any successful behaviour. Parsing AT command responses is not that complicated, have a look at the source code for atinout for an example.

Community
  • 1
  • 1
hlovdal
  • 26,565
  • 10
  • 94
  • 165