1
int8_t answer; 
int onModulePin= 2; 
int counter; 
char aux_str[150]; 
char aux; 
char frame[200]; 
char latitude[15]; 
char longitude[15]; 
char altitude[6]; 
char date[16]; 
char time[7]; 
char satellites[3]; 
char speedOTG[10]; 
char course[10]; 
int x = 0; 
char N_S,W_E;  
void setup() 
{ 
     
    //pinMode(onModulePin, OUTPUT); 
    Serial.begin(115200); 
     
    delay(3000); 
    delay(3000); 
 
 
     
    Serial.println("Starting..."); 
    delay(30000); 
    while (sendATcommand("AT+CREG?", "+CREG: 0,1", 2000) == 0); 
    // sets APN , user name and password 
    sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 2000); 
    sendATcommand("AT+SAPBR=3,1,\"APN\",\"gpinternet\"", "OK", 2000); 
    sendATcommand("AT+SAPBR=3,1,\"USER\",\"\"", "OK", 2000); 
    sendATcommand("AT+SAPBR=3,1,\"PWD\",\"\"", "OK", 2000); 
// gets the GPRS bearer 
     while (sendATcommand("AT+SAPBR=1,1", "OK", 20000) == 0) 
    { 
        delay(5000); 
    } 
     
    
    delay(3000); 
     
   // sets APN, user name and password 
//    sendATcommand("AT+CGPSPWR=1", "OK", 2000); 
//    sendATcommand("AT+CGPSRST=0", "OK", 2000); 
     
    while( (sendATcommand("AT+CGPSSTATUS?", "2D Fix", 5000) ||  
            sendATcommand("AT+CGPSSTATUS?", "3D Fix", 5000) || 
            sendATcommand("AT+CGPSSTATUS?", "Unknown", 5000)) == 0 ); 
 
 
}  
int8_t get_GPS() 
{ 
    int8_t counter, answer; 
    long previous; 
    // First get the NMEA string 
    // Clean the input buffer 
    while( Serial. Available() > 0) Serial. Read();  
    // request Basic string 
    sendATcommand("AT+CGPSINF=0", "AT+CGPSINF=0\r\n\r\n", 2000); 
    counter = 0; 
    answer = 0; 
    memset(frame, '\0', 100);    // Initialize the string 
    previous = millis(); 
// this loop waits for the NMEA string 
do 
{ 
        if(Serial.available() != 0){     
            frame[counter] = Serial.read(); 
            counter++; 
            // check if the desired answer is in the response of the module 
            if (strstr(frame, "OK") != NULL)     
            { 
                answer = 1; 
            } 
 
 
        } 
        // Waits for the asnwer with time out 
    } 
    while((answer == 0) && ((millis() - previous) < 2000));   
    frame[counter-3] = '\0';     // '\0' where it finds the token you supplied, and returns a pointer to the start of the string.                      
                                                            
// Parses the string  
    strtok(frame, ","); 
    strcpy(longitude,strtok(NULL, ",")); // Gets longitude 
    strcpy(latitude,strtok(NULL, ",")); // Gets latitude 
    strcpy(altitude,strtok(NULL, ".")); // Gets altitude  
    strtok(NULL, ",");     
    strcpy(date,strtok(NULL, ".")); // Gets date 
    strtok(NULL, ","); 
    strtok(NULL, ",");   
    strcpy(satellites,strtok(NULL, ",")); // Gets satellites 
    strcpy(speedOTG,strtok(NULL, ",")); // Gets speed over ground. 
  return answer;  
    // convert string to integer and add it to final float variable 
deg = atof(aux); 
    strcpy(aux, strtok(NULL, '\0')); 
    minutes=atof(aux); 
    if (deg < 100) 
    { 
        minutes += deg; 
 
 
        deg = 0; 
    } 
    else 
    { 
        minutes += int(deg) % 100; 
        deg = int(deg) / 100;     
    } 
     
    if( deg < 0 ) 
    { 
        neg = true; 
        deg*=-1; 
    } 
void loop()  
{ 
  
 delay(1000); 
delay(1000); 
 Serial.print("AT+CMGF=1\r");               // AT command to select SMS format. 
 delay(100); 
 Serial.println("AT + CMGS = \"+880XXXXXXXXXX\"");   // AT command to send SMS to recipient's mobile number,in international format.          
                                                                                                     
                                                                                                      
delay(100); 
 Serial.println("Here is my latitude and longitude;"); 

 
 Serial.println(latitude);   
 Serial.println(longitude);  // message to send 
  
   delay(100); 
 Serial.println((char)26);                       // End AT command with a ^Z, ASCII code 26 
  delay(100); 
 Serial.println(); 
  delay(5000);             
} 
// Clean the input buffer 
    while( Serial.available() > 0) Serial.read();     
     
    Serial.println(ATcommand);    // Send the AT command  
    x = 0; 
    previous = millis(); 
 
    // this loop waits for the answer 
    do 
{ 
        if(Serial.available() != 0){     
            response[x] = Serial.read(); 
            x++; 
            // check if the desired answer is in the response of the module 
            if (strstr(response, expected_answer1) != NULL)     
            { 
 
 
                answer = 1; 
            } 
        } 
        // Waits for the asnwer with time out 
    }while((answer == 0) && ((millis() - previous) < timeout));     
  //response[x] = 0; 
  //Serial.println(response); 
    return answer; 
}  
3
  • 1
    Sorry, but it looks like you have blindly copy & pasted some codes together like frankenstein. There are obviously some missing parts (looks like you lost multiple function declarations, for example after loop() and after get_GPS()). And maybe due to that you have mismatched curly braces. And indeed sendATcommand() is not declared anywhere. You cannot just copy different codes together, messing them up and then think, that it still works. You need to actually understand, what these codes mean and how they work. Please do a basic C/C++ tutorial to learn the syntax. – chrisl Apr 20 at 10:20
  • 3
    I’m voting to close this question because its about C/C++ syntax and not about Arduino. – chrisl Apr 20 at 10:20
  • 3
    Unless you write a sendATcommand() function the rest of the code will never be able to find it! Think about what you are doing - don't just blindly copy and paste random chunks of code from random websites. – Majenko Apr 20 at 11:00