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:
lionberg
Date:
2015-06-03
Revision:
36:2d90be7655e1
Parent:
33:bc73a2493821

File content as of revision 36:2d90be7655e1:

#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 = 2.5; //L/min
float massflowSet; 
float deltaflow;

int digital_pot_setpoint; //min = 0x7F, max = 0x00
char filename[] = "/sd/UPAS0005LOG000000000000.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/UPAS0005LOG_%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 2015-05-29 CQ mods.xlsx
    massflow = -2.4541*pow(omronVolt,(float)4)+15.522*pow(omronVolt,(float)3)-34.578*pow(omronVolt,(float)2)+34.05*omronVolt-11.794;
    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()-3;
    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,%.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);
    fclose(fp);
    //Unmount the filesystem
    sd.unmount();
       
    wait(1);
   
    }
    
   
    
  
    
    }
}