Real-time bike tracker using Adafruit Ultimate GPS, Huzzah wifi, and Pubnub
Dependencies: MBed_Adafruit-GPS-Library mbed
Diff: main.cpp
- Revision:
- 0:42cb15551bf3
- Child:
- 1:0701bf58c9fa
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Apr 19 00:45:31 2017 +0000 @@ -0,0 +1,57 @@ +#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"); + } + } +} \ No newline at end of file