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

main.cpp

Committer:
ngood
Date:
2015-06-05
Revision:
38:e02e037132b4
Parent:
37:838824df3f5f
Child:
40:ef7e1dcb3780

File content as of revision 38:e02e037132b4:

#include "mbed.h"
#include "SDFileSystem.h"
#include "Adafruit_ADS1015.h"
#include "MCP40D17.h"
#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.
#include "STC3100.h"
#include "LSM303.h"
#include "BME280.h"
#include "SI1145.h"

#define SERIAL_BAUD_RATE    9600
#define SCL         20  
#define SDA         22
#define Crono       0xD0       //D0 for the chronoDot

I2C i2c(p22, p20);
Adafruit_ADS1115 ads(&i2c);
MCP40D17 DigPot(&i2c);
BME280 bmesensor(p22, p20);
STC3100 gasG(p22, p20);
Serial  pc(USBTX, USBRX);
DigitalOut blower(p29, 0);
DigitalOut pbKill(p18, 1);
LSM303 movementsensor(p22, p20);
SI1145 lightsensor(p22, p20);

float press;
float temp;
float rh;

int uv;
int vis;
int ir;

float accel_x;
float accel_y;
float accel_z;
float mag_x;
float mag_y;
float mag_z;


int vInReading;
int vBlowerReading;
int omronDiff;

int omronReading;
float omronVolt; //V
float atmoRho; //g/L
float massflow; //g/min
float volflow; //L/min
float volflowSet = 1.0; //L/min
float massflowSet; 
float deltaflow;

int digital_pot_setpoint; //min = 0x7F, max = 0x00
char filename[] = "/sd/UPAS0010LOG000000000000.txt";

TwoWire Wire = TwoWire(NRF_TWI0);    
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)

uint8_t Seconds = 0;//Seconds
uint8_t Minutes = 0;//Minutes
uint8_t Hour    = 0;//Hour
uint8_t Date    = 0;//Date
uint8_t Month   = 0;//Month
uint8_t Year    = 0;//Year
double secondsD = 0;

void get_time()
{
    Wire.beginTransmission(Crono); // address DS3231
    Wire.write(0x0E);              // select register
    Wire.write(0x1C);              // write register bitmap, bit 7 is /EOSC
    Wire.endTransmission();
    wait(0.05);
    Wire.beginTransmission(Crono);
    Wire.write(0x00);
    Wire.endTransmission();
    Wire.requestFrom(Crono+1, 7);
    
    while( Wire.available() > 0 )
    {
        Seconds = Wire.read();
        Minutes = Wire.read();
        Hour    = Wire.read();
        uint8_t day =Wire.read(); // we don't uses this, it's just day of the week
        Date    = Wire.read();
        Month   = Wire.read();
        Year    = Wire.read();

    }
    Year    = ((Year&0xF0)>>4)*10 + (Year&0x0F);           //Year
    Month   = ((Month&0x10)>>4)*10 + (Month&0x0F);         //Month
    Date    = ((Date&0x30)>>4)*10 + (Date&0x0F);           //Date
    Hour    = ((Hour&0x30)>>4)*10 + (Hour&0x0F);         //Hour
    Minutes = ((Minutes&0x70)>>4)*10 + (Minutes&0x0F);   //Minutes
    Seconds = ((Seconds&0x70)>>4)*10 + (Seconds&0x0F);   //Seconds
}
  
int main()
{
    press = bmesensor.getPressure();
    temp = bmesensor.getTemperature();
    rh = bmesensor.getHumidity();
    
    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));
    massflowSet = volflowSet*atmoRho;
    //Digtal pot tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx
    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

   DigPot.writeRegister(digital_pot_setpoint);
   Wire.begin(SCL, SDA, TWI_FREQUENCY_100K);// 
   wait(1);
   blower = 1;
    
    get_time(); 
    sprintf(filename, "/sd/UPAS0010LOG_%02d-%02d-%02d_%02d-%02d-%02d.txt",Year,Month,Date,Hour,Minutes,Seconds);
    FILE *fp = fopen(filename, "w");
    fclose(fp);

         
    while(1){
     
    get_time(); 
    secondsD = Seconds;
    
    if(fmod(secondsD,10)==0){
    
    omronVolt = (omronReading*4.096)/(32768*2);
    
    //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet
    //UPAS0001=-2.8112*L14^4+15.636*L14^3-30.192*L14^2+25.945*L14-7.6599
    //massflow = -2.8112*pow(omronVolt,(float)4)+15.636*pow(omronVolt,(float)3)-30.192*pow(omronVolt,(float)2)+25.945*omronVolt-7.6599;
    //UPAS0002=-1.4365*L14^4+8.4231*L14^3-16.754*L14^2+15.071*L14-4.5382
    //massflow = -1.4365*pow(omronVolt,(float)4)+8.4231*pow(omronVolt,(float)3)-16.754*pow(omronVolt,(float)2)+15.071*omronVolt-4.5382;
    //UPAS0003=-2.7391*L14^4+15.215*L14^3-29.522*L14^2+25.424*L14-7.5516
    //massflow = -2.7391*pow(omronVolt,(float)4)+15.215*pow(omronVolt,(float)3)-29.522*pow(omronVolt,(float)2)+25.424*omronVolt-7.5516;
    //UPAS0004 =-1.5374*L14^4+9.2898*L14^3-18.894*L14^2+17.112*L14-5.1938
    //massflow = -1.5374*pow(omronVolt,(float)4)+9.2898*pow(omronVolt,(float)3)-18.894*pow(omronVolt,(float)2)+17.112*omronVolt-5.1938;
    //UPAS0005 =-0.4654*L14^4+3.507*L14^3-7.7254*L14^2+7.8831*L14-2.4317
    //massflow = -0.4654*pow(omronVolt,(float)4)+3.507*pow(omronVolt,(float)3)-7.7254*pow(omronVolt,(float)2)+7.8831*omronVolt-2.4317;
    //UPAS0006=-0.0507*L14^4+1.2322*L14^3-2.9659*L14^2+3.9118*L14-1.0765
    //massflow = -0.0507*pow(omronVolt,(float)4)+1.2322*pow(omronVolt,(float)3)-2.9659*pow(omronVolt,(float)2)+3.9118*omronVolt-1.0765;
    //UPAS0007=-1.5303*L14^4+10.411*L14^3-24.002*L14^2+24.321*L14-8.4713
    //massflow = -1.5303*pow(omronVolt,(float)4)+10.411*pow(omronVolt,(float)3)-24.002*pow(omronVolt,(float)2)+24.321*omronVolt-8.4713;
    //UPAS0008=-1.1291*L14^4+7.701*L14^3-17.501*L14^2+17.754*L14-6.073
    //massflow = -1.1291*pow(omronVolt,(float)4)+7.701*pow(omronVolt,(float)3)-17.501*pow(omronVolt,(float)2)+17.754*omronVolt-6.073;
    //UPAS0009=-2.662*L14^4+16.421*L14^3-35.797*L14^2+34.579*L14-11.77
    //massflow = -2.662*pow(omronVolt,(float)4)+16.421*pow(omronVolt,(float)3)-35.797*pow(omronVolt,(float)2)+34.579*omronVolt-11.77;
    //UPAS0010=-3.6933*L14^4+21.633*L14^3-44.694*L14^2+40.387*L14-12.82
    massflow = -3.6933*pow(omronVolt,(float)4)+21.633*pow(omronVolt,(float)3)-44.694*pow(omronVolt,(float)2)+40.387*omronVolt-12.82;
    
    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));
    volflow = massflow/atmoRho;
    deltaflow = volflow-volflowSet;
        
    /*if(abs(deltaflow)>.05*volflowSet){
        digital_pot_setpoint = f(Vsetpoint);
        DigPot.writeRegister(digital_pot_setpoint);
        }
    */
    movementsensor.getACCEL();
    movementsensor.getCOMPASS();
    accel_x = movementsensor.AccelData.x;
    accel_y = movementsensor.AccelData.y;
    accel_z = movementsensor.AccelData.z;
    mag_x = movementsensor.MagData.x;
    mag_y = movementsensor.MagData.y;
    mag_z = movementsensor.MagData.z;
    omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
    vInReading = ads.readADC_SingleEnded(1, 0xD583); // read channel 0
    vBlowerReading = ads.readADC_SingleEnded(2, 0xE783); // read channel 0
    omronDiff = ads.readADC_Differential(0x8583); // differential channel 2-3
    press = bmesensor.getPressure();
    temp = bmesensor.getTemperature()-5;
    rh = bmesensor.getHumidity();
    uv =  lightsensor.getUV();
    vis = lightsensor.getVIS();
    ir = lightsensor.getIR();

    
    //Mount the filesystem
    sd.mount();
    FILE *fp = fopen(filename, "a");
    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);
    fclose(fp);
    //Unmount the filesystem
    sd.unmount();
       
    wait(1);
   
    }
    
   
    
  
    
    }
}