[ISSUE] sendATcommand with Geeetech SIM900 + Arduino Mega

Hi guys,

First of all, sry for bad english.

I bought this month a Geeetech SIM900 to connect my Arduino Mega to the Internet. My project is send some files into a FTP server by this shield.

So first I tried to comunicate SIM900 with Mega 2560 using SoftwareSerial by pin 10, 11. With success… I did calls, sent messages, and I sent a file into my FTP server. Now I connect SIM900 to Mega 2560 using HWSerial at Serial1, and I can make calls and send messages too, resuming they are CONNECTED and COMMUNICATING.

Now, I want to do this automatically. Searching some projects at Google I found this function: sendATcommand, and I need to use this function because I need to know the answer of the shield to know which commands needs to be sent. Example: if shield returns OK, I can go foward, else wait 20 seconds and try again.

Actually, my code is pretty simple:

int8_t resposta;

void setup() {
  Serial.begin(19200);
  Serial.println("Serial: Begin..." );
  Serial1.begin(19200);
  Serial.println("Serial1: Begin..." );
  delay(1000);
  Serial.println("Testing SENDATCOMMAND...");
  delay(1000);
  Serial.println("Sending command: AT");
  delay(1000);
  sendATcommand("AT", "OK", 2000);
  while(resposta == 0){     // Send AT every two seconds and wait for the answer
    resposta = sendATcommand("AT", "OK", 2000); 
    Serial.println("NOT CONNECTED");   
}
  Serial.println("CONNECTED");
  delay(1000);
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout){
    uint8_t x=0,  answer=0;
    char response[100];
    unsigned long previous;
 
    memset(response, '\0', 100); // Clean response buffer
    
    delay(100); // Delay to be sure no passed commands interfere
    
    while( Serial1.available() > 0) Serial1.read();    // Wait for clean input buffer
    
    Serial1.println(ATcommand);    // Send the AT command 
    Serial.print("HERE 1");
    Serial.print("||");
    previous = millis();
 
    // this loop waits for the answer
    do{
        // if there are data in the UART input buffer, reads it and checks for the asnwer
        if(Serial1.available() != 0){    
            response[x] = Serial1.read();
            x++;
            Serial.print("HERE 2");
            Serial.print("|||");
            Serial.print(response);
            // check if the desired answer is in the response of the module
            if (strstr(response, expected_answer) != NULL){
                answer = 1;
                Serial.print(response);
                Serial.print("||||");
            }
        }
    // Waits for the answer with time out
    } while((answer == 0) && ((millis() - previous) < timeout));    
 
    return answer;
}

Note: I put “HERE 1, HERE 2” and Serial.print(response) to know where is the issue.

And the output is this:

Serial: Begin...
Serial1: Begin...
Testing SENDATCOMMAND...
Sending command: AT
HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHERE 2|||HEREHERE 2|||HERE HERE 2|||HERE 1HERE 2|||HERE 1|HERE 2|||HERE 1||HERE 2|||HERE 1||HHERE 2|||HERE 1||HEHERE 2|||HERE 1||HERHERE 2|||HERE 1||HEREHERE 2|||HERE 1||HERE HERE 2|||HERE 1||HERE 2HERE 2|||HERE 1||HERE 2|HERE 2|||HERE 1||HERE 2||HERE 2|||HERE 1||HERE 2|||HERE 2|||HERE 1||HERE 2|||HHERE 2|||HERE 1||HERE 2|||HHHERE 2|||HERE 1||HERE 2|||HHEHERE 2|||HERE 1||HERE 2|||HHERHERE 2|||HERE 1||HERE 2|||HHEREHERE 2|||HERE 1||HERE 2|||HHERE HERE 2|||HERE 1||HERE 2|||HHERE 2HERE 2|||HERE 1||HERE 2|||HHERE 2|HERE 2|||HERE 1||HERE 2|||HHERE 2||HERE 2|||HERE 1||HERE 2|||HHERE 2|||HERE 2|||HERE 1||HERE 2|||HHERE 2|||HHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHEHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHEREHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE HERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2HERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|HERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2||HERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HEHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHEHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHERHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHEREHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHERE HERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHERE 2HERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHERE 2|HERE […] 2|||HEHERE 2|||HERHERE 2|||HEREHEREHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHERE 2|||HEREHERE HERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHERE 2|||HEREHERE 2HERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHERE 2|||HEREHERE 2|HERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHERE 2|||HEREHERE 2||HERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHERE 2|||HEREHERE 2|||HERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHERE 2|||HEREHERE 2|||HHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHERE 2|||HEREHERE 2|||HEHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHERE 2|||HEREHERE 2|||HERHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHERE 2|||HEREHERE 2|||HEREHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHERE 2|||HEREHERE 2|||HERE HERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHERE 2|||HEREHERE 2|||HERE HHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHERE 2|||HEREHERE 2|||HERE HEHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHERE 2|||HEREHERE 2|||HERE HERHERE 2|||HERE 1||HERE[b][...][/b]RE 2|||HERE 1||HERE 2|||HHEREHERE 2|||HERE 1||HERE 2|||HHERE HERE 2|||HERE 1||HERE 2|||HHERE 2HERE 2|||HERE 1||HERE 2|||HHERE 2|HERE 2|||HERE 1||HERE 2|||HHERE 2||HERE 2|||HERE 1||HERE 2|||HHERE 2|||HERE 2|||HERE 1||HERE 2|||HHERE 2|||HHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHEHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHEREHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE HERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2HERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|HERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2||HERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HEHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHEHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHERHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHEREHERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHERE HERE 2|||HERE 1||HERE 2|||HHERE 2|||HEHERE 2|||HERHERE 2HERE 2|||HERE 1||HERE [….] 2|||HHERE 2|||HEHERE 2|||HERHERE 2|||HEREHERE 2|||HERE HERE 2H HE |NOT CONNECTED
.......

You can see that the result of this is NOT CONNECTED, but the shield is connected and ready (I can confirm this by the led, returning to SWSerial and send commands or how ever with a simple code by Serial1.println(“ATD+PHONE”); and it make a call).

Anyone know how to help me?

I’m getting crazy with this issue.

Thks.

The array response is NOT a string. You are passing something that is NOT a string to functions that expect strings. Any expectation of proper behavior when you do that is unrealistic.

Look up what distinguishes a string from a character array, an make your character array a string.