Connect to USB GPS

1. Get a USB GPS module from somewhere like Maplin (here's mine - http://www.maplin.co.uk/usb-gps-dongle-476503)

2. Add kernel modules to support USB serial converters (under Kernel Modules / USB Support / kmod-usb-serial), mine is PL2303 although I've also seen Option types.

3. opkg install gpsd

4. Plug in USB device. It should appear as /dev/ttyUSB0 or something like that.

5. Make sure gpsd is configured correctly under /etc/gpsd.conf

6. /etc/init.d/gpsd start

Now you have a gpsd daemon running.

Normally you connect to the gpsd daemon using a perl script or something similar but that might be too heavyweight for your needs. Here's the source code of a command line program to ask gpsd for the location, return the result then tell gpsd to stop returning results. gpsd_data.c

#include <stdio.h>

#include <netinet/tcp.h>

#include <sys/socket.h>

#include <sys/ioctl.h>

#include <termios.h>

#include <unistd.h>

#include <fcntl.h>

#include <sys/signal.h>

#include <sys/types.h>

#include <stdlib.h>

#include <string.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <netdb.h>

int main() {

struct sockaddr_in struc_Server_Address, struc_Client_Address;

// The hardware descriptor to use to communicate with the open port

int i_IP_Descriptor;

//The amount of bytes read from the port at the last attempt

int i_Read_Amount;

//Storage of the last byte read from the port

unsigned char uc_Read_Byte;

unsigned char uc_Write_Byte;

unsigned char uca_Write_Bytes[2000];

//Used in client mode. Is used to store returned information about the remote server

struct hostent *struc_Host;

char chra_Server_IP_Address[]="127.0.0.1";

int i_Port=2947;

char chra_Data_Line[2000];

int i_Data_Line_Length=0;

//printf("Starting!\n");

i_IP_Descriptor = socket(AF_INET, SOCK_STREAM, 0);

//Was client successfully created?

if (i_IP_Descriptor < 0) {

//Client socket could not be created so log error

printf("ip_interface.c: ERROR binding client socket\n");

//Error so return null

return 0;

}

//Get information regarding the remote server

struc_Host = (struct hostent*)gethostbyname(chra_Server_IP_Address);

//Could server stats be queried?

if (struc_Host == NULL) {

//Server did not reply so log error

printf("ip_interface.c: ERROR client no host like %s \n",chra_Server_IP_Address);

//Error so return null

return 0;

}

//Clear the server structure with zeros

bzero((char *) &struc_Server_Address, sizeof(struc_Server_Address));

//Set up the server connection settings

struc_Server_Address.sin_family = AF_INET;

bcopy((char *)struc_Host->h_addr,(char *)&struc_Server_Address.sin_addr.s_addr,struc_Host->h_length);

struc_Server_Address.sin_port = htons(i_Port);

//Attempt connection with remote server

//Was the connection a success?

if (connect(i_IP_Descriptor,(struct sockaddr *) &struc_Server_Address,sizeof(struc_Server_Address)) < 0) {

//Could not connect to server so log error

printf("ip_interface.c: ERROR client connecting to server\n");

//Error so return null

return 0;

}

int i_Send_Buf_Size=100;

int result = setsockopt(i_IP_Descriptor,SOL_SOCKET,SO_SNDBUF,&i_Send_Buf_Size,sizeof i_Send_Buf_Size);

if(result<0) {

printf("Could not set send buf!!");

}

int i_Recv_Buf_Size=100;

result = setsockopt(i_IP_Descriptor,SOL_SOCKET,SO_RCVBUF,&i_Recv_Buf_Size,sizeof i_Recv_Buf_Size);

if(result<0) {

printf("Could not set recv buf!!");

}

//printf("Client created correctly\n");

strcpy(uca_Write_Bytes,"?WATCH={\"enable\":true,\"json\":true,\"nmea \":true,\"scaled\":true,\"timing\":true}\n");

int i_Wrote_Bytes=send(i_IP_Descriptor,uca_Write_Bytes,strlen(uca_Write_Bytes), 0);

int i_Run=1;

while(i_Run) {

i_Read_Amount = read(i_IP_Descriptor, &uc_Read_Byte, 1);

if(i_Read_Amount>0) {

chra_Data_Line[i_Data_Line_Length]=uc_Read_Byte;

i_Data_Line_Length++;

if(uc_Read_Byte=='\n') {

chra_Data_Line[i_Data_Line_Length-1]=0;

if(strncmp(chra_Data_Line,"{\"class\":\"TPV\"",14)==0) {

printf("%s\n",chra_Data_Line);

i_Run=0;

strcpy(uca_Write_Bytes,"?WATCH={\"enable\":false,\"json \":false,\"nmea\":false,\"scaled\":false,\"timing\":false}\n");

i_Wrote_Bytes=send(i_IP_Descriptor,uca_Write_Bytes,strlen(uca_Write_Bytes), 0);

}

i_Data_Line_Length=0;

}

}

}

}

You can then use the following bash script to pull out the data:

POSITION=`gpsd_data`

DATETIME=`echo $(/bin/date +"%Y-%m-%d %H:%M:%S")`

LATITUDE=`echo "$POSITION" | cut -f 6 -d "," | cut -f 2 -d ":"`

LONGITUDE=`echo "$POSITION" | cut -f 7 -d "," | cut -f 2 -d ":"`

ALTITUDE=`echo "$POSITION" | cut -f 8 -d "," | cut -f 2 -d ":"`

TRACK=`echo "$POSITION" | cut -f 9 -d "," | cut -f 2 -d ":"`

SPEED=`echo "$POSITION" | cut -f 10 -d "," | cut -f 2 -d ":"`

CLIMB=`echo "$POSITION" | cut -f 11 -d "," | cut -f 2 -d ":"`

Attached the compiled binary and the source.