Please help with C programming to save GPS reception data in Raspberry Pi.

Joined
Dec 8, 2022
Messages
1
Reaction score
0
I want to connect the GPS to Raspberry Pi and save the data.
Excluding the MySQL source, the GPS contact data is output well.
A DB error appears when trying to save the received GPS data to MySQL. can you help me?



#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <wiringPi.h>
#include <wiringSerial.h>

#include <mysql/mysql.h>

#include <time.h>
#include <math.h>

#define DBHOST "192.168.0.4"
#define DBUSER "root"
#define DBPASS "pi"
#define DBNAME "pi"

MYSQL *connector;
MYSQL_RES *result;
MYSQL_ROW row;

int main ()
{
int serial_port;
char dat,buff[100],GGA_code[3];
unsigned char IsitGGAstring=0;
unsigned char GGA_index=0;
unsigned char is_GGA_received_completely = 0;

if ((serial_port = serialOpen ("/dev/ttyACM0", 115200)) < 0)
{
fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
return 1 ;
}

if (wiringPiSetup () == -1)
{
fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
return 1 ;
}

while(1){

if(serialDataAvail (serial_port) )
{
dat = serialGetchar(serial_port);
if(dat == '$'){
IsitGGAstring = 0;
GGA_index = 0;
}
else if(IsitGGAstring ==1){
buff[GGA_index++] = dat;
if(dat=='\r')
is_GGA_received_completely = 1;
}
else if(GGA_code[0]=='G' && GGA_code[1]=='G' && GGA_code[2]=='A'){
IsitGGAstring = 1;
GGA_code[0]= 0;
GGA_code[0]= 0;
GGA_code[0]= 0;
}
else{
GGA_code[0] = GGA_code[1];
GGA_code[1] = GGA_code[2];
GGA_code[2] = dat;
}
}
if(is_GGA_received_completely==1){
printf("GGA: %s",buff);
is_GGA_received_completely = 0;
}


connector = mysql_init(NULL);
if (connector, DBHOST, DBUSER, DBPASS, DBNAME, 3306, NULL, 0)
{
fprintf(stderr, "%s/n", mysql_error(connector));
return 0;
}

printf("MySQL(pidb) opened.\n");

delay(10000);

while(1)
{
char query[1024];

buff[0];

sprintf(query, "insert into gps data values (now(),%d)",
buff[0],0);

if (mysql_query(connector, query))
{
fprintf(stderr, "%s\n", mysql_error(connector));
printf("Write DB error\n");
}

delay(10000);
}

mysql_close(connector);

}
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,703
Messages
2,569,328
Members
44,612
Latest member
Billysak

Latest Threads

Top