Flotsam / Full-Project

Dependencies:   GPSLibrary GSM mbed-modifed Storage_Library Temp_Library Wakeup pH_Sensor

Revision:
5:e180c73f4f70
Parent:
3:6afcf4f3b2aa
Child:
6:10b0ff2803e5
Child:
8:b607768bfa0a
--- a/main.cpp	Fri Nov 06 19:25:01 2015 +0000
+++ b/main.cpp	Fri Nov 06 20:20:45 2015 +0000
@@ -1,6 +1,8 @@
 #include "Adafruit_FONA.h"
 #include "WakeUp.h"
 #include "mbed.h"
+#include "MBed_Adafruit_GPS.h"
+#include <string>
 
 #define FONA_TX D8
 #define FONA_RX D2
@@ -8,10 +10,142 @@
 #define FONA_RI D4
 #define FONA_KEY D5
 
+#define GPS_TX D6
+#define GPS_RX PB_11
+
+#define PH_TX PC_10
+#define PH_RX PC_11
+
 Adafruit_FONA fona(FONA_TX, FONA_RX, FONA_RST, FONA_RI);
 Serial pcSerial(USBTX, USBRX);
 DigitalOut key(FONA_KEY);
 
+// GPS
+char c; //when read via Adafruit_GPS::read(), the class returns single character stored here
+Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info?
+const int refresh_Time = 2000; //refresh time in ms
+Serial gps_Serial(GPS_TX, GPS_RX); // Serial object for GPS
+Adafruit_GPS myGPS(&gps_Serial);
+
+
+
+// pH sensor
+Serial ph_Serial (PH_TX, PH_RX);
+string sensorstring = "";
+bool input_stringcomplete = false;
+bool sensor_stringcomplete = false;
+float pH;
+
+void setupPCSerial() {
+    //pcSerial.baud(9600); // This is what the pH sensor used to have.
+    pcSerial.baud(115200);
+    pcSerial.printf("\n\nConnection established at 115200 baud...\n");
+}
+
+void setupPH() {
+    ph_Serial.baud(9600);
+    printf("Disabling continous mode.\n");
+    if(ph_Serial.writeable() <= 0) printf("Not writable\n");
+    // disable continuous mode
+    ph_Serial.printf("C,0");
+    ph_Serial.printf("%c", '\r');
+    printf("Waiting five seconds... ");
+    wait(5);
+    printf("done\n");
+}
+
+void setupGPS() {
+    myGPS.begin(9600);
+    myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //these commands are defined in MBed_Adafruit_GPS.h; a link is provided there for command creation
+    myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
+    myGPS.sendCommand(PGCMD_ANTENNA);
+    wait(1);
+    refresh_Timer.start();  //starts the clock on the timer
+    printf("setupGPS seems to be fine\n");
+}
+
+void setup() {
+    printf("\n=====\nSetup in Full Project\n");
+    setupPCSerial();
+    setupPH();
+    setupGPS();
+} 
+
+// Send default message to pH sensor, asking for data.
+void pHRequest() {
+    printf("Sending pH request...\n");
+    if(ph_Serial.writeable() <= 0) printf("Not writable\n");
+    // request one reading
+    ph_Serial.printf("R");
+    ph_Serial.printf("%c", '\r');
+}
+
+void pHRead() {
+    printf("Reading pH information.\n");
+    if (ph_Serial.readable() > 0) {                     //if we see that the Atlas Scientific product has sent a character.
+        printf("Receiving sensor string... [");
+        char inchar;
+        while((inchar = (char)ph_Serial.getc()) != '\r') {
+            sensorstring += inchar;
+            printf("%c", inchar);
+        }
+        printf("] ...sensor string received!\n");
+        sensor_stringcomplete = true;
+        pH = atof(sensorstring.c_str());                      //convert the string to a floating point number so it can be evaluated by the Arduino
+
+        if (pH >= 7.0) {                                  //if the pH is greater than or equal to 7.0
+            printf("high\n");                         //print "high" this is demonstrating that the Arduino is evaluating the pH as a number and not as a string
+        }
+
+        if (pH <= 6.999) {                                //if the pH is less than or equal to 6.999
+            printf("low\n");                          //print "low" this is demonstrating that the Arduino is evaluating the pH as a number and not as a string
+        }
+
+        //ph_Serial.printf("SLEEP");
+        //ph_Serial.printf("%c", '\r');
+
+        sensorstring = "";                                //clear the string:
+        sensor_stringcomplete = false;                    //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
+    } else {
+        printf("pH sensor is not readable\n");
+    }
+}
+
+// n_queries is the number of times we query the GPS. We need something like 23000 characters.
+void GPSRead(int n_queries) {
+    pcSerial.printf("\n");
+    for (int i = 0; i < n_queries; i++) {
+        c = myGPS.read();   //queries the GPS
+        
+        if (c) { pcSerial.printf("%c", c); } //this line will echo the GPS data if not paused
+        
+        //check if we recieved a new message from GPS, if so, attempt to parse it,
+        if ( myGPS.newNMEAreceived() ) {
+            if ( !myGPS.parse(myGPS.lastNMEA()) ) {
+                continue;   
+            }    
+        }
+        
+        //check if enough time has passed to warrant printing GPS info to screen
+        //note if refresh_Time is too low or pcSerial.baud is too low, GPS data may be lost during printing
+        if (refresh_Timer.read_ms() >= refresh_Time) {
+            refresh_Timer.reset();
+            pcSerial.printf("Time: %d:%d:%d.%u\n", myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds);   
+            pcSerial.printf("Date: %d/%d/20%d\n", myGPS.day, myGPS.month, myGPS.year);
+            pcSerial.printf("Fix: %d\n", (int) myGPS.fix);
+            pcSerial.printf("Quality: %d\n", (int) myGPS.fixquality);
+            if (myGPS.fix) {
+                pcSerial.printf("Location: %5.2f%c, %5.2f%c\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon);
+                pcSerial.printf("Speed: %5.2f knots\n", myGPS.speed);
+                pcSerial.printf("Angle: %5.2f\n", myGPS.angle);
+                pcSerial.printf("Altitude: %5.2f\n", myGPS.altitude);
+                pcSerial.printf("Satellites: %d\n", myGPS.satellites);
+            }
+        }
+    }
+    pcSerial.printf("\n");
+}
+
 void setupGSM()
 {
     printf("Starting FONA\n");
@@ -42,7 +176,7 @@
     deepsleep();
 }
 
-bool sendDataOverHTTP(const char* url, const char* data)
+bool sendDataOverHTTP(char* url, char* data)
 {
     uint16_t statuscode;
     int16_t length;
@@ -65,13 +199,23 @@
 
 int main()
 {
-
-    pcSerial.baud(9600);
+    setup();
+    
     changeGSMPowerState();
     setupGSM();
     //sendDataOverHTTP("http://requestb.in/10pbl2i1", "testing");
     
     changeGSMPowerState();
+    while (true) {
+        printf("~~~~~[pH]~~~~~\n");
+        pHRequest();
+        pHRead();
+        wait(1);
+        printf("~~~~~[GPS]~~~~\n");
+        
+        GPSRead(300000);
+        wait(1);
+    }
 
     return 0;
 }
\ No newline at end of file