Real-time bike tracker using Adafruit Ultimate GPS, Huzzah wifi, and Pubnub

Dependencies:   MBed_Adafruit-GPS-Library mbed

main.cpp

Committer:
ECE4180
Date:
2017-04-19
Revision:
0:42cb15551bf3
Child:
1:0701bf58c9fa

File content as of revision 0:42cb15551bf3:

#include "mbed.h"
#include "MBed_Adafruit_GPS.h"
#include "math.h"
Serial * gps_Serial;
Serial pc (USBTX, USBRX); //Set-up for debugging

//Take degree, minute,seconds (DMS) from GPS and convert to decimal degrees (DD) for Pubnub
float dms_convert(float dms)
{
    float sec, min, deg, dd;
    sec = (dms*100)-(floor(dms)*100);
    deg = floor(dms/100);
    min = floor(dms)-(deg*100);
    dd = deg +((min+(sec/60))/60);
    return dd;
}

int main()
{
    pc.baud(115200); //Set PC baud rate
    gps_Serial = new Serial(p9,p10);
    Adafruit_GPS myGPS(gps_Serial); //object of Adafruit's GPS class
    Timer refresh_Timer; //Timer for updated GPS information
    const int refresh_Time = 2000; //refresh time in ms


    //Initialization Commands for Adafruit_GPS
    myGPS.begin(9600);  //sets baud rate for GPS communication;
    myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
    myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
    myGPS.sendCommand(PGCMD_ANTENNA);
    float publat;
    float publong;

    pc.printf("Connection established at 115200 baud...\r\n");
    wait(1);
    refresh_Timer.start();  //starts the clock on the timer
    while(true) {
        myGPS.read();   //queries the GPS
        //If GPS has data, parse it
        if ( myGPS.newNMEAreceived() ) {
            if ( !myGPS.parse(myGPS.lastNMEA()) ) {
                continue;
            }
        }

        //check if enough time has passed to warrant sending new GPS data
        if (refresh_Timer.read_ms() >= refresh_Time) {
            refresh_Timer.reset();
            if (myGPS.fix) {
                publat = dms_convert(myGPS.latitude);
                publong = dms_convert(myGPS.longitude);
                pc.printf("DMS Lat: %f, DD Lat: %f\r\nDMS Long:%f, DD Long: %f\r\n", myGPS.latitude, publat, myGPS.longitude, publong);
            } else pc.printf("No Satelite Fix\r\n");
        }
    }
}