Help compiling program

I’m trying to program a realtime GPS - GPRS tracker.
When I compile the program I get these errors.

g++ -lrt -lpthread arduPi_template.cpp arduPi.cpp -o arduPi_template
arduPi_template.cpp:56:12: error: ‘char time [7]’ redeclared as different kind of symbol
/usr/include/time.h:186:15: error: previous declaration of ‘time_t time(time_t*)’
arduPi_template.cpp: In function ‘void setup()’:
arduPi_template.cpp:68:14: error: ‘power_on’ was not declared in this scope
arduPi_template.cpp:73:45: error: ‘sendATcommand’ was not declared in this scope
arduPi_template.cpp:78:23: error: ‘start_GPS’ was not declared in this scope
arduPi_template.cpp: In function ‘void loop()’:
arduPi_template.cpp:99:13: error: ‘get_GPS’ was not declared in this scope
arduPi_template.cpp:102:15: error: ‘send_HTTP’ was not declared in this scope
arduPi_template.cpp: In function ‘void power_on()’:
arduPi_template.cpp:113:44: error: ‘sendATcommand’ was not declared in this scope
arduPi_template.cpp: In function ‘int8_t start_GPS()’:
arduPi_template.cpp:136:45: error: ‘sendATcommand’ was not declared in this scope
arduPi_template.cpp: In function ‘int8_t get_GPS()’:
arduPi_template.cpp:163:63: error: ‘sendATcommand’ was not declared in this scope
arduPi_template.cpp:200:29: error: ‘convert2Degrees’ was not declared in this scope
arduPi_template.cpp: In function ‘void send_HTTP()’:
arduPi_template.cpp:313:54: error: ‘sendATcommand’ was not declared in this scope
arduPi_template.cpp:334:45: error: ‘F’ was not declared in this scope
arduPi_template.cpp:338:57: error: ‘F’ was not declared in this scope
arduPi_template.cpp:344:57: error: ‘F’ was not declared in this scope
arduPi_template.cpp:349:53: error: ‘F’ was not declared in this scope
arduPi_template.cpp:354:48: error: ‘F’ was not declared in this scope

In attachment you can find the program code.
Any help is welcome and thanks in advance!!

program.txt (9.58 KB)

Is your program meant to be built with this...

If yes, it is certainly possible for you to get help here but I suspect you will be better served by either the library's author or on a Raspberry Pi forum.

This is where I got the hardware from and I followed the instructions on the site.
I will also post the problem on a Raspberry Pi forum thanks.

If anyone can help me further here it would be off great help.

Are you able to compile an empty sketch...

void setup( void ) { }
void loop( void ) { }

Does it compile if you comment out the arduPi header / libraray?
My does.
My best guess is that you have conflicts with variables between your code and library.
For example you have variable time and function time.
In C++ such conflict exists between modules / libraries.
I feel it should not matter, but I ain't no expert on this, I would rename your time to start and try that.
I am not sure if the main function should be there also.

Vaclav

//Include ArduPi library
//#include "arduPi.h"

Dear coding badly,

When I compile given empty sketch I get the following.
(.text+0x34): undefined reference to 'main'
collect2: ld returned 1 exit status.

What would the next step be?

Dear vaclav,

I tried commenting out the library but then I get even more errors.


How does one declare something in scope.

Thank you for helping me further.

Can you post your latest code?
It should compile without the library.
Ditch the main. You do not need it. It is defined elsewhere.
I am not sure what would be the correct term - but the compiler works from your sketch text file and plugs it into main.
I don't have the link to main.cpp handy, just goolge for it and it should clear things a little.

As codingbadly said you should look at the link he send you.
It sure looks as whatever you got won't compile as run of the mill sketch.
Cheers
Vaclav

Try this, it should work (at least, it works for me :wink: ) :

  • rename variable time[7] as mytime[7];
  • declare all the functions just before the line setup()

for example, you should have :

int8_t answer;
.....
......
char altitude[6];
char mydate[16];
char mytime[7];
....
....
char course[10];
uint8_t get_GPS();    // declaration of function, implementaded later on 
void power_on();  // and so on for each funtion 
....
....
void setup(){
      pinMode(onModulePin, OUTPUT);
........
.........

}

edit (precision :wink: ) : I didn’t do it for all the errors messages, I just gave a try with time[7]mytime[7] and made the declaration for 2 functions : get_GPS() and power_on() . The corresponding errors disappeared, which seems normal, functions must be declared before being used - Arduino IDE doesn’t need it, but C++ compilers do

alnath:
Try this, it should work (at least, it works for me :wink: ) :

  • rename variable time[7] as mytime[7];
  • dclare all the functions just before the line setup()

for example, you should have :

int8_t answer;

.....
......
char altitude[6];
char mydate[16];
char mytime[7];
....
....
char course[10];
uint8_t get_GPS();    // declaration of function, implementaded later on
void power_on();  // and so on for each funtion
....
....
void setup(){
      pinMode(onModulePin, OUTPUT);
........
.........

}

Good catch, I think the compiler should flag undeclared functions, but too many errors confuse it and mask it.
You deserve attaboy!
Cheers
Vaclav .

Guess what , compiler does not care if the function is not declared!
I grew up with ANSI C standard.
I think without need for declaration one become lazy and sloppy coder.
Too bad.
Cheers
Vaclav

void setup() {
  //put your setup code here, to run once:
  Serial.begin(9600);
    
}

void loop() {
  // put your main code here, to run repeatedly: 
  int result = Test(100);
  Serial.print(result);
  delay (5000);
  
  
}

int Test(int TEST)
{
  return TEST*2;
}

Moderator edit: I think that without CODE TAGS, one becomes a sloppy poster.

Vaclav:
Guess what , compiler does not care if the function is not declared!

Yep, that’s what I said before, arduino doesn’t care, but other compilers do. On the PI, it is g++ : the functions must be either declared or defined before they are called.
BTW, PeterPBA , I’ve done all the changes, and it compiles OK now.
Actually, it still complained about “F is not declared…” → I had to change the Serial.println(F("…")); calls to Serial.println("…");
Then it gave some warning about SendATcommand(char*, char*, unsigned int); which I had to change by
SendATcommand(const char*, const char*, unsigned int);

Now, it compiles OK - If you want to try it, I’ve attached it at the post.
The file is called program.cpp , you need to rename it so that its name match your command line (or you change the name in the command line :wink: ) .
Warning : I don’t know if it works, I can’t test i****t, but it compiles without error/warning , that’s all I can tell you . I made NO other changes than those I’ve described above.
Lats thing : instead of rewriting a declaration for each function, I just reordered the code so that each function is defined before it is called :wink:

program.cpp (9.28 KB)

PeterPBA:
When I compile given empty sketch I get the following.
(.text+0x34): undefined reference to 'main'
collect2: ld returned 1 exit status.
What would the next step be?

Never mind. I see from the Cooking Hacks webpage they expect you to provide a main.

YES, indeed it runs without errors, BUT
now i placed some printf to know what exactly he is doing ... he seems to hang in the Power_on () ... answer stay 0

        while(answer == 0)
                         {  
                         // Send AT every two seconds and wait for the answer   
                        answer = sendATcommand("AT", "OK", 2000);    
                        }

now i placed some printf to know what exactly he is doing … he seems to hang in the Power_on () … answer stay 0

Well, maybe he needs a swift kick in the ass.

Or you do, for not posting ALL of your code.

This is the code as it is right now.
I compiles, but gets stuck as mentioned before.
Thanks you all for the great help you’ve been so far.

//Include ArduPi library
#include "arduPi.h"
 
int8_t answer;
int onModulePin= 2;
char data[100];
int data_size;
char aux_str[30];
char aux;
int x = 0;
char N_S,W_E;
char url[] = "your ip address or your domain";
char frame[200];
char latitude[15];
char longitude[15];
char altitude[6];
char date[16];
char mytime[7];
char satellites[3];
char speedOTG[10];
char course[10];

int8_t sendATcommand(const char* ATcommand, const char* expected_answer1, unsigned int timeout){
    uint8_t x=0,  answer=0;
    char response[100];
    unsigned long previous;
    memset(response, '\0', 100);    // Initialize the string
    delay(100);
    while( Serial.available() > 0) Serial.read();    // Clean the input buffer
    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));    
    return answer;
} 

void power_on(){
    uint8_t answer=0;
    // checks if the module is started
    answer = sendATcommand("AT", "OK", 2000);
    printf("%d\n",answer);  
    if (answer == 0)
    {
        // power on pulse

        digitalWrite(onModulePin,HIGH);
        delay(3000);
        digitalWrite(onModulePin,LOW);
        // waits for an answer from the module
        while(answer == 0){  
            // Send AT every two seconds and wait for the answer   
            answer = sendATcommand("AT", "OK", 2000);    
            printf("%d\n",answer);  
        }
    }
}
/* convert2Degrees ( input ) - performs the conversion from input 
 * parameters in  DD°MM.mmm’ notation to DD.dddddd° notation. 
 * 
 * Sign '+' is set for positive latitudes/longitudes (North, East)
 * Sign '-' is set for negative latitudes/longitudes (South, West)
 *  
 */

int8_t convert2Degrees(char* input){
    float deg;
    float minutes;
    boolean neg = false;    
    //auxiliar variable
    char aux[10];
    if (input[0] == '-')
    {
        neg = true;
        strcpy(aux, strtok(input+1, "."));
    }
    else
    {
        strcpy(aux, strtok(input, "."));
    }
    // convert string to integer and add it to final float variable
    deg = atof(aux);
    strcpy(aux, strtok(NULL, '\0'));
    minutes=atof(aux);
    minutes/=1000000;
    if (deg < 100)
    {
        minutes += deg;
        deg = 0;
    }
    else
    {
        minutes += int(deg) % 100;
        deg = int(deg) / 100;    
    }
    // add minutes to degrees 
    deg=deg+minutes/60;
    if (neg == true)
    {
        deg*=-1.0;
    }
    neg = false;
    if( deg < 0 ){
        neg = true;
        deg*=-1;
    }
    
    float numeroFloat=deg; 
    int parteEntera[10];
    int cifra; 
    long numero=(long)numeroFloat;  
    int size=0;
    
    while(1){
        size=size+1;
        cifra=numero%10;
        numero=numero/10;
        parteEntera[size-1]=cifra; 
        if (numero==0){
            break;
        }
    }
   
    int indice=0;
    if( neg ){
        indice++;
        input[0]='-';
    }
    for (int i=size-1; i >= 0; i--)
    {
        input[indice]=parteEntera[i]+'0'; 
        indice++;
    }
    input[indice]='.';
    indice++;
    numeroFloat=(numeroFloat-(int)numeroFloat);
    for (int i=1; i<=6 ; i++)
    {
        numeroFloat=numeroFloat*10;
        cifra= (long)numeroFloat;          
        numeroFloat=numeroFloat-cifra;
        input[indice]=char(cifra)+48;
        indice++;
    }
    input[indice]='\0';
}

int8_t start_GPS(){
    unsigned long previous;
    previous = millis();
    // starts the GPS
    sendATcommand("AT+CGPSPWR=1", "OK", 2000);
    sendATcommand("AT+CGPSRST=0", "OK", 2000);
    // waits for fix GPS
    while(( (sendATcommand("AT+CGPSSTATUS?", "2D Fix", 5000) || 
        sendATcommand("AT+CGPSSTATUS?", "3D Fix", 5000)) == 0 ) && 
        ((millis() - previous) < 90000));
    if ((millis() - previous) < 90000)
    {
        return 1;
    }
    else
    {
        return 0;    
    }
}
//int8_t get_GPS();

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'; 
    
    // 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. Unit is knots.
    strcpy(course,strtok(NULL, "\r")); // Gets course
    convert2Degrees(latitude);
    convert2Degrees(longitude);
    
    return answer;
}
void send_HTTP(){
    
    // Initializes HTTP service
    answer = sendATcommand("AT+HTTPINIT", "OK", 10000);
    if (answer == 1)
    {
        // Sets CID parameter
        answer = sendATcommand("AT+HTTPPARA=\"CID\",1", "OK", 5000);
        if (answer == 1)
        {
            // Sets url 
            sprintf(aux_str, "AT+HTTPPARA=\"URL\",\"http://%s/demo_sim908.php?", url);
            Serial.print(aux_str);
            sprintf(frame, "visor=false&latitude=%s&longitude=%s&altitude=%s&mytime=%s&satellites=%s&speedOTG=%s&course=%s",
            latitude, longitude, altitude, date, satellites, speedOTG, course);
            Serial.print(frame);
            answer = sendATcommand("\"", "OK", 5000);
            if (answer == 1)
            {
                // Starts GET action
                answer = sendATcommand("AT+HTTPACTION=0", "+HTTPACTION:0,200", 30000);
                if (answer == 1)
                {
                    Serial.println("Done!");
                }
                else
                {
                    Serial.println("Error getting url");
                }
            }
            else
            {
                Serial.println("Error setting the url");
            }
        }
        else
        {
            Serial.println("Error setting the CID");
        }    
    }
    else
    {
        Serial.println("Error initializating");
    }
    sendATcommand("AT+HTTPTERM", "OK", 5000);
    
}

void setup(){
    printf("Setup Pinmode\n");
    pinMode(onModulePin, OUTPUT);  
    printf("Setup Serial\n");
    Serial.begin(115200);   
    Serial.println("Starting...");
    printf("Setup Power On\n");
    power_on();
    printf("Setup Delay\n");
    delay(3000);
    // sets the PIN code
    printf("Setup Pin\n");
    sendATcommand("AT+CPIN=****", "OK", 2000);
    printf("Setup Delay\n");
    delay(3000);
    printf("Setup GPS Wait for signal\n");  
    // starts the GPS and waits for signal
    while ( start_GPS() == 0);
    printf("Setup CReg\n");  
    while (sendATcommand("AT+CREG?", "+CREG: 0,1", 2000) == 0);
    // sets APN , user name and password
    printf("Setup ATComms\n");  
    sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 2000);
    sendATcommand("AT+SAPBR=3,1,\"APN\",\"apn\"", "OK", 2000);
    sendATcommand("AT+SAPBR=3,1,\"USER\",\"user_name\"", "OK", 2000);
    sendATcommand("AT+SAPBR=3,1,\"PWD\",\"password\"", "OK", 2000);
    // gets the GPRS bearer
    while (sendATcommand("AT+SAPBR=1,1", "OK", 20000) == 0)
    {
        delay(5000);
    }
    printf("End Setup\n");  

}
void loop(){
    // gets GPS data
    get_GPS();
    
    // sends GPS data to the script
    send_HTTP();
    delay(5000);
}


int main (){
printf("Starting Setup\n");
setup();
printf("Finished Setup\n");
while(1){
loop();
}
return (0);
}
    Serial.begin(115200);

1 - are you sure your module communicates at that speed ?? I doubt it

2- a) which module is it ?
b) how and where do you tell it it must be in GPS or in GSM mode ?

3- how is it wired ?

    memset(response, '\0', 100);    // Initialize the string

Are 100 stop signs more effective than 1? Doing this so you can skip the proper NULL termination of the array is laziness.

What Arduino are you using? Have you checked that you are not running out of memory?

I got the hardware from this site, normally everyting should work.
I'm using a Raspberry Pi, I know it is an arduino forum but this is the only place that has been of any help and it should program the same.

Maybe you should go step by step and begin with the Step 5 of the tutorial :

Step 5: Using the shield in standalone mode
Sending and receiving SMS

Does it work ?
if no, how is the status Led ? is it OFF ? does she blink 64ms ON/800ms OFF or 64ms ON/3000ms OFF ?

I don't know this particular module, but I have a sim908 shield, and mine :

  • needs more than 3 seconds to start up
  • doesn't answer "OK" to the 1rst 'AT' command -

instead of printing the answer value (0 or 1) , you should print the module response, it would be more useful to see what the module sends.

I'm using a Raspberry Pi, I know it is an arduino forum but this is the only place that has been of any help and it should program the same.

That final assumption is rubbish.