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
main.cpp.orig@33:bc73a2493821, 2015-05-31 (annotated)
- Committer:
- lionberg
- Date:
- Sun May 31 16:10:43 2015 +0000
- Revision:
- 33:bc73a2493821
UPAS0005 with 31May calibration and updated log filenaming
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lionberg | 33:bc73a2493821 | 1 | #include "mbed.h" |
lionberg | 33:bc73a2493821 | 2 | #include "SDFileSystem.h" |
lionberg | 33:bc73a2493821 | 3 | #include "Adafruit_ADS1015.h" |
lionberg | 33:bc73a2493821 | 4 | #include "MCP40D17.h" |
lionberg | 33:bc73a2493821 | 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. |
lionberg | 33:bc73a2493821 | 6 | #include "STC3100.h" |
lionberg | 33:bc73a2493821 | 7 | #include "LSM303.h" |
lionberg | 33:bc73a2493821 | 8 | #include "BME280.h" |
lionberg | 33:bc73a2493821 | 9 | #include "SI1145.h" |
lionberg | 33:bc73a2493821 | 10 | |
lionberg | 33:bc73a2493821 | 11 | #define SERIAL_BAUD_RATE 9600 |
lionberg | 33:bc73a2493821 | 12 | #define SCL 20 |
lionberg | 33:bc73a2493821 | 13 | #define SDA 22 |
lionberg | 33:bc73a2493821 | 14 | #define Crono 0xD0 //D0 for the chronoDot |
lionberg | 33:bc73a2493821 | 15 | |
lionberg | 33:bc73a2493821 | 16 | I2C i2c(p22, p20); |
lionberg | 33:bc73a2493821 | 17 | Adafruit_ADS1115 ads(&i2c); |
lionberg | 33:bc73a2493821 | 18 | MCP40D17 DigPot(&i2c); |
lionberg | 33:bc73a2493821 | 19 | BME280 bmesensor(p22, p20); |
lionberg | 33:bc73a2493821 | 20 | STC3100 gasG(p22, p20); |
lionberg | 33:bc73a2493821 | 21 | Serial pc(USBTX, USBRX); |
lionberg | 33:bc73a2493821 | 22 | DigitalOut blower(p29, 0); |
lionberg | 33:bc73a2493821 | 23 | DigitalOut pbKill(p18, 1); |
lionberg | 33:bc73a2493821 | 24 | LSM303 movementsensor(p22, p20); |
lionberg | 33:bc73a2493821 | 25 | SI1145 lightsensor(p22, p20); |
lionberg | 33:bc73a2493821 | 26 | |
lionberg | 33:bc73a2493821 | 27 | float press; |
lionberg | 33:bc73a2493821 | 28 | float temp; |
lionberg | 33:bc73a2493821 | 29 | float rh; |
lionberg | 33:bc73a2493821 | 30 | |
lionberg | 33:bc73a2493821 | 31 | int uv; |
lionberg | 33:bc73a2493821 | 32 | int vis; |
lionberg | 33:bc73a2493821 | 33 | int ir; |
lionberg | 33:bc73a2493821 | 34 | |
lionberg | 33:bc73a2493821 | 35 | float accel_x; |
lionberg | 33:bc73a2493821 | 36 | float accel_y; |
lionberg | 33:bc73a2493821 | 37 | float accel_z; |
lionberg | 33:bc73a2493821 | 38 | float mag_x; |
lionberg | 33:bc73a2493821 | 39 | float mag_y; |
lionberg | 33:bc73a2493821 | 40 | float mag_z; |
lionberg | 33:bc73a2493821 | 41 | |
lionberg | 33:bc73a2493821 | 42 | |
lionberg | 33:bc73a2493821 | 43 | int vInReading; |
lionberg | 33:bc73a2493821 | 44 | int vBlowerReading; |
lionberg | 33:bc73a2493821 | 45 | int omronDiff; |
lionberg | 33:bc73a2493821 | 46 | |
lionberg | 33:bc73a2493821 | 47 | int omronReading; |
lionberg | 33:bc73a2493821 | 48 | float omronVolt; //V |
lionberg | 33:bc73a2493821 | 49 | float atmoRho; //g/L |
lionberg | 33:bc73a2493821 | 50 | float massflow; //g/min |
lionberg | 33:bc73a2493821 | 51 | float volflow; //L/min |
lionberg | 33:bc73a2493821 | 52 | float volflowSet = 1.0; //L/min |
lionberg | 33:bc73a2493821 | 53 | float massflowSet; |
lionberg | 33:bc73a2493821 | 54 | float deltaflow; |
lionberg | 33:bc73a2493821 | 55 | |
lionberg | 33:bc73a2493821 | 56 | int digital_pot_setpoint; //min = 0x7F, max = 0x00 |
lionberg | 33:bc73a2493821 | 57 | char filename[] = "/sd/UPAS0005LOG000000000000.txt"; |
lionberg | 33:bc73a2493821 | 58 | |
lionberg | 33:bc73a2493821 | 59 | TwoWire Wire = TwoWire(NRF_TWI0); |
lionberg | 33:bc73a2493821 | 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) |
lionberg | 33:bc73a2493821 | 61 | |
lionberg | 33:bc73a2493821 | 62 | uint8_t Seconds = 0;//Seconds |
lionberg | 33:bc73a2493821 | 63 | uint8_t Minutes = 0;//Minutes |
lionberg | 33:bc73a2493821 | 64 | uint8_t Hour = 0;//Hour |
lionberg | 33:bc73a2493821 | 65 | uint8_t Date = 0;//Date |
lionberg | 33:bc73a2493821 | 66 | uint8_t Month = 0;//Month |
lionberg | 33:bc73a2493821 | 67 | uint8_t Year = 0;//Year |
lionberg | 33:bc73a2493821 | 68 | double secondsD = 0; |
lionberg | 33:bc73a2493821 | 69 | |
lionberg | 33:bc73a2493821 | 70 | void get_time() |
lionberg | 33:bc73a2493821 | 71 | { |
lionberg | 33:bc73a2493821 | 72 | Wire.beginTransmission(Crono); // address DS3231 |
lionberg | 33:bc73a2493821 | 73 | Wire.write(0x0E); // select register |
lionberg | 33:bc73a2493821 | 74 | Wire.write(0x1C); // write register bitmap, bit 7 is /EOSC |
lionberg | 33:bc73a2493821 | 75 | Wire.endTransmission(); |
lionberg | 33:bc73a2493821 | 76 | wait(0.05); |
lionberg | 33:bc73a2493821 | 77 | Wire.beginTransmission(Crono); |
lionberg | 33:bc73a2493821 | 78 | Wire.write(0x00); |
lionberg | 33:bc73a2493821 | 79 | Wire.endTransmission(); |
lionberg | 33:bc73a2493821 | 80 | Wire.requestFrom(Crono+1, 7); |
lionberg | 33:bc73a2493821 | 81 | |
lionberg | 33:bc73a2493821 | 82 | while( Wire.available() > 0 ) |
lionberg | 33:bc73a2493821 | 83 | { |
lionberg | 33:bc73a2493821 | 84 | Seconds = Wire.read(); |
lionberg | 33:bc73a2493821 | 85 | Minutes = Wire.read(); |
lionberg | 33:bc73a2493821 | 86 | Hour = Wire.read(); |
lionberg | 33:bc73a2493821 | 87 | uint8_t day =Wire.read(); // we don't uses this, it's just day of the week |
lionberg | 33:bc73a2493821 | 88 | Date = Wire.read(); |
lionberg | 33:bc73a2493821 | 89 | Month = Wire.read(); |
lionberg | 33:bc73a2493821 | 90 | Year = Wire.read(); |
lionberg | 33:bc73a2493821 | 91 | |
lionberg | 33:bc73a2493821 | 92 | } |
lionberg | 33:bc73a2493821 | 93 | Year = ((Year&0xF0)>>4)*10 + (Year&0x0F); //Year |
lionberg | 33:bc73a2493821 | 94 | Month = ((Month&0x10)>>4)*10 + (Month&0x0F); //Month |
lionberg | 33:bc73a2493821 | 95 | Date = ((Date&0x30)>>4)*10 + (Date&0x0F); //Date |
lionberg | 33:bc73a2493821 | 96 | Hour = ((Hour&0x30)>>4)*10 + (Hour&0x0F); //Hour |
lionberg | 33:bc73a2493821 | 97 | Minutes = ((Minutes&0x70)>>4)*10 + (Minutes&0x0F); //Minutes |
lionberg | 33:bc73a2493821 | 98 | Seconds = ((Seconds&0x70)>>4)*10 + (Seconds&0x0F); //Seconds |
lionberg | 33:bc73a2493821 | 99 | } |
lionberg | 33:bc73a2493821 | 100 | |
lionberg | 33:bc73a2493821 | 101 | int main() |
lionberg | 33:bc73a2493821 | 102 | { |
lionberg | 33:bc73a2493821 | 103 | press = bmesensor.getPressure(); |
lionberg | 33:bc73a2493821 | 104 | temp = bmesensor.getTemperature(); |
lionberg | 33:bc73a2493821 | 105 | rh = bmesensor.getHumidity(); |
lionberg | 33:bc73a2493821 | 106 | |
lionberg | 33:bc73a2493821 | 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)); |
lionberg | 33:bc73a2493821 | 108 | massflowSet = volflowSet*atmoRho; |
lionberg | 33:bc73a2493821 | 109 | //Digtal pot tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx |
lionberg | 33:bc73a2493821 | 110 | digital_pot_setpoint = (int)floor(9.2456*pow(massflowSet,4)-89.538*pow(massflowSet,3)+329.03*pow(massflowSet,2)-566.12*massflowSet+412.72); //min = 0x7F, max = 0x00 |
lionberg | 33:bc73a2493821 | 111 | |
lionberg | 33:bc73a2493821 | 112 | DigPot.writeRegister(digital_pot_setpoint); |
lionberg | 33:bc73a2493821 | 113 | Wire.begin(SCL, SDA, TWI_FREQUENCY_100K);// |
lionberg | 33:bc73a2493821 | 114 | wait(1); |
lionberg | 33:bc73a2493821 | 115 | blower = 1; |
lionberg | 33:bc73a2493821 | 116 | |
lionberg | 33:bc73a2493821 | 117 | get_time(); |
lionberg | 33:bc73a2493821 | 118 | sprintf(filename, "/sd/UPAS0005LOG_%02d%02d%02d%02d%02d%02d.txt",Year,Month,Date,Hour,Minutes,Seconds); |
lionberg | 33:bc73a2493821 | 119 | FILE *fp = fopen(filename, "w"); |
lionberg | 33:bc73a2493821 | 120 | fclose(fp); |
lionberg | 33:bc73a2493821 | 121 | |
lionberg | 33:bc73a2493821 | 122 | |
lionberg | 33:bc73a2493821 | 123 | while(1){ |
lionberg | 33:bc73a2493821 | 124 | |
lionberg | 33:bc73a2493821 | 125 | get_time(); |
lionberg | 33:bc73a2493821 | 126 | secondsD = Seconds; |
lionberg | 33:bc73a2493821 | 127 | |
lionberg | 33:bc73a2493821 | 128 | if(fmod(secondsD,10)==0){ |
lionberg | 33:bc73a2493821 | 129 | |
lionberg | 33:bc73a2493821 | 130 | omronVolt = (omronReading*4.096)/(32768*2); |
lionberg | 33:bc73a2493821 | 131 | //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx |
lionberg | 33:bc73a2493821 | 132 | massflow = -2.4541*pow(omronVolt,(float)4)+15.522*pow(omronVolt,(float)3)-34.578*pow(omronVolt,(float)2)+34.05*omronVolt-11.794; |
lionberg | 33:bc73a2493821 | 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)); |
lionberg | 33:bc73a2493821 | 134 | volflow = massflow/atmoRho; |
lionberg | 33:bc73a2493821 | 135 | deltaflow = volflow-volflowSet; |
lionberg | 33:bc73a2493821 | 136 | |
lionberg | 33:bc73a2493821 | 137 | /*if(abs(deltaflow)>.05*volflowSet){ |
lionberg | 33:bc73a2493821 | 138 | digital_pot_setpoint = f(Vsetpoint); |
lionberg | 33:bc73a2493821 | 139 | DigPot.writeRegister(digital_pot_setpoint); |
lionberg | 33:bc73a2493821 | 140 | } |
lionberg | 33:bc73a2493821 | 141 | */ |
lionberg | 33:bc73a2493821 | 142 | movementsensor.getACCEL(); |
lionberg | 33:bc73a2493821 | 143 | movementsensor.getCOMPASS(); |
lionberg | 33:bc73a2493821 | 144 | accel_x = movementsensor.AccelData.x; |
lionberg | 33:bc73a2493821 | 145 | accel_y = movementsensor.AccelData.y; |
lionberg | 33:bc73a2493821 | 146 | accel_z = movementsensor.AccelData.z; |
lionberg | 33:bc73a2493821 | 147 | mag_x = movementsensor.MagData.x; |
lionberg | 33:bc73a2493821 | 148 | mag_y = movementsensor.MagData.y; |
lionberg | 33:bc73a2493821 | 149 | mag_z = movementsensor.MagData.z; |
lionberg | 33:bc73a2493821 | 150 | omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V |
lionberg | 33:bc73a2493821 | 151 | vInReading = ads.readADC_SingleEnded(1, 0xD583); // read channel 0 |
lionberg | 33:bc73a2493821 | 152 | vBlowerReading = ads.readADC_SingleEnded(2, 0xE783); // read channel 0 |
lionberg | 33:bc73a2493821 | 153 | omronDiff = ads.readADC_Differential(0x8583); // differential channel 2-3 |
lionberg | 33:bc73a2493821 | 154 | press = bmesensor.getPressure(); |
lionberg | 33:bc73a2493821 | 155 | temp = bmesensor.getTemperature()-3; |
lionberg | 33:bc73a2493821 | 156 | rh = bmesensor.getHumidity(); |
lionberg | 33:bc73a2493821 | 157 | uv = lightsensor.getUV(); |
lionberg | 33:bc73a2493821 | 158 | vis = lightsensor.getVIS(); |
lionberg | 33:bc73a2493821 | 159 | ir = lightsensor.getIR(); |
lionberg | 33:bc73a2493821 | 160 | |
lionberg | 33:bc73a2493821 | 161 | |
lionberg | 33:bc73a2493821 | 162 | //Mount the filesystem |
lionberg | 33:bc73a2493821 | 163 | sd.mount(); |
lionberg | 33:bc73a2493821 | 164 | FILE *fp = fopen(filename, "a"); |
lionberg | 33:bc73a2493821 | 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); |
lionberg | 33:bc73a2493821 | 166 | fclose(fp); |
lionberg | 33:bc73a2493821 | 167 | //Unmount the filesystem |
lionberg | 33:bc73a2493821 | 168 | sd.unmount(); |
lionberg | 33:bc73a2493821 | 169 | |
lionberg | 33:bc73a2493821 | 170 | wait(1); |
lionberg | 33:bc73a2493821 | 171 | |
lionberg | 33:bc73a2493821 | 172 | } |
lionberg | 33:bc73a2493821 | 173 | |
lionberg | 33:bc73a2493821 | 174 | |
lionberg | 33:bc73a2493821 | 175 | |
lionberg | 33:bc73a2493821 | 176 | |
lionberg | 33:bc73a2493821 | 177 | |
lionberg | 33:bc73a2493821 | 178 | } |
lionberg | 33:bc73a2493821 | 179 | } |
lionberg | 33:bc73a2493821 | 180 | |
lionberg | 33:bc73a2493821 | 181 | |
lionberg | 33:bc73a2493821 | 182 |