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:
joshuasmth04
Date:
Tue Jun 09 16:48:47 2015 +0000
Revision:
40:ef7e1dcb3780
Parent:
38:e02e037132b4
Child:
41:1fb3e0ac6f87
Added the library to control the RGB LED and turned the LED on as low as possible green.

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"
lionberg 37:838824df3f5f 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"
joshuasmth04 40:ef7e1dcb3780 10 #include "NCP5623BMUTBG.h"
caseyquinn 1:37babeb68ab9 11
caseyquinn 1:37babeb68ab9 12 #define SERIAL_BAUD_RATE 9600
lionberg 37:838824df3f5f 13 #define SCL 20
caseyquinn 7:a24d7156bc02 14 #define SDA 22
lionberg 37:838824df3f5f 15 #define Crono 0xD0 //D0 for the chronoDot
caseyquinn 0:14d46ef4b6cb 16
caseyquinn 0:14d46ef4b6cb 17 I2C i2c(p22, p20);
caseyquinn 0:14d46ef4b6cb 18 Adafruit_ADS1115 ads(&i2c);
caseyquinn 1:37babeb68ab9 19 MCP40D17 DigPot(&i2c);
caseyquinn 12:8c00a7f5d483 20 BME280 bmesensor(p22, p20);
caseyquinn 7:a24d7156bc02 21 STC3100 gasG(p22, p20);
caseyquinn 2:e596e685eb39 22 Serial pc(USBTX, USBRX);
caseyquinn 8:204c21adf693 23 DigitalOut blower(p29, 0);
caseyquinn 17:1baf7cab694e 24 DigitalOut pbKill(p18, 1);
caseyquinn 10:f9cb61b29340 25 LSM303 movementsensor(p22, p20);
caseyquinn 14:ad550174db8b 26 SI1145 lightsensor(p22, p20);
joshuasmth04 40:ef7e1dcb3780 27 NCP5623BMUTBG RGB_LED(p22, p20);
caseyquinn 0:14d46ef4b6cb 28
caseyquinn 20:ad9883973d86 29 float press;
caseyquinn 20:ad9883973d86 30 float temp;
caseyquinn 20:ad9883973d86 31 float rh;
caseyquinn 4:69bd7e8a994c 32
caseyquinn 20:ad9883973d86 33 int uv;
caseyquinn 20:ad9883973d86 34 int vis;
caseyquinn 20:ad9883973d86 35 int ir;
caseyquinn 8:204c21adf693 36
caseyquinn 10:f9cb61b29340 37 float accel_x;
caseyquinn 10:f9cb61b29340 38 float accel_y;
caseyquinn 10:f9cb61b29340 39 float accel_z;
caseyquinn 10:f9cb61b29340 40 float mag_x;
caseyquinn 10:f9cb61b29340 41 float mag_y;
caseyquinn 10:f9cb61b29340 42 float mag_z;
caseyquinn 10:f9cb61b29340 43
caseyquinn 20:ad9883973d86 44
caseyquinn 0:14d46ef4b6cb 45 int vInReading;
caseyquinn 0:14d46ef4b6cb 46 int vBlowerReading;
caseyquinn 0:14d46ef4b6cb 47 int omronDiff;
caseyquinn 20:ad9883973d86 48
caseyquinn 20:ad9883973d86 49 int omronReading;
caseyquinn 20:ad9883973d86 50 float omronVolt; //V
caseyquinn 20:ad9883973d86 51 float atmoRho; //g/L
caseyquinn 20:ad9883973d86 52 float massflow; //g/min
caseyquinn 20:ad9883973d86 53 float volflow; //L/min
caseyquinn 20:ad9883973d86 54 float volflowSet = 1.0; //L/min
lionberg 37:838824df3f5f 55 float massflowSet;
caseyquinn 20:ad9883973d86 56 float deltaflow;
caseyquinn 20:ad9883973d86 57
caseyquinn 22:8dd7e0d4c21c 58 int digital_pot_setpoint; //min = 0x7F, max = 0x00
lionberg 37:838824df3f5f 59 char filename[] = "/sd/UPAS0010LOG000000000000.txt";
caseyquinn 7:a24d7156bc02 60
lionberg 37:838824df3f5f 61 TwoWire Wire = TwoWire(NRF_TWI0);
caseyquinn 7:a24d7156bc02 62 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 63
lionberg 37:838824df3f5f 64 uint8_t Seconds = 0;//Seconds
lionberg 37:838824df3f5f 65 uint8_t Minutes = 0;//Minutes
lionberg 37:838824df3f5f 66 uint8_t Hour = 0;//Hour
lionberg 37:838824df3f5f 67 uint8_t Date = 0;//Date
lionberg 37:838824df3f5f 68 uint8_t Month = 0;//Month
lionberg 37:838824df3f5f 69 uint8_t Year = 0;//Year
caseyquinn 11:2297d364af61 70 double secondsD = 0;
caseyquinn 7:a24d7156bc02 71
lionberg 37:838824df3f5f 72 void get_time()
lionberg 37:838824df3f5f 73 {
lionberg 37:838824df3f5f 74 Wire.beginTransmission(Crono); // address DS3231
lionberg 37:838824df3f5f 75 Wire.write(0x0E); // select register
lionberg 37:838824df3f5f 76 Wire.write(0x1C); // write register bitmap, bit 7 is /EOSC
lionberg 37:838824df3f5f 77 Wire.endTransmission();
lionberg 37:838824df3f5f 78 wait(0.05);
lionberg 37:838824df3f5f 79 Wire.beginTransmission(Crono);
lionberg 37:838824df3f5f 80 Wire.write(0x00);
lionberg 37:838824df3f5f 81 Wire.endTransmission();
lionberg 37:838824df3f5f 82 Wire.requestFrom(Crono+1, 7);
lionberg 37:838824df3f5f 83
lionberg 37:838824df3f5f 84 while( Wire.available() > 0 )
lionberg 37:838824df3f5f 85 {
lionberg 37:838824df3f5f 86 Seconds = Wire.read();
lionberg 37:838824df3f5f 87 Minutes = Wire.read();
lionberg 37:838824df3f5f 88 Hour = Wire.read();
lionberg 37:838824df3f5f 89 uint8_t day =Wire.read(); // we don't uses this, it's just day of the week
lionberg 37:838824df3f5f 90 Date = Wire.read();
lionberg 37:838824df3f5f 91 Month = Wire.read();
lionberg 37:838824df3f5f 92 Year = Wire.read();
caseyquinn 20:ad9883973d86 93
lionberg 37:838824df3f5f 94 }
lionberg 37:838824df3f5f 95 Year = ((Year&0xF0)>>4)*10 + (Year&0x0F); //Year
lionberg 37:838824df3f5f 96 Month = ((Month&0x10)>>4)*10 + (Month&0x0F); //Month
lionberg 37:838824df3f5f 97 Date = ((Date&0x30)>>4)*10 + (Date&0x0F); //Date
lionberg 37:838824df3f5f 98 Hour = ((Hour&0x30)>>4)*10 + (Hour&0x0F); //Hour
lionberg 37:838824df3f5f 99 Minutes = ((Minutes&0x70)>>4)*10 + (Minutes&0x0F); //Minutes
lionberg 37:838824df3f5f 100 Seconds = ((Seconds&0x70)>>4)*10 + (Seconds&0x0F); //Seconds
lionberg 37:838824df3f5f 101 }
lionberg 37:838824df3f5f 102
caseyquinn 0:14d46ef4b6cb 103 int main()
caseyquinn 0:14d46ef4b6cb 104 {
joshuasmth04 40:ef7e1dcb3780 105 RGB_LED.set_led(1,1,1);
caseyquinn 22:8dd7e0d4c21c 106 press = bmesensor.getPressure();
caseyquinn 22:8dd7e0d4c21c 107 temp = bmesensor.getTemperature();
caseyquinn 22:8dd7e0d4c21c 108 rh = bmesensor.getHumidity();
lionberg 37:838824df3f5f 109
caseyquinn 22:8dd7e0d4c21c 110 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 111 massflowSet = volflowSet*atmoRho;
lionberg 26:9cf8e76d8274 112 //Digtal pot tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx
lionberg 33:bc73a2493821 113 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
joshuasmth04 35:bd978e6ccd82 114
lionberg 37:838824df3f5f 115 DigPot.writeRegister(digital_pot_setpoint);
lionberg 37:838824df3f5f 116 Wire.begin(SCL, SDA, TWI_FREQUENCY_100K);//
lionberg 37:838824df3f5f 117 wait(1);
lionberg 37:838824df3f5f 118 blower = 1;
lionberg 37:838824df3f5f 119
lionberg 37:838824df3f5f 120 get_time();
lionberg 37:838824df3f5f 121 sprintf(filename, "/sd/UPAS0010LOG_%02d-%02d-%02d_%02d-%02d-%02d.txt",Year,Month,Date,Hour,Minutes,Seconds);
caseyquinn 17:1baf7cab694e 122 FILE *fp = fopen(filename, "w");
caseyquinn 17:1baf7cab694e 123 fclose(fp);
caseyquinn 2:e596e685eb39 124
lionberg 37:838824df3f5f 125
lionberg 37:838824df3f5f 126 while(1){
lionberg 37:838824df3f5f 127
joshuasmth04 40:ef7e1dcb3780 128 RGB_LED.set_led(0,1,0);
lionberg 37:838824df3f5f 129 get_time();
lionberg 37:838824df3f5f 130 secondsD = Seconds;
lionberg 37:838824df3f5f 131
lionberg 37:838824df3f5f 132 if(fmod(secondsD,10)==0){
lionberg 37:838824df3f5f 133
lionberg 37:838824df3f5f 134 omronVolt = (omronReading*4.096)/(32768*2);
lionberg 37:838824df3f5f 135
lionberg 37:838824df3f5f 136 //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet
lionberg 37:838824df3f5f 137 //UPAS0001=-2.8112*L14^4+15.636*L14^3-30.192*L14^2+25.945*L14-7.6599
lionberg 37:838824df3f5f 138 //massflow = -2.8112*pow(omronVolt,(float)4)+15.636*pow(omronVolt,(float)3)-30.192*pow(omronVolt,(float)2)+25.945*omronVolt-7.6599;
lionberg 37:838824df3f5f 139 //UPAS0002=-1.4365*L14^4+8.4231*L14^3-16.754*L14^2+15.071*L14-4.5382
lionberg 37:838824df3f5f 140 //massflow = -1.4365*pow(omronVolt,(float)4)+8.4231*pow(omronVolt,(float)3)-16.754*pow(omronVolt,(float)2)+15.071*omronVolt-4.5382;
lionberg 37:838824df3f5f 141 //UPAS0003=-2.7391*L14^4+15.215*L14^3-29.522*L14^2+25.424*L14-7.5516
lionberg 37:838824df3f5f 142 //massflow = -2.7391*pow(omronVolt,(float)4)+15.215*pow(omronVolt,(float)3)-29.522*pow(omronVolt,(float)2)+25.424*omronVolt-7.5516;
lionberg 37:838824df3f5f 143 //UPAS0004 =-1.5374*L14^4+9.2898*L14^3-18.894*L14^2+17.112*L14-5.1938
lionberg 37:838824df3f5f 144 //massflow = -1.5374*pow(omronVolt,(float)4)+9.2898*pow(omronVolt,(float)3)-18.894*pow(omronVolt,(float)2)+17.112*omronVolt-5.1938;
lionberg 37:838824df3f5f 145 //UPAS0005 =-0.4654*L14^4+3.507*L14^3-7.7254*L14^2+7.8831*L14-2.4317
lionberg 37:838824df3f5f 146 //massflow = -0.4654*pow(omronVolt,(float)4)+3.507*pow(omronVolt,(float)3)-7.7254*pow(omronVolt,(float)2)+7.8831*omronVolt-2.4317;
lionberg 37:838824df3f5f 147 //UPAS0006=-0.0507*L14^4+1.2322*L14^3-2.9659*L14^2+3.9118*L14-1.0765
lionberg 37:838824df3f5f 148 //massflow = -0.0507*pow(omronVolt,(float)4)+1.2322*pow(omronVolt,(float)3)-2.9659*pow(omronVolt,(float)2)+3.9118*omronVolt-1.0765;
lionberg 37:838824df3f5f 149 //UPAS0007=-1.5303*L14^4+10.411*L14^3-24.002*L14^2+24.321*L14-8.4713
lionberg 37:838824df3f5f 150 //massflow = -1.5303*pow(omronVolt,(float)4)+10.411*pow(omronVolt,(float)3)-24.002*pow(omronVolt,(float)2)+24.321*omronVolt-8.4713;
lionberg 37:838824df3f5f 151 //UPAS0008=-1.1291*L14^4+7.701*L14^3-17.501*L14^2+17.754*L14-6.073
lionberg 37:838824df3f5f 152 //massflow = -1.1291*pow(omronVolt,(float)4)+7.701*pow(omronVolt,(float)3)-17.501*pow(omronVolt,(float)2)+17.754*omronVolt-6.073;
lionberg 37:838824df3f5f 153 //UPAS0009=-2.662*L14^4+16.421*L14^3-35.797*L14^2+34.579*L14-11.77
lionberg 37:838824df3f5f 154 //massflow = -2.662*pow(omronVolt,(float)4)+16.421*pow(omronVolt,(float)3)-35.797*pow(omronVolt,(float)2)+34.579*omronVolt-11.77;
lionberg 37:838824df3f5f 155 //UPAS0010=-3.6933*L14^4+21.633*L14^3-44.694*L14^2+40.387*L14-12.82
lionberg 37:838824df3f5f 156 massflow = -3.6933*pow(omronVolt,(float)4)+21.633*pow(omronVolt,(float)3)-44.694*pow(omronVolt,(float)2)+40.387*omronVolt-12.82;
lionberg 37:838824df3f5f 157
lionberg 37:838824df3f5f 158 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 37:838824df3f5f 159 volflow = massflow/atmoRho;
lionberg 37:838824df3f5f 160 deltaflow = volflow-volflowSet;
lionberg 37:838824df3f5f 161
lionberg 37:838824df3f5f 162 /*if(abs(deltaflow)>.05*volflowSet){
lionberg 37:838824df3f5f 163 digital_pot_setpoint = f(Vsetpoint);
lionberg 37:838824df3f5f 164 DigPot.writeRegister(digital_pot_setpoint);
caseyquinn 34:481403146690 165 }
lionberg 37:838824df3f5f 166 */
lionberg 37:838824df3f5f 167 movementsensor.getACCEL();
lionberg 37:838824df3f5f 168 movementsensor.getCOMPASS();
lionberg 37:838824df3f5f 169 accel_x = movementsensor.AccelData.x;
lionberg 37:838824df3f5f 170 accel_y = movementsensor.AccelData.y;
lionberg 37:838824df3f5f 171 accel_z = movementsensor.AccelData.z;
lionberg 37:838824df3f5f 172 mag_x = movementsensor.MagData.x;
lionberg 37:838824df3f5f 173 mag_y = movementsensor.MagData.y;
lionberg 37:838824df3f5f 174 mag_z = movementsensor.MagData.z;
lionberg 37:838824df3f5f 175 omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
lionberg 37:838824df3f5f 176 vInReading = ads.readADC_SingleEnded(1, 0xD583); // read channel 0
lionberg 37:838824df3f5f 177 vBlowerReading = ads.readADC_SingleEnded(2, 0xE783); // read channel 0
lionberg 37:838824df3f5f 178 omronDiff = ads.readADC_Differential(0x8583); // differential channel 2-3
lionberg 37:838824df3f5f 179 press = bmesensor.getPressure();
lionberg 37:838824df3f5f 180 temp = bmesensor.getTemperature()-5;
lionberg 37:838824df3f5f 181 rh = bmesensor.getHumidity();
lionberg 37:838824df3f5f 182 uv = lightsensor.getUV();
lionberg 37:838824df3f5f 183 vis = lightsensor.getVIS();
lionberg 37:838824df3f5f 184 ir = lightsensor.getIR();
joshuasmth04 35:bd978e6ccd82 185
lionberg 37:838824df3f5f 186
lionberg 37:838824df3f5f 187 //Mount the filesystem
lionberg 37:838824df3f5f 188 sd.mount();
lionberg 37:838824df3f5f 189 FILE *fp = fopen(filename, "a");
ngood 38:e02e037132b4 190 fprintf(fp, "%d,%d,%d,%d,%d,%d,%f,%f,%f,%f,%2.2f,%04.2f,%2.2f,%1.4f,%1.4f,%1.4f,%.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 37:838824df3f5f 191 fclose(fp);
lionberg 37:838824df3f5f 192 //Unmount the filesystem
lionberg 37:838824df3f5f 193 sd.unmount();
lionberg 37:838824df3f5f 194
lionberg 37:838824df3f5f 195 wait(1);
lionberg 37:838824df3f5f 196
lionberg 37:838824df3f5f 197 }
lionberg 37:838824df3f5f 198
lionberg 37:838824df3f5f 199
lionberg 37:838824df3f5f 200
lionberg 37:838824df3f5f 201
lionberg 37:838824df3f5f 202
caseyquinn 0:14d46ef4b6cb 203 }
caseyquinn 0:14d46ef4b6cb 204 }
caseyquinn 0:14d46ef4b6cb 205
caseyquinn 0:14d46ef4b6cb 206
caseyquinn 0:14d46ef4b6cb 207