Real-time bike tracker using Adafruit Ultimate GPS, Huzzah wifi, and Pubnub
Dependencies: MBed_Adafruit-GPS-Library mbed
main.cpp@1:0701bf58c9fa, 2017-04-19 (annotated)
- Committer:
- ECE4180
- Date:
- Wed Apr 19 01:00:53 2017 +0000
- Revision:
- 1:0701bf58c9fa
- Parent:
- 0:42cb15551bf3
- Child:
- 2:834f8d2ebe3f
GPS String sending to Pubnub and working.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ECE4180 | 0:42cb15551bf3 | 1 | #include "mbed.h" |
ECE4180 | 0:42cb15551bf3 | 2 | #include "MBed_Adafruit_GPS.h" |
ECE4180 | 0:42cb15551bf3 | 3 | #include "math.h" |
ECE4180 | 1:0701bf58c9fa | 4 | #include "PubNub.h" |
ECE4180 | 1:0701bf58c9fa | 5 | |
ECE4180 | 1:0701bf58c9fa | 6 | |
ECE4180 | 1:0701bf58c9fa | 7 | string pubkey("pub-c-f9091d93-c3a8-41a6-80c2-c6e3f840504f"); |
ECE4180 | 1:0701bf58c9fa | 8 | string subkey("sub-c-f558e36c-130b-11e7-b59a-02ee2ddab7fe"); |
ECE4180 | 1:0701bf58c9fa | 9 | string channel("demo"); |
ECE4180 | 1:0701bf58c9fa | 10 | |
ECE4180 | 1:0701bf58c9fa | 11 | PubNub pnub(pubkey, subkey, channel, p28, p27); // tx, rx |
ECE4180 | 1:0701bf58c9fa | 12 | |
ECE4180 | 1:0701bf58c9fa | 13 | int DataRX; |
ECE4180 | 1:0701bf58c9fa | 14 | volatile int tx_in; |
ECE4180 | 1:0701bf58c9fa | 15 | volatile int tx_out; |
ECE4180 | 1:0701bf58c9fa | 16 | volatile int rx_in; |
ECE4180 | 1:0701bf58c9fa | 17 | volatile int rx_out; |
ECE4180 | 1:0701bf58c9fa | 18 | char tx_buffer[4096]; |
ECE4180 | 1:0701bf58c9fa | 19 | char rx_buffer[4096]; |
ECE4180 | 1:0701bf58c9fa | 20 | |
ECE4180 | 0:42cb15551bf3 | 21 | Serial * gps_Serial; |
ECE4180 | 0:42cb15551bf3 | 22 | Serial pc (USBTX, USBRX); //Set-up for debugging |
ECE4180 | 0:42cb15551bf3 | 23 | |
ECE4180 | 0:42cb15551bf3 | 24 | //Take degree, minute,seconds (DMS) from GPS and convert to decimal degrees (DD) for Pubnub |
ECE4180 | 0:42cb15551bf3 | 25 | float dms_convert(float dms) |
ECE4180 | 0:42cb15551bf3 | 26 | { |
ECE4180 | 0:42cb15551bf3 | 27 | float sec, min, deg, dd; |
ECE4180 | 0:42cb15551bf3 | 28 | sec = (dms*100)-(floor(dms)*100); |
ECE4180 | 0:42cb15551bf3 | 29 | deg = floor(dms/100); |
ECE4180 | 0:42cb15551bf3 | 30 | min = floor(dms)-(deg*100); |
ECE4180 | 0:42cb15551bf3 | 31 | dd = deg +((min+(sec/60))/60); |
ECE4180 | 0:42cb15551bf3 | 32 | return dd; |
ECE4180 | 0:42cb15551bf3 | 33 | } |
ECE4180 | 0:42cb15551bf3 | 34 | |
ECE4180 | 1:0701bf58c9fa | 35 | // Interupt Routine to read in data from serial port |
ECE4180 | 1:0701bf58c9fa | 36 | void Rx_interrupt() |
ECE4180 | 1:0701bf58c9fa | 37 | { |
ECE4180 | 1:0701bf58c9fa | 38 | DataRX=1; |
ECE4180 | 1:0701bf58c9fa | 39 | // Loop just in case more than one character is in UART's receive FIFO buffer |
ECE4180 | 1:0701bf58c9fa | 40 | // Stop if buffer full |
ECE4180 | 1:0701bf58c9fa | 41 | while ((pnub.huz.esp.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) { |
ECE4180 | 1:0701bf58c9fa | 42 | rx_buffer[rx_in] = pnub.huz.esp.getc(); |
ECE4180 | 1:0701bf58c9fa | 43 | // Uncomment to Echo to USB serial to watch data flow |
ECE4180 | 1:0701bf58c9fa | 44 | pc.putc(rx_buffer[rx_in]); |
ECE4180 | 1:0701bf58c9fa | 45 | rx_in = (rx_in + 1) % buffer_size; |
ECE4180 | 1:0701bf58c9fa | 46 | } |
ECE4180 | 1:0701bf58c9fa | 47 | return; |
ECE4180 | 1:0701bf58c9fa | 48 | } |
ECE4180 | 1:0701bf58c9fa | 49 | |
ECE4180 | 1:0701bf58c9fa | 50 | |
ECE4180 | 1:0701bf58c9fa | 51 | // Interupt Routine to write out data to serial port |
ECE4180 | 1:0701bf58c9fa | 52 | void Tx_interrupt() |
ECE4180 | 1:0701bf58c9fa | 53 | { |
ECE4180 | 1:0701bf58c9fa | 54 | // Loop to fill more than one character in UART's transmit FIFO buffer |
ECE4180 | 1:0701bf58c9fa | 55 | // Stop if buffer empty |
ECE4180 | 1:0701bf58c9fa | 56 | while ((pnub.huz.esp.writeable()) && (tx_in != tx_out)) { |
ECE4180 | 1:0701bf58c9fa | 57 | pnub.huz.esp.putc(tx_buffer[tx_out]); |
ECE4180 | 1:0701bf58c9fa | 58 | tx_out = (tx_out + 1) % buffer_size; |
ECE4180 | 1:0701bf58c9fa | 59 | } |
ECE4180 | 1:0701bf58c9fa | 60 | return; |
ECE4180 | 1:0701bf58c9fa | 61 | } |
ECE4180 | 1:0701bf58c9fa | 62 | |
ECE4180 | 0:42cb15551bf3 | 63 | int main() |
ECE4180 | 0:42cb15551bf3 | 64 | { |
ECE4180 | 0:42cb15551bf3 | 65 | pc.baud(115200); //Set PC baud rate |
ECE4180 | 0:42cb15551bf3 | 66 | gps_Serial = new Serial(p9,p10); |
ECE4180 | 0:42cb15551bf3 | 67 | Adafruit_GPS myGPS(gps_Serial); //object of Adafruit's GPS class |
ECE4180 | 0:42cb15551bf3 | 68 | Timer refresh_Timer; //Timer for updated GPS information |
ECE4180 | 0:42cb15551bf3 | 69 | const int refresh_Time = 2000; //refresh time in ms |
ECE4180 | 0:42cb15551bf3 | 70 | |
ECE4180 | 0:42cb15551bf3 | 71 | |
ECE4180 | 0:42cb15551bf3 | 72 | //Initialization Commands for Adafruit_GPS |
ECE4180 | 0:42cb15551bf3 | 73 | myGPS.begin(9600); //sets baud rate for GPS communication; |
ECE4180 | 0:42cb15551bf3 | 74 | myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); |
ECE4180 | 0:42cb15551bf3 | 75 | myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); |
ECE4180 | 0:42cb15551bf3 | 76 | myGPS.sendCommand(PGCMD_ANTENNA); |
ECE4180 | 0:42cb15551bf3 | 77 | float publat; |
ECE4180 | 0:42cb15551bf3 | 78 | float publong; |
ECE4180 | 0:42cb15551bf3 | 79 | |
ECE4180 | 1:0701bf58c9fa | 80 | |
ECE4180 | 1:0701bf58c9fa | 81 | // Setup a serial interrupt function to receive data |
ECE4180 | 1:0701bf58c9fa | 82 | pnub.huz.esp.attach(&Rx_interrupt, Serial::RxIrq); |
ECE4180 | 1:0701bf58c9fa | 83 | // Setup a serial interrupt function to transmit data |
ECE4180 | 1:0701bf58c9fa | 84 | pnub.huz.esp.attach(&Tx_interrupt, Serial::TxIrq); |
ECE4180 | 1:0701bf58c9fa | 85 | |
ECE4180 | 1:0701bf58c9fa | 86 | char lat_buff[20]; |
ECE4180 | 1:0701bf58c9fa | 87 | |
ECE4180 | 0:42cb15551bf3 | 88 | pc.printf("Connection established at 115200 baud...\r\n"); |
ECE4180 | 0:42cb15551bf3 | 89 | wait(1); |
ECE4180 | 0:42cb15551bf3 | 90 | refresh_Timer.start(); //starts the clock on the timer |
ECE4180 | 0:42cb15551bf3 | 91 | while(true) { |
ECE4180 | 0:42cb15551bf3 | 92 | myGPS.read(); //queries the GPS |
ECE4180 | 0:42cb15551bf3 | 93 | //If GPS has data, parse it |
ECE4180 | 0:42cb15551bf3 | 94 | if ( myGPS.newNMEAreceived() ) { |
ECE4180 | 0:42cb15551bf3 | 95 | if ( !myGPS.parse(myGPS.lastNMEA()) ) { |
ECE4180 | 0:42cb15551bf3 | 96 | continue; |
ECE4180 | 0:42cb15551bf3 | 97 | } |
ECE4180 | 0:42cb15551bf3 | 98 | } |
ECE4180 | 0:42cb15551bf3 | 99 | |
ECE4180 | 0:42cb15551bf3 | 100 | //check if enough time has passed to warrant sending new GPS data |
ECE4180 | 0:42cb15551bf3 | 101 | if (refresh_Timer.read_ms() >= refresh_Time) { |
ECE4180 | 0:42cb15551bf3 | 102 | refresh_Timer.reset(); |
ECE4180 | 0:42cb15551bf3 | 103 | if (myGPS.fix) { |
ECE4180 | 0:42cb15551bf3 | 104 | publat = dms_convert(myGPS.latitude); |
ECE4180 | 0:42cb15551bf3 | 105 | publong = dms_convert(myGPS.longitude); |
ECE4180 | 0:42cb15551bf3 | 106 | pc.printf("DMS Lat: %f, DD Lat: %f\r\nDMS Long:%f, DD Long: %f\r\n", myGPS.latitude, publat, myGPS.longitude, publong); |
ECE4180 | 1:0701bf58c9fa | 107 | DataRX = 0; |
ECE4180 | 1:0701bf58c9fa | 108 | sprintf(lat_buff, "%f", publat); |
ECE4180 | 1:0701bf58c9fa | 109 | pnub.send_message(lat_buff); |
ECE4180 | 1:0701bf58c9fa | 110 | pc.printf("MESSAGE SENT\r\n"); |
ECE4180 | 1:0701bf58c9fa | 111 | wait(20); |
ECE4180 | 0:42cb15551bf3 | 112 | } else pc.printf("No Satelite Fix\r\n"); |
ECE4180 | 0:42cb15551bf3 | 113 | } |
ECE4180 | 0:42cb15551bf3 | 114 | } |
ECE4180 | 0:42cb15551bf3 | 115 | } |