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:
- 1:0701bf58c9fa
- Parent:
- 0:42cb15551bf3
- Child:
- 2:834f8d2ebe3f
File content as of revision 1:0701bf58c9fa:
#include "mbed.h" #include "MBed_Adafruit_GPS.h" #include "math.h" #include "PubNub.h" string pubkey("pub-c-f9091d93-c3a8-41a6-80c2-c6e3f840504f"); string subkey("sub-c-f558e36c-130b-11e7-b59a-02ee2ddab7fe"); string channel("demo"); PubNub pnub(pubkey, subkey, channel, p28, p27); // tx, rx int DataRX; volatile int tx_in; volatile int tx_out; volatile int rx_in; volatile int rx_out; char tx_buffer[4096]; char rx_buffer[4096]; 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; } // Interupt Routine to read in data from serial port void Rx_interrupt() { DataRX=1; // Loop just in case more than one character is in UART's receive FIFO buffer // Stop if buffer full while ((pnub.huz.esp.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) { rx_buffer[rx_in] = pnub.huz.esp.getc(); // Uncomment to Echo to USB serial to watch data flow pc.putc(rx_buffer[rx_in]); rx_in = (rx_in + 1) % buffer_size; } return; } // Interupt Routine to write out data to serial port void Tx_interrupt() { // Loop to fill more than one character in UART's transmit FIFO buffer // Stop if buffer empty while ((pnub.huz.esp.writeable()) && (tx_in != tx_out)) { pnub.huz.esp.putc(tx_buffer[tx_out]); tx_out = (tx_out + 1) % buffer_size; } return; } 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; // Setup a serial interrupt function to receive data pnub.huz.esp.attach(&Rx_interrupt, Serial::RxIrq); // Setup a serial interrupt function to transmit data pnub.huz.esp.attach(&Tx_interrupt, Serial::TxIrq); char lat_buff[20]; 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); DataRX = 0; sprintf(lat_buff, "%f", publat); pnub.send_message(lat_buff); pc.printf("MESSAGE SENT\r\n"); wait(20); } else pc.printf("No Satelite Fix\r\n"); } } }