Program to control UPAS with MicroChip BLE chip + iPhone App

Dependencies:   ADS1115 BME280 Calibration CronoDot EEPROM LSM303 MCP40D17 MicroBLE NCP5623BMUTBG SDFileSystem SI1145 STC3100 mbed

Committer:
jelord
Date:
Mon Feb 08 22:49:54 2016 +0000
Revision:
3:122bfc998c4c
Parent:
2:88fcbfadec6a
MicroBLE App mirrors same func. as UPAS_BLE_SD_iOS with this commit.  Callbacks do not work properly/consistently

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 "MCP40D17.h"
jelord 2:88fcbfadec6a 5 #include "STC3100.h"
jelord 2:88fcbfadec6a 6 #include "LSM303.h"
jelord 2:88fcbfadec6a 7 #include "BME280.h"
jelord 2:88fcbfadec6a 8 #include "SI1145.h"
jelord 2:88fcbfadec6a 9 #include "NCP5623BMUTBG.h"
jelord 2:88fcbfadec6a 10 #include "CronoDot.h"
jelord 2:88fcbfadec6a 11 #include "EEPROM.h"
jelord 2:88fcbfadec6a 12 #include "Calibration.h"
jelord 1:9fbb5b665068 13 Serial pc(USBTX, USBRX);
jelord 1:9fbb5b665068 14 Serial microChannel(D8, D2); // tx, rx
jelord 1:9fbb5b665068 15 Timer t;
jelord 1:9fbb5b665068 16 struct tm tt;
jelord 0:2cb2b2ea316f 17
jelord 2:88fcbfadec6a 18 I2C i2c(D14, D15);
jelord 2:88fcbfadec6a 19 Adafruit_ADS1115 ads(&i2c);
jelord 2:88fcbfadec6a 20 MCP40D17 DigPot(&i2c);
jelord 2:88fcbfadec6a 21 BME280 bmesensor(D14, D15);
jelord 2:88fcbfadec6a 22 STC3100 gasG(D14, D15);
jelord 2:88fcbfadec6a 23 DigitalOut blower(D9, 0);
jelord 2:88fcbfadec6a 24 DigitalOut pbKill(D7, 1);
jelord 2:88fcbfadec6a 25 LSM303 movementsensor(D14, D15);
jelord 2:88fcbfadec6a 26 SI1145 lightsensor(D14, D15);
jelord 2:88fcbfadec6a 27 NCP5623BMUTBG RGB_LED(D14, D15);
jelord 3:122bfc998c4c 28 CronoDot RTC_UPAS(D14, D15);
jelord 2:88fcbfadec6a 29 EEPROM E2PROM(D14, D15);
jelord 2:88fcbfadec6a 30 //DigitalOut GPS_EN(p4,0); //pin 4 is used to enable and disable the GPS, in order to recive serial communications
jelord 2:88fcbfadec6a 31 Calibration calibrations(1); //Default serial/calibration if there are no values for the selected option
jelord 0:2cb2b2ea316f 32
jelord 3:122bfc998c4c 33 Timeout stop; //This is the stop call back object
jelord 3:122bfc998c4c 34 Timeout logg; //This is the logging call back object
jelord 3:122bfc998c4c 35
jelord 3:122bfc998c4c 36 uint16_t serial_num = 1; // Default serial/calibration number
jelord 3:122bfc998c4c 37 int RunReady =0;
jelord 3:122bfc998c4c 38
jelord 3:122bfc998c4c 39
jelord 3:122bfc998c4c 40 float press = 1.1;
jelord 3:122bfc998c4c 41 float temp = 75.5;
jelord 3:122bfc998c4c 42 float rh = 12.1;
jelord 3:122bfc998c4c 43
jelord 3:122bfc998c4c 44 int uv = 8;
jelord 3:122bfc998c4c 45 int vis = 7;
jelord 3:122bfc998c4c 46 int ir = 6;
jelord 3:122bfc998c4c 47
jelord 3:122bfc998c4c 48 float compass = 0.1;
jelord 3:122bfc998c4c 49 float accel_x = 0.2;
jelord 3:122bfc998c4c 50 float accel_y = 0.3;
jelord 3:122bfc998c4c 51 float accel_z = 0.4;
jelord 3:122bfc998c4c 52 float accel_comp = 0.5;
jelord 3:122bfc998c4c 53 float angle_x = 0.6;
jelord 3:122bfc998c4c 54 float angle_y = 0.7;
jelord 3:122bfc998c4c 55 float angle_z = 0.8;
jelord 3:122bfc998c4c 56 float mag_x = 0.9;
jelord 3:122bfc998c4c 57 float mag_y = 1.0;
jelord 3:122bfc998c4c 58 float mag_z = 1.2;
jelord 3:122bfc998c4c 59
jelord 3:122bfc998c4c 60 int vInReading = 18;
jelord 3:122bfc998c4c 61 int vBlowerReading = 19;
jelord 3:122bfc998c4c 62 int omronDiff = 20;
jelord 3:122bfc998c4c 63 float omronVolt = 1.2; //V
jelord 3:122bfc998c4c 64 int omronReading = 100;
jelord 3:122bfc998c4c 65 float atmoRho = 1.5; //g/L
jelord 3:122bfc998c4c 66
jelord 3:122bfc998c4c 67 float massflow = 87.6; //g/min
jelord 3:122bfc998c4c 68 float volflow = 88.8; //L/min
jelord 3:122bfc998c4c 69 float volflowSet = 1.0; //L/min
jelord 3:122bfc998c4c 70 int logInerval = 10; //seconds
jelord 3:122bfc998c4c 71 double secondsD = 0;
jelord 3:122bfc998c4c 72 double lastsecondD = 0;
jelord 3:122bfc998c4c 73 float massflowSet = 19.2;
jelord 3:122bfc998c4c 74 float deltaVflow = 0.0;
jelord 3:122bfc998c4c 75 float deltaMflow = 0.0;
jelord 3:122bfc998c4c 76 float gainFlow = 98.1;
jelord 3:122bfc998c4c 77 float sampledVol = 2.0; //L, total sampled volume
jelord 3:122bfc998c4c 78
jelord 3:122bfc998c4c 79 int digital_pot_setpoint = 5 ; //min = 0x7F, max = 0x00
jelord 3:122bfc998c4c 80 int digital_pot_set = 6;
jelord 3:122bfc998c4c 81 int digital_pot_change = 7;
jelord 3:122bfc998c4c 82 int digitalpotMax = 127;
jelord 3:122bfc998c4c 83 int digitalpotMin = 2;
jelord 3:122bfc998c4c 84
jelord 3:122bfc998c4c 85 int dutyUp = 4;
jelord 3:122bfc998c4c 86 int dutyDown = 3;
jelord 3:122bfc998c4c 87
jelord 3:122bfc998c4c 88 // variables are only place holders for the US_Menu //
jelord 3:122bfc998c4c 89 int refreshtime = 6;
jelord 3:122bfc998c4c 90 float home_lat, home_lon, work_lat, work_lon;
jelord 3:122bfc998c4c 91 //*************************************************//
jelord 3:122bfc998c4c 92
jelord 3:122bfc998c4c 93 //int refresh_Time = 10; // refresh time in s, note calling read_GPS()(or similar) will still take how ever long it needs(hopefully < 1s)
jelord 3:122bfc998c4c 94
jelord 3:122bfc998c4c 95 char filename[] = "/sd/XXXX0000LOG000000000000---------------.txt";
jelord 3:122bfc998c4c 96
jelord 3:122bfc998c4c 97 SDFileSystem sd(D11, D12, D13, D10, "sd"); // I believe this matches Todd's pinout, let me know if this doesn't work. (p12, p13, p15, p14)
jelord 3:122bfc998c4c 98
jelord 2:88fcbfadec6a 99 void sendData();
jelord 2:88fcbfadec6a 100
jelord 3:122bfc998c4c 101 //int timeout = 2;
jelord 2:88fcbfadec6a 102
jelord 3:122bfc998c4c 103 //void pc_recv(void){
jelord 3:122bfc998c4c 104 // while(pc.readable()){
jelord 3:122bfc998c4c 105 // pc.getc();
jelord 3:122bfc998c4c 106 // }
jelord 3:122bfc998c4c 107 //}
jelord 0:2cb2b2ea316f 108
jelord 0:2cb2b2ea316f 109 static uint8_t rx_buf[20];
jelord 0:2cb2b2ea316f 110 static uint8_t rx_len=0;
jelord 1:9fbb5b665068 111 static int haltBLE = 1;
jelord 1:9fbb5b665068 112 static int transmissionValue = 0;
jelord 2:88fcbfadec6a 113 uint8_t writeData[20] = {0,};
jelord 2:88fcbfadec6a 114 static uint8_t dataLength = 0;
jelord 3:122bfc998c4c 115 static int runReady = 0;
jelord 3:122bfc998c4c 116 static uint8_t startAndEndTime[12] = {0,};
jelord 0:2cb2b2ea316f 117
jelord 0:2cb2b2ea316f 118 void uartMicro(void){
jelord 3:122bfc998c4c 119 if(runReady!=1){
jelord 3:122bfc998c4c 120 haltBLE = 2;
jelord 3:122bfc998c4c 121 while(microChannel.readable()){
jelord 3:122bfc998c4c 122 rx_buf[rx_len++] = microChannel.getc();
jelord 2:88fcbfadec6a 123
jelord 3:122bfc998c4c 124 //Code block to verify what is being transmitted. To function correctly, all data must terminate with \0 or \n
jelord 3:122bfc998c4c 125 if(transmissionValue==0){
jelord 3:122bfc998c4c 126
jelord 3:122bfc998c4c 127 if (rx_buf[0] == 0x01)transmissionValue = 1; //rtc
jelord 3:122bfc998c4c 128 else if(rx_buf[0] == 0x02)transmissionValue = 2; //sample start and end times
jelord 3:122bfc998c4c 129 else if(rx_buf[0] == 0x03)transmissionValue = 3; //sample name
jelord 3:122bfc998c4c 130 else if(rx_buf[0] == 0x04)transmissionValue = 4; //Send Data Check
jelord 3:122bfc998c4c 131
jelord 3:122bfc998c4c 132 else if(rx_buf[0] == 0x05)transmissionValue = 5; //log interval
jelord 3:122bfc998c4c 133 else if(rx_buf[0] == 0x06)transmissionValue = 6; //Flow Rate
jelord 3:122bfc998c4c 134 else if(rx_buf[0] == 0x07)transmissionValue = 7; //Serial Number
jelord 3:122bfc998c4c 135 else if(rx_buf[0] == 0x08)transmissionValue = 8; //Run Enable
jelord 3:122bfc998c4c 136 else transmissionValue = 100; //Not useful data
jelord 3:122bfc998c4c 137 }
jelord 3:122bfc998c4c 138
jelord 3:122bfc998c4c 139 if(rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n' || rx_buf[rx_len-1] == 0xff){
jelord 3:122bfc998c4c 140 if((transmissionValue == 1 || transmissionValue == 2 || transmissionValue == 3 || transmissionValue == 4 || transmissionValue == 5 ||
jelord 3:122bfc998c4c 141 transmissionValue == 6 || transmissionValue == 7) && rx_buf[rx_len-1] != 0xff)
jelord 3:122bfc998c4c 142 {}else{
jelord 3:122bfc998c4c 143 if(transmissionValue == 4 ) sendData();
jelord 3:122bfc998c4c 144 if(transmissionValue == 8){
jelord 3:122bfc998c4c 145 runReady = 1;
jelord 3:122bfc998c4c 146 microChannel.attach(NULL,microChannel.RxIrq);
jelord 3:122bfc998c4c 147 }
jelord 3:122bfc998c4c 148 haltBLE = 1;
jelord 3:122bfc998c4c 149 transmissionValue = 0;
jelord 3:122bfc998c4c 150 dataLength = 0;
jelord 2:88fcbfadec6a 151
jelord 3:122bfc998c4c 152 }
jelord 2:88fcbfadec6a 153 }
jelord 1:9fbb5b665068 154 }
jelord 3:122bfc998c4c 155 if(haltBLE!=1){
jelord 2:88fcbfadec6a 156
jelord 3:122bfc998c4c 157 if((transmissionValue!=100) && (dataLength!= 0)) writeData[dataLength-1] = rx_buf[0];
jelord 3:122bfc998c4c 158
jelord 3:122bfc998c4c 159 if(transmissionValue ==100){
jelord 3:122bfc998c4c 160 //pc.putc(rx_buf[0]);
jelord 2:88fcbfadec6a 161
jelord 3:122bfc998c4c 162 }else if(transmissionValue ==1){ //process and store RTC values
jelord 3:122bfc998c4c 163
jelord 3:122bfc998c4c 164 if(dataLength==6)RTC_UPAS.set_time(writeData[0],writeData[1],writeData[2],writeData[3],writeData[3],writeData[4],writeData[5]);
jelord 3:122bfc998c4c 165
jelord 3:122bfc998c4c 166 }else if(transmissionValue ==2){ //process and store sample start/end
jelord 3:122bfc998c4c 167 if(dataLength ==12)E2PROM.write(0x00015, writeData, 12);
jelord 3:122bfc998c4c 168
jelord 3:122bfc998c4c 169 }else if(transmissionValue ==3){ //process and store sample name
jelord 3:122bfc998c4c 170 if(dataLength ==8)E2PROM.write(0x00001,writeData,8);
jelord 3:122bfc998c4c 171
jelord 3:122bfc998c4c 172 }else if(transmissionValue ==5){ //process and store Log Interval
jelord 3:122bfc998c4c 173 if(dataLength ==1)E2PROM.write(0x00014,writeData,1);
jelord 2:88fcbfadec6a 174
jelord 3:122bfc998c4c 175 }else if(transmissionValue ==6){ //process and store Flow Rate
jelord 3:122bfc998c4c 176 if(dataLength ==4)E2PROM.write(0x00010,writeData,4);
jelord 3:122bfc998c4c 177
jelord 3:122bfc998c4c 178 }else if(transmissionValue ==7){ //process and store Serial Number
jelord 3:122bfc998c4c 179 if(dataLength ==2)E2PROM.write(0x00034,writeData,2);
jelord 3:122bfc998c4c 180 }
jelord 3:122bfc998c4c 181 dataLength++;
jelord 1:9fbb5b665068 182 }
jelord 3:122bfc998c4c 183
jelord 3:122bfc998c4c 184 rx_len = 0;
jelord 3:122bfc998c4c 185 }else{
jelord 3:122bfc998c4c 186 while(microChannel.readable())
jelord 3:122bfc998c4c 187 uint8_t extract = microChannel.getc();
jelord 3:122bfc998c4c 188 }
jelord 2:88fcbfadec6a 189
jelord 2:88fcbfadec6a 190 }
jelord 2:88fcbfadec6a 191 void sendData(){
jelord 2:88fcbfadec6a 192
jelord 2:88fcbfadec6a 193 uint8_t sampleTimePassValues[13] = {0x01,};
jelord 2:88fcbfadec6a 194 uint8_t subjectLabelOriginal[9] = {0x02,};
jelord 2:88fcbfadec6a 195 uint8_t dataLogOriginal[2] = {0x03,};
jelord 2:88fcbfadec6a 196 uint8_t flowRateOriginal[5] = {0x04,};
jelord 2:88fcbfadec6a 197 //uint8_t presetRunModeCheck[1] = {0,}; Commented and currently unused to prevent mem issues
jelord 2:88fcbfadec6a 198 E2PROM.read(0x00015, sampleTimePassValues+1, 12);
jelord 2:88fcbfadec6a 199 E2PROM.read(0x00001, subjectLabelOriginal+1,8);
jelord 2:88fcbfadec6a 200 E2PROM.read(0x00014,dataLogOriginal+1,1);
jelord 2:88fcbfadec6a 201 E2PROM.read(0x00010,flowRateOriginal+1,4);
jelord 2:88fcbfadec6a 202
jelord 2:88fcbfadec6a 203 for(int i=0; i<13; i++){
jelord 2:88fcbfadec6a 204 microChannel.putc(sampleTimePassValues[i]);
jelord 2:88fcbfadec6a 205 }
jelord 2:88fcbfadec6a 206 wait(.25);
jelord 2:88fcbfadec6a 207
jelord 2:88fcbfadec6a 208 for(int i=0; i<9; i++){
jelord 2:88fcbfadec6a 209 microChannel.putc(subjectLabelOriginal[i]);
jelord 2:88fcbfadec6a 210 }
jelord 2:88fcbfadec6a 211 wait(.25);
jelord 2:88fcbfadec6a 212
jelord 2:88fcbfadec6a 213 for(int i=0; i<2; i++){
jelord 2:88fcbfadec6a 214 microChannel.putc(dataLogOriginal[i]);
jelord 2:88fcbfadec6a 215 }
jelord 2:88fcbfadec6a 216 wait(.25);
jelord 2:88fcbfadec6a 217
jelord 2:88fcbfadec6a 218 for(int i=0; i<5; i++){
jelord 2:88fcbfadec6a 219 microChannel.putc(flowRateOriginal[i]);
jelord 2:88fcbfadec6a 220 }
jelord 0:2cb2b2ea316f 221
jelord 2:88fcbfadec6a 222
jelord 2:88fcbfadec6a 223 }
jelord 3:122bfc998c4c 224
jelord 3:122bfc998c4c 225 void check_stop() // this checks if it's time to stop and shutdown
jelord 3:122bfc998c4c 226 {
jelord 3:122bfc998c4c 227
jelord 3:122bfc998c4c 228 if(RTC_UPAS.compare(startAndEndTime[6], startAndEndTime[7], startAndEndTime[8], startAndEndTime[9], startAndEndTime[10], startAndEndTime[11])) {
jelord 3:122bfc998c4c 229 pbKill = 0; // this is were we shut everything down
jelord 3:122bfc998c4c 230 pc.printf("If you're reading this something has gone very wrong.");
jelord 3:122bfc998c4c 231 }
jelord 3:122bfc998c4c 232 stop.detach();
jelord 3:122bfc998c4c 233 stop.attach(&check_stop, 9);
jelord 3:122bfc998c4c 234
jelord 3:122bfc998c4c 235 }
jelord 3:122bfc998c4c 236 int r = 1;
jelord 3:122bfc998c4c 237 int g = 0;
jelord 3:122bfc998c4c 238 int b = 1;
jelord 3:122bfc998c4c 239
jelord 3:122bfc998c4c 240 void log_data()
jelord 3:122bfc998c4c 241 {
jelord 3:122bfc998c4c 242
jelord 3:122bfc998c4c 243
jelord 3:122bfc998c4c 244 RGB_LED.set_led(r,g,b);
jelord 3:122bfc998c4c 245 logg.detach();
jelord 3:122bfc998c4c 246 logg.attach(&log_data, logInerval); // reading and logging data must take significintly less than 0.5s. This can be increased.
jelord 3:122bfc998c4c 247
jelord 3:122bfc998c4c 248 RTC_UPAS.get_time();
jelord 3:122bfc998c4c 249
jelord 3:122bfc998c4c 250 secondsD = RTC_UPAS.seconds;
jelord 3:122bfc998c4c 251 lastsecondD = secondsD;
jelord 3:122bfc998c4c 252
jelord 3:122bfc998c4c 253 omronVolt = (omronReading*4.096)/(32768*2);
jelord 3:122bfc998c4c 254
jelord 3:122bfc998c4c 255 FILE *fp = fopen(filename, "a");
jelord 3:122bfc998c4c 256 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);
jelord 3:122bfc998c4c 257 fprintf(fp, "%1.3f,%1.3f,%2.2f,%4.2f,%2.1f,%1.3f,", omronVolt,massflow,temp,press,rh,atmoRho);
jelord 3:122bfc998c4c 258 fprintf(fp, "%1.3f,%5.1f,%1.1f,%1.1f,%1.1f,%1.1f,", volflow, sampledVol, accel_x, accel_y, accel_z, accel_comp);
jelord 3:122bfc998c4c 259 fprintf(fp, "%.1f,%.1f,%.1f,%.3f,%.3f,%.3f,%.1f,", angle_x,angle_y,angle_z,mag_x, mag_y, mag_z,compass);
jelord 3:122bfc998c4c 260 fprintf(fp, "%d,%d,%d,%d,%d,%d," ,uv,omronReading, vInReading, vBlowerReading, omronDiff,gasG.getAmps());
jelord 3:122bfc998c4c 261 fprintf(fp, "%d,%d,%d,%1.3f,%1.3f\r\n", gasG.getVolts(), gasG.getCharge(),digital_pot_set, deltaMflow, deltaVflow);
jelord 3:122bfc998c4c 262 fclose(fp);
jelord 3:122bfc998c4c 263 //wait_ms(5);
jelord 3:122bfc998c4c 264
jelord 3:122bfc998c4c 265 }
jelord 1:9fbb5b665068 266
jelord 1:9fbb5b665068 267 int main(){
jelord 2:88fcbfadec6a 268
jelord 1:9fbb5b665068 269 pc.baud(115200); // set what you want here depending on your terminal program speed
jelord 1:9fbb5b665068 270 pc.printf("\f\n\r-------------Startup-------------\n\r");
jelord 1:9fbb5b665068 271 wait(0.5);
jelord 3:122bfc998c4c 272 //timeout=2;
jelord 3:122bfc998c4c 273 uint8_t serialNumberAndType[6] = {0x50,0x53};
jelord 3:122bfc998c4c 274 E2PROM.read(0x00034,serialNumberAndType+2,2);
jelord 3:122bfc998c4c 275 int tempSerialNum = serialNumberAndType[2]+serialNumberAndType[3];
jelord 3:122bfc998c4c 276 int serialNumDigits[4];
jelord 3:122bfc998c4c 277 serialNumDigits[0] = tempSerialNum / 1000 % 10;
jelord 3:122bfc998c4c 278 serialNumDigits[1] = tempSerialNum / 100 % 10;
jelord 3:122bfc998c4c 279 serialNumDigits[2] = tempSerialNum / 10 % 10;
jelord 3:122bfc998c4c 280 serialNumDigits[3] = tempSerialNum % 10;
jelord 3:122bfc998c4c 281
jelord 3:122bfc998c4c 282 serialNumberAndType[2] = serialNumDigits[0]+48;
jelord 3:122bfc998c4c 283 serialNumberAndType[3] = serialNumDigits[1]+48;
jelord 3:122bfc998c4c 284 serialNumberAndType[4] = serialNumDigits[2]+48;
jelord 3:122bfc998c4c 285 serialNumberAndType[5] = serialNumDigits[3]+48;
jelord 2:88fcbfadec6a 286
jelord 3:122bfc998c4c 287 //pc.attach(pc_recv);
jelord 1:9fbb5b665068 288 microChannel.attach(uartMicro,microChannel.RxIrq);
jelord 3:122bfc998c4c 289 microChannel.baud(115200);
jelord 0:2cb2b2ea316f 290
jelord 1:9fbb5b665068 291 microChannel.printf("$$$");
jelord 3:122bfc998c4c 292 wait(0.5);
jelord 3:122bfc998c4c 293 microChannel.printf("SN,");
jelord 3:122bfc998c4c 294 for(int i=0;i<6;i++)microChannel.putc(serialNumberAndType[i]);
jelord 3:122bfc998c4c 295 microChannel.printf("\r");
jelord 3:122bfc998c4c 296 wait(0.5);
jelord 1:9fbb5b665068 297 microChannel.printf("A\r");
jelord 3:122bfc998c4c 298 wait(0.5);
jelord 2:88fcbfadec6a 299 microChannel.printf("---\r");
jelord 3:122bfc998c4c 300 wait(0.5);
jelord 2:88fcbfadec6a 301
jelord 2:88fcbfadec6a 302
jelord 2:88fcbfadec6a 303
jelord 2:88fcbfadec6a 304
jelord 2:88fcbfadec6a 305 RGB_LED.set_led(1,1,1);
jelord 3:122bfc998c4c 306 while(runReady!=1) {
jelord 3:122bfc998c4c 307 wait(1);
jelord 3:122bfc998c4c 308 //pc.printf("Waiting for BLE instruction");
jelord 3:122bfc998c4c 309
jelord 3:122bfc998c4c 310 }
jelord 3:122bfc998c4c 311
jelord 3:122bfc998c4c 312
jelord 3:122bfc998c4c 313 //wait(1);
jelord 3:122bfc998c4c 314
jelord 3:122bfc998c4c 315 E2PROM.read(0x00015, startAndEndTime, 12); //Grab start and end times from EEPROM
jelord 3:122bfc998c4c 316 RGB_LED.set_led(0,1,0);
jelord 3:122bfc998c4c 317 while(!RTC_UPAS.compare(startAndEndTime[0], startAndEndTime[1], startAndEndTime[2], startAndEndTime[3], startAndEndTime[4], startAndEndTime[5])) { // this while waits for the start time by looping until the start time
jelord 3:122bfc998c4c 318 wait(0.5);
jelord 3:122bfc998c4c 319
jelord 3:122bfc998c4c 320 RTC_UPAS.get_time();
jelord 3:122bfc998c4c 321
jelord 3:122bfc998c4c 322 }
jelord 3:122bfc998c4c 323
jelord 3:122bfc998c4c 324
jelord 3:122bfc998c4c 325 //Get the proper serial number
jelord 3:122bfc998c4c 326 uint8_t serialBytes[2] = {0,};
jelord 3:122bfc998c4c 327 E2PROM.read(0x00034, serialBytes,2);
jelord 3:122bfc998c4c 328 serial_num = ((uint16_t)serialBytes[1] << 8) | serialBytes[0];
jelord 3:122bfc998c4c 329 calibrations.initialize(serial_num);
jelord 3:122bfc998c4c 330
jelord 3:122bfc998c4c 331 uint8_t logByte[1] = {0,};
jelord 3:122bfc998c4c 332 E2PROM.read(0x00014,logByte,1);
jelord 3:122bfc998c4c 333 logInerval = logByte[0];
jelord 3:122bfc998c4c 334
jelord 3:122bfc998c4c 335 stop.attach(&check_stop, 30); // check if we should shut down every 9 seconds, starting 60s after the start.
jelord 3:122bfc998c4c 336
jelord 3:122bfc998c4c 337 //Use the flow rate value stored in eeprom
jelord 3:122bfc998c4c 338 uint8_t flowRateBytes[4] = {0,};
jelord 3:122bfc998c4c 339 E2PROM.read(0x00010,flowRateBytes,4);
jelord 3:122bfc998c4c 340 E2PROM.byteToFloat(flowRateBytes, &volflowSet);
jelord 3:122bfc998c4c 341
jelord 3:122bfc998c4c 342 if(volflowSet<=1.0) {
jelord 3:122bfc998c4c 343 gainFlow = 100;
jelord 3:122bfc998c4c 344 } else if(volflowSet>=2.0) {
jelord 3:122bfc998c4c 345 gainFlow = 25;
jelord 3:122bfc998c4c 346 } else {
jelord 3:122bfc998c4c 347 gainFlow = 25;
jelord 0:2cb2b2ea316f 348 }
jelord 3:122bfc998c4c 349
jelord 3:122bfc998c4c 350 RGB_LED.set_led(1,0,0);
jelord 3:122bfc998c4c 351 press = bmesensor.getPressure();
jelord 3:122bfc998c4c 352 temp = bmesensor.getTemperature();
jelord 3:122bfc998c4c 353 rh = bmesensor.getHumidity();
jelord 3:122bfc998c4c 354
jelord 3:122bfc998c4c 355 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));
jelord 3:122bfc998c4c 356 massflowSet = volflowSet*atmoRho;
jelord 3:122bfc998c4c 357 //Digtal pot tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx
jelord 3:122bfc998c4c 358
jelord 3:122bfc998c4c 359
jelord 3:122bfc998c4c 360
jelord 3:122bfc998c4c 361
jelord 3:122bfc998c4c 362 DigPot.writeRegister(digital_pot_setpoint);
jelord 3:122bfc998c4c 363 wait(1);
jelord 3:122bfc998c4c 364 blower = 1;
jelord 3:122bfc998c4c 365
jelord 3:122bfc998c4c 366 uint8_t subjectLabelOriginal[8] = {0,};
jelord 3:122bfc998c4c 367 E2PROM.read(0x00001, subjectLabelOriginal,8);
jelord 3:122bfc998c4c 368 sprintf(filename, "/sd/UPAS%04dLOG_%02d-%02d-%02d_%02d=%02d=%02d_%c%c%c%c%c%c%c%c.txt",serial_num,RTC_UPAS.year,RTC_UPAS.month,RTC_UPAS.date,RTC_UPAS.hour,RTC_UPAS.minutes,RTC_UPAS.seconds,subjectLabelOriginal[0],subjectLabelOriginal[1],subjectLabelOriginal[2],subjectLabelOriginal[3],subjectLabelOriginal[4],subjectLabelOriginal[5],subjectLabelOriginal[6],subjectLabelOriginal[7]);
jelord 3:122bfc998c4c 369 FILE *fp = fopen(filename, "w");
jelord 3:122bfc998c4c 370 fclose(fp);
jelord 3:122bfc998c4c 371
jelord 3:122bfc998c4c 372 //---------------------------------------------------------------------------------------------//
jelord 3:122bfc998c4c 373 //Following lines are needed to enter into the initiallization flow control loop
jelord 3:122bfc998c4c 374
jelord 3:122bfc998c4c 375 wait(5);
jelord 3:122bfc998c4c 376
jelord 3:122bfc998c4c 377
jelord 3:122bfc998c4c 378 sampledVol = 0.0;
jelord 3:122bfc998c4c 379 RGB_LED.set_led(0,1,0);
jelord 3:122bfc998c4c 380
jelord 3:122bfc998c4c 381
jelord 3:122bfc998c4c 382
jelord 3:122bfc998c4c 383 //** end of initalization **//
jelord 3:122bfc998c4c 384 //---------------------------------------------------------------------------------------------//
jelord 3:122bfc998c4c 385 //---------------------------------------------------------------------------------------------//
jelord 3:122bfc998c4c 386 // Main Control Loop
jelord 3:122bfc998c4c 387
jelord 3:122bfc998c4c 388
jelord 3:122bfc998c4c 389 //logg.attach(&log_data, 30); // uses callbacks or block Interrupts for anything that uses i2c
jelord 3:122bfc998c4c 390 while(!RTC_UPAS.compare(startAndEndTime[6], startAndEndTime[7], startAndEndTime[8], startAndEndTime[9], startAndEndTime[10], startAndEndTime[11])){
jelord 3:122bfc998c4c 391 wait(logInerval);
jelord 3:122bfc998c4c 392 RGB_LED.set_led(1,1,0);
jelord 3:122bfc998c4c 393 FILE *fp = fopen(filename, "a");
jelord 3:122bfc998c4c 394 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);
jelord 3:122bfc998c4c 395 fprintf(fp, "%1.3f,%1.3f,%2.2f,%4.2f,%2.1f,%1.3f,", omronVolt,massflow,temp,press,rh,atmoRho);
jelord 3:122bfc998c4c 396 fprintf(fp, "%1.3f,%5.1f,%1.1f,%1.1f,%1.1f,%1.1f,", volflow, sampledVol, accel_x, accel_y, accel_z, accel_comp);
jelord 3:122bfc998c4c 397 fprintf(fp, "%.1f,%.1f,%.1f,%.3f,%.3f,%.3f,%.1f,", angle_x,angle_y,angle_z,mag_x, mag_y, mag_z,compass);
jelord 3:122bfc998c4c 398 fprintf(fp, "%d,%d,%d,%d,%d,%d," ,uv,omronReading, vInReading, vBlowerReading, omronDiff,gasG.getAmps());
jelord 3:122bfc998c4c 399 fprintf(fp, "%d,%d,%d,%1.3f,%1.3f\r\n", gasG.getVolts(), gasG.getCharge(),digital_pot_set, deltaMflow, deltaVflow);
jelord 3:122bfc998c4c 400 fclose(fp);
jelord 3:122bfc998c4c 401 }
jelord 3:122bfc998c4c 402 RGB_LED.set_led(1,0,0);
jelord 3:122bfc998c4c 403
jelord 0:2cb2b2ea316f 404 }