all working version 2.0

Dependencies:   ADS1115 BME280 CronoDot SDFileSystem mbed

Fork of Outdoor_UPAS_v1_2_powerfunction by scott kelleher

Committer:
scottkelleher
Date:
Tue May 24 20:52:26 2016 +0000
Revision:
59:4221935a12d8
Parent:
58:3233c10a668c
Child:
60:e434c8c85020
Child:
61:69ec677bf39f
latest version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jelord 0:2cb2b2ea316f 1 #include "mbed.h"
jelord 2:88fcbfadec6a 2 #include "SDFileSystem.h"
jelord 2:88fcbfadec6a 3 #include "Adafruit_ADS1015.h"
jelord 2:88fcbfadec6a 4 #include "BME280.h"
jelord 2:88fcbfadec6a 5 #include "CronoDot.h"
scottkelleher 57:1695e252298d 6
caseyquinn 14:7cdb643da356 7
caseyquinn 56:49387b72460e 8 //Edit for fork
caseyquinn 12:5b4f3245606a 9 /////////////////////////////////////////////
caseyquinn 12:5b4f3245606a 10 //General Items
caseyquinn 12:5b4f3245606a 11 /////////////////////////////////////////////
caseyquinn 14:7cdb643da356 12 I2C i2c(PB_9, PB_8);//(D14, D15); SDA,SCL
caseyquinn 12:5b4f3245606a 13 Serial pc(USBTX, USBRX);
caseyquinn 12:5b4f3245606a 14 BME280 bmesensor(PB_9, PB_8);//(D14, D15);
caseyquinn 17:3e6dda6e6335 15 CronoDot RTC_UPAS(PB_9, PB_8);//(D14, D15);
caseyquinn 17:3e6dda6e6335 16
caseyquinn 13:455601f62aad 17
caseyquinn 12:5b4f3245606a 18 /////////////////////////////////////////////
caseyquinn 12:5b4f3245606a 19 //Analog to Digital Converter
caseyquinn 12:5b4f3245606a 20 /////////////////////////////////////////////
scottkelleher 59:4221935a12d8 21 Adafruit_ADS1115 ads(&i2c);
scottkelleher 59:4221935a12d8 22 Adafruit_ADS1115 ads2(&i2c);
caseyquinn 12:5b4f3245606a 23 //DigitalIn ADS_ALRT(PA_10); //Connected but currently unused. (ADS1115) http://www.ti.com/lit/ds/symlink/ads1115.pdf
caseyquinn 12:5b4f3245606a 24
caseyquinn 12:5b4f3245606a 25
caseyquinn 23:1ca41779b8ec 26
caseyquinn 12:5b4f3245606a 27 /////////////////////////////////////////////
caseyquinn 12:5b4f3245606a 28 //SD Card
caseyquinn 12:5b4f3245606a 29 /////////////////////////////////////////////
scottkelleher 57:1695e252298d 30 char filename[] = "/sd/SHARP_LOG00.txt";
scottkelleher 57:1695e252298d 31 SDFileSystem sd(D11, D12, D13, D10, "sd"); // (MOSI, MISO, SCK, SEL)
scottkelleher 57:1695e252298d 32 //DigitalIn sdCD(PA_11, PullUp);
caseyquinn 52:95949e34b1f6 33
caseyquinn 14:7cdb643da356 34
caseyquinn 12:5b4f3245606a 35 /////////////////////////////////////////////
caseyquinn 12:5b4f3245606a 36 //Callbacks
caseyquinn 12:5b4f3245606a 37 /////////////////////////////////////////////
caseyquinn 8:c4a8f9b67cee 38 Ticker logg; //This is the logging callback object
scottkelleher 57:1695e252298d 39
jelord 3:122bfc998c4c 40
caseyquinn 12:5b4f3245606a 41 /////////////////////////////////////////////
caseyquinn 12:5b4f3245606a 42 //Varible Definitions
caseyquinn 12:5b4f3245606a 43 /////////////////////////////////////////////
jelord 3:122bfc998c4c 44 uint16_t serial_num = 1; // Default serial/calibration number
jelord 3:122bfc998c4c 45 int RunReady =0;
jelord 3:122bfc998c4c 46
caseyquinn 25:fbf7d44e7da4 47 bool ledOn = 0;
caseyquinn 25:fbf7d44e7da4 48
caseyquinn 4:5d004fd997d5 49 struct tm STtime;
caseyquinn 7:29b01d5812ee 50 char timestr[32];
caseyquinn 43:05aa1fb68946 51 char yrstr[4];
caseyquinn 43:05aa1fb68946 52 char mostr[4];
caseyquinn 43:05aa1fb68946 53 char daystr[4];
caseyquinn 43:05aa1fb68946 54 char hrstr[4];
caseyquinn 43:05aa1fb68946 55 char minstr[4];
caseyquinn 43:05aa1fb68946 56 char secstr[4];
caseyquinn 43:05aa1fb68946 57
caseyquinn 43:05aa1fb68946 58 int stYr, stMo, stDay, stHr, stMin, stSec;
jelord 3:122bfc998c4c 59
caseyquinn 36:2e344db70d35 60
caseyquinn 9:8646fd501832 61 float press;
caseyquinn 9:8646fd501832 62 float temp;
caseyquinn 9:8646fd501832 63 float rh;
scottkelleher 57:1695e252298d 64 float atmoRho; //g/L
scottkelleher 59:4221935a12d8 65 int logInerval = 2;//seconds
jelord 3:122bfc998c4c 66
scottkelleher 57:1695e252298d 67 DigitalOut sharp1LED(D7, 1);
scottkelleher 57:1695e252298d 68 DigitalOut sharp2LED(D6, 1);
scottkelleher 57:1695e252298d 69 DigitalOut sharp3LED(D5, 1);
scottkelleher 57:1695e252298d 70 DigitalOut sharp4LED(D4, 1);
scottkelleher 59:4221935a12d8 71 DigitalOut sharp5LED(D3, 1);
scottkelleher 59:4221935a12d8 72 DigitalOut sharp6LED(D2, 1);
jelord 3:122bfc998c4c 73
scottkelleher 57:1695e252298d 74 int samplingTime = 280;
scottkelleher 57:1695e252298d 75 int deltaTime = 40;
scottkelleher 59:4221935a12d8 76 int sharp1, sharp2, sharp3, sharp4, sharp5, sharp6;
scottkelleher 59:4221935a12d8 77 float sharpVolt1, sharpVolt2, sharpVolt3, sharpVolt4, sharpVolt5, sharpVolt6; //V
caseyquinn 12:5b4f3245606a 78
caseyquinn 11:aa21628a9b15 79
caseyquinn 11:aa21628a9b15 80
caseyquinn 5:c3252e5d45ca 81
caseyquinn 9:8646fd501832 82 //////////////////////////////////////////////////////////////
scottkelleher 57:1695e252298d 83 //SD Logging Function
caseyquinn 9:8646fd501832 84 //////////////////////////////////////////////////////////////
scottkelleher 57:1695e252298d 85 void log_data()
caseyquinn 52:95949e34b1f6 86 {
scottkelleher 57:1695e252298d 87 //Get RTC time(s)
scottkelleher 57:1695e252298d 88 ///////////////////////////
scottkelleher 57:1695e252298d 89 RTC_UPAS.get_time();
scottkelleher 57:1695e252298d 90 time_t seconds = time(NULL);
scottkelleher 57:1695e252298d 91 strftime(timestr, 32, "%y%m%d%H%M%S", localtime(&seconds));
scottkelleher 57:1695e252298d 92 /*
caseyquinn 43:05aa1fb68946 93 strftime(yrstr, 4, "%y", localtime(&seconds));
caseyquinn 43:05aa1fb68946 94 stYr = atoi(yrstr);
caseyquinn 43:05aa1fb68946 95
caseyquinn 43:05aa1fb68946 96 strftime(mostr, 4, "%m", localtime(&seconds));
caseyquinn 43:05aa1fb68946 97 stMo = atoi(mostr);
caseyquinn 43:05aa1fb68946 98
caseyquinn 43:05aa1fb68946 99 strftime(daystr, 4, "%d", localtime(&seconds));
caseyquinn 43:05aa1fb68946 100 stDay = atoi(daystr);
caseyquinn 43:05aa1fb68946 101
caseyquinn 43:05aa1fb68946 102 strftime(hrstr, 4, "%H", localtime(&seconds));
caseyquinn 43:05aa1fb68946 103 stHr = atoi(hrstr);
caseyquinn 43:05aa1fb68946 104
caseyquinn 43:05aa1fb68946 105 strftime(minstr, 4, "%M", localtime(&seconds));
caseyquinn 43:05aa1fb68946 106 stMin = atoi(minstr);
caseyquinn 43:05aa1fb68946 107
caseyquinn 43:05aa1fb68946 108 strftime(secstr, 4, "%S", localtime(&seconds));
caseyquinn 43:05aa1fb68946 109 stSec = atoi(secstr);
scottkelleher 57:1695e252298d 110 */
caseyquinn 44:075fa63d5df1 111 //pc.printf("%s,%s,%d,%s,%d,%s,%d,%s,%d,%s,%d,%s,%d\r\n", timestr,yrstr,stYr,mostr,stMo,daystr,stDay,hrstr,stHr,minstr,stMin,secstr,stSec);
caseyquinn 36:2e344db70d35 112
caseyquinn 46:99d129bfdbbd 113
caseyquinn 28:42932d3b105d 114 //Get Sensor Data except GPS
caseyquinn 28:42932d3b105d 115 ////////////////////////////
caseyquinn 7:29b01d5812ee 116 press = bmesensor.getPressure();
scottkelleher 58:3233c10a668c 117 temp = bmesensor.getTemperature();
caseyquinn 7:29b01d5812ee 118 rh = bmesensor.getHumidity();
scottkelleher 57:1695e252298d 119 atmoRho = ((press-((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)))*100)/(287.0531*(temp+273.15))+((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)*100)/(461.4964*(temp+273.15));
caseyquinn 35:e0bdd6389a75 120
scottkelleher 59:4221935a12d8 121 //pc.printf("%f,%f,%f,%f\r\n", temp, press, rh, atmoRho);
scottkelleher 59:4221935a12d8 122
scottkelleher 57:1695e252298d 123 sharp1LED = 0;
scottkelleher 57:1695e252298d 124 sharp2LED = 0;
scottkelleher 57:1695e252298d 125 sharp3LED = 0;
scottkelleher 57:1695e252298d 126 sharp4LED = 0;
scottkelleher 59:4221935a12d8 127 sharp5LED = 0;
scottkelleher 59:4221935a12d8 128 sharp6LED = 0;
scottkelleher 59:4221935a12d8 129
scottkelleher 59:4221935a12d8 130 wait_us(samplingTime);
caseyquinn 14:7cdb643da356 131
scottkelleher 57:1695e252298d 132 sharp1 = ads.readADC_SingleEnded(0, 0xC383); // read channel 1
scottkelleher 57:1695e252298d 133 sharpVolt1 = (sharp1*4.096)/(32768*1);
scottkelleher 57:1695e252298d 134 sharp2 = ads.readADC_SingleEnded(1, 0xD383); // read channel 1
scottkelleher 57:1695e252298d 135 sharpVolt2 = (sharp2*4.096)/(32768*1);
scottkelleher 57:1695e252298d 136 sharp3 = ads.readADC_SingleEnded(2, 0xE383); // read channel 1
scottkelleher 57:1695e252298d 137 sharpVolt3 = (sharp3*4.096)/(32768*1);
scottkelleher 57:1695e252298d 138 sharp4 = ads.readADC_SingleEnded(3, 0xF383); // read channel 1
scottkelleher 57:1695e252298d 139 sharpVolt4 = (sharp4*4.096)/(32768*1);
caseyquinn 50:5f1b378e4f64 140
scottkelleher 59:4221935a12d8 141 sharp5 = ads2.readADC_SingleEnded(0, 0xC383); // read channel 1
scottkelleher 59:4221935a12d8 142 sharpVolt5 = (sharp4*4.096)/(32768*1);
scottkelleher 59:4221935a12d8 143 sharp6 = ads2.readADC_SingleEnded(1, 0xD383); // read channel 1
scottkelleher 59:4221935a12d8 144 sharpVolt6 = (sharp4*4.096)/(32768*1);
scottkelleher 59:4221935a12d8 145
scottkelleher 59:4221935a12d8 146 wait_us(deltaTime);
caseyquinn 50:5f1b378e4f64 147
scottkelleher 57:1695e252298d 148 sharp1LED = 1;
scottkelleher 57:1695e252298d 149 sharp2LED = 1;
scottkelleher 57:1695e252298d 150 sharp3LED = 1;
scottkelleher 57:1695e252298d 151 sharp4LED = 1;
scottkelleher 59:4221935a12d8 152 sharp5LED = 1;
scottkelleher 59:4221935a12d8 153 sharp6LED = 1;
caseyquinn 14:7cdb643da356 154
scottkelleher 59:4221935a12d8 155 pc.printf("%d,%f,%d,%f,\r\n" ,sharp5,sharpVolt5, sharp6, sharpVolt6);
scottkelleher 59:4221935a12d8 156 //pc.printf("%d,%f,%d,%f,%d,%f,%d,%f\r\n" ,sharp1,sharpVolt1, sharp2, sharpVolt2,sharp3,sharpVolt3, sharp4, sharpVolt4);
scottkelleher 59:4221935a12d8 157 //pc.printf("%2.2f,%4.2f,%2.1f,%1.3f\r\n", temp,press,rh,atmoRho);
scottkelleher 59:4221935a12d8 158 //pc.printf("%d,%f,%d,%f\r\n" ,sharp3,sharpVolt3, sharp4, sharpVolt4);
caseyquinn 27:922f53fa649c 159
caseyquinn 7:29b01d5812ee 160 FILE *fp = fopen(filename, "a");
scottkelleher 59:4221935a12d8 161 // fprintf(fp, "%02d,%02d,%02d,%02d,%02d,%02d,",RTC_UPAS.year, RTC_UPAS.month,RTC_UPAS.date,RTC_UPAS.hour,RTC_UPAS.minutes,RTC_UPAS.seconds);
scottkelleher 59:4221935a12d8 162 //fprintf(fp, "%s,", timestr);
scottkelleher 59:4221935a12d8 163 fprintf(fp, "%2.2f,%4.2f,%2.1f,%1.3f", temp,press,rh,atmoRho);
scottkelleher 57:1695e252298d 164 fprintf(fp, "%d,%f,%d,%f," ,sharp1,sharpVolt1, sharp2, sharpVolt2);
scottkelleher 59:4221935a12d8 165 fprintf(fp, "%d,%f,%d,%f" ,sharp3,sharpVolt3, sharp4, sharpVolt4);
scottkelleher 59:4221935a12d8 166 fprintf("%d,%f,%d,%f,\r\n" ,sharp5,sharpVolt5, sharp6, sharpVolt6);
scottkelleher 57:1695e252298d 167 fclose(fp);
caseyquinn 7:29b01d5812ee 168 free(fp);
caseyquinn 25:fbf7d44e7da4 169
caseyquinn 25:fbf7d44e7da4 170
caseyquinn 14:7cdb643da356 171
caseyquinn 12:5b4f3245606a 172
caseyquinn 25:fbf7d44e7da4 173
caseyquinn 12:5b4f3245606a 174
jelord 3:122bfc998c4c 175 }
caseyquinn 8:c4a8f9b67cee 176
caseyquinn 25:fbf7d44e7da4 177
caseyquinn 9:8646fd501832 178 //////////////////////////////////////////////////////////////
caseyquinn 9:8646fd501832 179 //Main Function
caseyquinn 9:8646fd501832 180 //////////////////////////////////////////////////////////////
jelord 1:9fbb5b665068 181 int main(){
caseyquinn 27:922f53fa649c 182
caseyquinn 29:fd74725294d5 183
scottkelleher 57:1695e252298d 184
caseyquinn 10:06fbb1c9e3bd 185 pc.baud(115200); // set what you want here depending on your terminal program speed
caseyquinn 10:06fbb1c9e3bd 186 pc.printf("\f\n\r-------------Startup-------------\n\r");
jelord 1:9fbb5b665068 187 wait(0.5);
caseyquinn 5:c3252e5d45ca 188
caseyquinn 36:2e344db70d35 189
scottkelleher 57:1695e252298d 190 RTC_UPAS.set_time(0,0,0,1,1,1,16);//sets chronodot RTC
caseyquinn 29:fd74725294d5 191
scottkelleher 57:1695e252298d 192 ///////////////////////
scottkelleher 57:1695e252298d 193 //sets ST RTC
scottkelleher 57:1695e252298d 194 //////////////////////
scottkelleher 57:1695e252298d 195 STtime.tm_sec = 0; // 0-59
scottkelleher 57:1695e252298d 196 STtime.tm_min = 0; // 0-59
scottkelleher 57:1695e252298d 197 STtime.tm_hour = 0; // 0-23
scottkelleher 57:1695e252298d 198 STtime.tm_mday = 1; // 1-31
scottkelleher 57:1695e252298d 199 STtime.tm_mon = 0; // 0-11
scottkelleher 57:1695e252298d 200 STtime.tm_year = 116; // year since 1900 (116 = 2016)
scottkelleher 57:1695e252298d 201 time_t STseconds = mktime(&STtime);
scottkelleher 57:1695e252298d 202 set_time(STseconds); // Set RTC time
caseyquinn 29:fd74725294d5 203
scottkelleher 57:1695e252298d 204
caseyquinn 52:95949e34b1f6 205 RTC_UPAS.get_time();
caseyquinn 46:99d129bfdbbd 206
caseyquinn 52:95949e34b1f6 207
caseyquinn 43:05aa1fb68946 208 time_t seconds = time(NULL);
caseyquinn 43:05aa1fb68946 209 strftime(timestr, 32, "%y-%m-%d-%H=%M=%S", localtime(&seconds));
caseyquinn 52:95949e34b1f6 210
caseyquinn 52:95949e34b1f6 211 strftime(yrstr, 4, "%y", localtime(&seconds));
caseyquinn 52:95949e34b1f6 212 stYr = atoi(yrstr);
caseyquinn 52:95949e34b1f6 213
caseyquinn 52:95949e34b1f6 214 strftime(mostr, 4, "%m", localtime(&seconds));
caseyquinn 52:95949e34b1f6 215 stMo = atoi(mostr);
caseyquinn 52:95949e34b1f6 216
caseyquinn 52:95949e34b1f6 217 strftime(daystr, 4, "%d", localtime(&seconds));
caseyquinn 52:95949e34b1f6 218 stDay = atoi(daystr);
caseyquinn 52:95949e34b1f6 219
caseyquinn 52:95949e34b1f6 220 strftime(hrstr, 4, "%H", localtime(&seconds));
caseyquinn 52:95949e34b1f6 221 stHr = atoi(hrstr);
caseyquinn 52:95949e34b1f6 222
caseyquinn 52:95949e34b1f6 223 strftime(minstr, 4, "%M", localtime(&seconds));
caseyquinn 52:95949e34b1f6 224 stMin = atoi(minstr);
caseyquinn 52:95949e34b1f6 225
caseyquinn 52:95949e34b1f6 226 strftime(secstr, 4, "%S", localtime(&seconds));
caseyquinn 52:95949e34b1f6 227 stSec = atoi(secstr);
caseyquinn 52:95949e34b1f6 228
caseyquinn 52:95949e34b1f6 229
caseyquinn 52:95949e34b1f6 230
caseyquinn 52:95949e34b1f6 231
scottkelleher 57:1695e252298d 232 // sprintf(filename,"/sd/SHARP_LOG00.txt");
scottkelleher 57:1695e252298d 233
scottkelleher 57:1695e252298d 234 for (uint8_t i = 0; i < 100; i++) {
scottkelleher 57:1695e252298d 235 filename[13] = i/10 + '0';
scottkelleher 57:1695e252298d 236 filename[14] = i%10 + '0';
scottkelleher 57:1695e252298d 237 FILE *fp = fopen(filename, "r");
scottkelleher 57:1695e252298d 238 if (fp == NULL) {
scottkelleher 57:1695e252298d 239 // only open a new file if it doesn't exist
scottkelleher 57:1695e252298d 240 FILE *fp = fopen(filename, "w");
scottkelleher 57:1695e252298d 241 fclose(fp);
scottkelleher 57:1695e252298d 242 break; // leave the loop!
scottkelleher 57:1695e252298d 243 }
scottkelleher 57:1695e252298d 244 }
caseyquinn 36:2e344db70d35 245
caseyquinn 29:fd74725294d5 246
caseyquinn 14:7cdb643da356 247
caseyquinn 46:99d129bfdbbd 248 while(fmod((double)stSec,10)!=0) {
caseyquinn 46:99d129bfdbbd 249 //pc.printf("%f, %f\r\n", floor(secondsD), floor(lastsecondD));
caseyquinn 46:99d129bfdbbd 250 time_t seconds = time(NULL);
caseyquinn 46:99d129bfdbbd 251 strftime(secstr, 4, "%S", localtime(&seconds));
caseyquinn 46:99d129bfdbbd 252 stSec = atoi(secstr);
caseyquinn 46:99d129bfdbbd 253 wait_ms(100);
caseyquinn 46:99d129bfdbbd 254 }
caseyquinn 46:99d129bfdbbd 255
scottkelleher 59:4221935a12d8 256 logg.attach(&log_data, 3);
scottkelleher 57:1695e252298d 257
caseyquinn 35:e0bdd6389a75 258
caseyquinn 35:e0bdd6389a75 259
caseyquinn 30:aa6324845a84 260
jelord 3:122bfc998c4c 261 //** end of initalization **//
jelord 3:122bfc998c4c 262 //---------------------------------------------------------------------------------------------//
jelord 3:122bfc998c4c 263 //---------------------------------------------------------------------------------------------//
jelord 3:122bfc998c4c 264 // Main Control Loop
jelord 3:122bfc998c4c 265
caseyquinn 7:29b01d5812ee 266 while (1) {
caseyquinn 7:29b01d5812ee 267 // Do other things...
caseyquinn 46:99d129bfdbbd 268
caseyquinn 46:99d129bfdbbd 269 }
caseyquinn 7:29b01d5812ee 270
caseyquinn 8:c4a8f9b67cee 271
caseyquinn 4:5d004fd997d5 272
jelord 3:122bfc998c4c 273
jelord 0:2cb2b2ea316f 274 }