all working version 2.0

Dependencies:   ADS1115 BME280 CronoDot SDFileSystem mbed

Fork of Outdoor_UPAS_v1_2_powerfunction by scott kelleher

Committer:
scottkelleher
Date:
Fri May 20 18:27:55 2016 +0000
Revision:
57:1695e252298d
Parent:
56:49387b72460e
Child:
58:3233c10a668c
first cut of 4x sharp sensors on nucleo L152

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 /////////////////////////////////////////////
caseyquinn 12:5b4f3245606a 21 Adafruit_ADS1115 ads(&i2c);
scottkelleher 57:1695e252298d 22 //Adafruit_ADS1115 ads2(&i2c, 0x49);
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 57:1695e252298d 65 int logInerval = 5;//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);
jelord 3:122bfc998c4c 71
scottkelleher 57:1695e252298d 72 int samplingTime = 280;
scottkelleher 57:1695e252298d 73 int deltaTime = 40;
scottkelleher 57:1695e252298d 74 int sharp1, sharp2, sharp3, sharp4;
scottkelleher 57:1695e252298d 75 float sharpVolt1, sharpVolt2, sharpVolt3, sharpVolt4; //V
caseyquinn 12:5b4f3245606a 76
caseyquinn 11:aa21628a9b15 77
caseyquinn 11:aa21628a9b15 78
caseyquinn 5:c3252e5d45ca 79
caseyquinn 9:8646fd501832 80 //////////////////////////////////////////////////////////////
scottkelleher 57:1695e252298d 81 //SD Logging Function
caseyquinn 9:8646fd501832 82 //////////////////////////////////////////////////////////////
scottkelleher 57:1695e252298d 83 void log_data()
caseyquinn 52:95949e34b1f6 84 {
scottkelleher 57:1695e252298d 85 //Get RTC time(s)
scottkelleher 57:1695e252298d 86 ///////////////////////////
scottkelleher 57:1695e252298d 87 RTC_UPAS.get_time();
scottkelleher 57:1695e252298d 88 time_t seconds = time(NULL);
scottkelleher 57:1695e252298d 89 strftime(timestr, 32, "%y%m%d%H%M%S", localtime(&seconds));
scottkelleher 57:1695e252298d 90 /*
caseyquinn 43:05aa1fb68946 91 strftime(yrstr, 4, "%y", localtime(&seconds));
caseyquinn 43:05aa1fb68946 92 stYr = atoi(yrstr);
caseyquinn 43:05aa1fb68946 93
caseyquinn 43:05aa1fb68946 94 strftime(mostr, 4, "%m", localtime(&seconds));
caseyquinn 43:05aa1fb68946 95 stMo = atoi(mostr);
caseyquinn 43:05aa1fb68946 96
caseyquinn 43:05aa1fb68946 97 strftime(daystr, 4, "%d", localtime(&seconds));
caseyquinn 43:05aa1fb68946 98 stDay = atoi(daystr);
caseyquinn 43:05aa1fb68946 99
caseyquinn 43:05aa1fb68946 100 strftime(hrstr, 4, "%H", localtime(&seconds));
caseyquinn 43:05aa1fb68946 101 stHr = atoi(hrstr);
caseyquinn 43:05aa1fb68946 102
caseyquinn 43:05aa1fb68946 103 strftime(minstr, 4, "%M", localtime(&seconds));
caseyquinn 43:05aa1fb68946 104 stMin = atoi(minstr);
caseyquinn 43:05aa1fb68946 105
caseyquinn 43:05aa1fb68946 106 strftime(secstr, 4, "%S", localtime(&seconds));
caseyquinn 43:05aa1fb68946 107 stSec = atoi(secstr);
scottkelleher 57:1695e252298d 108 */
caseyquinn 44:075fa63d5df1 109 //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 110
caseyquinn 46:99d129bfdbbd 111
caseyquinn 28:42932d3b105d 112 //Get Sensor Data except GPS
caseyquinn 28:42932d3b105d 113 ////////////////////////////
caseyquinn 7:29b01d5812ee 114 press = bmesensor.getPressure();
caseyquinn 7:29b01d5812ee 115 temp = bmesensor.getTemperature()-5.0;
caseyquinn 7:29b01d5812ee 116 rh = bmesensor.getHumidity();
scottkelleher 57:1695e252298d 117 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 118
scottkelleher 57:1695e252298d 119 sharp1LED = 0;
scottkelleher 57:1695e252298d 120 sharp2LED = 0;
scottkelleher 57:1695e252298d 121 sharp3LED = 0;
scottkelleher 57:1695e252298d 122 sharp4LED = 0;
caseyquinn 35:e0bdd6389a75 123
scottkelleher 57:1695e252298d 124 wait_ms(samplingTime);
caseyquinn 14:7cdb643da356 125
scottkelleher 57:1695e252298d 126 sharp1 = ads.readADC_SingleEnded(0, 0xC383); // read channel 1
scottkelleher 57:1695e252298d 127 sharpVolt1 = (sharp1*4.096)/(32768*1);
scottkelleher 57:1695e252298d 128 sharp2 = ads.readADC_SingleEnded(1, 0xD383); // read channel 1
scottkelleher 57:1695e252298d 129 sharpVolt2 = (sharp2*4.096)/(32768*1);
scottkelleher 57:1695e252298d 130 sharp3 = ads.readADC_SingleEnded(2, 0xE383); // read channel 1
scottkelleher 57:1695e252298d 131 sharpVolt3 = (sharp3*4.096)/(32768*1);
scottkelleher 57:1695e252298d 132 sharp4 = ads.readADC_SingleEnded(3, 0xF383); // read channel 1
scottkelleher 57:1695e252298d 133 sharpVolt4 = (sharp4*4.096)/(32768*1);
caseyquinn 50:5f1b378e4f64 134
scottkelleher 57:1695e252298d 135 wait_ms(deltaTime);
caseyquinn 50:5f1b378e4f64 136
scottkelleher 57:1695e252298d 137 sharp1LED = 1;
scottkelleher 57:1695e252298d 138 sharp2LED = 1;
scottkelleher 57:1695e252298d 139 sharp3LED = 1;
scottkelleher 57:1695e252298d 140 sharp4LED = 1;
caseyquinn 14:7cdb643da356 141
caseyquinn 27:922f53fa649c 142
caseyquinn 21:bb10efc5bf57 143
caseyquinn 7:29b01d5812ee 144 FILE *fp = fopen(filename, "a");
caseyquinn 21:bb10efc5bf57 145 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);
caseyquinn 7:29b01d5812ee 146 fprintf(fp, "%s,", timestr);
scottkelleher 57:1695e252298d 147 fprintf(fp, "%2.2f,%4.2f,%2.1f,%1.3f,", temp,press,rh,atmoRho);
scottkelleher 57:1695e252298d 148 fprintf(fp, "%d,%f,%d,%f," ,sharp1,sharpVolt1, sharp2, sharpVolt2);
scottkelleher 57:1695e252298d 149 fprintf(fp, "%d,%f,%d,%f\r\n" ,sharp3,sharpVolt3, sharp4, sharpVolt4);
scottkelleher 57:1695e252298d 150 fclose(fp);
caseyquinn 7:29b01d5812ee 151 free(fp);
caseyquinn 25:fbf7d44e7da4 152
caseyquinn 25:fbf7d44e7da4 153
caseyquinn 14:7cdb643da356 154
caseyquinn 12:5b4f3245606a 155
caseyquinn 25:fbf7d44e7da4 156
caseyquinn 12:5b4f3245606a 157
jelord 3:122bfc998c4c 158 }
caseyquinn 8:c4a8f9b67cee 159
caseyquinn 25:fbf7d44e7da4 160
caseyquinn 9:8646fd501832 161 //////////////////////////////////////////////////////////////
caseyquinn 9:8646fd501832 162 //Main Function
caseyquinn 9:8646fd501832 163 //////////////////////////////////////////////////////////////
jelord 1:9fbb5b665068 164 int main(){
caseyquinn 27:922f53fa649c 165
caseyquinn 29:fd74725294d5 166
scottkelleher 57:1695e252298d 167
caseyquinn 10:06fbb1c9e3bd 168 pc.baud(115200); // set what you want here depending on your terminal program speed
caseyquinn 10:06fbb1c9e3bd 169 pc.printf("\f\n\r-------------Startup-------------\n\r");
jelord 1:9fbb5b665068 170 wait(0.5);
caseyquinn 5:c3252e5d45ca 171
caseyquinn 36:2e344db70d35 172
scottkelleher 57:1695e252298d 173 RTC_UPAS.set_time(0,0,0,1,1,1,16);//sets chronodot RTC
caseyquinn 29:fd74725294d5 174
scottkelleher 57:1695e252298d 175 ///////////////////////
scottkelleher 57:1695e252298d 176 //sets ST RTC
scottkelleher 57:1695e252298d 177 //////////////////////
scottkelleher 57:1695e252298d 178 STtime.tm_sec = 0; // 0-59
scottkelleher 57:1695e252298d 179 STtime.tm_min = 0; // 0-59
scottkelleher 57:1695e252298d 180 STtime.tm_hour = 0; // 0-23
scottkelleher 57:1695e252298d 181 STtime.tm_mday = 1; // 1-31
scottkelleher 57:1695e252298d 182 STtime.tm_mon = 0; // 0-11
scottkelleher 57:1695e252298d 183 STtime.tm_year = 116; // year since 1900 (116 = 2016)
scottkelleher 57:1695e252298d 184 time_t STseconds = mktime(&STtime);
scottkelleher 57:1695e252298d 185 set_time(STseconds); // Set RTC time
caseyquinn 29:fd74725294d5 186
scottkelleher 57:1695e252298d 187
caseyquinn 52:95949e34b1f6 188 RTC_UPAS.get_time();
caseyquinn 46:99d129bfdbbd 189
caseyquinn 52:95949e34b1f6 190
caseyquinn 43:05aa1fb68946 191 time_t seconds = time(NULL);
caseyquinn 43:05aa1fb68946 192 strftime(timestr, 32, "%y-%m-%d-%H=%M=%S", localtime(&seconds));
caseyquinn 52:95949e34b1f6 193
caseyquinn 52:95949e34b1f6 194 strftime(yrstr, 4, "%y", localtime(&seconds));
caseyquinn 52:95949e34b1f6 195 stYr = atoi(yrstr);
caseyquinn 52:95949e34b1f6 196
caseyquinn 52:95949e34b1f6 197 strftime(mostr, 4, "%m", localtime(&seconds));
caseyquinn 52:95949e34b1f6 198 stMo = atoi(mostr);
caseyquinn 52:95949e34b1f6 199
caseyquinn 52:95949e34b1f6 200 strftime(daystr, 4, "%d", localtime(&seconds));
caseyquinn 52:95949e34b1f6 201 stDay = atoi(daystr);
caseyquinn 52:95949e34b1f6 202
caseyquinn 52:95949e34b1f6 203 strftime(hrstr, 4, "%H", localtime(&seconds));
caseyquinn 52:95949e34b1f6 204 stHr = atoi(hrstr);
caseyquinn 52:95949e34b1f6 205
caseyquinn 52:95949e34b1f6 206 strftime(minstr, 4, "%M", localtime(&seconds));
caseyquinn 52:95949e34b1f6 207 stMin = atoi(minstr);
caseyquinn 52:95949e34b1f6 208
caseyquinn 52:95949e34b1f6 209 strftime(secstr, 4, "%S", localtime(&seconds));
caseyquinn 52:95949e34b1f6 210 stSec = atoi(secstr);
caseyquinn 52:95949e34b1f6 211
caseyquinn 52:95949e34b1f6 212
caseyquinn 52:95949e34b1f6 213
caseyquinn 52:95949e34b1f6 214
scottkelleher 57:1695e252298d 215 // sprintf(filename,"/sd/SHARP_LOG00.txt");
scottkelleher 57:1695e252298d 216
scottkelleher 57:1695e252298d 217 for (uint8_t i = 0; i < 100; i++) {
scottkelleher 57:1695e252298d 218 filename[13] = i/10 + '0';
scottkelleher 57:1695e252298d 219 filename[14] = i%10 + '0';
scottkelleher 57:1695e252298d 220 FILE *fp = fopen(filename, "r");
scottkelleher 57:1695e252298d 221 if (fp == NULL) {
scottkelleher 57:1695e252298d 222 // only open a new file if it doesn't exist
scottkelleher 57:1695e252298d 223 FILE *fp = fopen(filename, "w");
scottkelleher 57:1695e252298d 224 fclose(fp);
scottkelleher 57:1695e252298d 225 break; // leave the loop!
scottkelleher 57:1695e252298d 226 }
scottkelleher 57:1695e252298d 227 }
caseyquinn 36:2e344db70d35 228
caseyquinn 29:fd74725294d5 229
caseyquinn 14:7cdb643da356 230
caseyquinn 46:99d129bfdbbd 231 while(fmod((double)stSec,10)!=0) {
caseyquinn 46:99d129bfdbbd 232 //pc.printf("%f, %f\r\n", floor(secondsD), floor(lastsecondD));
caseyquinn 46:99d129bfdbbd 233 time_t seconds = time(NULL);
caseyquinn 46:99d129bfdbbd 234 strftime(secstr, 4, "%S", localtime(&seconds));
caseyquinn 46:99d129bfdbbd 235 stSec = atoi(secstr);
caseyquinn 46:99d129bfdbbd 236 wait_ms(100);
caseyquinn 46:99d129bfdbbd 237 }
caseyquinn 46:99d129bfdbbd 238
scottkelleher 57:1695e252298d 239 logg.attach(&log_data, 10);
scottkelleher 57:1695e252298d 240
caseyquinn 35:e0bdd6389a75 241
caseyquinn 35:e0bdd6389a75 242
caseyquinn 30:aa6324845a84 243
jelord 3:122bfc998c4c 244 //** end of initalization **//
jelord 3:122bfc998c4c 245 //---------------------------------------------------------------------------------------------//
jelord 3:122bfc998c4c 246 //---------------------------------------------------------------------------------------------//
jelord 3:122bfc998c4c 247 // Main Control Loop
jelord 3:122bfc998c4c 248
caseyquinn 7:29b01d5812ee 249 while (1) {
caseyquinn 7:29b01d5812ee 250 // Do other things...
caseyquinn 46:99d129bfdbbd 251
caseyquinn 46:99d129bfdbbd 252 }
caseyquinn 7:29b01d5812ee 253
caseyquinn 8:c4a8f9b67cee 254
caseyquinn 4:5d004fd997d5 255
jelord 3:122bfc998c4c 256
jelord 0:2cb2b2ea316f 257 }