Real-time bike tracker using Adafruit Ultimate GPS, Huzzah wifi, and Pubnub
Dependencies: MBed_Adafruit-GPS-Library mbed
main.cpp@4:e20e2500914f, 2017-04-21 (annotated)
- Committer:
- ECE4180
- Date:
- Fri Apr 21 00:37:35 2017 +0000
- Revision:
- 4:e20e2500914f
- Parent:
- 3:ceca81e8ac2b
- Child:
- 5:786cb04aaecc
bootyful and we love you
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 | 4:e20e2500914f | 5 | #include "Watchdog.h" |
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 | 4:e20e2500914f | 21 | Watchdog wdt; |
ECE4180 | 4:e20e2500914f | 22 | |
ECE4180 | 0:42cb15551bf3 | 23 | Serial * gps_Serial; |
ECE4180 | 0:42cb15551bf3 | 24 | Serial pc (USBTX, USBRX); //Set-up for debugging |
ECE4180 | 0:42cb15551bf3 | 25 | |
ECE4180 | 0:42cb15551bf3 | 26 | //Take degree, minute,seconds (DMS) from GPS and convert to decimal degrees (DD) for Pubnub |
ECE4180 | 0:42cb15551bf3 | 27 | float dms_convert(float dms) |
ECE4180 | 0:42cb15551bf3 | 28 | { |
ECE4180 | 0:42cb15551bf3 | 29 | float sec, min, deg, dd; |
ECE4180 | 0:42cb15551bf3 | 30 | sec = (dms*100)-(floor(dms)*100); |
ECE4180 | 0:42cb15551bf3 | 31 | deg = floor(dms/100); |
ECE4180 | 0:42cb15551bf3 | 32 | min = floor(dms)-(deg*100); |
ECE4180 | 0:42cb15551bf3 | 33 | dd = deg +((min+(sec/60))/60); |
ECE4180 | 0:42cb15551bf3 | 34 | return dd; |
ECE4180 | 0:42cb15551bf3 | 35 | } |
ECE4180 | 0:42cb15551bf3 | 36 | |
ECE4180 | 1:0701bf58c9fa | 37 | // Interupt Routine to read in data from serial port |
ECE4180 | 1:0701bf58c9fa | 38 | void Rx_interrupt() |
ECE4180 | 1:0701bf58c9fa | 39 | { |
ECE4180 | 1:0701bf58c9fa | 40 | DataRX=1; |
ECE4180 | 1:0701bf58c9fa | 41 | // Loop just in case more than one character is in UART's receive FIFO buffer |
ECE4180 | 1:0701bf58c9fa | 42 | // Stop if buffer full |
ECE4180 | 1:0701bf58c9fa | 43 | while ((pnub.huz.esp.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) { |
ECE4180 | 1:0701bf58c9fa | 44 | rx_buffer[rx_in] = pnub.huz.esp.getc(); |
ECE4180 | 1:0701bf58c9fa | 45 | // Uncomment to Echo to USB serial to watch data flow |
ECE4180 | 3:ceca81e8ac2b | 46 | pc.putc(rx_buffer[rx_in]); |
ECE4180 | 1:0701bf58c9fa | 47 | rx_in = (rx_in + 1) % buffer_size; |
ECE4180 | 1:0701bf58c9fa | 48 | } |
ECE4180 | 1:0701bf58c9fa | 49 | return; |
ECE4180 | 1:0701bf58c9fa | 50 | } |
ECE4180 | 1:0701bf58c9fa | 51 | |
ECE4180 | 1:0701bf58c9fa | 52 | |
ECE4180 | 1:0701bf58c9fa | 53 | // Interupt Routine to write out data to serial port |
ECE4180 | 1:0701bf58c9fa | 54 | void Tx_interrupt() |
ECE4180 | 1:0701bf58c9fa | 55 | { |
ECE4180 | 1:0701bf58c9fa | 56 | // Loop to fill more than one character in UART's transmit FIFO buffer |
ECE4180 | 1:0701bf58c9fa | 57 | // Stop if buffer empty |
ECE4180 | 1:0701bf58c9fa | 58 | while ((pnub.huz.esp.writeable()) && (tx_in != tx_out)) { |
ECE4180 | 1:0701bf58c9fa | 59 | pnub.huz.esp.putc(tx_buffer[tx_out]); |
ECE4180 | 1:0701bf58c9fa | 60 | tx_out = (tx_out + 1) % buffer_size; |
ECE4180 | 1:0701bf58c9fa | 61 | } |
ECE4180 | 1:0701bf58c9fa | 62 | return; |
ECE4180 | 1:0701bf58c9fa | 63 | } |
ECE4180 | 1:0701bf58c9fa | 64 | |
ECE4180 | 0:42cb15551bf3 | 65 | int main() |
ECE4180 | 0:42cb15551bf3 | 66 | { |
ECE4180 | 4:e20e2500914f | 67 | wdt.kick(8.0); |
ECE4180 | 0:42cb15551bf3 | 68 | pc.baud(115200); //Set PC baud rate |
ECE4180 | 2:834f8d2ebe3f | 69 | pnub.huz.setssid("Verizon-SM-G900V-258B"); |
ECE4180 | 2:834f8d2ebe3f | 70 | pnub.huz.setpwd("trnd533)"); |
ECE4180 | 2:834f8d2ebe3f | 71 | //pnub.huz.espconfig(); |
ECE4180 | 0:42cb15551bf3 | 72 | gps_Serial = new Serial(p9,p10); |
ECE4180 | 0:42cb15551bf3 | 73 | Adafruit_GPS myGPS(gps_Serial); //object of Adafruit's GPS class |
ECE4180 | 0:42cb15551bf3 | 74 | Timer refresh_Timer; //Timer for updated GPS information |
ECE4180 | 0:42cb15551bf3 | 75 | const int refresh_Time = 2000; //refresh time in ms |
ECE4180 | 0:42cb15551bf3 | 76 | |
ECE4180 | 0:42cb15551bf3 | 77 | |
ECE4180 | 0:42cb15551bf3 | 78 | //Initialization Commands for Adafruit_GPS |
ECE4180 | 0:42cb15551bf3 | 79 | myGPS.begin(9600); //sets baud rate for GPS communication; |
ECE4180 | 0:42cb15551bf3 | 80 | myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); |
ECE4180 | 0:42cb15551bf3 | 81 | myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); |
ECE4180 | 0:42cb15551bf3 | 82 | myGPS.sendCommand(PGCMD_ANTENNA); |
ECE4180 | 0:42cb15551bf3 | 83 | float publat; |
ECE4180 | 0:42cb15551bf3 | 84 | float publong; |
ECE4180 | 2:834f8d2ebe3f | 85 | float oldlat = 0.0; |
ECE4180 | 2:834f8d2ebe3f | 86 | float oldlong = 0.0; |
ECE4180 | 3:ceca81e8ac2b | 87 | char c; |
ECE4180 | 1:0701bf58c9fa | 88 | |
ECE4180 | 1:0701bf58c9fa | 89 | // Setup a serial interrupt function to receive data |
ECE4180 | 1:0701bf58c9fa | 90 | pnub.huz.esp.attach(&Rx_interrupt, Serial::RxIrq); |
ECE4180 | 1:0701bf58c9fa | 91 | // Setup a serial interrupt function to transmit data |
ECE4180 | 1:0701bf58c9fa | 92 | pnub.huz.esp.attach(&Tx_interrupt, Serial::TxIrq); |
ECE4180 | 1:0701bf58c9fa | 93 | |
ECE4180 | 2:834f8d2ebe3f | 94 | char lat_buff[30]; |
ECE4180 | 1:0701bf58c9fa | 95 | |
ECE4180 | 0:42cb15551bf3 | 96 | pc.printf("Connection established at 115200 baud...\r\n"); |
ECE4180 | 0:42cb15551bf3 | 97 | wait(1); |
ECE4180 | 0:42cb15551bf3 | 98 | refresh_Timer.start(); //starts the clock on the timer |
ECE4180 | 0:42cb15551bf3 | 99 | while(true) { |
ECE4180 | 4:e20e2500914f | 100 | c = myGPS.read(); //queries the GPS |
ECE4180 | 4:e20e2500914f | 101 | |
ECE4180 | 3:ceca81e8ac2b | 102 | //if (c) { pc.printf("%c", c); } //this line will echo the GPS data if not paused |
ECE4180 | 0:42cb15551bf3 | 103 | //If GPS has data, parse it |
ECE4180 | 4:e20e2500914f | 104 | |
ECE4180 | 4:e20e2500914f | 105 | //check if we recieved a new message from GPS, if so, attempt to parse it, |
ECE4180 | 0:42cb15551bf3 | 106 | if ( myGPS.newNMEAreceived() ) { |
ECE4180 | 0:42cb15551bf3 | 107 | if ( !myGPS.parse(myGPS.lastNMEA()) ) { |
ECE4180 | 4:e20e2500914f | 108 | continue; |
ECE4180 | 4:e20e2500914f | 109 | } |
ECE4180 | 0:42cb15551bf3 | 110 | } |
ECE4180 | 0:42cb15551bf3 | 111 | |
ECE4180 | 0:42cb15551bf3 | 112 | //check if enough time has passed to warrant sending new GPS data |
ECE4180 | 0:42cb15551bf3 | 113 | if (refresh_Timer.read_ms() >= refresh_Time) { |
ECE4180 | 0:42cb15551bf3 | 114 | refresh_Timer.reset(); |
ECE4180 | 0:42cb15551bf3 | 115 | if (myGPS.fix) { |
ECE4180 | 0:42cb15551bf3 | 116 | publat = dms_convert(myGPS.latitude); |
ECE4180 | 0:42cb15551bf3 | 117 | publong = dms_convert(myGPS.longitude); |
ECE4180 | 3:ceca81e8ac2b | 118 | pc.printf("DD Old Lat: %f, DD Old Long: %f \r\nDD New Lat %f, DD New Long %f\r\n",oldlat, oldlong, publat, publong); |
ECE4180 | 2:834f8d2ebe3f | 119 | if (publat != oldlat || publong !=oldlong) { |
ECE4180 | 3:ceca81e8ac2b | 120 | //pc.printf("DMS Lat: %f%c, DMS Long: %f%c\r\n",myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon); |
ECE4180 | 2:834f8d2ebe3f | 121 | oldlat = publat; |
ECE4180 | 2:834f8d2ebe3f | 122 | oldlong = publong; |
ECE4180 | 2:834f8d2ebe3f | 123 | DataRX = 0; |
ECE4180 | 2:834f8d2ebe3f | 124 | sprintf(lat_buff, "%f %f", publat, publong); |
ECE4180 | 2:834f8d2ebe3f | 125 | pnub.send_message(lat_buff); |
ECE4180 | 2:834f8d2ebe3f | 126 | pc.printf("Message Sent\r\n"); |
ECE4180 | 3:ceca81e8ac2b | 127 | continue; |
ECE4180 | 3:ceca81e8ac2b | 128 | } else pc.printf("No New Data\r\n"); |
ECE4180 | 2:834f8d2ebe3f | 129 | } else { |
ECE4180 | 2:834f8d2ebe3f | 130 | pc.printf("No Satelite Fix\r\n"); |
ECE4180 | 2:834f8d2ebe3f | 131 | } |
ECE4180 | 0:42cb15551bf3 | 132 | } |
ECE4180 | 4:e20e2500914f | 133 | wdt.kick(); |
ECE4180 | 0:42cb15551bf3 | 134 | } |
ECE4180 | 0:42cb15551bf3 | 135 | } |