Skip to content

Instantly share code, notes, and snippets.

@vishnumaiea
Last active December 8, 2018 08:03
Show Gist options
  • Save vishnumaiea/c403bdeafba22d126404c30097e4886d to your computer and use it in GitHub Desktop.
Save vishnumaiea/c403bdeafba22d126404c30097e4886d to your computer and use it in GitHub Desktop.
GPS Module Code
#include <SoftwareSerial.h>
/***********************************************************************
Copyright (c) 2015 Vishnu M Aiea
Original Source : www.rhydoLabz.com
Date created : 06-02-2015
Last modified : 06-02-2015
Contact : [email protected]
Website : www.vishnumaiea.in
The following program is useful for monitoring Location data and
velocity data from a GPS module with UART through a serial monitor.
The module used here is from www.rhydolabz.com and the part number
is 'GPS-2261'. The module has the MediaTek MT3329 chipset which uses
the standard NMEA 0183 output format.
This program prints Latitude and Longitude in DMS format along with
directions and speed in knots.
The original code is from the rhydoLabz website and I modified it
for monitoring minimum location/transit data.
***********************************************************************/
SoftwareSerial gpsSerial(13, 12); //Rx and Tx
int byte_buffer; // receive serial data byte
unsigned int finish = 0; // indicates end of message
unsigned int pos_cnt = 0; // position counter
unsigned int lat_cnt = 0; // latitude data counter
unsigned int log_cnt = 0; // longitude data counter
unsigned int spd_cnt = 0; //speed data counter
unsigned int flag = 0; // GPS flag
unsigned int com_cnt = 0; // comma counter
char latitude[20]; // latitude array
char longitude[20]; // longitude array
char GPS_speed[10]; // GPS speed array
char lat_dir; // latitude direction
char long_dir; // longitude direction
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
gpsSerial.begin(9600);
}
void loop() {
Receive_GPRMC();
Serial.print("Latitude = ");
Serial.println(latitude);
Serial.print("Lat Direction = ");
Serial.println(lat_dir);
Serial.print("Longitude = ");
Serial.println(longitude);
Serial.print("Long Direction = ");
Serial.println(long_dir);
Serial.print("Speed = ");
Serial.println(GPS_speed);
Serial.println();
finish = 0;
pos_cnt = 0;
}
void Receive_GPRMC() {
while (finish == 0) {
while (gpsSerial.available() > 0) { // Check GPS data
byte_buffer = gpsSerial.read();
Serial.println(byte_buffer);
flag = 1;
if ( byte_buffer == '$' && pos_cnt == 0) // finding GPRMC header
pos_cnt = 1;
if ( byte_buffer == 'G' && pos_cnt == 1)
pos_cnt = 2;
if ( byte_buffer == 'P' && pos_cnt == 2)
pos_cnt = 3;
if ( byte_buffer == 'R' && pos_cnt == 3)
pos_cnt = 4;
if ( byte_buffer == 'M' && pos_cnt == 4)
pos_cnt = 5;
if ( byte_buffer == 'C' && pos_cnt == 5)
pos_cnt = 6;
if ( pos_cnt == 6 && byte_buffer == ',') { // count commas in message
com_cnt++;
flag = 0;
}
if (com_cnt == 3 && flag == 1) {
latitude[lat_cnt++] = byte_buffer; // Latitude
flag = 0;
}
if (com_cnt == 4 && flag == 1) {
lat_dir = byte_buffer; // Latitude Direction N/S
flag = 0;
}
if (com_cnt == 5 && flag == 1) {
longitude[log_cnt++] = byte_buffer; // Longitude
flag = 0;
}
if (com_cnt == 6 && flag == 1) {
long_dir = byte_buffer; // Longitude Direction E/W
flag = 0;
}
if (com_cnt == 7 && flag == 1) {
GPS_speed[spd_cnt++] = byte_buffer; // Speed in knot
flag = 0;
}
if ( byte_buffer == '*' && com_cnt >= 7) { // end of GPRMC message
com_cnt = 0;
lat_cnt = 0;
log_cnt = 0;
spd_cnt = 0;
flag = 0;
finish = 1;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment