Flotsam / Full-Project

Dependencies:   GPSLibrary GSM mbed-modifed Storage_Library Temp_Library Wakeup pH_Sensor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "main.h"
00002 #include "GSM_Wrapper.h"
00003 #include "WakeUp.h"
00004 #include "pH_Sensor.h"
00005 #include "storage.h"
00006 #include "GPS_Wrapper.h"
00007 #include "Temp_Sensor.h"
00008 #include <string>
00009 
00010 #define PC_BAUD 115200  // High baud rate for PC serial connection
00011 #define GSM_INITIAL_DELAY 20 // Delay to allow GSM to initally turn on
00012 
00013 // Global sensor objects
00014 pH_Sensor pH;       // pH Sensor object - initialized in setup()
00015 GPS_Sensor GPS;     // GPS sensor object - initialized in setup()
00016 GSM_Sensor GSM;     // GSM sensor object - initialized in setup()
00017 Temp_Sensor Temp;   // Temperature sensor object - initialized in setup()
00018 Storage storage;    // Storage sensor object - initialized in setup()
00019 Serial pcSerial(USBTX, USBRX);  // PC serial communication -- initialized in setup(). Used for debugging
00020 
00021 // Function that sets up the PC serial connection. Used for printing debug statements
00022 void setupPCSerial() {
00023     wait(2);
00024     pcSerial.baud(PC_BAUD);
00025     pcSerial.printf("\n\n PC Serial connection established at 115200 baud.\n");
00026     wait(2);
00027 }
00028 
00029 /* Function: setup
00030  * ---------------
00031  * Sets up all the sensors and PC serial.
00032  */
00033 void setup() {
00034     setupPCSerial();
00035     GPS.setup();
00036     pH.setup();
00037     // Delay to allow GSM to turn on initially
00038     wait(GMS_INITIAL_DELAY);
00039     // Turn the GSM back off
00040     GSM.changePowerState();
00041 } 
00042 
00043 /* Function: enterSleep
00044  * Enters deep sleep. Turns off all necessary
00045  * sensors, and resets the clock after waking
00046  * up (and turns the GPS on so that it can start getting
00047  * a fix).
00048  */
00049 void enterSleep(int sec)
00050 {
00051     GPS.turnOff();
00052     wait(2);
00053     WakeUp::set(sec);
00054     deepsleep();
00055     WakeUp::resetADC();
00056     GPS.turnOn();
00057     wait(2);
00058 }
00059 
00060 /* Function: read
00061  * --------------
00062  * Calls the read function for each sensor, and adds it
00063  * to the reading buffer. Writes it to the perminant storage.
00064  */
00065 void read(struct reading& lastReadingBuffer)
00066 {
00067     printf("~~~~~[pH]~~~~~\n");
00068     lastReadingBuffer.pH = pH.read();
00069     printf("~~~~~[Temperature]~~~~~\n");
00070     lastReadingBuffer.temperature = Temp.read();
00071     printf("~~~~~[GPS]~~~~~\n");
00072     GPS.read(lastReadingBuffer);
00073     storage.write((uint8_t *) &lastReadingBuffer, sizeof(struct reading));
00074     GSM.changePowerState();
00075 }
00076 
00077 /* Function: send
00078  * --------------
00079  * Sends the data over GSM by reading from the
00080  * nonvolitile storage. Only if it succeeds does it
00081  * change the toSend bool to false (thus attempting to send
00082  * the next time if it fails).
00083  */
00084 void send(int& nreadings, bool& toSend)
00085 {
00086     struct reading data[nreadings];
00087     storage.read((uint8_t*) data);
00088     size_t bufsize = SIZE_OF_ENTRY*nreadings+1;
00089     char buffer[bufsize];
00090     for(int i = 0; i < nreadings; i++) {
00091         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,
00092                         data[i].month, data[i].year, data[i].hour, data[i].minutes);
00093     }
00094     if(GSM.send((uint8_t*) buffer, bufsize)) {
00095         toSend = false;
00096         nreadings = 0;
00097         storage.reset();
00098     }
00099 }
00100 
00101 /* Function: main
00102  * --------------
00103  * Never returns. Calls setup, initializes nreadings and toSend,
00104  * then repeatedly reads the sensors and sends the data (if 
00105  * the number of readings is correct) before entering sleep.
00106  */
00107 int main()
00108 {
00109     setup();
00110     int nreadings = 0;
00111     bool toSend = false;
00112     while (true) {
00113         struct reading lastReadingBuffer;
00114         read(lastReadingBuffer);
00115         nreadings++;
00116         if(nreadings == N_READINGS_PER_SEND) toSend = true;
00117         if(toSend) send(nreadings, toSend);
00118         GSM.changePowerState();
00119         wait(2);
00120         enterSleep(N_SECONDS_SLEEP);
00121     }
00122 }