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

Dependencies:   MBed_Adafruit-GPS-Library mbed

Revision:
1:0701bf58c9fa
Parent:
0:42cb15551bf3
Child:
2:834f8d2ebe3f
--- a/main.cpp	Wed Apr 19 00:45:31 2017 +0000
+++ b/main.cpp	Wed Apr 19 01:00:53 2017 +0000
@@ -1,6 +1,23 @@
 #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
 
@@ -15,6 +32,34 @@
     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
@@ -32,6 +77,14 @@
     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
@@ -51,6 +104,11 @@
                 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");
         }
     }