Full project folder
Dependencies: GPSLibrary GSM mbed-modifed Storage_Library Temp_Library Wakeup pH_Sensor
Revision 15:5f366ddcce18, committed 2015-12-07
- Comitter:
- ptcrews
- Date:
- Mon Dec 07 00:07:31 2015 +0000
- Parent:
- 14:196ed16cd62b
- Commit message:
- Added comments.
Changed in this revision
diff -r 196ed16cd62b -r 5f366ddcce18 GPS_Library.lib --- a/GPS_Library.lib Sat Dec 05 07:36:48 2015 +0000 +++ b/GPS_Library.lib Mon Dec 07 00:07:31 2015 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/teams/Flotsam/code/GPSLibrary/#d28ff2012e9f +http://developer.mbed.org/teams/Flotsam/code/GPSLibrary/#1249c2cfdede
diff -r 196ed16cd62b -r 5f366ddcce18 GSM_Library.lib --- a/GSM_Library.lib Sat Dec 05 07:36:48 2015 +0000 +++ b/GSM_Library.lib Mon Dec 07 00:07:31 2015 +0000 @@ -1,1 +1,1 @@ -https://developer.mbed.org/teams/Flotsam/code/GSM/#7f509d0d5c0e +https://developer.mbed.org/teams/Flotsam/code/GSM/#922cf54e97ec
diff -r 196ed16cd62b -r 5f366ddcce18 Storage_Library.lib --- a/Storage_Library.lib Sat Dec 05 07:36:48 2015 +0000 +++ b/Storage_Library.lib Mon Dec 07 00:07:31 2015 +0000 @@ -1,1 +1,1 @@ -https://developer.mbed.org/teams/Flotsam/code/Storage_Library/#d096cf64dd33 +https://developer.mbed.org/teams/Flotsam/code/Storage_Library/#d43bb3c9c037
diff -r 196ed16cd62b -r 5f366ddcce18 Temp_Library.lib --- a/Temp_Library.lib Sat Dec 05 07:36:48 2015 +0000 +++ b/Temp_Library.lib Mon Dec 07 00:07:31 2015 +0000 @@ -1,1 +1,1 @@ -https://developer.mbed.org/teams/Flotsam/code/Temp_Library/#a42f4353261f +https://developer.mbed.org/teams/Flotsam/code/Temp_Library/#fc58ae197a88
diff -r 196ed16cd62b -r 5f366ddcce18 main.cpp --- 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);
diff -r 196ed16cd62b -r 5f366ddcce18 main.h --- a/main.h Sat Dec 05 07:36:48 2015 +0000 +++ b/main.h Mon Dec 07 00:07:31 2015 +0000 @@ -1,34 +1,35 @@ #include "mbed.h" -#define N_READINGS_PER_SEND 5 -#define N_SECONDS_SLEEP 360 +#define URL "" //URL -- Required +#define NETWORK_APN "fast.tmobile.com" //APN URL -#define TMP_ANALOG A0 +#define N_READINGS_PER_SEND 5 // Readings to take before sending +#define N_SECONDS_SLEEP 360 // Seconds between readings + +// Temperature sensor constant +#define TMP_ANALOG A0 // Analog communication for temperature sensor // Global Positioning System constants -#define GPS_TX D6 -#define GPS_RX PB_11 -#define GPS_EN D7 -#define N_GPS_QUERIES 300000 -#define SEC_WAIT_FOR_FIX 60 -#define GPS_BAUD 9600 +#define GPS_TX D6 // Denotes TX port on the Nucleo, attached to RX port on GPS +#define GPS_RX PB_11 // Likewise, TX port on GPS, RX on Nucleo +#define GPS_EN D7 // Enable pin for power on/off. Connected to a digital pin // pH sensor constants -#define PH_TX PC_10 -#define PH_RX PC_11 +#define PH_TX PC_10 // Serial communication for pH +#define PH_RX PC_11 // Serial communication for pH // Cellular communication constants -#define FONA_TX D8 -#define FONA_RX D2 -#define FONA_RST D3 -#define FONA_RI D4 -#define FONA_KEY D5 +#define FONA_TX D8 // Serial communication for GSM +#define FONA_RX D2 // Serial communication for GSM +#define FONA_RST D3 // Required for ported Adafruit Library +#define FONA_RI D4 // "Ring Indicator" to recieve calls +#define FONA_KEY D5 // Turns FONA off/on (pull to GRND for 2 seconds to change state) -#define READINGSIZE sizeof(struct reading) -#define URL "http://requestb.in/1ihfre81" +#define SIZE_OF_ENTRY (45+5*sizeof(int)) // Size of each entry when converted to a string in main.cpp:send(); #ifndef _MAIN_H #define _MAIN_H +// Reading struct that includes the data for each reading we take struct reading { float temperature; float pH;
diff -r 196ed16cd62b -r 5f366ddcce18 pH_Library.lib --- a/pH_Library.lib Sat Dec 05 07:36:48 2015 +0000 +++ b/pH_Library.lib Mon Dec 07 00:07:31 2015 +0000 @@ -1,1 +1,1 @@ -https://developer.mbed.org/teams/Flotsam/code/pH_Sensor/#1314058dbadb +https://developer.mbed.org/teams/Flotsam/code/pH_Sensor/#767f53c397ad