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;
}
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;
}