Program to control UPAS with MicroChip BLE chip + iPhone App
Dependencies: ADS1115 BME280 Calibration CronoDot EEPROM LSM303 MCP40D17 MicroBLE NCP5623BMUTBG SDFileSystem SI1145 STC3100 mbed
main.cpp@2:88fcbfadec6a, 2016-02-03 (annotated)
- Committer:
- jelord
- Date:
- Wed Feb 03 01:29:18 2016 +0000
- Revision:
- 2:88fcbfadec6a
- Parent:
- 1:9fbb5b665068
- Child:
- 3:122bfc998c4c
App can now read/write to sample name, log interval, flow rate, and start/stop time. Still missing RTC
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jelord | 0:2cb2b2ea316f | 1 | #include "mbed.h" |
jelord | 2:88fcbfadec6a | 2 | #include "SDFileSystem.h" |
jelord | 2:88fcbfadec6a | 3 | #include "Adafruit_ADS1015.h" |
jelord | 2:88fcbfadec6a | 4 | #include "MCP40D17.h" |
jelord | 2:88fcbfadec6a | 5 | #include "STC3100.h" |
jelord | 2:88fcbfadec6a | 6 | #include "LSM303.h" |
jelord | 2:88fcbfadec6a | 7 | #include "BME280.h" |
jelord | 2:88fcbfadec6a | 8 | #include "SI1145.h" |
jelord | 2:88fcbfadec6a | 9 | #include "NCP5623BMUTBG.h" |
jelord | 2:88fcbfadec6a | 10 | #include "CronoDot.h" |
jelord | 2:88fcbfadec6a | 11 | #include "EEPROM.h" |
jelord | 2:88fcbfadec6a | 12 | #include "Calibration.h" |
jelord | 1:9fbb5b665068 | 13 | Serial pc(USBTX, USBRX); |
jelord | 1:9fbb5b665068 | 14 | Serial microChannel(D8, D2); // tx, rx |
jelord | 1:9fbb5b665068 | 15 | Timer t; |
jelord | 1:9fbb5b665068 | 16 | struct tm tt; |
jelord | 0:2cb2b2ea316f | 17 | |
jelord | 2:88fcbfadec6a | 18 | I2C i2c(D14, D15); |
jelord | 2:88fcbfadec6a | 19 | Adafruit_ADS1115 ads(&i2c); |
jelord | 2:88fcbfadec6a | 20 | MCP40D17 DigPot(&i2c); |
jelord | 2:88fcbfadec6a | 21 | BME280 bmesensor(D14, D15); |
jelord | 2:88fcbfadec6a | 22 | STC3100 gasG(D14, D15); |
jelord | 2:88fcbfadec6a | 23 | DigitalOut blower(D9, 0); |
jelord | 2:88fcbfadec6a | 24 | DigitalOut pbKill(D7, 1); |
jelord | 2:88fcbfadec6a | 25 | LSM303 movementsensor(D14, D15); |
jelord | 2:88fcbfadec6a | 26 | SI1145 lightsensor(D14, D15); |
jelord | 2:88fcbfadec6a | 27 | NCP5623BMUTBG RGB_LED(D14, D15); |
jelord | 2:88fcbfadec6a | 28 | //CronoDot RTC(D14, D15); |
jelord | 2:88fcbfadec6a | 29 | EEPROM E2PROM(D14, D15); |
jelord | 2:88fcbfadec6a | 30 | //DigitalOut GPS_EN(p4,0); //pin 4 is used to enable and disable the GPS, in order to recive serial communications |
jelord | 2:88fcbfadec6a | 31 | Calibration calibrations(1); //Default serial/calibration if there are no values for the selected option |
jelord | 0:2cb2b2ea316f | 32 | |
jelord | 2:88fcbfadec6a | 33 | void sendData(); |
jelord | 2:88fcbfadec6a | 34 | |
jelord | 2:88fcbfadec6a | 35 | int timeout = 2; |
jelord | 2:88fcbfadec6a | 36 | |
jelord | 1:9fbb5b665068 | 37 | void pc_recv(void){ |
jelord | 1:9fbb5b665068 | 38 | while(pc.readable()){ |
jelord | 1:9fbb5b665068 | 39 | pc.getc(); |
jelord | 0:2cb2b2ea316f | 40 | } |
jelord | 0:2cb2b2ea316f | 41 | } |
jelord | 0:2cb2b2ea316f | 42 | |
jelord | 0:2cb2b2ea316f | 43 | static uint8_t rx_buf[20]; |
jelord | 0:2cb2b2ea316f | 44 | static uint8_t rx_len=0; |
jelord | 1:9fbb5b665068 | 45 | static int haltBLE = 1; |
jelord | 1:9fbb5b665068 | 46 | static int transmissionValue = 0; |
jelord | 2:88fcbfadec6a | 47 | uint8_t writeData[20] = {0,}; |
jelord | 2:88fcbfadec6a | 48 | static uint8_t dataLength = 0; |
jelord | 0:2cb2b2ea316f | 49 | |
jelord | 0:2cb2b2ea316f | 50 | void uartMicro(void){ |
jelord | 1:9fbb5b665068 | 51 | |
jelord | 2:88fcbfadec6a | 52 | haltBLE = 2; |
jelord | 2:88fcbfadec6a | 53 | //if(transmissionValue==4)pc.printf("DEBUG"); |
jelord | 0:2cb2b2ea316f | 54 | while(microChannel.readable()){ |
jelord | 0:2cb2b2ea316f | 55 | rx_buf[rx_len++] = microChannel.getc(); |
jelord | 2:88fcbfadec6a | 56 | |
jelord | 2:88fcbfadec6a | 57 | //Code block to verify what is being transmitted. To function correctly, all data must terminate with \0 or \n |
jelord | 2:88fcbfadec6a | 58 | if(transmissionValue==0){ |
jelord | 2:88fcbfadec6a | 59 | |
jelord | 2:88fcbfadec6a | 60 | if (rx_buf[0] == 0x01)transmissionValue = 1; //rtc |
jelord | 2:88fcbfadec6a | 61 | else if(rx_buf[0] == 0x02)transmissionValue = 2; //sample start and end times |
jelord | 2:88fcbfadec6a | 62 | else if(rx_buf[0] == 0x03)transmissionValue = 3; //sample name |
jelord | 2:88fcbfadec6a | 63 | else if(rx_buf[0] == 0x04)transmissionValue = 4; //Run Check |
jelord | 2:88fcbfadec6a | 64 | |
jelord | 2:88fcbfadec6a | 65 | else if(rx_buf[0] == 0x05)transmissionValue = 5; //log interval |
jelord | 2:88fcbfadec6a | 66 | else if(rx_buf[0] == 0x06)transmissionValue = 6; //Flow Rate |
jelord | 2:88fcbfadec6a | 67 | else if(rx_buf[0] == 0x07)transmissionValue = 7; //Serial Number |
jelord | 2:88fcbfadec6a | 68 | else transmissionValue = 100; //Not useful data |
jelord | 2:88fcbfadec6a | 69 | } |
jelord | 2:88fcbfadec6a | 70 | |
jelord | 2:88fcbfadec6a | 71 | if(rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n' || rx_buf[rx_len-1] == 0xff){ |
jelord | 2:88fcbfadec6a | 72 | if((transmissionValue == 1 || transmissionValue == 2 || transmissionValue == 3 || transmissionValue == 4 || transmissionValue == 5 || |
jelord | 2:88fcbfadec6a | 73 | transmissionValue == 6 || transmissionValue == 7) && rx_buf[rx_len-1] != 0xff) |
jelord | 2:88fcbfadec6a | 74 | {}else{ |
jelord | 2:88fcbfadec6a | 75 | if(transmissionValue == 4)sendData(); |
jelord | 2:88fcbfadec6a | 76 | haltBLE = 1; |
jelord | 2:88fcbfadec6a | 77 | transmissionValue = 0; |
jelord | 2:88fcbfadec6a | 78 | dataLength = 0; |
jelord | 2:88fcbfadec6a | 79 | } |
jelord | 1:9fbb5b665068 | 80 | } |
jelord | 0:2cb2b2ea316f | 81 | } |
jelord | 1:9fbb5b665068 | 82 | if(haltBLE!=1){ |
jelord | 2:88fcbfadec6a | 83 | |
jelord | 2:88fcbfadec6a | 84 | if((transmissionValue!=100) && (dataLength!= 0)) writeData[dataLength-1] = rx_buf[0]; |
jelord | 2:88fcbfadec6a | 85 | |
jelord | 2:88fcbfadec6a | 86 | if(transmissionValue ==100){ |
jelord | 2:88fcbfadec6a | 87 | pc.putc(rx_buf[0]); |
jelord | 2:88fcbfadec6a | 88 | |
jelord | 2:88fcbfadec6a | 89 | }else if(transmissionValue ==1){ //process and store RTC values |
jelord | 2:88fcbfadec6a | 90 | |
jelord | 2:88fcbfadec6a | 91 | // if(dataLength==6)RTC.set_time(writeData[0],writeData[1],writeData[2],writeData[3],writeData[3],writeData[4],writeData[5]); |
jelord | 2:88fcbfadec6a | 92 | |
jelord | 2:88fcbfadec6a | 93 | }else if(transmissionValue ==2){ //process and store sample start/end |
jelord | 2:88fcbfadec6a | 94 | if(dataLength ==12)E2PROM.write(0x00015, writeData, 12); |
jelord | 2:88fcbfadec6a | 95 | |
jelord | 2:88fcbfadec6a | 96 | }else if(transmissionValue ==3){ //process and store sample name |
jelord | 2:88fcbfadec6a | 97 | if(dataLength ==8)E2PROM.write(0x00001,writeData,8); |
jelord | 2:88fcbfadec6a | 98 | }else if(transmissionValue ==4){ //process and store Run Check |
jelord | 2:88fcbfadec6a | 99 | //if(dataLength==1)E2PROM.write(0x00033,writeData,1); |
jelord | 2:88fcbfadec6a | 100 | |
jelord | 2:88fcbfadec6a | 101 | |
jelord | 2:88fcbfadec6a | 102 | |
jelord | 2:88fcbfadec6a | 103 | }else if(transmissionValue ==5){ //process and store Log Interval |
jelord | 2:88fcbfadec6a | 104 | if(dataLength ==1)E2PROM.write(0x00014,writeData,1); |
jelord | 2:88fcbfadec6a | 105 | |
jelord | 2:88fcbfadec6a | 106 | }else if(transmissionValue ==6){ //process and store Flow Rate |
jelord | 2:88fcbfadec6a | 107 | if(dataLength ==4)E2PROM.write(0x00010,writeData,4); |
jelord | 2:88fcbfadec6a | 108 | |
jelord | 2:88fcbfadec6a | 109 | }else if(transmissionValue ==7){ //process and store Serial Number |
jelord | 2:88fcbfadec6a | 110 | if(dataLength ==2)E2PROM.write(0x00034,writeData,2); |
jelord | 1:9fbb5b665068 | 111 | } |
jelord | 2:88fcbfadec6a | 112 | dataLength++; |
jelord | 0:2cb2b2ea316f | 113 | } |
jelord | 2:88fcbfadec6a | 114 | // |
jelord | 0:2cb2b2ea316f | 115 | rx_len = 0; |
jelord | 2:88fcbfadec6a | 116 | |
jelord | 2:88fcbfadec6a | 117 | } |
jelord | 2:88fcbfadec6a | 118 | void sendData(){ |
jelord | 2:88fcbfadec6a | 119 | |
jelord | 2:88fcbfadec6a | 120 | uint8_t sampleTimePassValues[13] = {0x01,}; |
jelord | 2:88fcbfadec6a | 121 | uint8_t subjectLabelOriginal[9] = {0x02,}; |
jelord | 2:88fcbfadec6a | 122 | uint8_t dataLogOriginal[2] = {0x03,}; |
jelord | 2:88fcbfadec6a | 123 | uint8_t flowRateOriginal[5] = {0x04,}; |
jelord | 2:88fcbfadec6a | 124 | //uint8_t presetRunModeCheck[1] = {0,}; Commented and currently unused to prevent mem issues |
jelord | 2:88fcbfadec6a | 125 | E2PROM.read(0x00015, sampleTimePassValues+1, 12); |
jelord | 2:88fcbfadec6a | 126 | E2PROM.read(0x00001, subjectLabelOriginal+1,8); |
jelord | 2:88fcbfadec6a | 127 | E2PROM.read(0x00014,dataLogOriginal+1,1); |
jelord | 2:88fcbfadec6a | 128 | E2PROM.read(0x00010,flowRateOriginal+1,4); |
jelord | 2:88fcbfadec6a | 129 | |
jelord | 2:88fcbfadec6a | 130 | for(int i=0; i<13; i++){ |
jelord | 2:88fcbfadec6a | 131 | microChannel.putc(sampleTimePassValues[i]); |
jelord | 2:88fcbfadec6a | 132 | } |
jelord | 2:88fcbfadec6a | 133 | wait(.25); |
jelord | 2:88fcbfadec6a | 134 | |
jelord | 2:88fcbfadec6a | 135 | for(int i=0; i<9; i++){ |
jelord | 2:88fcbfadec6a | 136 | microChannel.putc(subjectLabelOriginal[i]); |
jelord | 2:88fcbfadec6a | 137 | } |
jelord | 2:88fcbfadec6a | 138 | wait(.25); |
jelord | 2:88fcbfadec6a | 139 | |
jelord | 2:88fcbfadec6a | 140 | for(int i=0; i<2; i++){ |
jelord | 2:88fcbfadec6a | 141 | microChannel.putc(dataLogOriginal[i]); |
jelord | 2:88fcbfadec6a | 142 | } |
jelord | 2:88fcbfadec6a | 143 | wait(.25); |
jelord | 2:88fcbfadec6a | 144 | |
jelord | 2:88fcbfadec6a | 145 | for(int i=0; i<5; i++){ |
jelord | 2:88fcbfadec6a | 146 | microChannel.putc(flowRateOriginal[i]); |
jelord | 2:88fcbfadec6a | 147 | } |
jelord | 2:88fcbfadec6a | 148 | //pc.printf("End of data"); |
jelord | 0:2cb2b2ea316f | 149 | |
jelord | 2:88fcbfadec6a | 150 | |
jelord | 2:88fcbfadec6a | 151 | } |
jelord | 1:9fbb5b665068 | 152 | |
jelord | 1:9fbb5b665068 | 153 | int main(){ |
jelord | 2:88fcbfadec6a | 154 | |
jelord | 1:9fbb5b665068 | 155 | pc.baud(115200); // set what you want here depending on your terminal program speed |
jelord | 1:9fbb5b665068 | 156 | pc.printf("\f\n\r-------------Startup-------------\n\r"); |
jelord | 1:9fbb5b665068 | 157 | wait(0.5); |
jelord | 1:9fbb5b665068 | 158 | timeout=2; |
jelord | 2:88fcbfadec6a | 159 | |
jelord | 1:9fbb5b665068 | 160 | pc.attach(pc_recv); |
jelord | 1:9fbb5b665068 | 161 | microChannel.attach(uartMicro,microChannel.RxIrq); |
jelord | 1:9fbb5b665068 | 162 | microChannel.baud(115200); // change this to the new ESP8266 baudrate if it is changed at any time. |
jelord | 0:2cb2b2ea316f | 163 | |
jelord | 1:9fbb5b665068 | 164 | //uint8_t tempBuf[20] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10,0x11,0x12,0x13}; |
jelord | 1:9fbb5b665068 | 165 | microChannel.printf("$$$"); |
jelord | 1:9fbb5b665068 | 166 | wait(1); |
jelord | 2:88fcbfadec6a | 167 | microChannel.printf("SN, JakeMicroX\r"); |
jelord | 1:9fbb5b665068 | 168 | wait(1); |
jelord | 1:9fbb5b665068 | 169 | microChannel.printf("A\r"); |
jelord | 1:9fbb5b665068 | 170 | wait(1); |
jelord | 2:88fcbfadec6a | 171 | microChannel.printf("---\r"); |
jelord | 2:88fcbfadec6a | 172 | wait(2); |
jelord | 2:88fcbfadec6a | 173 | |
jelord | 2:88fcbfadec6a | 174 | |
jelord | 2:88fcbfadec6a | 175 | |
jelord | 2:88fcbfadec6a | 176 | |
jelord | 2:88fcbfadec6a | 177 | RGB_LED.set_led(1,1,1); |
jelord | 1:9fbb5b665068 | 178 | while(1) { |
jelord | 2:88fcbfadec6a | 179 | for(int i=0;i<10000000;i++); |
jelord | 0:2cb2b2ea316f | 180 | } |
jelord | 0:2cb2b2ea316f | 181 | } |