Full project folder
Dependencies: GPSLibrary GSM mbed-modifed Storage_Library Temp_Library Wakeup pH_Sensor
Diff: main.cpp
- Revision:
- 15:5f366ddcce18
- Parent:
- 14:196ed16cd62b
--- a/main.cpp Sat Dec 05 07:36:48 2015 +0000 +++ b/main.cpp Mon Dec 07 00:07:31 2015 +0000 @@ -1,36 +1,51 @@ +#include "main.h" #include "GSM_Wrapper.h" #include "WakeUp.h" -#include "main.h" #include "pH_Sensor.h" #include "storage.h" #include "GPS_Wrapper.h" #include "Temp_Sensor.h" #include <string> -struct reading lastReadingBuffer; -pH_Sensor pH; -GPS_Sensor GPS; -GSM_Sensor GSM; -Temp_Sensor Temp; -Storage storage; -Serial pcSerial(USBTX, USBRX); +#define PC_BAUD 115200 // High baud rate for PC serial connection +#define GSM_INITIAL_DELAY 20 // Delay to allow GSM to initally turn on +// Global sensor objects +pH_Sensor pH; // pH Sensor object - initialized in setup() +GPS_Sensor GPS; // GPS sensor object - initialized in setup() +GSM_Sensor GSM; // GSM sensor object - initialized in setup() +Temp_Sensor Temp; // Temperature sensor object - initialized in setup() +Storage storage; // Storage sensor object - initialized in setup() +Serial pcSerial(USBTX, USBRX); // PC serial communication -- initialized in setup(). Used for debugging + +// Function that sets up the PC serial connection. Used for printing debug statements void setupPCSerial() { wait(2); - pcSerial.baud(115200); + pcSerial.baud(PC_BAUD); pcSerial.printf("\n\n PC Serial connection established at 115200 baud.\n"); wait(2); } +/* Function: setup + * --------------- + * Sets up all the sensors and PC serial. + */ void setup() { setupPCSerial(); GPS.setup(); pH.setup(); - wait(20); + // Delay to allow GSM to turn on initially + wait(GMS_INITIAL_DELAY); + // Turn the GSM back off GSM.changePowerState(); } - +/* Function: enterSleep + * Enters deep sleep. Turns off all necessary + * sensors, and resets the clock after waking + * up (and turns the GPS on so that it can start getting + * a fix). + */ void enterSleep(int sec) { GPS.turnOff(); @@ -42,15 +57,38 @@ wait(2); } -void sendData(int& nreadings, bool& toSend) +/* Function: read + * -------------- + * Calls the read function for each sensor, and adds it + * to the reading buffer. Writes it to the perminant storage. + */ +void read(struct reading& lastReadingBuffer) { - struct reading* data = new struct reading[nreadings]; + printf("~~~~~[pH]~~~~~\n"); + lastReadingBuffer.pH = pH.read(); + printf("~~~~~[Temperature]~~~~~\n"); + lastReadingBuffer.temperature = Temp.read(); + printf("~~~~~[GPS]~~~~~\n"); + GPS.read(lastReadingBuffer); + storage.write((uint8_t *) &lastReadingBuffer, sizeof(struct reading)); + GSM.changePowerState(); +} + +/* Function: send + * -------------- + * Sends the data over GSM by reading from the + * nonvolitile storage. Only if it succeeds does it + * change the toSend bool to false (thus attempting to send + * the next time if it fails). + */ +void send(int& nreadings, bool& toSend) +{ + struct reading data[nreadings]; storage.read((uint8_t*) data); - int sizeofentry = 8+8+10+10+5*sizeof(int)+8+1; - size_t bufsize = sizeofentry*nreadings+1; + size_t bufsize = SIZE_OF_ENTRY*nreadings+1; char buffer[bufsize]; for(int i = 0; i < nreadings; i++) { - int n = sprintf(buffer+sizeofentry*i, "%8f %8f %10f %10f %d %d %d %d %d\t", data[i].temperature, data[i].pH, data[i].latitude, data[i].longitude, data[i].day, + int n = sprintf(buffer+SIZE_OF_ENTRY*i, "%8f %8f %10f %10f %d %d %d %d %d\t", data[i].temperature, data[i].pH, data[i].latitude, data[i].longitude, data[i].day, data[i].month, data[i].year, data[i].hour, data[i].minutes); } if(GSM.send((uint8_t*) buffer, bufsize)) { @@ -58,31 +96,25 @@ nreadings = 0; storage.reset(); } - delete[] data; } -void read() -{ - printf("~~~~~[pH]~~~~~\n"); - lastReadingBuffer.pH = pH.read(); - printf("~~~~~[Temperature]~~~~~\n"); - lastReadingBuffer.temperature = Temp.read(); - printf("~~~~~[GPS]~~~~~\n"); - GPS.read(lastReadingBuffer); - storage.write((uint8_t *) &lastReadingBuffer, READINGSIZE); - GSM.changePowerState(); -} - +/* Function: main + * -------------- + * Never returns. Calls setup, initializes nreadings and toSend, + * then repeatedly reads the sensors and sends the data (if + * the number of readings is correct) before entering sleep. + */ int main() { setup(); int nreadings = 0; bool toSend = false; while (true) { - read(); + struct reading lastReadingBuffer; + read(lastReadingBuffer); nreadings++; if(nreadings == N_READINGS_PER_SEND) toSend = true; - if(toSend) sendData(nreadings, toSend); + if(toSend) send(nreadings, toSend); GSM.changePowerState(); wait(2); enterSleep(N_SECONDS_SLEEP);