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:
caseyquinn
Date:
Wed Sep 16 00:11:34 2015 +0000
Revision:
82:e01326a63ae9
Parent:
81:480f0310ef9a
Child:
83:9153d6c3af81
Made some minor adjustments to the demo mode loop and now when in UBCUO mode the UPAS doesn't shut down after exiting the menu.

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 71:78edbceff4fc 14 #include "BLE_Menu.h"
joshuasmth04 77:24fbeb2bfe05 15 #include "Calibration.h"
joshuasmth04 71:78edbceff4fc 16
joshuasmth04 71:78edbceff4fc 17 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
joshuasmth04 71:78edbceff4fc 18 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
joshuasmth04 71:78edbceff4fc 19 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
caseyquinn 1:37babeb68ab9 20
caseyquinn 1:37babeb68ab9 21 #define SERIAL_BAUD_RATE 9600
joshuasmth04 41:1fb3e0ac6f87 22 #define SCL 20
caseyquinn 7:a24d7156bc02 23 #define SDA 22
caseyquinn 81:480f0310ef9a 24
caseyquinn 0:14d46ef4b6cb 25
joshuasmth04 71:78edbceff4fc 26 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
joshuasmth04 71:78edbceff4fc 27 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
joshuasmth04 71:78edbceff4fc 28
joshuasmth04 71:78edbceff4fc 29 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
joshuasmth04 71:78edbceff4fc 30 static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
joshuasmth04 71:78edbceff4fc 31 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
joshuasmth04 71:78edbceff4fc 32 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
joshuasmth04 71:78edbceff4fc 33
joshuasmth04 71:78edbceff4fc 34 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
joshuasmth04 71:78edbceff4fc 35 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
joshuasmth04 71:78edbceff4fc 36 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
joshuasmth04 71:78edbceff4fc 37 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
joshuasmth04 71:78edbceff4fc 38
joshuasmth04 71:78edbceff4fc 39 BLEDevice ble;
joshuasmth04 71:78edbceff4fc 40 BLE_Menu bleMenu(ble, txPayload, rxPayload, uart_base_uuid_rev, uart_base_uuid, uartService);
joshuasmth04 71:78edbceff4fc 41
joshuasmth04 72:0b36575ab00d 42 I2C i2c(p22, p20);
joshuasmth04 72:0b36575ab00d 43 Adafruit_ADS1115 ads(&i2c);
joshuasmth04 72:0b36575ab00d 44 MCP40D17 DigPot(&i2c);
joshuasmth04 72:0b36575ab00d 45 BME280 bmesensor(p22, p20);
joshuasmth04 72:0b36575ab00d 46 STC3100 gasG(p22, p20);
joshuasmth04 72:0b36575ab00d 47 Serial pc(USBTX, USBRX);
joshuasmth04 72:0b36575ab00d 48 DigitalOut blower(p29, 0);
joshuasmth04 72:0b36575ab00d 49 DigitalOut pbKill(p18, 1);
joshuasmth04 72:0b36575ab00d 50 LSM303 movementsensor(p22, p20);
joshuasmth04 72:0b36575ab00d 51 SI1145 lightsensor(p22, p20);
joshuasmth04 72:0b36575ab00d 52 NCP5623BMUTBG RGB_LED(p22, p20);
joshuasmth04 72:0b36575ab00d 53 CronoDot RTC(p22, p20);
joshuasmth04 72:0b36575ab00d 54 EEPROM E2PROM(p22, p20);
joshuasmth04 72:0b36575ab00d 55 US_Menu Menu;
joshuasmth04 77:24fbeb2bfe05 56 DigitalOut GPS_EN(p4,0); //pin 4 is used to enable and disable the GPS, in order to recive serial communications
joshuasmth04 77:24fbeb2bfe05 57 Calibration calibrations(1); //Default serial/calibration if there are no values for the selected option
caseyquinn 57:0b554f7aa9a3 58
caseyquinn 57:0b554f7aa9a3 59 Timeout stop; //This is the stop call back object
joshuasmth04 77:24fbeb2bfe05 60 Timeout logg; //This is the logging call back object
caseyquinn 67:300418575137 61
joshuasmth04 77:24fbeb2bfe05 62 uint16_t serial_num = 1; // Default serial/calibration number
joshuasmth04 77:24fbeb2bfe05 63 int RunReady =0;
caseyquinn 70:81f04e69e08a 64
caseyquinn 70:81f04e69e08a 65
caseyquinn 20:ad9883973d86 66 float press;
caseyquinn 20:ad9883973d86 67 float temp;
caseyquinn 20:ad9883973d86 68 float rh;
caseyquinn 4:69bd7e8a994c 69
caseyquinn 20:ad9883973d86 70 int uv;
caseyquinn 20:ad9883973d86 71 int vis;
caseyquinn 20:ad9883973d86 72 int ir;
caseyquinn 8:204c21adf693 73
caseyquinn 67:300418575137 74 float compass;
caseyquinn 10:f9cb61b29340 75 float accel_x;
caseyquinn 10:f9cb61b29340 76 float accel_y;
caseyquinn 10:f9cb61b29340 77 float accel_z;
lionberg 52:80480b2fafba 78 float accel_comp;
caseyquinn 10:f9cb61b29340 79 float mag_x;
caseyquinn 10:f9cb61b29340 80 float mag_y;
caseyquinn 10:f9cb61b29340 81 float mag_z;
caseyquinn 10:f9cb61b29340 82
caseyquinn 0:14d46ef4b6cb 83 int vInReading;
caseyquinn 0:14d46ef4b6cb 84 int vBlowerReading;
caseyquinn 0:14d46ef4b6cb 85 int omronDiff;
lionberg 52:80480b2fafba 86 float omronVolt; //V
caseyquinn 20:ad9883973d86 87 int omronReading;
lionberg 52:80480b2fafba 88 float atmoRho; //g/L
caseyquinn 49:19e828650618 89
caseyquinn 20:ad9883973d86 90 float massflow; //g/min
caseyquinn 20:ad9883973d86 91 float volflow; //L/min
caseyquinn 67:300418575137 92 float volflowSet = 1.0; //L/min
caseyquinn 81:480f0310ef9a 93 int logInerval = 10; //seconds
caseyquinn 57:0b554f7aa9a3 94 double secondsD = 0;
joshuasmth04 41:1fb3e0ac6f87 95 float massflowSet;
caseyquinn 42:fc2f2b9f07ae 96 float deltaVflow = 0.0;
caseyquinn 42:fc2f2b9f07ae 97 float deltaMflow = 0.0;
caseyquinn 47:3146e8c949a9 98 float gainFlow;
lionberg 52:80480b2fafba 99 float sampledVol; //L, total sampled volume
caseyquinn 20:ad9883973d86 100
caseyquinn 55:f24d70f519cd 101 int digital_pot_setpoint; //min = 0x7F, max = 0x00
caseyquinn 55:f24d70f519cd 102 int digital_pot_set;
caseyquinn 55:f24d70f519cd 103 int digital_pot_change;
caseyquinn 55:f24d70f519cd 104 int digitalpotMax = 127;
caseyquinn 55:f24d70f519cd 105 int digitalpotMin = 2;
caseyquinn 55:f24d70f519cd 106
joshuasmth04 59:a9b21b3d9afc 107 int dutyUp;
joshuasmth04 59:a9b21b3d9afc 108 int dutyDown;
caseyquinn 67:300418575137 109 uint8_t f_sec, f_min, f_hour, f_date, f_month, f_year;
joshuasmth04 59:a9b21b3d9afc 110
joshuasmth04 72:0b36575ab00d 111 // variables are only place holders for the US_Menu //
joshuasmth04 72:0b36575ab00d 112 int refreshtime;
joshuasmth04 72:0b36575ab00d 113 float home_lat, home_lon, work_lat, work_lon;
joshuasmth04 77:24fbeb2bfe05 114 //*************************************************//
joshuasmth04 72:0b36575ab00d 115
caseyquinn 67:300418575137 116 //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 117
caseyquinn 57:0b554f7aa9a3 118 char device_name[] = "---------------";
caseyquinn 81:480f0310ef9a 119 char filename[] = "/sd/XXXX0000LOG000000000000---------------.txt";
caseyquinn 7:a24d7156bc02 120 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 121
caseyquinn 7:a24d7156bc02 122
joshuasmth04 71:78edbceff4fc 123 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
joshuasmth04 71:78edbceff4fc 124 {
joshuasmth04 71:78edbceff4fc 125 bleMenu.disconnectionCallback();
joshuasmth04 71:78edbceff4fc 126 }
joshuasmth04 71:78edbceff4fc 127
joshuasmth04 71:78edbceff4fc 128 void WrittenHandler(const GattCharacteristicWriteCBParams *Handler) // called any time the phone sends a message
joshuasmth04 71:78edbceff4fc 129 {
joshuasmth04 71:78edbceff4fc 130 bleMenu.WrittenHandler(pc, txPayload, txCharacteristic, Handler);
joshuasmth04 71:78edbceff4fc 131 //returns things to the txPayload
joshuasmth04 71:78edbceff4fc 132 }
joshuasmth04 71:78edbceff4fc 133
joshuasmth04 62:edc9632bcc43 134 void check_stop() // this checks if it's time to stop and shutdown
caseyquinn 57:0b554f7aa9a3 135 {
joshuasmth04 63:66796aef8d68 136 //RTC.get_time(); //debug
joshuasmth04 63:66796aef8d68 137 //pc.printf("%02d:%02d:%02d on %d/%d/%d) \r\n",RTC.hour, RTC.minutes, RTC.seconds, RTC.month, RTC.date, RTC.year);//debig
caseyquinn 67:300418575137 138 if(RTC.compare(f_sec, f_min, f_hour, f_date, f_month, f_year)) {
caseyquinn 57:0b554f7aa9a3 139 pbKill = 0; // this is were we shut everything down
caseyquinn 57:0b554f7aa9a3 140 }
caseyquinn 57:0b554f7aa9a3 141 stop.detach();
joshuasmth04 77:24fbeb2bfe05 142 stop.attach(&check_stop, 9);
joshuasmth04 62:edc9632bcc43 143 }
caseyquinn 58:7239c2ab2b65 144
joshuasmth04 41:1fb3e0ac6f87 145
caseyquinn 57:0b554f7aa9a3 146 void log_data()
joshuasmth04 59:a9b21b3d9afc 147 {
caseyquinn 70:81f04e69e08a 148 logg.detach();
caseyquinn 70:81f04e69e08a 149 logg.attach(&log_data, logInerval);
joshuasmth04 59:a9b21b3d9afc 150 RTC.get_time();
caseyquinn 70:81f04e69e08a 151 secondsD = RTC.seconds;
joshuasmth04 77:24fbeb2bfe05 152 while(fmod(secondsD,logInerval)!=0) {
caseyquinn 70:81f04e69e08a 153 RTC.get_time();
caseyquinn 70:81f04e69e08a 154 secondsD = RTC.seconds;
joshuasmth04 77:24fbeb2bfe05 155 }
joshuasmth04 77:24fbeb2bfe05 156
joshuasmth04 77:24fbeb2bfe05 157
joshuasmth04 59:a9b21b3d9afc 158 omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
joshuasmth04 59:a9b21b3d9afc 159 omronVolt = (omronReading*4.096)/(32768*2);
joshuasmth04 59:a9b21b3d9afc 160
joshuasmth04 77:24fbeb2bfe05 161 if(omronVolt<=calibrations.omronVMin) {
joshuasmth04 77:24fbeb2bfe05 162 massflow = calibrations.omronMFMin;
joshuasmth04 77:24fbeb2bfe05 163 } else if(omronVolt>=calibrations.omronVMax) {
joshuasmth04 77:24fbeb2bfe05 164 massflow = calibrations.omronMFMax;
joshuasmth04 59:a9b21b3d9afc 165 } else {
joshuasmth04 77:24fbeb2bfe05 166 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 167 }
joshuasmth04 59:a9b21b3d9afc 168
joshuasmth04 59:a9b21b3d9afc 169 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 170 volflow = massflow/atmoRho;
joshuasmth04 59:a9b21b3d9afc 171 sampledVol = sampledVol + ((((float)logInerval)/60.0)*volflow);
joshuasmth04 59:a9b21b3d9afc 172 deltaVflow = volflow-volflowSet;
joshuasmth04 59:a9b21b3d9afc 173 massflowSet = volflowSet*atmoRho;
joshuasmth04 59:a9b21b3d9afc 174 deltaMflow = massflow-massflowSet;
joshuasmth04 59:a9b21b3d9afc 175
joshuasmth04 59:a9b21b3d9afc 176 if(abs(deltaMflow)>.025) {
joshuasmth04 59:a9b21b3d9afc 177 digital_pot_change = (int)(gainFlow*deltaMflow);
joshuasmth04 59:a9b21b3d9afc 178
joshuasmth04 59:a9b21b3d9afc 179
joshuasmth04 59:a9b21b3d9afc 180 if(abs(digital_pot_change)>=50) {
joshuasmth04 59:a9b21b3d9afc 181 digital_pot_set = (int)(digital_pot_set+(int)((10.0*deltaMflow)));
joshuasmth04 59:a9b21b3d9afc 182 RGB_LED.set_led(1,0,0);
joshuasmth04 41:1fb3e0ac6f87 183
joshuasmth04 59:a9b21b3d9afc 184 } else if(digital_pot_change+digital_pot_set>=digitalpotMax&abs(digital_pot_change)<50) {
joshuasmth04 59:a9b21b3d9afc 185 digital_pot_set = digitalpotMax;
joshuasmth04 59:a9b21b3d9afc 186 RGB_LED.set_led(1,0,0);
joshuasmth04 59:a9b21b3d9afc 187 } else if(digital_pot_change+digital_pot_set<=digitalpotMin&abs(digital_pot_change)<50) {
joshuasmth04 59:a9b21b3d9afc 188 digital_pot_set = digitalpotMin;
joshuasmth04 59:a9b21b3d9afc 189 RGB_LED.set_led(1,0,0);
joshuasmth04 59:a9b21b3d9afc 190 } else {
joshuasmth04 59:a9b21b3d9afc 191 digital_pot_set = (digital_pot_set+ digital_pot_change);
joshuasmth04 59:a9b21b3d9afc 192 RGB_LED.set_led(1,1,0);
joshuasmth04 59:a9b21b3d9afc 193 }
joshuasmth04 59:a9b21b3d9afc 194
joshuasmth04 59:a9b21b3d9afc 195 DigPot.writeRegister(digital_pot_set);
joshuasmth04 59:a9b21b3d9afc 196
joshuasmth04 59:a9b21b3d9afc 197 } else {
joshuasmth04 59:a9b21b3d9afc 198 RGB_LED.set_led(0,1,0);
joshuasmth04 59:a9b21b3d9afc 199 }
joshuasmth04 59:a9b21b3d9afc 200
joshuasmth04 59:a9b21b3d9afc 201 movementsensor.getACCEL();
joshuasmth04 59:a9b21b3d9afc 202 movementsensor.getCOMPASS();
caseyquinn 67:300418575137 203 compass = movementsensor.getCOMPASS_HEADING();
joshuasmth04 59:a9b21b3d9afc 204 accel_x = movementsensor.AccelData.x;
joshuasmth04 59:a9b21b3d9afc 205 accel_y = movementsensor.AccelData.y;
joshuasmth04 59:a9b21b3d9afc 206 accel_z = movementsensor.AccelData.z;
joshuasmth04 59:a9b21b3d9afc 207 accel_comp = pow(accel_x,(float)2)+pow(accel_y,(float)2)+pow(accel_z,(float)2)-1.0;
joshuasmth04 59:a9b21b3d9afc 208 mag_x = movementsensor.MagData.x;
joshuasmth04 59:a9b21b3d9afc 209 mag_y = movementsensor.MagData.y;
joshuasmth04 59:a9b21b3d9afc 210 mag_z = movementsensor.MagData.z;
joshuasmth04 59:a9b21b3d9afc 211
joshuasmth04 59:a9b21b3d9afc 212 vInReading = ads.readADC_SingleEnded(1, 0xD583); // read channel 0
joshuasmth04 59:a9b21b3d9afc 213 vBlowerReading = ads.readADC_SingleEnded(2, 0xE783); // read channel 0
joshuasmth04 59:a9b21b3d9afc 214 omronDiff = ads.readADC_Differential(0x8583); // differential channel 2-3
joshuasmth04 59:a9b21b3d9afc 215 press = bmesensor.getPressure();
joshuasmth04 59:a9b21b3d9afc 216 temp = bmesensor.getTemperature()-5.0;
joshuasmth04 59:a9b21b3d9afc 217 rh = bmesensor.getHumidity();
joshuasmth04 59:a9b21b3d9afc 218 uv = lightsensor.getUV();
joshuasmth04 59:a9b21b3d9afc 219 vis = lightsensor.getVIS();
joshuasmth04 59:a9b21b3d9afc 220 ir = lightsensor.getIR();
joshuasmth04 59:a9b21b3d9afc 221
joshuasmth04 59:a9b21b3d9afc 222 FILE *fp = fopen(filename, "a");
caseyquinn 67:300418575137 223 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 224 fclose(fp);
joshuasmth04 77:24fbeb2bfe05 225
joshuasmth04 59:a9b21b3d9afc 226 }
caseyquinn 57:0b554f7aa9a3 227
caseyquinn 57:0b554f7aa9a3 228 int main()
caseyquinn 57:0b554f7aa9a3 229 {
caseyquinn 57:0b554f7aa9a3 230
caseyquinn 57:0b554f7aa9a3 231 // Setup and Initialization
caseyquinn 57:0b554f7aa9a3 232 //---------------------------------------------------------------------------------------------//
joshuasmth04 77:24fbeb2bfe05 233 Menu.read_menu(E2PROM, logInerval,refreshtime, volflowSet, device_name, dutyUp, dutyDown, home_lat, home_lon, work_lat, work_lon, RunReady, serial_num); //Read all data from the EEPROM here
joshuasmth04 77:24fbeb2bfe05 234
joshuasmth04 71:78edbceff4fc 235 //**************//BLE initialization//**************//
joshuasmth04 78:a465de6cc47e 236 bleMenu.read_menu(E2PROM, logInerval,refreshtime, volflowSet, device_name, dutyUp, dutyDown, home_lat, home_lon, work_lat, work_lon, RunReady, serial_num); //Read all data from the EEPROM here
joshuasmth04 71:78edbceff4fc 237 uint8_t BLE_name[] = {device_name[0], device_name[1], device_name[2], device_name[3], device_name[4], device_name[5], device_name[6], device_name[7], device_name[8]};
joshuasmth04 77:24fbeb2bfe05 238 ble.init();
joshuasmth04 77:24fbeb2bfe05 239 // setup advertising
joshuasmth04 71:78edbceff4fc 240 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
joshuasmth04 71:78edbceff4fc 241 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
joshuasmth04 71:78edbceff4fc 242 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,(const uint8_t *)BLE_name, sizeof(BLE_name) - 1); // ~8 char max!
joshuasmth04 71:78edbceff4fc 243 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
joshuasmth04 77:24fbeb2bfe05 244 // 100ms; in multiples of 0.625ms.
joshuasmth04 71:78edbceff4fc 245 ble.setAdvertisingInterval(160);
joshuasmth04 71:78edbceff4fc 246 ble.addService(uartService);
joshuasmth04 77:24fbeb2bfe05 247 ble.startAdvertising();
joshuasmth04 71:78edbceff4fc 248 ble.onDisconnection(disconnectionCallback); //what happens when disconected
joshuasmth04 71:78edbceff4fc 249 ble.onDataWritten(WrittenHandler); //what happens when the phone sends a message
joshuasmth04 71:78edbceff4fc 250 //**************//BLE initialization//**************//
joshuasmth04 77:24fbeb2bfe05 251
joshuasmth04 77:24fbeb2bfe05 252
joshuasmth04 77:24fbeb2bfe05 253
caseyquinn 57:0b554f7aa9a3 254 //Test for errors
joshuasmth04 71:78edbceff4fc 255 //RunReady = 0; //debug always open the menu
joshuasmth04 76:8130cb6776c6 256 //if(RTC.OSF()) { //Force the menu open if the RTC needs reset.
joshuasmth04 76:8130cb6776c6 257 // RunReady = 0;
joshuasmth04 76:8130cb6776c6 258 //}
joshuasmth04 79:ed555e9081d0 259 uint8_t temp_crr = Menu.crr;
caseyquinn 82:e01326a63ae9 260 if(Menu.crr == 1){ // if in Demo mode allow 1 second for each menu to change out of demo mode
caseyquinn 82:e01326a63ae9 261 RGB_LED.set_led(1,1,1);
caseyquinn 82:e01326a63ae9 262 wait(5);
caseyquinn 82:e01326a63ae9 263 RGB_LED.set_led(1,1,0);
joshuasmth04 80:5487ba127c82 264 Menu.Start(pc, E2PROM, RTC, logInerval, refreshtime, volflowSet, device_name, dutyUp, dutyDown, home_lat, home_lon, work_lat, work_lon, Menu.menu_ops, 2, serial_num, calibrations); //Forces you to open the menu
joshuasmth04 80:5487ba127c82 265 bleMenu.open_menu(txPayload, rxCharacteristic, E2PROM, RTC, logInerval, refreshtime, volflowSet, device_name, dutyUp, dutyDown, home_lat, home_lon, work_lat, work_lon, Menu.menu_ops ,RunReady, 3, serial_num, calibrations);
joshuasmth04 80:5487ba127c82 266 }else if(RunReady == 0) {
joshuasmth04 71:78edbceff4fc 267 RGB_LED.set_led(0,1,1); // error code/color
joshuasmth04 71:78edbceff4fc 268 pc.printf("Change Name\r\n");
joshuasmth04 77:24fbeb2bfe05 269 while(RunReady == 0) {
joshuasmth04 71:78edbceff4fc 270 RGB_LED.set_led(0,1,1); // error code/color
joshuasmth04 77:24fbeb2bfe05 271 RunReady = Menu.Start(pc, E2PROM, RTC, logInerval, refreshtime, volflowSet, device_name, dutyUp, dutyDown, home_lat, home_lon, work_lat, work_lon, Menu.menu_ops, 0.25, serial_num, calibrations); //Forces you to open the menu
joshuasmth04 71:78edbceff4fc 272 RGB_LED.set_led(0,10,10); // error code/color
joshuasmth04 78:a465de6cc47e 273 bleMenu.open_menu(txPayload, rxCharacteristic, E2PROM, RTC, logInerval, refreshtime, volflowSet, device_name, dutyUp, dutyDown, home_lat, home_lon, work_lat, work_lon, Menu.menu_ops ,RunReady, 0.25, serial_num, calibrations);
joshuasmth04 71:78edbceff4fc 274 }
caseyquinn 82:e01326a63ae9 275 if(Menu.menu_ops == 513 && Menu.crr == 0){
joshuasmth04 77:24fbeb2bfe05 276 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 77:24fbeb2bfe05 277 pbKill = 0;
joshuasmth04 77:24fbeb2bfe05 278 }
joshuasmth04 71:78edbceff4fc 279 }
joshuasmth04 77:24fbeb2bfe05 280
caseyquinn 82:e01326a63ae9 281 // Add in compare function for 519(UBCUO) mode to have it wait for the start time to start
joshuasmth04 71:78edbceff4fc 282 RunReady = 0;
joshuasmth04 79:ed555e9081d0 283 if(bleMenu.crr != temp_crr){
joshuasmth04 79:ed555e9081d0 284 Menu.crr = bleMenu.crr; // if this was changed in the UBM move it to the primary USM before saving
joshuasmth04 79:ed555e9081d0 285 }
joshuasmth04 77:24fbeb2bfe05 286 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 78:a465de6cc47e 287 calibrations.initialize(serial_num);
joshuasmth04 78:a465de6cc47e 288
caseyquinn 82:e01326a63ae9 289 if(!Menu.crr == 1){ // don't shut off when in demo mode
joshuasmth04 79:ed555e9081d0 290 stop.attach(&check_stop, 60); // check if we should shut down every 5 seconds, starting 60s after the start.
joshuasmth04 79:ed555e9081d0 291 }
joshuasmth04 79:ed555e9081d0 292
joshuasmth04 79:ed555e9081d0 293
caseyquinn 57:0b554f7aa9a3 294
caseyquinn 81:480f0310ef9a 295 if(volflowSet<=1.0) {
joshuasmth04 59:a9b21b3d9afc 296 gainFlow = 100;
caseyquinn 81:480f0310ef9a 297 } else if(volflowSet>=2.0) {
joshuasmth04 59:a9b21b3d9afc 298 gainFlow = 25;
joshuasmth04 59:a9b21b3d9afc 299 } else {
joshuasmth04 59:a9b21b3d9afc 300 gainFlow = 25;
joshuasmth04 59:a9b21b3d9afc 301 }
joshuasmth04 59:a9b21b3d9afc 302
caseyquinn 57:0b554f7aa9a3 303 RGB_LED.set_led(1,0,0);
caseyquinn 57:0b554f7aa9a3 304 press = bmesensor.getPressure();
caseyquinn 57:0b554f7aa9a3 305 temp = bmesensor.getTemperature();
caseyquinn 57:0b554f7aa9a3 306 rh = bmesensor.getHumidity();
caseyquinn 57:0b554f7aa9a3 307
caseyquinn 57:0b554f7aa9a3 308 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 309 massflowSet = volflowSet*atmoRho;
caseyquinn 57:0b554f7aa9a3 310 //Digtal pot tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx
caseyquinn 81:480f0310ef9a 311
caseyquinn 81:480f0310ef9a 312 //--------------------------------------------------------------//
caseyquinn 81:480f0310ef9a 313 // Calibration value checks //
caseyquinn 81:480f0310ef9a 314 //--------------------------------------------------------------//
caseyquinn 81:480f0310ef9a 315 while(1){
caseyquinn 81:480f0310ef9a 316 pc.printf("%f,%f,%f,%f,%f\r\n",calibrations.DP4, calibrations.DP3, calibrations.DP2, calibrations.DP1, calibrations.DP0);
caseyquinn 81:480f0310ef9a 317 pc.printf("%f,%f,%f,%f\r\n",calibrations.omronVMin, calibrations.omronVMax, calibrations.omronMFMin, calibrations.omronMFMax);
caseyquinn 81:480f0310ef9a 318 pc.printf("%f,%f,%f,%f,%f\r\n",calibrations.MF4, calibrations.MF3, calibrations.MF2, calibrations.MF1, calibrations.MF0);
caseyquinn 81:480f0310ef9a 319 wait(1);
caseyquinn 81:480f0310ef9a 320 }
caseyquinn 81:480f0310ef9a 321 //--------------------------------------------------------------//
caseyquinn 81:480f0310ef9a 322 //--------------------------------------------------------------//
caseyquinn 81:480f0310ef9a 323
joshuasmth04 77:24fbeb2bfe05 324 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
caseyquinn 81:480f0310ef9a 325 //pc.printf("%d\r\n", digital_pot_setpoint);
caseyquinn 81:480f0310ef9a 326
joshuasmth04 59:a9b21b3d9afc 327 if(digital_pot_setpoint>=digitalpotMax) {
caseyquinn 57:0b554f7aa9a3 328 digital_pot_setpoint = digitalpotMax;
joshuasmth04 59:a9b21b3d9afc 329 } else if(digital_pot_setpoint<=digitalpotMin) {
caseyquinn 57:0b554f7aa9a3 330 digital_pot_setpoint = digitalpotMin;
joshuasmth04 59:a9b21b3d9afc 331 }
joshuasmth04 59:a9b21b3d9afc 332
caseyquinn 57:0b554f7aa9a3 333 DigPot.writeRegister(digital_pot_setpoint);
caseyquinn 57:0b554f7aa9a3 334 wait(1);
caseyquinn 57:0b554f7aa9a3 335 blower = 1;
joshuasmth04 77:24fbeb2bfe05 336 if(Menu.crr == 1) {
joshuasmth04 77:24fbeb2bfe05 337 RTC.get_time();
joshuasmth04 77:24fbeb2bfe05 338 f_sec = RTC.seconds;
joshuasmth04 77:24fbeb2bfe05 339 f_min = RTC.minutes;
joshuasmth04 77:24fbeb2bfe05 340 f_hour = RTC.hour;
joshuasmth04 77:24fbeb2bfe05 341 if(RTC.month == 1 | RTC.month == 3 | RTC.month == 5 | RTC.month == 7 | RTC.month == 8 | RTC.month == 10) {
joshuasmth04 77:24fbeb2bfe05 342 if(RTC.date == 31) {
caseyquinn 67:300418575137 343 f_date = 1;
caseyquinn 67:300418575137 344 f_month = RTC.month +1;
joshuasmth04 77:24fbeb2bfe05 345 f_year = RTC.year;
joshuasmth04 77:24fbeb2bfe05 346 } else {
joshuasmth04 77:24fbeb2bfe05 347 f_date = RTC.date+1;
joshuasmth04 77:24fbeb2bfe05 348 f_month = RTC.month;
joshuasmth04 77:24fbeb2bfe05 349 f_year = RTC.year;
joshuasmth04 77:24fbeb2bfe05 350 }
joshuasmth04 77:24fbeb2bfe05 351 } else if(RTC.month == 4 | RTC.month == 6 | RTC.month == 9 | RTC.month == 11) {
joshuasmth04 77:24fbeb2bfe05 352 if(RTC.date == 30) {
joshuasmth04 77:24fbeb2bfe05 353 f_date = 1;
joshuasmth04 77:24fbeb2bfe05 354 f_month = RTC.month +1;
joshuasmth04 77:24fbeb2bfe05 355 f_year = RTC.year;
joshuasmth04 77:24fbeb2bfe05 356 } else {
caseyquinn 67:300418575137 357 f_date = RTC.date+1;
caseyquinn 67:300418575137 358 f_month = RTC.month;
caseyquinn 67:300418575137 359 f_year = RTC.year;
joshuasmth04 77:24fbeb2bfe05 360 }
joshuasmth04 77:24fbeb2bfe05 361 } else if(RTC.month == 2) {
joshuasmth04 77:24fbeb2bfe05 362 if(RTC.year == 16 | RTC.year == 20 | RTC.year == 24| RTC.year == 28) {
joshuasmth04 77:24fbeb2bfe05 363 if(RTC.date == 29) {
joshuasmth04 77:24fbeb2bfe05 364 f_date = 1;
joshuasmth04 77:24fbeb2bfe05 365 f_month = RTC.month +1;
joshuasmth04 77:24fbeb2bfe05 366 f_year = RTC.year;
joshuasmth04 77:24fbeb2bfe05 367 } else {
joshuasmth04 77:24fbeb2bfe05 368 f_date = RTC.date+1;
joshuasmth04 77:24fbeb2bfe05 369 f_month = RTC.month;
joshuasmth04 77:24fbeb2bfe05 370 f_year = RTC.year;
joshuasmth04 77:24fbeb2bfe05 371 }
joshuasmth04 77:24fbeb2bfe05 372 } else {
joshuasmth04 77:24fbeb2bfe05 373 if(RTC.date == 28) {
joshuasmth04 77:24fbeb2bfe05 374 f_date = 1;
joshuasmth04 77:24fbeb2bfe05 375 f_month = RTC.month +1;
joshuasmth04 77:24fbeb2bfe05 376 f_year = RTC.year;
joshuasmth04 77:24fbeb2bfe05 377 } else {
joshuasmth04 77:24fbeb2bfe05 378 f_date = RTC.date+1;
joshuasmth04 77:24fbeb2bfe05 379 f_month = RTC.month;
joshuasmth04 77:24fbeb2bfe05 380 f_year = RTC.year;
caseyquinn 67:300418575137 381 }
caseyquinn 67:300418575137 382 }
joshuasmth04 77:24fbeb2bfe05 383 } else if(RTC.month == 12) {
joshuasmth04 77:24fbeb2bfe05 384 if(RTC.date == 31) {
caseyquinn 67:300418575137 385 f_date = 1;
joshuasmth04 77:24fbeb2bfe05 386 f_month = 1;
joshuasmth04 77:24fbeb2bfe05 387 f_year = RTC.year+1;
joshuasmth04 77:24fbeb2bfe05 388 } else {
caseyquinn 67:300418575137 389 f_date = RTC.date+1;
caseyquinn 67:300418575137 390 f_month = RTC.month;
caseyquinn 67:300418575137 391 f_year = RTC.year;
joshuasmth04 77:24fbeb2bfe05 392 }
caseyquinn 67:300418575137 393 }
joshuasmth04 77:24fbeb2bfe05 394 }
joshuasmth04 77:24fbeb2bfe05 395
caseyquinn 81:480f0310ef9a 396 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 397 FILE *fp = fopen(filename, "w");
caseyquinn 57:0b554f7aa9a3 398 fclose(fp);
joshuasmth04 59:a9b21b3d9afc 399 //pc.printf("%d\r\n",digital_pot_setpoint);
joshuasmth04 59:a9b21b3d9afc 400
joshuasmth04 59:a9b21b3d9afc 401 //---------------------------------------------------------------------------------------------//
joshuasmth04 59:a9b21b3d9afc 402 //Following lines are needed to enter into the initiallization flow control loop
joshuasmth04 59:a9b21b3d9afc 403
caseyquinn 57:0b554f7aa9a3 404 wait(10);
joshuasmth04 59:a9b21b3d9afc 405
caseyquinn 57:0b554f7aa9a3 406 omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
caseyquinn 57:0b554f7aa9a3 407 omronVolt = (omronReading*4.096)/(32768*2);
joshuasmth04 77:24fbeb2bfe05 408 if(omronVolt<=calibrations.omronVMin) {
joshuasmth04 77:24fbeb2bfe05 409 massflow = calibrations.omronMFMin;
joshuasmth04 77:24fbeb2bfe05 410 } else if(omronVolt>=calibrations.omronVMax) {
joshuasmth04 77:24fbeb2bfe05 411 massflow = calibrations.omronMFMax;
joshuasmth04 59:a9b21b3d9afc 412 } else {
joshuasmth04 77:24fbeb2bfe05 413 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 414 }
joshuasmth04 59:a9b21b3d9afc 415 deltaMflow = massflow-massflowSet;
joshuasmth04 59:a9b21b3d9afc 416 digital_pot_set = digital_pot_setpoint;
joshuasmth04 59:a9b21b3d9afc 417 wait(5);
joshuasmth04 59:a9b21b3d9afc 418
joshuasmth04 59:a9b21b3d9afc 419 //---------------------------------------------------------------------------------------------//
joshuasmth04 59:a9b21b3d9afc 420 //Sets the flow withen +-1.5% of the desired flow rate based on mass flow
joshuasmth04 59:a9b21b3d9afc 421
joshuasmth04 59:a9b21b3d9afc 422 while(abs(deltaMflow)>.015) {
joshuasmth04 77:24fbeb2bfe05 423
joshuasmth04 59:a9b21b3d9afc 424 omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
joshuasmth04 59:a9b21b3d9afc 425 omronVolt = (omronReading*4.096)/(32768*2);
joshuasmth04 59:a9b21b3d9afc 426 //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx
joshuasmth04 77:24fbeb2bfe05 427 if(omronVolt<=calibrations.omronVMin) {
joshuasmth04 77:24fbeb2bfe05 428 massflow = calibrations.omronMFMin;
joshuasmth04 77:24fbeb2bfe05 429 } else if(omronVolt>=calibrations.omronVMax) {
joshuasmth04 77:24fbeb2bfe05 430 massflow = calibrations.omronMFMax;
joshuasmth04 59:a9b21b3d9afc 431 } else {
joshuasmth04 77:24fbeb2bfe05 432 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 433 }
caseyquinn 57:0b554f7aa9a3 434
joshuasmth04 59:a9b21b3d9afc 435 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 436 volflow = massflow/atmoRho;
joshuasmth04 59:a9b21b3d9afc 437 massflowSet = volflowSet*atmoRho;
joshuasmth04 59:a9b21b3d9afc 438 deltaMflow = massflow-massflowSet;
joshuasmth04 63:66796aef8d68 439 //pc.printf("%f,%f,%f,%f,%d,%u,%x\r\n",volflow,massflow,massflowSet,deltaMflow,digital_pot_set,digital_pot_set,digital_pot_set);
caseyquinn 67:300418575137 440 pc.printf("%d,%d,%d,%d,%d,%d\r\n",f_sec,f_min,f_hour,f_date,f_month,f_year,digital_pot_set);
joshuasmth04 59:a9b21b3d9afc 441 digital_pot_set = (int)(digital_pot_set+(int)((gainFlow*deltaMflow)));
joshuasmth04 59:a9b21b3d9afc 442 if(digital_pot_set>=digitalpotMax) {
joshuasmth04 59:a9b21b3d9afc 443 digital_pot_set = digitalpotMax;
joshuasmth04 59:a9b21b3d9afc 444 } else if(digital_pot_set<=digitalpotMin) {
joshuasmth04 59:a9b21b3d9afc 445 digital_pot_set = digitalpotMin;
joshuasmth04 59:a9b21b3d9afc 446 }
joshuasmth04 59:a9b21b3d9afc 447
joshuasmth04 59:a9b21b3d9afc 448 wait(2);
joshuasmth04 59:a9b21b3d9afc 449 DigPot.writeRegister(digital_pot_set);
joshuasmth04 59:a9b21b3d9afc 450 wait(1);
joshuasmth04 59:a9b21b3d9afc 451
joshuasmth04 59:a9b21b3d9afc 452
joshuasmth04 59:a9b21b3d9afc 453 }
joshuasmth04 59:a9b21b3d9afc 454
joshuasmth04 59:a9b21b3d9afc 455 sampledVol = 0.0;
joshuasmth04 59:a9b21b3d9afc 456 RGB_LED.set_led(0,1,0);
joshuasmth04 77:24fbeb2bfe05 457
caseyquinn 67:300418575137 458
joshuasmth04 59:a9b21b3d9afc 459
joshuasmth04 59:a9b21b3d9afc 460 //** end of initalization **//
joshuasmth04 79:ed555e9081d0 461 //---------------------------------------------------------------------------------------------//
joshuasmth04 59:a9b21b3d9afc 462 //---------------------------------------------------------------------------------------------//
joshuasmth04 79:ed555e9081d0 463 if(Menu.crr == 2){ //demo mode never shuts down
joshuasmth04 79:ed555e9081d0 464 //default demo mode settings
joshuasmth04 79:ed555e9081d0 465 volflowSet = 1.0; //L/min
joshuasmth04 79:ed555e9081d0 466 logInerval = 2;
joshuasmth04 79:ed555e9081d0 467 }
joshuasmth04 59:a9b21b3d9afc 468 //---------------------------------------------------------------------------------------------//
joshuasmth04 59:a9b21b3d9afc 469 //---------------------------------------------------------------------------------------------//
joshuasmth04 59:a9b21b3d9afc 470 // Main Control Loop
joshuasmth04 59:a9b21b3d9afc 471
joshuasmth04 62:edc9632bcc43 472 logg.attach(&log_data, logInerval); // uses callbacks or block Interrupts for anything that uses i2c
joshuasmth04 59:a9b21b3d9afc 473 while(1) {
joshuasmth04 62:edc9632bcc43 474 //__disable_irq(); // Disable Interrupts
joshuasmth04 62:edc9632bcc43 475 //RTC.get_time();
joshuasmth04 62:edc9632bcc43 476 //__enable_irq(); // Enable Interrupts
joshuasmth04 62:edc9632bcc43 477 //secondsD = (double)RTC.seconds;
joshuasmth04 62:edc9632bcc43 478 //if(fmod(secondsD,logInerval)==0) {
joshuasmth04 77:24fbeb2bfe05 479 //log_data();
joshuasmth04 64:2906b0c48403 480 //}
caseyquinn 0:14d46ef4b6cb 481 }
joshuasmth04 59:a9b21b3d9afc 482
caseyquinn 0:14d46ef4b6cb 483 }
caseyquinn 0:14d46ef4b6cb 484
caseyquinn 0:14d46ef4b6cb 485