Code supports writing to the SD card as well as working with the Volckens group smartphone apps for the mbed HRM1017

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

Fork of UPAS_BLE_and_USB by Volckens Group Sensors

Committer:
caseyquinn
Date:
Sun May 31 15:28:20 2015 +0000
Revision:
31:f8504ef165fb
Parent:
30:8a33a94a1a89
Child:
33:bc73a2493821
File name formatting modification

Who changed what in which revision?

UserRevisionLine numberNew contents of line
caseyquinn 0:14d46ef4b6cb 1 #include "mbed.h"
caseyquinn 0:14d46ef4b6cb 2 #include "SDFileSystem.h"
caseyquinn 0:14d46ef4b6cb 3 #include "Adafruit_ADS1015.h"
caseyquinn 1:37babeb68ab9 4 #include "MCP40D17.h"
caseyquinn 10:f9cb61b29340 5 #include "wire.h" //Used with the RTC in this code. Will need to try and remove this, and also create the correct library/update addresses and registers for the RTC.
caseyquinn 7:a24d7156bc02 6 #include "STC3100.h"
caseyquinn 10:f9cb61b29340 7 #include "LSM303.h"
caseyquinn 12:8c00a7f5d483 8 #include "BME280.h"
caseyquinn 14:ad550174db8b 9 #include "SI1145.h"
caseyquinn 1:37babeb68ab9 10
caseyquinn 1:37babeb68ab9 11 #define SERIAL_BAUD_RATE 9600
caseyquinn 7:a24d7156bc02 12 #define SCL 20
caseyquinn 7:a24d7156bc02 13 #define SDA 22
caseyquinn 7:a24d7156bc02 14 #define Crono 0xD0 //D0 for the chronoDot
caseyquinn 0:14d46ef4b6cb 15
caseyquinn 0:14d46ef4b6cb 16 I2C i2c(p22, p20);
caseyquinn 0:14d46ef4b6cb 17 Adafruit_ADS1115 ads(&i2c);
caseyquinn 1:37babeb68ab9 18 MCP40D17 DigPot(&i2c);
caseyquinn 12:8c00a7f5d483 19 BME280 bmesensor(p22, p20);
caseyquinn 7:a24d7156bc02 20 STC3100 gasG(p22, p20);
caseyquinn 2:e596e685eb39 21 Serial pc(USBTX, USBRX);
caseyquinn 8:204c21adf693 22 DigitalOut blower(p29, 0);
caseyquinn 17:1baf7cab694e 23 DigitalOut pbKill(p18, 1);
caseyquinn 10:f9cb61b29340 24 LSM303 movementsensor(p22, p20);
caseyquinn 14:ad550174db8b 25 SI1145 lightsensor(p22, p20);
caseyquinn 0:14d46ef4b6cb 26
caseyquinn 20:ad9883973d86 27 float press;
caseyquinn 20:ad9883973d86 28 float temp;
caseyquinn 20:ad9883973d86 29 float rh;
caseyquinn 4:69bd7e8a994c 30
caseyquinn 20:ad9883973d86 31 int uv;
caseyquinn 20:ad9883973d86 32 int vis;
caseyquinn 20:ad9883973d86 33 int ir;
caseyquinn 8:204c21adf693 34
caseyquinn 10:f9cb61b29340 35 float accel_x;
caseyquinn 10:f9cb61b29340 36 float accel_y;
caseyquinn 10:f9cb61b29340 37 float accel_z;
caseyquinn 10:f9cb61b29340 38 float mag_x;
caseyquinn 10:f9cb61b29340 39 float mag_y;
caseyquinn 10:f9cb61b29340 40 float mag_z;
caseyquinn 10:f9cb61b29340 41
caseyquinn 20:ad9883973d86 42
caseyquinn 0:14d46ef4b6cb 43 int vInReading;
caseyquinn 0:14d46ef4b6cb 44 int vBlowerReading;
caseyquinn 0:14d46ef4b6cb 45 int omronDiff;
caseyquinn 20:ad9883973d86 46
caseyquinn 20:ad9883973d86 47 int omronReading;
caseyquinn 20:ad9883973d86 48 float omronVolt; //V
caseyquinn 20:ad9883973d86 49 float atmoRho; //g/L
caseyquinn 20:ad9883973d86 50 float massflow; //g/min
caseyquinn 20:ad9883973d86 51 float volflow; //L/min
caseyquinn 20:ad9883973d86 52 float volflowSet = 1.0; //L/min
caseyquinn 22:8dd7e0d4c21c 53 float massflowSet;
caseyquinn 20:ad9883973d86 54 float deltaflow;
caseyquinn 20:ad9883973d86 55
caseyquinn 22:8dd7e0d4c21c 56 int digital_pot_setpoint; //min = 0x7F, max = 0x00
caseyquinn 31:f8504ef165fb 57 char filename[] = "/sd/UPAS0004LOG_00-00-00_00:00:00.txt";
caseyquinn 7:a24d7156bc02 58
caseyquinn 7:a24d7156bc02 59 TwoWire Wire = TwoWire(NRF_TWI0);
caseyquinn 7:a24d7156bc02 60 SDFileSystem sd(SPIS_PSELMOSI, SPIS_PSELMISO, SPIS_PSELSCK, SPIS_PSELSS, "sd"); // I believe this matches Todd's pinout, let me know if this doesn't work. (p12, p13, p15, p14)
caseyquinn 7:a24d7156bc02 61
caseyquinn 7:a24d7156bc02 62 uint8_t Seconds = 0;//Seconds
caseyquinn 7:a24d7156bc02 63 uint8_t Minutes = 0;//Minutes
caseyquinn 7:a24d7156bc02 64 uint8_t Hour = 0;//Hour
caseyquinn 7:a24d7156bc02 65 uint8_t Date = 0;//Date
caseyquinn 7:a24d7156bc02 66 uint8_t Month = 0;//Month
caseyquinn 7:a24d7156bc02 67 uint8_t Year = 0;//Year
caseyquinn 11:2297d364af61 68 double secondsD = 0;
caseyquinn 7:a24d7156bc02 69
caseyquinn 7:a24d7156bc02 70 void get_time()
caseyquinn 7:a24d7156bc02 71 {
caseyquinn 7:a24d7156bc02 72 Wire.beginTransmission(Crono); // address DS3231
caseyquinn 7:a24d7156bc02 73 Wire.write(0x0E); // select register
caseyquinn 7:a24d7156bc02 74 Wire.write(0x1C); // write register bitmap, bit 7 is /EOSC
caseyquinn 7:a24d7156bc02 75 Wire.endTransmission();
caseyquinn 7:a24d7156bc02 76 wait(0.05);
caseyquinn 7:a24d7156bc02 77 Wire.beginTransmission(Crono);
caseyquinn 7:a24d7156bc02 78 Wire.write(0x00);
caseyquinn 7:a24d7156bc02 79 Wire.endTransmission();
caseyquinn 7:a24d7156bc02 80 Wire.requestFrom(Crono+1, 7);
caseyquinn 2:e596e685eb39 81
caseyquinn 7:a24d7156bc02 82 while( Wire.available() > 0 )
caseyquinn 7:a24d7156bc02 83 {
caseyquinn 7:a24d7156bc02 84 Seconds = Wire.read();
caseyquinn 7:a24d7156bc02 85 Minutes = Wire.read();
caseyquinn 7:a24d7156bc02 86 Hour = Wire.read();
caseyquinn 7:a24d7156bc02 87 uint8_t day =Wire.read(); // we don't uses this, it's just day of the week
caseyquinn 7:a24d7156bc02 88 Date = Wire.read();
caseyquinn 7:a24d7156bc02 89 Month = Wire.read();
caseyquinn 7:a24d7156bc02 90 Year = Wire.read();
caseyquinn 20:ad9883973d86 91
caseyquinn 7:a24d7156bc02 92 }
caseyquinn 7:a24d7156bc02 93 Year = ((Year&0xF0)>>4)*10 + (Year&0x0F); //Year
caseyquinn 7:a24d7156bc02 94 Month = ((Month&0x10)>>4)*10 + (Month&0x0F); //Month
caseyquinn 7:a24d7156bc02 95 Date = ((Date&0x30)>>4)*10 + (Date&0x0F); //Date
caseyquinn 9:2057c3779b6a 96 Hour = ((Hour&0x30)>>4)*10 + (Hour&0x0F); //Hour
caseyquinn 7:a24d7156bc02 97 Minutes = ((Minutes&0x70)>>4)*10 + (Minutes&0x0F); //Minutes
caseyquinn 7:a24d7156bc02 98 Seconds = ((Seconds&0x70)>>4)*10 + (Seconds&0x0F); //Seconds
caseyquinn 7:a24d7156bc02 99 }
caseyquinn 2:e596e685eb39 100
caseyquinn 0:14d46ef4b6cb 101 int main()
caseyquinn 0:14d46ef4b6cb 102 {
caseyquinn 22:8dd7e0d4c21c 103 press = bmesensor.getPressure();
caseyquinn 22:8dd7e0d4c21c 104 temp = bmesensor.getTemperature();
caseyquinn 22:8dd7e0d4c21c 105 rh = bmesensor.getHumidity();
caseyquinn 22:8dd7e0d4c21c 106
caseyquinn 22:8dd7e0d4c21c 107 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 22:8dd7e0d4c21c 108 massflowSet = volflowSet*atmoRho;
lionberg 26:9cf8e76d8274 109 //Digtal pot tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx
lionberg 30:8a33a94a1a89 110 digital_pot_setpoint = (int)floor(7.6546*pow(massflowSet,4)-77.167*pow(massflowSet,3)+293*pow(massflowSet,2)-516.79*massflowSet+383.83); //min = 0x7F, max = 0x00
caseyquinn 8:204c21adf693 111
caseyquinn 8:204c21adf693 112 DigPot.writeRegister(digital_pot_setpoint);
caseyquinn 8:204c21adf693 113 Wire.begin(SCL, SDA, TWI_FREQUENCY_100K);//
caseyquinn 20:ad9883973d86 114 wait(1);
caseyquinn 20:ad9883973d86 115 blower = 1;
caseyquinn 16:210e5eaeb89b 116
caseyquinn 17:1baf7cab694e 117 get_time();
caseyquinn 31:f8504ef165fb 118 sprintf(filename, "/sd/UPAS0004LOG_%02d-%02d-%02d_%02d:%02d:%02d.txt",Year,Month,Date,Hour,Minutes,Seconds);
caseyquinn 17:1baf7cab694e 119 FILE *fp = fopen(filename, "w");
caseyquinn 17:1baf7cab694e 120 fclose(fp);
caseyquinn 2:e596e685eb39 121
caseyquinn 1:37babeb68ab9 122
caseyquinn 0:14d46ef4b6cb 123 while(1){
caseyquinn 7:a24d7156bc02 124
caseyquinn 8:204c21adf693 125 get_time();
caseyquinn 11:2297d364af61 126 secondsD = Seconds;
caseyquinn 11:2297d364af61 127
caseyquinn 11:2297d364af61 128 if(fmod(secondsD,10)==0){
caseyquinn 16:210e5eaeb89b 129
caseyquinn 21:765896ebd61c 130 omronVolt = (omronReading*4.096)/(32768*2);
lionberg 26:9cf8e76d8274 131 //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx
lionberg 30:8a33a94a1a89 132 massflow = -1.747*pow(omronVolt,(float)4)+10.808*pow(omronVolt,(float)3)-23.114*pow(omronVolt,(float)2)+22.124*omronVolt-7.3176;
caseyquinn 20:ad9883973d86 133 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 20:ad9883973d86 134 volflow = massflow/atmoRho;
caseyquinn 20:ad9883973d86 135 deltaflow = volflow-volflowSet;
caseyquinn 20:ad9883973d86 136
caseyquinn 20:ad9883973d86 137 /*if(abs(deltaflow)>.05*volflowSet){
caseyquinn 21:765896ebd61c 138 digital_pot_setpoint = f(Vsetpoint);
caseyquinn 20:ad9883973d86 139 DigPot.writeRegister(digital_pot_setpoint);
caseyquinn 20:ad9883973d86 140 }
caseyquinn 20:ad9883973d86 141 */
caseyquinn 10:f9cb61b29340 142 movementsensor.getACCEL();
caseyquinn 10:f9cb61b29340 143 movementsensor.getCOMPASS();
caseyquinn 10:f9cb61b29340 144 accel_x = movementsensor.AccelData.x;
caseyquinn 10:f9cb61b29340 145 accel_y = movementsensor.AccelData.y;
caseyquinn 10:f9cb61b29340 146 accel_z = movementsensor.AccelData.z;
caseyquinn 10:f9cb61b29340 147 mag_x = movementsensor.MagData.x;
caseyquinn 10:f9cb61b29340 148 mag_y = movementsensor.MagData.y;
caseyquinn 10:f9cb61b29340 149 mag_z = movementsensor.MagData.z;
caseyquinn 6:2328feb48792 150 omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
caseyquinn 0:14d46ef4b6cb 151 vInReading = ads.readADC_SingleEnded(1, 0xD583); // read channel 0
caseyquinn 0:14d46ef4b6cb 152 vBlowerReading = ads.readADC_SingleEnded(2, 0xE783); // read channel 0
caseyquinn 0:14d46ef4b6cb 153 omronDiff = ads.readADC_Differential(0x8583); // differential channel 2-3
caseyquinn 20:ad9883973d86 154 press = bmesensor.getPressure();
lionberg 27:6830122ebc00 155 temp = bmesensor.getTemperature()-3;
caseyquinn 20:ad9883973d86 156 rh = bmesensor.getHumidity();
caseyquinn 20:ad9883973d86 157 uv = lightsensor.getUV();
caseyquinn 20:ad9883973d86 158 vis = lightsensor.getVIS();
caseyquinn 20:ad9883973d86 159 ir = lightsensor.getIR();
caseyquinn 20:ad9883973d86 160
caseyquinn 20:ad9883973d86 161
caseyquinn 20:ad9883973d86 162 //Mount the filesystem
caseyquinn 2:e596e685eb39 163 sd.mount();
caseyquinn 3:2d059d88582b 164 FILE *fp = fopen(filename, "a");
lionberg 28:39747a95e989 165 fprintf(fp, "%d,%d,%d,%d,%d,%d,%f,%f,%f,%f,%2.2f,%04.2f,%2.2f,%.0f,%.0f,%.0f,%.0f,%.0f,%.0f,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\r\n", Year,Month,Date,Hour,Minutes,Seconds,omronVolt,atmoRho,volflow,massflow,temp,press,rh,accel_x, accel_y, accel_z, mag_x, mag_y, mag_z, uv, vis, ir, omronReading,vInReading, vBlowerReading, omronDiff, gasG.getAmps(), gasG.getVolts(), gasG.getCharge(), digital_pot_setpoint);
caseyquinn 2:e596e685eb39 166 fclose(fp);
caseyquinn 2:e596e685eb39 167 //Unmount the filesystem
caseyquinn 2:e596e685eb39 168 sd.unmount();
caseyquinn 20:ad9883973d86 169
caseyquinn 11:2297d364af61 170 wait(1);
caseyquinn 2:e596e685eb39 171
caseyquinn 11:2297d364af61 172 }
caseyquinn 20:ad9883973d86 173
caseyquinn 2:e596e685eb39 174
caseyquinn 2:e596e685eb39 175
caseyquinn 20:ad9883973d86 176
caseyquinn 20:ad9883973d86 177
caseyquinn 0:14d46ef4b6cb 178 }
caseyquinn 0:14d46ef4b6cb 179 }
caseyquinn 0:14d46ef4b6cb 180
caseyquinn 0:14d46ef4b6cb 181
caseyquinn 0:14d46ef4b6cb 182