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:
jelord
Date:
Thu Dec 17 05:30:38 2015 +0000
Revision:
97:c596f8ed4562
Parent:
96:03106adb45c9
Child:
98:d70ff5a0b2e6
log interval is now settable/usable from iphone app

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 7:a24d7156bc02 5 #include "STC3100.h"
caseyquinn 10:f9cb61b29340 6 #include "LSM303.h"
caseyquinn 12:8c00a7f5d483 7 #include "BME280.h"
caseyquinn 14:ad550174db8b 8 #include "SI1145.h"
joshuasmth04 40:ef7e1dcb3780 9 #include "NCP5623BMUTBG.h"
joshuasmth04 41:1fb3e0ac6f87 10 #include "CronoDot.h"
caseyquinn 56:c4d6bdd7c3fb 11 #include "EEPROM.h"
joshuasmth04 72:0b36575ab00d 12 #include "US_Menu.h"
joshuasmth04 77:24fbeb2bfe05 13 #include "BLEDevice.h"
joshuasmth04 77:24fbeb2bfe05 14 #include "Calibration.h"
jelord 94:c57720890e76 15 #include "UPAS_Service.h"
joshuasmth04 71:78edbceff4fc 16
caseyquinn 1:37babeb68ab9 17
caseyquinn 1:37babeb68ab9 18 #define SERIAL_BAUD_RATE 9600
joshuasmth04 41:1fb3e0ac6f87 19 #define SCL 20
caseyquinn 7:a24d7156bc02 20 #define SDA 22
caseyquinn 81:480f0310ef9a 21
caseyquinn 0:14d46ef4b6cb 22
joshuasmth04 71:78edbceff4fc 23
jelord 94:c57720890e76 24
joshuasmth04 71:78edbceff4fc 25
jelord 97:c596f8ed4562 26 uint8_t startAndEndTime[12] = {0,};
jelord 97:c596f8ed4562 27 uint8_t logIntervalReadOut[1] = {0,};
jelord 94:c57720890e76 28 const static char DEVICE_NAME[] = "UPAS"; //Will hold the actual name of the whichever UPAS is being connected to
jelord 94:c57720890e76 29 static const uint16_t uuid16_list[] = {UPAS_Service::UPAS_SERVICE_UUID}; //Currently a custom 16-bit representation of 128-bit UUID
joshuasmth04 71:78edbceff4fc 30 BLEDevice ble;
jelord 94:c57720890e76 31 UPAS_Service *upasServicePtr;
jelord 94:c57720890e76 32 //BLE_Menu bleMenu(ble, txPayload, rxPayload, uart_base_uuid_rev, uart_base_uuid, uartService);
joshuasmth04 71:78edbceff4fc 33
joshuasmth04 72:0b36575ab00d 34 I2C i2c(p22, p20);
joshuasmth04 72:0b36575ab00d 35 Adafruit_ADS1115 ads(&i2c);
joshuasmth04 72:0b36575ab00d 36 MCP40D17 DigPot(&i2c);
joshuasmth04 72:0b36575ab00d 37 BME280 bmesensor(p22, p20);
joshuasmth04 72:0b36575ab00d 38 STC3100 gasG(p22, p20);
joshuasmth04 72:0b36575ab00d 39 Serial pc(USBTX, USBRX);
joshuasmth04 72:0b36575ab00d 40 DigitalOut blower(p29, 0);
joshuasmth04 72:0b36575ab00d 41 DigitalOut pbKill(p18, 1);
joshuasmth04 72:0b36575ab00d 42 LSM303 movementsensor(p22, p20);
joshuasmth04 72:0b36575ab00d 43 SI1145 lightsensor(p22, p20);
joshuasmth04 72:0b36575ab00d 44 NCP5623BMUTBG RGB_LED(p22, p20);
joshuasmth04 72:0b36575ab00d 45 CronoDot RTC(p22, p20);
joshuasmth04 72:0b36575ab00d 46 EEPROM E2PROM(p22, p20);
joshuasmth04 72:0b36575ab00d 47 US_Menu Menu;
joshuasmth04 77:24fbeb2bfe05 48 DigitalOut GPS_EN(p4,0); //pin 4 is used to enable and disable the GPS, in order to recive serial communications
joshuasmth04 77:24fbeb2bfe05 49 Calibration calibrations(1); //Default serial/calibration if there are no values for the selected option
caseyquinn 57:0b554f7aa9a3 50
caseyquinn 57:0b554f7aa9a3 51 Timeout stop; //This is the stop call back object
joshuasmth04 77:24fbeb2bfe05 52 Timeout logg; //This is the logging call back object
caseyquinn 67:300418575137 53
joshuasmth04 77:24fbeb2bfe05 54 uint16_t serial_num = 1; // Default serial/calibration number
joshuasmth04 77:24fbeb2bfe05 55 int RunReady =0;
caseyquinn 70:81f04e69e08a 56
caseyquinn 70:81f04e69e08a 57
caseyquinn 20:ad9883973d86 58 float press;
caseyquinn 20:ad9883973d86 59 float temp;
caseyquinn 20:ad9883973d86 60 float rh;
caseyquinn 4:69bd7e8a994c 61
caseyquinn 20:ad9883973d86 62 int uv;
caseyquinn 20:ad9883973d86 63 int vis;
caseyquinn 20:ad9883973d86 64 int ir;
caseyquinn 8:204c21adf693 65
caseyquinn 67:300418575137 66 float compass;
caseyquinn 10:f9cb61b29340 67 float accel_x;
caseyquinn 10:f9cb61b29340 68 float accel_y;
caseyquinn 10:f9cb61b29340 69 float accel_z;
lionberg 52:80480b2fafba 70 float accel_comp;
caseyquinn 10:f9cb61b29340 71 float mag_x;
caseyquinn 10:f9cb61b29340 72 float mag_y;
caseyquinn 10:f9cb61b29340 73 float mag_z;
caseyquinn 10:f9cb61b29340 74
caseyquinn 0:14d46ef4b6cb 75 int vInReading;
caseyquinn 0:14d46ef4b6cb 76 int vBlowerReading;
caseyquinn 0:14d46ef4b6cb 77 int omronDiff;
lionberg 52:80480b2fafba 78 float omronVolt; //V
caseyquinn 20:ad9883973d86 79 int omronReading;
lionberg 52:80480b2fafba 80 float atmoRho; //g/L
caseyquinn 49:19e828650618 81
caseyquinn 20:ad9883973d86 82 float massflow; //g/min
caseyquinn 20:ad9883973d86 83 float volflow; //L/min
caseyquinn 67:300418575137 84 float volflowSet = 1.0; //L/min
caseyquinn 81:480f0310ef9a 85 int logInerval = 10; //seconds
caseyquinn 57:0b554f7aa9a3 86 double secondsD = 0;
joshuasmth04 41:1fb3e0ac6f87 87 float massflowSet;
caseyquinn 42:fc2f2b9f07ae 88 float deltaVflow = 0.0;
caseyquinn 42:fc2f2b9f07ae 89 float deltaMflow = 0.0;
caseyquinn 47:3146e8c949a9 90 float gainFlow;
lionberg 52:80480b2fafba 91 float sampledVol; //L, total sampled volume
caseyquinn 20:ad9883973d86 92
caseyquinn 55:f24d70f519cd 93 int digital_pot_setpoint; //min = 0x7F, max = 0x00
caseyquinn 55:f24d70f519cd 94 int digital_pot_set;
caseyquinn 55:f24d70f519cd 95 int digital_pot_change;
caseyquinn 55:f24d70f519cd 96 int digitalpotMax = 127;
caseyquinn 55:f24d70f519cd 97 int digitalpotMin = 2;
caseyquinn 55:f24d70f519cd 98
joshuasmth04 59:a9b21b3d9afc 99 int dutyUp;
joshuasmth04 59:a9b21b3d9afc 100 int dutyDown;
joshuasmth04 59:a9b21b3d9afc 101
joshuasmth04 72:0b36575ab00d 102 // variables are only place holders for the US_Menu //
joshuasmth04 72:0b36575ab00d 103 int refreshtime;
joshuasmth04 72:0b36575ab00d 104 float home_lat, home_lon, work_lat, work_lon;
joshuasmth04 77:24fbeb2bfe05 105 //*************************************************//
joshuasmth04 72:0b36575ab00d 106
caseyquinn 67:300418575137 107 //int refresh_Time = 10; // refresh time in s, note calling read_GPS()(or similar) will still take how ever long it needs(hopefully < 1s)
caseyquinn 42:fc2f2b9f07ae 108
caseyquinn 57:0b554f7aa9a3 109 char device_name[] = "---------------";
caseyquinn 81:480f0310ef9a 110 char filename[] = "/sd/XXXX0000LOG000000000000---------------.txt";
caseyquinn 7:a24d7156bc02 111 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 112
caseyquinn 7:a24d7156bc02 113
joshuasmth04 71:78edbceff4fc 114 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
joshuasmth04 71:78edbceff4fc 115 {
jelord 94:c57720890e76 116 ble.startAdvertising();
joshuasmth04 71:78edbceff4fc 117 }
joshuasmth04 71:78edbceff4fc 118
joshuasmth04 71:78edbceff4fc 119 void WrittenHandler(const GattCharacteristicWriteCBParams *Handler) // called any time the phone sends a message
joshuasmth04 71:78edbceff4fc 120 {
jelord 94:c57720890e76 121 uint8_t *writeData = const_cast<uint8_t*>(Handler->data);
jelord 94:c57720890e76 122 // check to see what characteristic was written, by handle
jelord 94:c57720890e76 123 if(Handler->charHandle == upasServicePtr->rtcCharacteristic.getValueAttribute().getHandle()) {
jelord 94:c57720890e76 124
jelord 94:c57720890e76 125
jelord 94:c57720890e76 126
jelord 94:c57720890e76 127 E2PROM.write(0x00015, writeData+6, 12);
jelord 94:c57720890e76 128 RTC.set_time(writeData[0],writeData[1],writeData[2],writeData[3],writeData[3],writeData[4],writeData[5]);
jelord 94:c57720890e76 129
jelord 94:c57720890e76 130 }else if(Handler->charHandle == upasServicePtr->sampleTimeCharacteristic.getValueAttribute().getHandle()){
jelord 94:c57720890e76 131
jelord 94:c57720890e76 132 E2PROM.write(0x00015, writeData, 12);
jelord 94:c57720890e76 133
jelord 94:c57720890e76 134 }else if(Handler->charHandle == upasServicePtr->subjectLabelCharacteristic.getValueAttribute().getHandle()){
jelord 94:c57720890e76 135 E2PROM.write(0x00001,writeData,15);
jelord 94:c57720890e76 136
jelord 94:c57720890e76 137 }else if(Handler->charHandle == upasServicePtr->runReadyCharacteristic.getValueAttribute().getHandle()){
jelord 94:c57720890e76 138 uint8_t runData = writeData[0];
jelord 94:c57720890e76 139
jelord 94:c57720890e76 140 if(runData == 10){
jelord 94:c57720890e76 141 RunReady = 10;
jelord 97:c596f8ed4562 142
jelord 94:c57720890e76 143 }else{
jelord 94:c57720890e76 144 RunReady = 2;
jelord 94:c57720890e76 145 }
jelord 94:c57720890e76 146
jelord 96:03106adb45c9 147 }else if(Handler->charHandle == upasServicePtr->logIntevalCharacteristic.getValueAttribute().getHandle()){
jelord 94:c57720890e76 148 /* Trigger demo mode*/
jelord 96:03106adb45c9 149 //RGB_LED.set_led(3,1,0);
jelord 96:03106adb45c9 150 E2PROM.write(0x00014,writeData,1);
jelord 96:03106adb45c9 151
jelord 96:03106adb45c9 152 }else if(Handler->charHandle == upasServicePtr->flowRateCharacteristic.getValueAttribute().getHandle()){
jelord 96:03106adb45c9 153 //RGB_LED.set_led(3,1,0);
jelord 96:03106adb45c9 154 E2PROM.write(0x00010,writeData,4);
jelord 94:c57720890e76 155 }
joshuasmth04 71:78edbceff4fc 156 }
joshuasmth04 71:78edbceff4fc 157
joshuasmth04 62:edc9632bcc43 158 void check_stop() // this checks if it's time to stop and shutdown
caseyquinn 57:0b554f7aa9a3 159 {
jelord 94:c57720890e76 160
jelord 94:c57720890e76 161 if(RTC.compare(startAndEndTime[6], startAndEndTime[7], startAndEndTime[8], startAndEndTime[9], startAndEndTime[10], startAndEndTime[11])) {
caseyquinn 57:0b554f7aa9a3 162 pbKill = 0; // this is were we shut everything down
joshuasmth04 92:bb36c4bedb8e 163 pc.printf("If you're reading this something has gone very wrong.");
caseyquinn 57:0b554f7aa9a3 164 }
caseyquinn 57:0b554f7aa9a3 165 stop.detach();
joshuasmth04 77:24fbeb2bfe05 166 stop.attach(&check_stop, 9);
jelord 97:c596f8ed4562 167
joshuasmth04 62:edc9632bcc43 168 }
caseyquinn 58:7239c2ab2b65 169
joshuasmth04 41:1fb3e0ac6f87 170
caseyquinn 57:0b554f7aa9a3 171 void log_data()
joshuasmth04 59:a9b21b3d9afc 172 {
caseyquinn 70:81f04e69e08a 173 logg.detach();
jelord 94:c57720890e76 174
jelord 94:c57720890e76 175 logg.attach(&log_data, logInerval); // reading and logging data must take significintly less than 0.5s. This can be increased.
jelord 94:c57720890e76 176
joshuasmth04 59:a9b21b3d9afc 177 RTC.get_time();
jelord 94:c57720890e76 178
joshuasmth04 59:a9b21b3d9afc 179 omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
joshuasmth04 59:a9b21b3d9afc 180 omronVolt = (omronReading*4.096)/(32768*2);
joshuasmth04 59:a9b21b3d9afc 181
joshuasmth04 77:24fbeb2bfe05 182 if(omronVolt<=calibrations.omronVMin) {
joshuasmth04 77:24fbeb2bfe05 183 massflow = calibrations.omronMFMin;
joshuasmth04 77:24fbeb2bfe05 184 } else if(omronVolt>=calibrations.omronVMax) {
joshuasmth04 77:24fbeb2bfe05 185 massflow = calibrations.omronMFMax;
joshuasmth04 59:a9b21b3d9afc 186 } else {
joshuasmth04 77:24fbeb2bfe05 187 massflow = calibrations.MF4*pow(omronVolt,(float)4)+calibrations.MF3*pow(omronVolt,(float)3)+calibrations.MF2*pow(omronVolt,(float)2)+calibrations.MF1*omronVolt+calibrations.MF0;
joshuasmth04 59:a9b21b3d9afc 188 }
joshuasmth04 59:a9b21b3d9afc 189
joshuasmth04 59:a9b21b3d9afc 190 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));
joshuasmth04 59:a9b21b3d9afc 191 volflow = massflow/atmoRho;
joshuasmth04 59:a9b21b3d9afc 192 sampledVol = sampledVol + ((((float)logInerval)/60.0)*volflow);
joshuasmth04 59:a9b21b3d9afc 193 deltaVflow = volflow-volflowSet;
joshuasmth04 59:a9b21b3d9afc 194 massflowSet = volflowSet*atmoRho;
joshuasmth04 59:a9b21b3d9afc 195 deltaMflow = massflow-massflowSet;
joshuasmth04 59:a9b21b3d9afc 196 if(abs(deltaMflow)>.025) {
joshuasmth04 59:a9b21b3d9afc 197 digital_pot_change = (int)(gainFlow*deltaMflow);
joshuasmth04 59:a9b21b3d9afc 198
joshuasmth04 59:a9b21b3d9afc 199
joshuasmth04 59:a9b21b3d9afc 200 if(abs(digital_pot_change)>=50) {
joshuasmth04 59:a9b21b3d9afc 201 digital_pot_set = (int)(digital_pot_set+(int)((10.0*deltaMflow)));
joshuasmth04 59:a9b21b3d9afc 202 RGB_LED.set_led(1,0,0);
joshuasmth04 41:1fb3e0ac6f87 203
joshuasmth04 59:a9b21b3d9afc 204 } else if(digital_pot_change+digital_pot_set>=digitalpotMax&abs(digital_pot_change)<50) {
joshuasmth04 59:a9b21b3d9afc 205 digital_pot_set = digitalpotMax;
joshuasmth04 59:a9b21b3d9afc 206 RGB_LED.set_led(1,0,0);
joshuasmth04 59:a9b21b3d9afc 207 } else if(digital_pot_change+digital_pot_set<=digitalpotMin&abs(digital_pot_change)<50) {
joshuasmth04 59:a9b21b3d9afc 208 digital_pot_set = digitalpotMin;
joshuasmth04 59:a9b21b3d9afc 209 RGB_LED.set_led(1,0,0);
joshuasmth04 59:a9b21b3d9afc 210 } else {
joshuasmth04 59:a9b21b3d9afc 211 digital_pot_set = (digital_pot_set+ digital_pot_change);
joshuasmth04 59:a9b21b3d9afc 212 RGB_LED.set_led(1,1,0);
joshuasmth04 59:a9b21b3d9afc 213 }
joshuasmth04 59:a9b21b3d9afc 214
joshuasmth04 59:a9b21b3d9afc 215 DigPot.writeRegister(digital_pot_set);
joshuasmth04 59:a9b21b3d9afc 216
joshuasmth04 59:a9b21b3d9afc 217 } else {
joshuasmth04 59:a9b21b3d9afc 218 RGB_LED.set_led(0,1,0);
joshuasmth04 59:a9b21b3d9afc 219 }
joshuasmth04 59:a9b21b3d9afc 220 movementsensor.getACCEL();
joshuasmth04 59:a9b21b3d9afc 221 movementsensor.getCOMPASS();
caseyquinn 67:300418575137 222 compass = movementsensor.getCOMPASS_HEADING();
joshuasmth04 59:a9b21b3d9afc 223 accel_x = movementsensor.AccelData.x;
joshuasmth04 59:a9b21b3d9afc 224 accel_y = movementsensor.AccelData.y;
joshuasmth04 59:a9b21b3d9afc 225 accel_z = movementsensor.AccelData.z;
joshuasmth04 59:a9b21b3d9afc 226 accel_comp = pow(accel_x,(float)2)+pow(accel_y,(float)2)+pow(accel_z,(float)2)-1.0;
joshuasmth04 59:a9b21b3d9afc 227 mag_x = movementsensor.MagData.x;
joshuasmth04 59:a9b21b3d9afc 228 mag_y = movementsensor.MagData.y;
joshuasmth04 59:a9b21b3d9afc 229 mag_z = movementsensor.MagData.z;
joshuasmth04 59:a9b21b3d9afc 230 vInReading = ads.readADC_SingleEnded(1, 0xD583); // read channel 0
joshuasmth04 59:a9b21b3d9afc 231 vBlowerReading = ads.readADC_SingleEnded(2, 0xE783); // read channel 0
joshuasmth04 59:a9b21b3d9afc 232 omronDiff = ads.readADC_Differential(0x8583); // differential channel 2-3
joshuasmth04 59:a9b21b3d9afc 233 press = bmesensor.getPressure();
joshuasmth04 59:a9b21b3d9afc 234 temp = bmesensor.getTemperature()-5.0;
joshuasmth04 59:a9b21b3d9afc 235 rh = bmesensor.getHumidity();
joshuasmth04 59:a9b21b3d9afc 236 uv = lightsensor.getUV();
joshuasmth04 59:a9b21b3d9afc 237 vis = lightsensor.getVIS();
joshuasmth04 59:a9b21b3d9afc 238 ir = lightsensor.getIR();
joshuasmth04 59:a9b21b3d9afc 239 FILE *fp = fopen(filename, "a");
caseyquinn 67:300418575137 240 fprintf(fp, "%02d,%02d,%02d,%02d,%02d,%02d,%1.3f,%1.3f,%2.2f,%4.2f,%2.1f,%1.3f,%1.3f,%5.1f,%1.1f,%1.1f,%1.1f,%1.1f,%d,%d,%d,%d,%d,%d,%d,%d,%d,%1.3f,%1.3f,%f\r\n",RTC.year, RTC.month,RTC.date,RTC.hour,RTC.minutes,RTC.seconds,omronVolt,massflow,temp,press,rh,atmoRho,volflow,sampledVol,accel_x,accel_y,accel_z,accel_comp,uv,omronReading, vInReading, vBlowerReading, omronDiff,gasG.getAmps(), gasG.getVolts(), gasG.getCharge(),digital_pot_set, deltaMflow, deltaVflow, compass);
joshuasmth04 59:a9b21b3d9afc 241 fclose(fp);
caseyquinn 93:b53a9a7cb8f1 242
joshuasmth04 59:a9b21b3d9afc 243 }
jelord 96:03106adb45c9 244 /*EEPROM ADDRESSING:
jelord 96:03106adb45c9 245 0:Status bit-Unused
jelord 96:03106adb45c9 246 1-15:Device Name
jelord 96:03106adb45c9 247 16-19:Flow Rate
jelord 96:03106adb45c9 248 20: Data Log Interval
jelord 96:03106adb45c9 249 21-26: Start Time: ssmmHHddMMyy
jelord 96:03106adb45c9 250 27-32: Stop Time: ssmmHHddMMyy
jelord 96:03106adb45c9 251 33: Duty Up
jelord 96:03106adb45c9 252 34: Duty Down
jelord 96:03106adb45c9 253 35-38: Home Latitude
jelord 96:03106adb45c9 254 39-42: Home Longitude
jelord 96:03106adb45c9 255 43-46: Work Latitude
jelord 96:03106adb45c9 256 47-50: Work Longitude
jelord 96:03106adb45c9 257 51: Runready: Currently useless, should be 0
jelord 96:03106adb45c9 258 52-53: Device Calibration
jelord 96:03106adb45c9 259 54: Consider RunReady
jelord 96:03106adb45c9 260 55-56: Menu Options
jelord 96:03106adb45c9 261 57+ Nothing*/
caseyquinn 57:0b554f7aa9a3 262 int main()
caseyquinn 57:0b554f7aa9a3 263 {
joshuasmth04 91:f838d9a5b596 264 RGB_LED.set_led(1,1,1);
caseyquinn 57:0b554f7aa9a3 265 // Setup and Initialization
caseyquinn 57:0b554f7aa9a3 266 //---------------------------------------------------------------------------------------------//
joshuasmth04 71:78edbceff4fc 267 //**************//BLE initialization//**************//
jelord 94:c57720890e76 268 RTC.get_time();
jelord 94:c57720890e76 269 uint8_t rtcPassValues[6] = {RTC.seconds, RTC.minutes,RTC.hour,RTC.date,RTC.month,RTC.year};
jelord 94:c57720890e76 270 uint8_t sampleTimePassValues[12] = {0,};
jelord 94:c57720890e76 271 uint8_t subjectLabelOriginal[15] = {0,};
jelord 96:03106adb45c9 272 uint8_t dataLogOriginal[1] = {0,};
jelord 96:03106adb45c9 273 uint8_t flowRateOriginal[4] = {0,};
jelord 94:c57720890e76 274 E2PROM.read(0x00015, sampleTimePassValues, 12);
jelord 94:c57720890e76 275 E2PROM.read(0x00001, subjectLabelOriginal,15);
jelord 96:03106adb45c9 276 E2PROM.read(0x00014,dataLogOriginal,1);
jelord 96:03106adb45c9 277 E2PROM.read(0x00010,flowRateOriginal,4);
jelord 94:c57720890e76 278
joshuasmth04 77:24fbeb2bfe05 279 ble.init();
jelord 94:c57720890e76 280 ble.onDisconnection(disconnectionCallback);
jelord 94:c57720890e76 281 ble.onDataWritten(WrittenHandler); //add writeCharCallback (custom function) to whenever data is being written to device
jelord 96:03106adb45c9 282 UPAS_Service upasService(ble, false,rtcPassValues,sampleTimePassValues,subjectLabelOriginal,dataLogOriginal,flowRateOriginal); //Create a GattService that is defined in UPAS_Service.h
jelord 94:c57720890e76 283 upasServicePtr = &upasService; //Create a pointer to the service (Allows advertisement without specifically adding the service
jelord 94:c57720890e76 284
jelord 94:c57720890e76 285 /* setup advertising
jelord 94:c57720890e76 286 Following lines do the follow:
jelord 94:c57720890e76 287 1:Declare the device as Bluetooth Smart(Low-Energy)
jelord 94:c57720890e76 288 2.Advertise the UPAS service that will send and receive the 57-bits of settable values in the UPAS EEPROM
jelord 94:c57720890e76 289 3.Advertise the name that will be associated with the UPAS
jelord 94:c57720890e76 290 4.Allow the UPAS to advertise unrestricted (this might change) */
jelord 94:c57720890e76 291
jelord 94:c57720890e76 292 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
jelord 94:c57720890e76 293 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
jelord 94:c57720890e76 294 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
joshuasmth04 71:78edbceff4fc 295 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
jelord 94:c57720890e76 296 ble.setAdvertisingInterval(160); /* 160ms. */
joshuasmth04 77:24fbeb2bfe05 297 ble.startAdvertising();
jelord 94:c57720890e76 298
joshuasmth04 71:78edbceff4fc 299 //**************//BLE initialization//**************//
jelord 96:03106adb45c9 300
jelord 96:03106adb45c9 301 //Logic Loop waiting for responses from the BLE iPhone App.
jelord 96:03106adb45c9 302 //Will not break loop without response from the app
jelord 94:c57720890e76 303 while (1) {
jelord 94:c57720890e76 304 ble.waitForEvent();
jelord 94:c57720890e76 305 if(RunReady==2 && blower ==0){ //Code used to see if one-click run should begin
jelord 94:c57720890e76 306 calibrations.initialize(serial_num);
jelord 94:c57720890e76 307 RGB_LED.set_led(3,0,2);
jelord 94:c57720890e76 308 blower=1;
jelord 94:c57720890e76 309 RunReady=0;
jelord 94:c57720890e76 310 }
jelord 94:c57720890e76 311 if(RunReady==2 && blower ==1){ //Code used to see if one-click run is done.
jelord 94:c57720890e76 312 blower=0;
jelord 94:c57720890e76 313 RunReady=0;
jelord 94:c57720890e76 314 }
jelord 94:c57720890e76 315
jelord 94:c57720890e76 316 if(RunReady==10){ //Check to see if app is done with configurations
jelord 94:c57720890e76 317 blower = 0;
jelord 94:c57720890e76 318 ble.stopAdvertising();
jelord 94:c57720890e76 319 break;
jelord 94:c57720890e76 320 }
jelord 94:c57720890e76 321 }
jelord 94:c57720890e76 322
jelord 94:c57720890e76 323 E2PROM.read(0x00015, startAndEndTime, 12); //Grab start and end times from EEPROM
jelord 94:c57720890e76 324 while(!RTC.compare(startAndEndTime[0], startAndEndTime[1], startAndEndTime[2], startAndEndTime[3], startAndEndTime[4], startAndEndTime[5])) { // this while waits for the start time by looping until the start time
jelord 94:c57720890e76 325 wait(0.5);
jelord 94:c57720890e76 326 RTC.get_time();
jelord 94:c57720890e76 327
jelord 94:c57720890e76 328 }
jelord 94:c57720890e76 329 calibrations.initialize(serial_num);
jelord 94:c57720890e76 330 blower=1;
jelord 97:c596f8ed4562 331 E2PROM.read(0x00014,logIntervalReadOut,1);
jelord 97:c596f8ed4562 332 logInerval = logIntervalReadOut[0];
jelord 94:c57720890e76 333
jelord 94:c57720890e76 334 // while(!RTC.compare(startAndEndTime[6], startAndEndTime[7], startAndEndTime[8], startAndEndTime[9], startAndEndTime[10], startAndEndTime[11])) { //Waits for end time
jelord 94:c57720890e76 335 // wait(0.5);
jelord 94:c57720890e76 336 // RTC.get_time();
jelord 94:c57720890e76 337 //
jelord 94:c57720890e76 338 // }
jelord 94:c57720890e76 339 //pbKill = 0; // this is were we shut everything down
joshuasmth04 77:24fbeb2bfe05 340
joshuasmth04 77:24fbeb2bfe05 341
joshuasmth04 77:24fbeb2bfe05 342
jelord 94:c57720890e76 343
jelord 94:c57720890e76 344
joshuasmth04 85:a95dd5f03818 345
jelord 94:c57720890e76 346
jelord 94:c57720890e76 347
jelord 94:c57720890e76 348
joshuasmth04 89:047e8558fd09 349
joshuasmth04 89:047e8558fd09 350 pc.printf("You're done, you can now disconect the USB cable.\r\n");
caseyquinn 84:85d11d422da3 351 RunReady = 0;
jelord 95:0ae0ffddc544 352 // Menu.save_menu(E2PROM, logInerval, refreshtime, volflowSet, device_name, dutyUp, dutyDown, home_lat, home_lon, work_lat, work_lon, RunReady, serial_num); //Save all data to the EEPROM
joshuasmth04 91:f838d9a5b596 353
jelord 97:c596f8ed4562 354 //RGB_LED.set_led(1,1,0);
joshuasmth04 85:a95dd5f03818 355
joshuasmth04 85:a95dd5f03818 356
joshuasmth04 78:a465de6cc47e 357 calibrations.initialize(serial_num);
joshuasmth04 85:a95dd5f03818 358
jelord 94:c57720890e76 359
jelord 96:03106adb45c9 360 stop.attach(&check_stop, 30); // check if we should shut down every 9 seconds, starting 60s after the start.
joshuasmth04 85:a95dd5f03818 361
caseyquinn 81:480f0310ef9a 362 if(volflowSet<=1.0) {
joshuasmth04 59:a9b21b3d9afc 363 gainFlow = 100;
caseyquinn 81:480f0310ef9a 364 } else if(volflowSet>=2.0) {
joshuasmth04 59:a9b21b3d9afc 365 gainFlow = 25;
joshuasmth04 59:a9b21b3d9afc 366 } else {
caseyquinn 88:9142f21a4715 367 gainFlow = 25;
joshuasmth04 59:a9b21b3d9afc 368 }
joshuasmth04 59:a9b21b3d9afc 369
caseyquinn 57:0b554f7aa9a3 370 RGB_LED.set_led(1,0,0);
caseyquinn 57:0b554f7aa9a3 371 press = bmesensor.getPressure();
caseyquinn 57:0b554f7aa9a3 372 temp = bmesensor.getTemperature();
caseyquinn 57:0b554f7aa9a3 373 rh = bmesensor.getHumidity();
caseyquinn 57:0b554f7aa9a3 374
caseyquinn 57:0b554f7aa9a3 375 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 57:0b554f7aa9a3 376 massflowSet = volflowSet*atmoRho;
caseyquinn 57:0b554f7aa9a3 377 //Digtal pot tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx
joshuasmth04 85:a95dd5f03818 378
joshuasmth04 85:a95dd5f03818 379
joshuasmth04 77:24fbeb2bfe05 380 digital_pot_setpoint = (int)floor(calibrations.DP4*pow(massflowSet,4)+calibrations.DP3*pow(massflowSet,3)+calibrations.DP2*pow(massflowSet,2)+calibrations.DP1*massflowSet+calibrations.DP0); //min = 0x7F, max = 0x00
joshuasmth04 85:a95dd5f03818 381
joshuasmth04 59:a9b21b3d9afc 382 if(digital_pot_setpoint>=digitalpotMax) {
caseyquinn 57:0b554f7aa9a3 383 digital_pot_setpoint = digitalpotMax;
joshuasmth04 59:a9b21b3d9afc 384 } else if(digital_pot_setpoint<=digitalpotMin) {
caseyquinn 57:0b554f7aa9a3 385 digital_pot_setpoint = digitalpotMin;
joshuasmth04 59:a9b21b3d9afc 386 }
joshuasmth04 59:a9b21b3d9afc 387
caseyquinn 57:0b554f7aa9a3 388 DigPot.writeRegister(digital_pot_setpoint);
caseyquinn 57:0b554f7aa9a3 389 wait(1);
caseyquinn 57:0b554f7aa9a3 390 blower = 1;
jelord 97:c596f8ed4562 391
jelord 94:c57720890e76 392
jelord 94:c57720890e76 393 //Menu.save_menu(E2PROM, logInerval, refreshtime, volflowSet, device_name, dutyUp, dutyDown, home_lat, home_lon, work_lat, work_lon, RunReady, serial_num); //Save all data to the EEPROM
joshuasmth04 92:bb36c4bedb8e 394
caseyquinn 81:480f0310ef9a 395 sprintf(filename, "/sd/UPAS%04dLOG_%02d-%02d-%02d_%02d=%02d=%02d_%s.txt",serial_num,RTC.year,RTC.month,RTC.date,RTC.hour,RTC.minutes,RTC.seconds,device_name);
caseyquinn 57:0b554f7aa9a3 396 FILE *fp = fopen(filename, "w");
caseyquinn 57:0b554f7aa9a3 397 fclose(fp);
joshuasmth04 59:a9b21b3d9afc 398
joshuasmth04 59:a9b21b3d9afc 399 //---------------------------------------------------------------------------------------------//
joshuasmth04 59:a9b21b3d9afc 400 //Following lines are needed to enter into the initiallization flow control loop
joshuasmth04 59:a9b21b3d9afc 401
caseyquinn 57:0b554f7aa9a3 402 wait(10);
joshuasmth04 59:a9b21b3d9afc 403
caseyquinn 57:0b554f7aa9a3 404 omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
caseyquinn 57:0b554f7aa9a3 405 omronVolt = (omronReading*4.096)/(32768*2);
joshuasmth04 77:24fbeb2bfe05 406 if(omronVolt<=calibrations.omronVMin) {
joshuasmth04 77:24fbeb2bfe05 407 massflow = calibrations.omronMFMin;
joshuasmth04 77:24fbeb2bfe05 408 } else if(omronVolt>=calibrations.omronVMax) {
joshuasmth04 77:24fbeb2bfe05 409 massflow = calibrations.omronMFMax;
joshuasmth04 59:a9b21b3d9afc 410 } else {
joshuasmth04 77:24fbeb2bfe05 411 massflow = calibrations.MF4*pow(omronVolt,(float)4)+calibrations.MF3*pow(omronVolt,(float)3)+calibrations.MF2*pow(omronVolt,(float)2)+calibrations.MF1*omronVolt+calibrations.MF0;
joshuasmth04 59:a9b21b3d9afc 412 }
joshuasmth04 59:a9b21b3d9afc 413 deltaMflow = massflow-massflowSet;
joshuasmth04 59:a9b21b3d9afc 414 digital_pot_set = digital_pot_setpoint;
joshuasmth04 59:a9b21b3d9afc 415 wait(5);
joshuasmth04 59:a9b21b3d9afc 416
joshuasmth04 59:a9b21b3d9afc 417 //---------------------------------------------------------------------------------------------//
joshuasmth04 59:a9b21b3d9afc 418 //Sets the flow withen +-1.5% of the desired flow rate based on mass flow
joshuasmth04 59:a9b21b3d9afc 419
joshuasmth04 59:a9b21b3d9afc 420 while(abs(deltaMflow)>.015) {
joshuasmth04 77:24fbeb2bfe05 421
joshuasmth04 59:a9b21b3d9afc 422 omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
joshuasmth04 59:a9b21b3d9afc 423 omronVolt = (omronReading*4.096)/(32768*2);
joshuasmth04 59:a9b21b3d9afc 424 //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx
joshuasmth04 77:24fbeb2bfe05 425 if(omronVolt<=calibrations.omronVMin) {
joshuasmth04 77:24fbeb2bfe05 426 massflow = calibrations.omronMFMin;
joshuasmth04 77:24fbeb2bfe05 427 } else if(omronVolt>=calibrations.omronVMax) {
joshuasmth04 77:24fbeb2bfe05 428 massflow = calibrations.omronMFMax;
joshuasmth04 59:a9b21b3d9afc 429 } else {
joshuasmth04 77:24fbeb2bfe05 430 massflow = calibrations.MF4*pow(omronVolt,(float)4)+calibrations.MF3*pow(omronVolt,(float)3)+calibrations.MF2*pow(omronVolt,(float)2)+calibrations.MF1*omronVolt+calibrations.MF0;
joshuasmth04 59:a9b21b3d9afc 431 }
caseyquinn 57:0b554f7aa9a3 432
joshuasmth04 59:a9b21b3d9afc 433 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));
joshuasmth04 59:a9b21b3d9afc 434 volflow = massflow/atmoRho;
joshuasmth04 59:a9b21b3d9afc 435 massflowSet = volflowSet*atmoRho;
joshuasmth04 59:a9b21b3d9afc 436 deltaMflow = massflow-massflowSet;
jelord 94:c57720890e76 437
joshuasmth04 59:a9b21b3d9afc 438 digital_pot_set = (int)(digital_pot_set+(int)((gainFlow*deltaMflow)));
joshuasmth04 59:a9b21b3d9afc 439 if(digital_pot_set>=digitalpotMax) {
joshuasmth04 59:a9b21b3d9afc 440 digital_pot_set = digitalpotMax;
joshuasmth04 59:a9b21b3d9afc 441 } else if(digital_pot_set<=digitalpotMin) {
joshuasmth04 59:a9b21b3d9afc 442 digital_pot_set = digitalpotMin;
joshuasmth04 59:a9b21b3d9afc 443 }
joshuasmth04 59:a9b21b3d9afc 444
joshuasmth04 59:a9b21b3d9afc 445 wait(2);
joshuasmth04 59:a9b21b3d9afc 446 DigPot.writeRegister(digital_pot_set);
joshuasmth04 59:a9b21b3d9afc 447 wait(1);
joshuasmth04 59:a9b21b3d9afc 448
joshuasmth04 59:a9b21b3d9afc 449
joshuasmth04 59:a9b21b3d9afc 450 }
joshuasmth04 59:a9b21b3d9afc 451
joshuasmth04 59:a9b21b3d9afc 452 sampledVol = 0.0;
joshuasmth04 59:a9b21b3d9afc 453 RGB_LED.set_led(0,1,0);
joshuasmth04 77:24fbeb2bfe05 454
caseyquinn 67:300418575137 455
joshuasmth04 59:a9b21b3d9afc 456
joshuasmth04 59:a9b21b3d9afc 457 //** end of initalization **//
joshuasmth04 59:a9b21b3d9afc 458 //---------------------------------------------------------------------------------------------//
joshuasmth04 59:a9b21b3d9afc 459 //---------------------------------------------------------------------------------------------//
joshuasmth04 59:a9b21b3d9afc 460 // Main Control Loop
joshuasmth04 59:a9b21b3d9afc 461
caseyquinn 93:b53a9a7cb8f1 462
joshuasmth04 62:edc9632bcc43 463 logg.attach(&log_data, logInerval); // uses callbacks or block Interrupts for anything that uses i2c
joshuasmth04 59:a9b21b3d9afc 464
caseyquinn 0:14d46ef4b6cb 465 }
caseyquinn 0:14d46ef4b6cb 466