Flotsam / Full-Project

Dependencies:   GPSLibrary GSM mbed-modifed Storage_Library Temp_Library Wakeup pH_Sensor

main.cpp

Committer:
kbaichoo
Date:
2015-11-09
Revision:
7:92f2d8c79565
Parent:
6:10b0ff2803e5

File content as of revision 7:92f2d8c79565:

#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
#define FONA_RST D3
#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

#define TMP_ANALOG A0
#define ADC_CONVERSION 3.3/5.0
Adafruit_FONA fona(FONA_TX, FONA_RX, FONA_RST, FONA_RI);
Serial pcSerial(USBTX, USBRX);
DigitalOut key(FONA_KEY);
AnalogIn temperature(TMP_ANALOG);

// 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");
    }
}

//function to get both the 
double AD22100K_AI_value_to_Celsius() {                                               // Convert Analog-input value to temperature
  //1023 is to scale it up to the arduino read values.
  float voltage = (int)((temperature.read() * ADC_CONVERSION) * 1023);
   
  float temperatureValue = (voltage * 0.217226044) - 61.1111111; // conversion factor simplified.
  pcSerial.printf("AI_Val: %f\n", temperatureValue);
  return temperatureValue;       // 22.5 mV / °C; Ratiometric measurement, conversion valid for 5 V!
}

//Found this online and it claims that it resets ADC to work after deepsleep \_O_/
void resetADC()
{
    // Enable the HSI (to clock the ADC)
    RCC_OscInitTypeDef RCC_OscInitStruct;
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
    RCC_OscInitStruct.HSIState       = RCC_HSI_ON;
    RCC_OscInitStruct.PLL.PLLState   = RCC_PLL_NONE;
    HAL_RCC_OscConfig(&RCC_OscInitStruct);  
}  

// 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");
    while(!fona.begin(9600)) {
        printf("Cannot find FONA\n");
        wait(1);
    }
    fona.begin(9600);
    fona.setGPRSNetworkSettings("pwg", "", "");
    bool enable = false;
    while(enable != true) {
        fona.enableGPRS(true);
        fona.enableGPRS(false);
        enable = fona.enableGPRS(true);
    }
}

void changeGSMPowerState()
{
    key.write(0);
    wait(2);
    key.write(1);
}

void enterSleep(int msec)
{
    if(msec > 0) WakeUp::set_ms(msec);
    deepsleep();
    resetADC();
}

bool sendDataOverHTTP(char* url, char* data)
{
    uint16_t statuscode;
    int16_t length;
    if (!fona.HTTP_POST_start(url, "text/plain", (uint8_t *) data, strlen(data), &statuscode, (uint16_t *)&length)) {
        pcSerial.printf("Failed!\r\n");
        return false;
    }
    while (length > 0) {
        while (fona.readable()) {
            char c = fona.getc();
            pcSerial.putc(c);
            length--;
            if (! length) break;
        }
    }
    pcSerial.printf("\r\n****\r\n");
    fona.HTTP_POST_end();
    return true;
}

int main()
{
    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;
}