Flotsam / Full-Project

Dependencies:   GPSLibrary GSM mbed-modifed Storage_Library Temp_Library Wakeup pH_Sensor

Committer:
ptcrews
Date:
Wed Nov 18 21:36:49 2015 +0000
Revision:
11:cc22917d6634
Parent:
10:01bbf4f1c250
Child:
12:973566676eba
Added conversion to string format. Sends the data correctly.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ptcrews 1:83d3982f32d5 1 #include "Adafruit_FONA.h"
ptcrews 2:8de680cf7a19 2 #include "WakeUp.h"
ptcrews 1:83d3982f32d5 3 #include "mbed.h"
tomli 5:e180c73f4f70 4 #include "MBed_Adafruit_GPS.h"
tomli 5:e180c73f4f70 5 #include <string>
ptcrews 1:83d3982f32d5 6
tomli 8:b607768bfa0a 7 // Cellular communication constants
ptcrews 1:83d3982f32d5 8 #define FONA_TX D8
ptcrews 1:83d3982f32d5 9 #define FONA_RX D2
ptcrews 1:83d3982f32d5 10 #define FONA_RST D3
ptcrews 1:83d3982f32d5 11 #define FONA_RI D4
ptcrews 1:83d3982f32d5 12 #define FONA_KEY D5
ptcrews 1:83d3982f32d5 13
tomli 8:b607768bfa0a 14 // Global Positioning System constants
tomli 5:e180c73f4f70 15 #define GPS_TX D6
tomli 5:e180c73f4f70 16 #define GPS_RX PB_11
tomli 5:e180c73f4f70 17
tomli 8:b607768bfa0a 18 // pH sensor constants
tomli 5:e180c73f4f70 19 #define PH_TX PC_10
tomli 5:e180c73f4f70 20 #define PH_RX PC_11
tomli 5:e180c73f4f70 21
tomli 10:01bbf4f1c250 22 #define TMP_ANALOG A0
tomli 10:01bbf4f1c250 23 #define ADC_CONVERSION 3.3/5.0
tomli 10:01bbf4f1c250 24 AnalogIn temperature(TMP_ANALOG);
tomli 10:01bbf4f1c250 25
tomli 10:01bbf4f1c250 26 #define READINGSIZE sizeof(struct reading)
ptcrews 11:cc22917d6634 27 #define URL "http://requestb.in/17asutd1"
tomli 10:01bbf4f1c250 28
tomli 8:b607768bfa0a 29 // Cellular communication global variables
ptcrews 1:83d3982f32d5 30 Adafruit_FONA fona(FONA_TX, FONA_RX, FONA_RST, FONA_RI);
ptcrews 1:83d3982f32d5 31 Serial pcSerial(USBTX, USBRX);
ptcrews 1:83d3982f32d5 32 DigitalOut key(FONA_KEY);
ptcrews 1:83d3982f32d5 33
tomli 10:01bbf4f1c250 34 struct reading {
tomli 10:01bbf4f1c250 35 float temperature;
tomli 10:01bbf4f1c250 36 float pH;
tomli 10:01bbf4f1c250 37 float latitude; //Signed positive if N, negative if S
tomli 10:01bbf4f1c250 38 float longitude; //Signed positive if E, negative if W
tomli 10:01bbf4f1c250 39 uint8_t day;
tomli 10:01bbf4f1c250 40 uint8_t month;
tomli 10:01bbf4f1c250 41 uint8_t year;
tomli 10:01bbf4f1c250 42 uint8_t hour;
tomli 10:01bbf4f1c250 43 uint8_t minutes;
tomli 10:01bbf4f1c250 44 };
tomli 10:01bbf4f1c250 45
tomli 8:b607768bfa0a 46 // GPS global variables
tomli 5:e180c73f4f70 47 char c; //when read via Adafruit_GPS::read(), the class returns single character stored here
tomli 5:e180c73f4f70 48 Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info?
tomli 5:e180c73f4f70 49 const int refresh_Time = 2000; //refresh time in ms
tomli 5:e180c73f4f70 50 Serial gps_Serial(GPS_TX, GPS_RX); // Serial object for GPS
tomli 5:e180c73f4f70 51 Adafruit_GPS myGPS(&gps_Serial);
tomli 5:e180c73f4f70 52
tomli 8:b607768bfa0a 53 // pH sensor global variables
tomli 5:e180c73f4f70 54 Serial ph_Serial (PH_TX, PH_RX);
tomli 5:e180c73f4f70 55 string sensorstring = "";
tomli 5:e180c73f4f70 56 bool input_stringcomplete = false;
tomli 5:e180c73f4f70 57 bool sensor_stringcomplete = false;
tomli 5:e180c73f4f70 58 float pH;
tomli 5:e180c73f4f70 59
tomli 10:01bbf4f1c250 60 struct reading lastReadingBuffer;
tomli 10:01bbf4f1c250 61
tomli 5:e180c73f4f70 62 void setupPCSerial() {
tomli 5:e180c73f4f70 63 pcSerial.baud(115200);
tomli 8:b607768bfa0a 64 pcSerial.printf("\n\n PC Serial connection established at 115200 baud.\n");
tomli 5:e180c73f4f70 65 }
tomli 5:e180c73f4f70 66
tomli 8:b607768bfa0a 67 void disableContinuousMode() {
tomli 8:b607768bfa0a 68 printf("pH sensor will NOT run in continous mode.\n");
tomli 5:e180c73f4f70 69 if(ph_Serial.writeable() <= 0) printf("Not writable\n");
tomli 5:e180c73f4f70 70 // disable continuous mode
tomli 5:e180c73f4f70 71 ph_Serial.printf("C,0");
tomli 5:e180c73f4f70 72 ph_Serial.printf("%c", '\r');
tomli 5:e180c73f4f70 73 printf("Waiting five seconds... ");
tomli 5:e180c73f4f70 74 wait(5);
tomli 8:b607768bfa0a 75 }
tomli 8:b607768bfa0a 76
tomli 8:b607768bfa0a 77 void setupPH() {
tomli 8:b607768bfa0a 78 ph_Serial.baud(9600);
tomli 8:b607768bfa0a 79 disableContinuousMode();
tomli 5:e180c73f4f70 80 }
tomli 5:e180c73f4f70 81
tomli 5:e180c73f4f70 82 void setupGPS() {
tomli 5:e180c73f4f70 83 myGPS.begin(9600);
tomli 5:e180c73f4f70 84 myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //these commands are defined in MBed_Adafruit_GPS.h; a link is provided there for command creation
tomli 5:e180c73f4f70 85 myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
tomli 5:e180c73f4f70 86 myGPS.sendCommand(PGCMD_ANTENNA);
tomli 5:e180c73f4f70 87 wait(1);
tomli 5:e180c73f4f70 88 refresh_Timer.start(); //starts the clock on the timer
tomli 5:e180c73f4f70 89 printf("setupGPS seems to be fine\n");
tomli 5:e180c73f4f70 90 }
tomli 5:e180c73f4f70 91
tomli 5:e180c73f4f70 92 void setup() {
tomli 5:e180c73f4f70 93 printf("\n=====\nSetup in Full Project\n");
tomli 5:e180c73f4f70 94 setupPCSerial();
tomli 5:e180c73f4f70 95 setupPH();
tomli 5:e180c73f4f70 96 setupGPS();
tomli 5:e180c73f4f70 97 }
tomli 5:e180c73f4f70 98
tomli 5:e180c73f4f70 99 // Send default message to pH sensor, asking for data.
tomli 5:e180c73f4f70 100 void pHRequest() {
tomli 5:e180c73f4f70 101 printf("Sending pH request...\n");
tomli 5:e180c73f4f70 102 if(ph_Serial.writeable() <= 0) printf("Not writable\n");
tomli 5:e180c73f4f70 103 // request one reading
tomli 5:e180c73f4f70 104 ph_Serial.printf("R");
tomli 5:e180c73f4f70 105 ph_Serial.printf("%c", '\r');
tomli 5:e180c73f4f70 106 }
tomli 5:e180c73f4f70 107
tomli 5:e180c73f4f70 108 void pHRead() {
tomli 5:e180c73f4f70 109 printf("Reading pH information.\n");
tomli 5:e180c73f4f70 110 if (ph_Serial.readable() > 0) { //if we see that the Atlas Scientific product has sent a character.
tomli 5:e180c73f4f70 111 printf("Receiving sensor string... [");
tomli 5:e180c73f4f70 112 char inchar;
tomli 5:e180c73f4f70 113 while((inchar = (char)ph_Serial.getc()) != '\r') {
tomli 5:e180c73f4f70 114 sensorstring += inchar;
tomli 5:e180c73f4f70 115 printf("%c", inchar);
tomli 5:e180c73f4f70 116 }
tomli 5:e180c73f4f70 117 printf("] ...sensor string received!\n");
tomli 5:e180c73f4f70 118 sensor_stringcomplete = true;
tomli 10:01bbf4f1c250 119 printf(sensorstring.c_str());
tomli 10:01bbf4f1c250 120 // +1 is to get rid of control character
tomli 10:01bbf4f1c250 121 pH = atof(sensorstring.c_str()+1); //convert the string to a floating point number so it can be evaluated by the Arduino
tomli 5:e180c73f4f70 122
tomli 5:e180c73f4f70 123 if (pH >= 7.0) { //if the pH is greater than or equal to 7.0
tomli 5:e180c73f4f70 124 printf("high\n"); //print "high" this is demonstrating that the Arduino is evaluating the pH as a number and not as a string
tomli 5:e180c73f4f70 125 }
tomli 5:e180c73f4f70 126
tomli 5:e180c73f4f70 127 if (pH <= 6.999) { //if the pH is less than or equal to 6.999
tomli 5:e180c73f4f70 128 printf("low\n"); //print "low" this is demonstrating that the Arduino is evaluating the pH as a number and not as a string
tomli 5:e180c73f4f70 129 }
tomli 5:e180c73f4f70 130
tomli 5:e180c73f4f70 131 //ph_Serial.printf("SLEEP");
tomli 5:e180c73f4f70 132 //ph_Serial.printf("%c", '\r');
tomli 5:e180c73f4f70 133
tomli 5:e180c73f4f70 134 sensorstring = ""; //clear the string:
tomli 5:e180c73f4f70 135 sensor_stringcomplete = false; //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
tomli 5:e180c73f4f70 136 } else {
tomli 5:e180c73f4f70 137 printf("pH sensor is not readable\n");
tomli 5:e180c73f4f70 138 }
tomli 5:e180c73f4f70 139 }
tomli 5:e180c73f4f70 140
tomli 5:e180c73f4f70 141 // n_queries is the number of times we query the GPS. We need something like 23000 characters.
tomli 5:e180c73f4f70 142 void GPSRead(int n_queries) {
tomli 5:e180c73f4f70 143 pcSerial.printf("\n");
tomli 5:e180c73f4f70 144 for (int i = 0; i < n_queries; i++) {
tomli 5:e180c73f4f70 145 c = myGPS.read(); //queries the GPS
tomli 5:e180c73f4f70 146
tomli 5:e180c73f4f70 147 if (c) { pcSerial.printf("%c", c); } //this line will echo the GPS data if not paused
tomli 5:e180c73f4f70 148
tomli 5:e180c73f4f70 149 //check if we recieved a new message from GPS, if so, attempt to parse it,
tomli 5:e180c73f4f70 150 if ( myGPS.newNMEAreceived() ) {
tomli 5:e180c73f4f70 151 if ( !myGPS.parse(myGPS.lastNMEA()) ) {
tomli 5:e180c73f4f70 152 continue;
tomli 5:e180c73f4f70 153 }
tomli 5:e180c73f4f70 154 }
tomli 5:e180c73f4f70 155
tomli 5:e180c73f4f70 156 //check if enough time has passed to warrant printing GPS info to screen
tomli 5:e180c73f4f70 157 //note if refresh_Time is too low or pcSerial.baud is too low, GPS data may be lost during printing
tomli 5:e180c73f4f70 158 if (refresh_Timer.read_ms() >= refresh_Time) {
tomli 5:e180c73f4f70 159 refresh_Timer.reset();
tomli 5:e180c73f4f70 160 pcSerial.printf("Time: %d:%d:%d.%u\n", myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds);
tomli 5:e180c73f4f70 161 pcSerial.printf("Date: %d/%d/20%d\n", myGPS.day, myGPS.month, myGPS.year);
tomli 5:e180c73f4f70 162 pcSerial.printf("Fix: %d\n", (int) myGPS.fix);
tomli 5:e180c73f4f70 163 pcSerial.printf("Quality: %d\n", (int) myGPS.fixquality);
tomli 10:01bbf4f1c250 164
tomli 10:01bbf4f1c250 165 lastReadingBuffer.hour = myGPS.hour;
tomli 10:01bbf4f1c250 166 lastReadingBuffer.minutes = myGPS.minute;
tomli 10:01bbf4f1c250 167 lastReadingBuffer.day = myGPS.day;
tomli 10:01bbf4f1c250 168 lastReadingBuffer.month = myGPS.month;
tomli 10:01bbf4f1c250 169 lastReadingBuffer.year = myGPS.year;
tomli 10:01bbf4f1c250 170 lastReadingBuffer.latitude = 0.0;
tomli 10:01bbf4f1c250 171 lastReadingBuffer.longitude = 0.0;
tomli 5:e180c73f4f70 172 if (myGPS.fix) {
tomli 10:01bbf4f1c250 173 float mylatitude = myGPS.latitude;
tomli 10:01bbf4f1c250 174 if(myGPS.lat == 'S')
tomli 10:01bbf4f1c250 175 mylatitude *= -1;
tomli 10:01bbf4f1c250 176 float mylongitude = myGPS.longitude;
tomli 10:01bbf4f1c250 177 if(myGPS.lon == 'W')
tomli 10:01bbf4f1c250 178 mylongitude *= -1;
tomli 10:01bbf4f1c250 179 lastReadingBuffer.latitude = mylatitude;
tomli 10:01bbf4f1c250 180 lastReadingBuffer.longitude = mylongitude;
tomli 5:e180c73f4f70 181 pcSerial.printf("Location: %5.2f%c, %5.2f%c\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon);
tomli 5:e180c73f4f70 182 pcSerial.printf("Speed: %5.2f knots\n", myGPS.speed);
tomli 5:e180c73f4f70 183 pcSerial.printf("Angle: %5.2f\n", myGPS.angle);
tomli 5:e180c73f4f70 184 pcSerial.printf("Altitude: %5.2f\n", myGPS.altitude);
tomli 5:e180c73f4f70 185 pcSerial.printf("Satellites: %d\n", myGPS.satellites);
tomli 5:e180c73f4f70 186 }
tomli 5:e180c73f4f70 187 }
tomli 5:e180c73f4f70 188 }
tomli 5:e180c73f4f70 189 pcSerial.printf("\n");
tomli 5:e180c73f4f70 190 }
tomli 5:e180c73f4f70 191
ptcrews 3:6afcf4f3b2aa 192 void setupGSM()
ptcrews 3:6afcf4f3b2aa 193 {
ptcrews 3:6afcf4f3b2aa 194 printf("Starting FONA\n");
ptcrews 3:6afcf4f3b2aa 195 while(!fona.begin(9600)) {
ptcrews 3:6afcf4f3b2aa 196 printf("Cannot find FONA\n");
ptcrews 3:6afcf4f3b2aa 197 wait(1);
ptcrews 3:6afcf4f3b2aa 198 }
ptcrews 3:6afcf4f3b2aa 199 fona.begin(9600);
ptcrews 11:cc22917d6634 200 printf("After begin\n");
ptcrews 11:cc22917d6634 201 fona.setGPRSNetworkSettings("fast.t-mobile.com", "", "");
ptcrews 11:cc22917d6634 202 printf("After set setting\n");
ptcrews 3:6afcf4f3b2aa 203 bool enable = false;
ptcrews 3:6afcf4f3b2aa 204 while(enable != true) {
ptcrews 3:6afcf4f3b2aa 205 fona.enableGPRS(true);
ptcrews 3:6afcf4f3b2aa 206 fona.enableGPRS(false);
ptcrews 3:6afcf4f3b2aa 207 enable = fona.enableGPRS(true);
ptcrews 3:6afcf4f3b2aa 208 }
ptcrews 11:cc22917d6634 209 printf("After enable\n");
ptcrews 3:6afcf4f3b2aa 210 }
ptcrews 3:6afcf4f3b2aa 211
ptcrews 2:8de680cf7a19 212 void changeGSMPowerState()
ptcrews 1:83d3982f32d5 213 {
ptcrews 1:83d3982f32d5 214 key.write(0);
ptcrews 1:83d3982f32d5 215 wait(2);
ptcrews 1:83d3982f32d5 216 key.write(1);
ptcrews 1:83d3982f32d5 217 }
ptcrews 1:83d3982f32d5 218
tomli 10:01bbf4f1c250 219 //Found this online and it claims that it resets ADC to work after deepsleep \_O_/
tomli 10:01bbf4f1c250 220 void resetADC()
tomli 10:01bbf4f1c250 221 {
tomli 10:01bbf4f1c250 222 // Enable the HSI (to clock the ADC)
tomli 10:01bbf4f1c250 223 RCC_OscInitTypeDef RCC_OscInitStruct;
tomli 10:01bbf4f1c250 224 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
tomli 10:01bbf4f1c250 225 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
tomli 10:01bbf4f1c250 226 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
tomli 10:01bbf4f1c250 227 HAL_RCC_OscConfig(&RCC_OscInitStruct);
tomli 10:01bbf4f1c250 228 }
tomli 10:01bbf4f1c250 229
ptcrews 3:6afcf4f3b2aa 230 void enterSleep(int msec)
ptcrews 3:6afcf4f3b2aa 231 {
ptcrews 2:8de680cf7a19 232 if(msec > 0) WakeUp::set_ms(msec);
ptcrews 2:8de680cf7a19 233 deepsleep();
tomli 10:01bbf4f1c250 234 resetADC();
ptcrews 2:8de680cf7a19 235 }
ptcrews 3:6afcf4f3b2aa 236
tomli 10:01bbf4f1c250 237 bool sendDataOverHTTP(char* url, uint8_t* data, int dlength)
ptcrews 3:6afcf4f3b2aa 238 {
ptcrews 3:6afcf4f3b2aa 239 uint16_t statuscode;
ptcrews 3:6afcf4f3b2aa 240 int16_t length;
tomli 10:01bbf4f1c250 241 if (!fona.HTTP_POST_start(url, "text/plain", data, dlength, &statuscode, (uint16_t *)&length)) {
ptcrews 3:6afcf4f3b2aa 242 pcSerial.printf("Failed!\r\n");
ptcrews 3:6afcf4f3b2aa 243 return false;
ptcrews 3:6afcf4f3b2aa 244 }
ptcrews 3:6afcf4f3b2aa 245 while (length > 0) {
ptcrews 3:6afcf4f3b2aa 246 while (fona.readable()) {
ptcrews 3:6afcf4f3b2aa 247 char c = fona.getc();
ptcrews 3:6afcf4f3b2aa 248 pcSerial.putc(c);
ptcrews 3:6afcf4f3b2aa 249 length--;
ptcrews 3:6afcf4f3b2aa 250 if (! length) break;
ptcrews 3:6afcf4f3b2aa 251 }
ptcrews 3:6afcf4f3b2aa 252 }
ptcrews 3:6afcf4f3b2aa 253 pcSerial.printf("\r\n****\r\n");
ptcrews 3:6afcf4f3b2aa 254 fona.HTTP_POST_end();
ptcrews 3:6afcf4f3b2aa 255 return true;
ptcrews 3:6afcf4f3b2aa 256 }
ptcrews 2:8de680cf7a19 257
tomli 10:01bbf4f1c250 258 //define where the EEPROM begins
tomli 10:01bbf4f1c250 259 #define stadr 0x08080000
tomli 10:01bbf4f1c250 260
tomli 10:01bbf4f1c250 261 //Our current offset in the EEPROM
tomli 10:01bbf4f1c250 262 int offset = 0;
tomli 10:01bbf4f1c250 263 int roffset = 0;
tomli 10:01bbf4f1c250 264
tomli 10:01bbf4f1c250 265 //This function writes a byte of data to the EEPROM at the appropriate location.
tomli 10:01bbf4f1c250 266 //This is called by my writeEEPROMbytes.
tomli 10:01bbf4f1c250 267 //Use this if you want to write a single byte.
tomli 10:01bbf4f1c250 268 HAL_StatusTypeDef writeEEPROMByte(uint8_t data)
tomli 10:01bbf4f1c250 269 {
tomli 10:01bbf4f1c250 270 HAL_StatusTypeDef status;
tomli 10:01bbf4f1c250 271 int address = offset + stadr;
tomli 10:01bbf4f1c250 272 offset++;
tomli 10:01bbf4f1c250 273 HAL_FLASHEx_DATAEEPROM_Unlock(); //Unprotect the EEPROM to allow writing
tomli 10:01bbf4f1c250 274 status = HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_BYTE, address, data);
tomli 10:01bbf4f1c250 275 HAL_FLASHEx_DATAEEPROM_Lock(); // Reprotect the EEPROM
tomli 10:01bbf4f1c250 276 return status;
tomli 10:01bbf4f1c250 277 }
tomli 10:01bbf4f1c250 278
tomli 10:01bbf4f1c250 279 //This function takes an array of bytes and its size and writes them to the EEPROM
tomli 10:01bbf4f1c250 280 //Use this if you want to write a lot of bytes.
tomli 10:01bbf4f1c250 281 void writeEEPROMbytes(uint8_t* data, uint8_t size)
tomli 10:01bbf4f1c250 282 {
tomli 10:01bbf4f1c250 283 for(int i = 0; i < size; i++)
tomli 10:01bbf4f1c250 284 {
tomli 10:01bbf4f1c250 285 writeEEPROMByte(data[i]);
tomli 10:01bbf4f1c250 286 }
tomli 10:01bbf4f1c250 287 }
tomli 10:01bbf4f1c250 288
tomli 10:01bbf4f1c250 289 //This function reads a byte of data from the EEPROM at the offset passed in.
tomli 10:01bbf4f1c250 290 //This is called by my getEEPROM function
tomli 10:01bbf4f1c250 291 uint8_t readEEPROMByte(uint32_t off) {
tomli 10:01bbf4f1c250 292 uint8_t tmp = 0;
tomli 10:01bbf4f1c250 293 off = off + stadr;
tomli 10:01bbf4f1c250 294 tmp = *(__IO uint32_t*)off;
tomli 10:01bbf4f1c250 295
tomli 10:01bbf4f1c250 296 return tmp;
tomli 10:01bbf4f1c250 297 }
tomli 10:01bbf4f1c250 298
tomli 10:01bbf4f1c250 299 //Call this function when you have sent all the data in the EEPROM through GSM
tomli 10:01bbf4f1c250 300 //It just resets the offset to 0 so we start writing from the start.
tomli 10:01bbf4f1c250 301 void resetEEPROM()
tomli 10:01bbf4f1c250 302 {
tomli 10:01bbf4f1c250 303 offset = 0;
tomli 10:01bbf4f1c250 304 }
tomli 10:01bbf4f1c250 305
tomli 10:01bbf4f1c250 306 //This takes an array of bytes an fills it with our entire EEPROM
tomli 10:01bbf4f1c250 307 //Call this function when you want to send the data over GSM
tomli 10:01bbf4f1c250 308 void getEEPROM(uint8_t *ptr)
tomli 10:01bbf4f1c250 309 {
tomli 10:01bbf4f1c250 310 int nbytes = offset;
tomli 10:01bbf4f1c250 311 printf("The number of bytes in the EEPROM is %d\n", nbytes);
tomli 10:01bbf4f1c250 312 for(int i = 0; i < nbytes; i++)
tomli 10:01bbf4f1c250 313 {
tomli 10:01bbf4f1c250 314 ptr[i] = readEEPROMByte(i);
tomli 10:01bbf4f1c250 315 }
tomli 10:01bbf4f1c250 316 //printf("WARNING DEBUG CODE BREAKS THINGS");
tomli 10:01bbf4f1c250 317 //roffset += nbytes;
tomli 10:01bbf4f1c250 318 return;
tomli 10:01bbf4f1c250 319 }
tomli 10:01bbf4f1c250 320
tomli 10:01bbf4f1c250 321 //function to get both the
tomli 10:01bbf4f1c250 322 float AD22100K_AI_value_to_Celsius() { // Convert Analog-input value to temperature
tomli 10:01bbf4f1c250 323 //1023 is to scale it up to the arduino read values.
tomli 10:01bbf4f1c250 324 float voltage = (int)((temperature.read() * ADC_CONVERSION) * 1023);
tomli 10:01bbf4f1c250 325
tomli 10:01bbf4f1c250 326 float temperatureValue = (voltage * 0.217226044) - 61.1111111; // conversion factor simplified.
tomli 10:01bbf4f1c250 327 pcSerial.printf("AI_Val: %f\n", temperatureValue);
tomli 10:01bbf4f1c250 328 lastReadingBuffer.temperature = temperatureValue;
tomli 10:01bbf4f1c250 329 return temperatureValue; // 22.5 mV / °C; Ratiometric measurement, conversion valid for 5 V!
tomli 10:01bbf4f1c250 330 }
tomli 10:01bbf4f1c250 331
ptcrews 11:cc22917d6634 332 bool sendDataToURL(uint8_t* data, int size)
tomli 10:01bbf4f1c250 333 {
tomli 10:01bbf4f1c250 334 changeGSMPowerState();
tomli 10:01bbf4f1c250 335 setupGSM();
ptcrews 11:cc22917d6634 336 printf("Setup GSM\n");
ptcrews 11:cc22917d6634 337 bool res = sendDataOverHTTP(URL, data, size);
ptcrews 11:cc22917d6634 338 printf("After sent data\n");
tomli 10:01bbf4f1c250 339 changeGSMPowerState();
tomli 10:01bbf4f1c250 340 return res;
tomli 10:01bbf4f1c250 341 }
tomli 9:b6e9751fbcba 342
tomli 9:b6e9751fbcba 343 // In the final version of this code:
tomli 9:b6e9751fbcba 344 // We wake up ten times an hour to record information from our sensors.
tomli 9:b6e9751fbcba 345 // but we wait until we have accumulated 240 measurements before we
tomli 9:b6e9751fbcba 346 // start attempting to send away all our data. Once we successfully send
tomli 9:b6e9751fbcba 347 // our information, we clear our memory and begin from the start.
tomli 9:b6e9751fbcba 348
tomli 9:b6e9751fbcba 349 // TODO: Sleep mode. How does the Nucleo enter / exit sleep mode?
tomli 9:b6e9751fbcba 350 // How do we power on / power off the other devices:
tomli 9:b6e9751fbcba 351 // - GPS?
tomli 9:b6e9751fbcba 352 // use the EN pin on the GPS
tomli 9:b6e9751fbcba 353 // - pH sensor?
tomli 9:b6e9751fbcba 354 // There's a sleep command
tomli 9:b6e9751fbcba 355 // - Temperature sensor?
tomli 10:01bbf4f1c250 356 // Should automatically switch on / off when the nucleo enters deep sleep mode.
tomli 9:b6e9751fbcba 357 // - GSM?
tomli 9:b6e9751fbcba 358 // The enable pin is modified by changeGSMPowerStat();
tomli 9:b6e9751fbcba 359 // TODO: Web communication
tomli 9:b6e9751fbcba 360 // - write server
tomli 9:b6e9751fbcba 361
ptcrews 1:83d3982f32d5 362 int main()
ptcrews 1:83d3982f32d5 363 {
tomli 5:e180c73f4f70 364 setup();
tomli 10:01bbf4f1c250 365 int nreadings = 0;
tomli 10:01bbf4f1c250 366 bool toSend = false;
tomli 5:e180c73f4f70 367 while (true) {
tomli 5:e180c73f4f70 368 printf("~~~~~[pH]~~~~~\n");
tomli 5:e180c73f4f70 369 pHRequest();
tomli 5:e180c73f4f70 370 pHRead();
tomli 10:01bbf4f1c250 371 lastReadingBuffer.pH = pH;
tomli 5:e180c73f4f70 372 wait(1);
tomli 10:01bbf4f1c250 373 printf("~~~~~[GPS]~~~~~\n");
tomli 5:e180c73f4f70 374 GPSRead(300000);
tomli 5:e180c73f4f70 375 wait(1);
tomli 10:01bbf4f1c250 376 printf("~~~~~[Temperature]~~~~~\n");
tomli 10:01bbf4f1c250 377 AD22100K_AI_value_to_Celsius();
tomli 10:01bbf4f1c250 378 wait(1);
tomli 10:01bbf4f1c250 379 writeEEPROMbytes((uint8_t *) &lastReadingBuffer, READINGSIZE);
tomli 10:01bbf4f1c250 380 nreadings++;
tomli 10:01bbf4f1c250 381 if(nreadings == 1)
tomli 10:01bbf4f1c250 382 toSend = true;
tomli 10:01bbf4f1c250 383 if(toSend) {
tomli 10:01bbf4f1c250 384 struct reading* data = new struct reading[nreadings];
tomli 10:01bbf4f1c250 385 getEEPROM((uint8_t*) data);
ptcrews 11:cc22917d6634 386 char buffer[((8+8+10+10+5*sizeof(int)+8)*nreadings) + 1];
tomli 10:01bbf4f1c250 387 for(int i = 0; i < nreadings; i++)
tomli 10:01bbf4f1c250 388 {
ptcrews 11:cc22917d6634 389 int n = sprintf(buffer, "%8f %8f %10f %10f %d %d %d %d %d", data[i].temperature, data[i].pH, data[i].latitude, data[i].longitude, data[i].day,
ptcrews 11:cc22917d6634 390 data[i].month, data[i].year, data[i].hour, data[i].minutes);
ptcrews 11:cc22917d6634 391 printf("%s\n", buffer);
ptcrews 11:cc22917d6634 392 printf("%d\n", n);
ptcrews 11:cc22917d6634 393 printf("%d\n", (sizeof(buffer)));
ptcrews 11:cc22917d6634 394 printf("%d\n", data[i].minutes);
tomli 10:01bbf4f1c250 395 }
tomli 10:01bbf4f1c250 396 printf("HERE\n");
ptcrews 11:cc22917d6634 397 bool res = sendDataToURL((uint8_t*) buffer, sizeof(buffer));
tomli 10:01bbf4f1c250 398 if(res) {
tomli 10:01bbf4f1c250 399 toSend = false;
tomli 10:01bbf4f1c250 400 nreadings = 0;
tomli 10:01bbf4f1c250 401 resetEEPROM();
tomli 10:01bbf4f1c250 402 }
tomli 10:01bbf4f1c250 403 delete[] data;
tomli 10:01bbf4f1c250 404 }
tomli 10:01bbf4f1c250 405 /*
tomli 10:01bbf4f1c250 406 printf("Reading Size is %d\n", READINGSIZE);
tomli 10:01bbf4f1c250 407 printf("ROM Latitude: %f\n", verifier.latitude);
tomli 10:01bbf4f1c250 408 printf("ROM Longitude: %f\n", verifier.longitude);
tomli 10:01bbf4f1c250 409 printf("ROM pH: %f\n", verifier.pH);
tomli 10:01bbf4f1c250 410 printf("ROM Temperature: %f\n", verifier.temperature);
tomli 10:01bbf4f1c250 411 printf("ROM Day: %d\n", verifier.day);
tomli 10:01bbf4f1c250 412 printf("ROM Month: %d\n", verifier.month);
tomli 10:01bbf4f1c250 413 printf("ROM Year: %d\n", verifier.year);
tomli 10:01bbf4f1c250 414 printf("ROM Hour: %d\n", verifier.hour);
tomli 10:01bbf4f1c250 415 printf("ROM Minutes: %d\n", verifier.minutes);
tomli 10:01bbf4f1c250 416 */
tomli 10:01bbf4f1c250 417 wait(2);
tomli 10:01bbf4f1c250 418 enterSleep(10000);
tomli 5:e180c73f4f70 419 }
ptcrews 1:83d3982f32d5 420
ptcrews 1:83d3982f32d5 421 return 0;
ptcrews 1:83d3982f32d5 422 }