![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
t
Dependencies: SDFileSystem mbed
Fork of SDFileSystem_HelloWorld by
main.cpp@1:f793eace78a5, 2017-11-10 (annotated)
- Committer:
- amithy
- Date:
- Fri Nov 10 01:01:04 2017 +0000
- Revision:
- 1:f793eace78a5
- Parent:
- 0:bdbd3d6fc5d5
for testing export
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
amithy | 1:f793eace78a5 | 1 | #define LOGDELAY 10 |
amithy | 1:f793eace78a5 | 2 | |
mbed_official | 0:bdbd3d6fc5d5 | 3 | #include "mbed.h" |
amithy | 1:f793eace78a5 | 4 | #include "BufferedSerial.h" |
mbed_official | 0:bdbd3d6fc5d5 | 5 | #include "SDFileSystem.h" |
amithy | 1:f793eace78a5 | 6 | #include "ble/BLE.h" |
amithy | 1:f793eace78a5 | 7 | #include "ble/services/DeviceInformationService.h" |
amithy | 1:f793eace78a5 | 8 | #include "nrf_soc.h" // for internal Thermo sensor |
amithy | 1:f793eace78a5 | 9 | #include "nrf51_rtc.h" |
amithy | 1:f793eace78a5 | 10 | #include "Dht22.h" |
amithy | 1:f793eace78a5 | 11 | #include <string> |
amithy | 1:f793eace78a5 | 12 | |
amithy | 1:f793eace78a5 | 13 | //Serial pc(USBTX, USBRX); |
amithy | 1:f793eace78a5 | 14 | |
amithy | 1:f793eace78a5 | 15 | |
amithy | 1:f793eace78a5 | 16 | char DEVICE_NAME[] = "IAQ"; //enter device name |
amithy | 1:f793eace78a5 | 17 | const static char datapath[] = "/sd/datalog.txt"; |
amithy | 1:f793eace78a5 | 18 | Ticker wakeUp; |
amithy | 1:f793eace78a5 | 19 | char hpmbuffer[10]; |
amithy | 1:f793eace78a5 | 20 | bool triggered = true; |
amithy | 1:f793eace78a5 | 21 | |
amithy | 1:f793eace78a5 | 22 | /* |
amithy | 1:f793eace78a5 | 23 | // Sensen Pin Configuration |
amithy | 1:f793eace78a5 | 24 | */ |
amithy | 1:f793eace78a5 | 25 | |
amithy | 1:f793eace78a5 | 26 | SDFileSystem sd(p25, p28, p29, p23, "sd");//Create an SDFileSystem object mosi, miso, sclk |
amithy | 1:f793eace78a5 | 27 | I2C i2c(p30 , p7); //sda, scl |
amithy | 1:f793eace78a5 | 28 | DigitalOut adp(p21); |
amithy | 1:f793eace78a5 | 29 | DigitalOut sample_led(p17); //yellow - sampling |
amithy | 1:f793eace78a5 | 30 | DigitalOut status_led(p18); //red - status |
amithy | 1:f793eace78a5 | 31 | |
amithy | 1:f793eace78a5 | 32 | |
amithy | 1:f793eace78a5 | 33 | |
amithy | 1:f793eace78a5 | 34 | /* |
amithy | 1:f793eace78a5 | 35 | // Sensor Configuration |
amithy | 1:f793eace78a5 | 36 | */ |
amithy | 1:f793eace78a5 | 37 | |
amithy | 1:f793eace78a5 | 38 | //analog sensors |
amithy | 1:f793eace78a5 | 39 | AnalogIn spec(p1); |
amithy | 1:f793eace78a5 | 40 | AnalogIn figCO(p2); |
amithy | 1:f793eace78a5 | 41 | AnalogIn sharpVOut(p3); |
amithy | 1:f793eace78a5 | 42 | |
amithy | 1:f793eace78a5 | 43 | //digital sensors |
amithy | 1:f793eace78a5 | 44 | BufferedSerial hpm(p9,p11); //tx, rx |
amithy | 1:f793eace78a5 | 45 | //SoftwareSerial plantower(p10, p12); |
amithy | 1:f793eace78a5 | 46 | DigitalOut sharpLED(p13); //low = on |
amithy | 1:f793eace78a5 | 47 | DigitalOut plantowerReset(p10); |
amithy | 1:f793eace78a5 | 48 | DigitalOut plantowerSet(p12); |
amithy | 1:f793eace78a5 | 49 | Dht22 dht(p14); |
amithy | 1:f793eace78a5 | 50 | const int figCO2 = 0x69 << 1; // 8bit I2C address, 0x69 for figaro CO2 |
amithy | 1:f793eace78a5 | 51 | const int DS1307 = 0x68 << 1; |
amithy | 1:f793eace78a5 | 52 | |
amithy | 1:f793eace78a5 | 53 | |
amithy | 1:f793eace78a5 | 54 | /* |
amithy | 1:f793eace78a5 | 55 | // Global variables for data |
amithy | 1:f793eace78a5 | 56 | */ |
amithy | 1:f793eace78a5 | 57 | char rtcbuff[8]; |
amithy | 1:f793eace78a5 | 58 | float dataBuffer[30]; |
amithy | 1:f793eace78a5 | 59 | uint32_t timeNow = 0; |
amithy | 1:f793eace78a5 | 60 | uint32_t wakeTime = 0; |
amithy | 1:f793eace78a5 | 61 | float sharpPM = 0.0; |
amithy | 1:f793eace78a5 | 62 | float specCO = 0.0; |
amithy | 1:f793eace78a5 | 63 | float figaroCO = 0.0; |
amithy | 1:f793eace78a5 | 64 | int dhtTemp = 0; |
amithy | 1:f793eace78a5 | 65 | int dhtHum = 0; |
amithy | 1:f793eace78a5 | 66 | int figaroCO2 = 0; |
amithy | 1:f793eace78a5 | 67 | int honeywellPM2_5 = 0; |
amithy | 1:f793eace78a5 | 68 | int honeywellPM10 = 0; |
amithy | 1:f793eace78a5 | 69 | int plantowerPM1 = 0; |
amithy | 1:f793eace78a5 | 70 | int plantowerPM2_5 = 0; |
amithy | 1:f793eace78a5 | 71 | int plantowerPM10 = 0; |
amithy | 1:f793eace78a5 | 72 | int32_t p_temp = 0; |
amithy | 1:f793eace78a5 | 73 | float temperature = 0; |
amithy | 1:f793eace78a5 | 74 | |
amithy | 1:f793eace78a5 | 75 | /* |
amithy | 1:f793eace78a5 | 76 | // I2C functions |
amithy | 1:f793eace78a5 | 77 | */ |
amithy | 1:f793eace78a5 | 78 | |
amithy | 1:f793eace78a5 | 79 | void writeReg(int addr8bit, char reg, char val) |
amithy | 1:f793eace78a5 | 80 | { |
amithy | 1:f793eace78a5 | 81 | char tempData[2]; |
amithy | 1:f793eace78a5 | 82 | tempData[0] = reg; |
amithy | 1:f793eace78a5 | 83 | tempData[1] = val; |
amithy | 1:f793eace78a5 | 84 | i2c.write(addr8bit, tempData, 2); |
amithy | 1:f793eace78a5 | 85 | } |
amithy | 1:f793eace78a5 | 86 | |
amithy | 1:f793eace78a5 | 87 | void readReg(int addr8bit, uint8_t reg) |
amithy | 1:f793eace78a5 | 88 | { |
amithy | 1:f793eace78a5 | 89 | char tempData[2]; |
amithy | 1:f793eace78a5 | 90 | tempData[0] = reg; |
amithy | 1:f793eace78a5 | 91 | i2c.write(addr8bit, &tempData[0], 1, true); |
amithy | 1:f793eace78a5 | 92 | i2c.read(addr8bit, &tempData[1], 1, false); |
amithy | 1:f793eace78a5 | 93 | } |
amithy | 1:f793eace78a5 | 94 | |
amithy | 1:f793eace78a5 | 95 | int twosComp( char bit_msb, char bit_lsb) |
amithy | 1:f793eace78a5 | 96 | { |
amithy | 1:f793eace78a5 | 97 | int16_t myInt=0; |
amithy | 1:f793eace78a5 | 98 | myInt = (bit_msb << 8) | (bit_lsb & 0xff); |
amithy | 1:f793eace78a5 | 99 | return myInt; |
amithy | 1:f793eace78a5 | 100 | } |
amithy | 1:f793eace78a5 | 101 | |
amithy | 1:f793eace78a5 | 102 | |
amithy | 1:f793eace78a5 | 103 | /* |
amithy | 1:f793eace78a5 | 104 | // Sensor interface functions |
amithy | 1:f793eace78a5 | 105 | */ |
amithy | 1:f793eace78a5 | 106 | |
amithy | 1:f793eace78a5 | 107 | void getDHT() |
amithy | 1:f793eace78a5 | 108 | { |
amithy | 1:f793eace78a5 | 109 | dht.read(); |
amithy | 1:f793eace78a5 | 110 | dhtTemp = dht.getCelsius(); |
amithy | 1:f793eace78a5 | 111 | dhtHum = dht.getHumidity(); |
amithy | 1:f793eace78a5 | 112 | } |
amithy | 1:f793eace78a5 | 113 | |
amithy | 1:f793eace78a5 | 114 | void getFigaroCO2() |
amithy | 1:f793eace78a5 | 115 | { |
amithy | 1:f793eace78a5 | 116 | char readme_msb, readme_lsb; |
amithy | 1:f793eace78a5 | 117 | |
amithy | 1:f793eace78a5 | 118 | char reg = 0x03; |
amithy | 1:f793eace78a5 | 119 | i2c.write(figCO2, ®, 1, true); |
amithy | 1:f793eace78a5 | 120 | i2c.read(figCO2, &readme_lsb, 1, false); |
amithy | 1:f793eace78a5 | 121 | |
amithy | 1:f793eace78a5 | 122 | reg = 0x04; |
amithy | 1:f793eace78a5 | 123 | i2c.write(figCO2, ®, 1, true); |
amithy | 1:f793eace78a5 | 124 | i2c.read(figCO2, &readme_msb, 1, false); |
amithy | 1:f793eace78a5 | 125 | |
amithy | 1:f793eace78a5 | 126 | figaroCO2 = twosComp(readme_msb,readme_lsb); |
amithy | 1:f793eace78a5 | 127 | } |
amithy | 1:f793eace78a5 | 128 | |
amithy | 1:f793eace78a5 | 129 | void getSharpPM() |
amithy | 1:f793eace78a5 | 130 | { |
amithy | 1:f793eace78a5 | 131 | sharpVOut.read(); |
amithy | 1:f793eace78a5 | 132 | wait_ms(5); |
amithy | 1:f793eace78a5 | 133 | sharpPM = 0.0; |
amithy | 1:f793eace78a5 | 134 | for(int j = 0; j<5; j++) { |
amithy | 1:f793eace78a5 | 135 | sharpLED = 0; |
amithy | 1:f793eace78a5 | 136 | wait_us(280); |
amithy | 1:f793eace78a5 | 137 | sharpPM += sharpVOut.read(); |
amithy | 1:f793eace78a5 | 138 | wait_us(40); |
amithy | 1:f793eace78a5 | 139 | sharpLED = 1; |
amithy | 1:f793eace78a5 | 140 | wait_us(9680); |
amithy | 1:f793eace78a5 | 141 | } |
amithy | 1:f793eace78a5 | 142 | sharpPM = sharpPM/5.0; |
amithy | 1:f793eace78a5 | 143 | } |
amithy | 1:f793eace78a5 | 144 | |
amithy | 1:f793eace78a5 | 145 | void getSpecCO() |
amithy | 1:f793eace78a5 | 146 | { |
amithy | 1:f793eace78a5 | 147 | spec.read(); |
amithy | 1:f793eace78a5 | 148 | wait_ms(5); |
amithy | 1:f793eace78a5 | 149 | specCO = 0.0; |
amithy | 1:f793eace78a5 | 150 | for(int i=0; i<128; i++) |
amithy | 1:f793eace78a5 | 151 | { |
amithy | 1:f793eace78a5 | 152 | specCO += spec.read(); |
amithy | 1:f793eace78a5 | 153 | wait_ms(2); |
amithy | 1:f793eace78a5 | 154 | } |
amithy | 1:f793eace78a5 | 155 | specCO = specCO/128.0; |
amithy | 1:f793eace78a5 | 156 | } |
amithy | 1:f793eace78a5 | 157 | |
amithy | 1:f793eace78a5 | 158 | void getFigaroCO() |
amithy | 1:f793eace78a5 | 159 | { |
amithy | 1:f793eace78a5 | 160 | figCO.read(); |
amithy | 1:f793eace78a5 | 161 | wait_ms(5); |
amithy | 1:f793eace78a5 | 162 | figaroCO = 0.0; |
amithy | 1:f793eace78a5 | 163 | //for(int i=0; i<128; i++) |
amithy | 1:f793eace78a5 | 164 | //{ |
amithy | 1:f793eace78a5 | 165 | figaroCO = figCO.read(); |
amithy | 1:f793eace78a5 | 166 | // wait_ms(2); |
amithy | 1:f793eace78a5 | 167 | //} |
amithy | 1:f793eace78a5 | 168 | //figaroCO = figaroCO/128.0; |
amithy | 1:f793eace78a5 | 169 | } |
amithy | 1:f793eace78a5 | 170 | |
amithy | 1:f793eace78a5 | 171 | void getHPM() |
amithy | 1:f793eace78a5 | 172 | { |
amithy | 1:f793eace78a5 | 173 | for(int i=0; i<8; i++) { |
amithy | 1:f793eace78a5 | 174 | hpmbuffer[i]=1; |
amithy | 1:f793eace78a5 | 175 | } |
amithy | 1:f793eace78a5 | 176 | for(int i=0; i<100; i++) { |
amithy | 1:f793eace78a5 | 177 | if(hpm.getc()==0x42) { |
amithy | 1:f793eace78a5 | 178 | if(hpm.getc()==0x4d) { |
amithy | 1:f793eace78a5 | 179 | for(int i=0; i<8; i++) { |
amithy | 1:f793eace78a5 | 180 | hpmbuffer[i]=hpm.getc(); |
amithy | 1:f793eace78a5 | 181 | } |
amithy | 1:f793eace78a5 | 182 | honeywellPM2_5 = twosComp(hpmbuffer[4],hpmbuffer[5]); |
amithy | 1:f793eace78a5 | 183 | honeywellPM10 = twosComp(hpmbuffer[6],hpmbuffer[7]); |
amithy | 1:f793eace78a5 | 184 | break; |
amithy | 1:f793eace78a5 | 185 | } |
amithy | 1:f793eace78a5 | 186 | } |
amithy | 1:f793eace78a5 | 187 | } |
amithy | 1:f793eace78a5 | 188 | } |
amithy | 1:f793eace78a5 | 189 | |
amithy | 1:f793eace78a5 | 190 | void getTime() |
amithy | 1:f793eace78a5 | 191 | { |
amithy | 1:f793eace78a5 | 192 | char readreg; |
amithy | 1:f793eace78a5 | 193 | |
amithy | 1:f793eace78a5 | 194 | char reg = 0x00; |
amithy | 1:f793eace78a5 | 195 | i2c.write(DS1307, ®, 1, true); |
amithy | 1:f793eace78a5 | 196 | i2c.read(DS1307, &readreg, 1, false); |
amithy | 1:f793eace78a5 | 197 | rtcbuff[0] = readreg - 6 * (readreg >> 4);; |
amithy | 1:f793eace78a5 | 198 | |
amithy | 1:f793eace78a5 | 199 | reg = 0x01; |
amithy | 1:f793eace78a5 | 200 | i2c.write(DS1307, ®, 1, true); |
amithy | 1:f793eace78a5 | 201 | i2c.read(DS1307, &readreg, 1, false); |
amithy | 1:f793eace78a5 | 202 | rtcbuff[1] = readreg - 6 * (readreg >> 4);; |
amithy | 1:f793eace78a5 | 203 | |
amithy | 1:f793eace78a5 | 204 | reg = 0x02; |
amithy | 1:f793eace78a5 | 205 | i2c.write(DS1307, ®, 1, true); |
amithy | 1:f793eace78a5 | 206 | i2c.read(DS1307, &readreg, 1, false); |
amithy | 1:f793eace78a5 | 207 | rtcbuff[2] = readreg - 6 * (readreg >> 4);; |
amithy | 1:f793eace78a5 | 208 | |
amithy | 1:f793eace78a5 | 209 | reg = 0x03; |
amithy | 1:f793eace78a5 | 210 | i2c.write(DS1307, ®, 1, true); |
amithy | 1:f793eace78a5 | 211 | i2c.read(DS1307, &readreg, 1, false); |
amithy | 1:f793eace78a5 | 212 | rtcbuff[3] = readreg - 6 * (readreg >> 4);; |
amithy | 1:f793eace78a5 | 213 | |
amithy | 1:f793eace78a5 | 214 | reg = 0x04; |
amithy | 1:f793eace78a5 | 215 | i2c.write(DS1307, ®, 1, true); |
amithy | 1:f793eace78a5 | 216 | i2c.read(DS1307, &readreg, 1, false); |
amithy | 1:f793eace78a5 | 217 | rtcbuff[4] = readreg - 6 * (readreg >> 4);; |
amithy | 1:f793eace78a5 | 218 | |
amithy | 1:f793eace78a5 | 219 | reg = 0x05; |
amithy | 1:f793eace78a5 | 220 | i2c.write(DS1307, ®, 1, true); |
amithy | 1:f793eace78a5 | 221 | i2c.read(DS1307, &readreg, 1, false); |
amithy | 1:f793eace78a5 | 222 | rtcbuff[5] = readreg - 6 * (readreg >> 4);; |
amithy | 1:f793eace78a5 | 223 | |
amithy | 1:f793eace78a5 | 224 | reg = 0x06; |
amithy | 1:f793eace78a5 | 225 | i2c.write(DS1307, ®, 1, true); |
amithy | 1:f793eace78a5 | 226 | i2c.read(DS1307, &readreg, 1, false); |
amithy | 1:f793eace78a5 | 227 | rtcbuff[6] = readreg - 6 * (readreg >> 4);; |
amithy | 1:f793eace78a5 | 228 | |
amithy | 1:f793eace78a5 | 229 | struct tm t; |
amithy | 1:f793eace78a5 | 230 | |
amithy | 1:f793eace78a5 | 231 | t.tm_year = (int)rtcbuff[6]+100; |
amithy | 1:f793eace78a5 | 232 | t.tm_mon = (int)rtcbuff[5]-1; // Month, 0 - jan |
amithy | 1:f793eace78a5 | 233 | t.tm_mday = (int)rtcbuff[4]; // Day of the month |
amithy | 1:f793eace78a5 | 234 | t.tm_hour = (int)rtcbuff[2]; |
amithy | 1:f793eace78a5 | 235 | t.tm_min = (int)rtcbuff[1]; |
amithy | 1:f793eace78a5 | 236 | t.tm_sec = (int)rtcbuff[0]; |
amithy | 1:f793eace78a5 | 237 | t.tm_isdst = 0; // Is DST on? 1 = yes, 0 = no, -1 = unknown |
amithy | 1:f793eace78a5 | 238 | timeNow = mktime(&t); |
amithy | 1:f793eace78a5 | 239 | } |
amithy | 1:f793eace78a5 | 240 | |
amithy | 1:f793eace78a5 | 241 | |
amithy | 1:f793eace78a5 | 242 | void getData() |
amithy | 1:f793eace78a5 | 243 | { |
amithy | 1:f793eace78a5 | 244 | getSharpPM(); |
amithy | 1:f793eace78a5 | 245 | wait(0.25); |
amithy | 1:f793eace78a5 | 246 | |
amithy | 1:f793eace78a5 | 247 | getSpecCO(); |
amithy | 1:f793eace78a5 | 248 | wait(0.25); |
amithy | 1:f793eace78a5 | 249 | |
amithy | 1:f793eace78a5 | 250 | getFigaroCO(); |
amithy | 1:f793eace78a5 | 251 | wait(0.25); |
mbed_official | 0:bdbd3d6fc5d5 | 252 | |
amithy | 1:f793eace78a5 | 253 | getFigaroCO2(); |
amithy | 1:f793eace78a5 | 254 | wait(0.25); |
amithy | 1:f793eace78a5 | 255 | |
amithy | 1:f793eace78a5 | 256 | getHPM(); |
amithy | 1:f793eace78a5 | 257 | for(int i = 0; i<5; i++) |
amithy | 1:f793eace78a5 | 258 | { |
amithy | 1:f793eace78a5 | 259 | if (honeywellPM2_5 == 0 && honeywellPM10 == 0) |
amithy | 1:f793eace78a5 | 260 | { |
amithy | 1:f793eace78a5 | 261 | wait(0.25); |
amithy | 1:f793eace78a5 | 262 | getHPM(); |
amithy | 1:f793eace78a5 | 263 | } |
amithy | 1:f793eace78a5 | 264 | } |
amithy | 1:f793eace78a5 | 265 | wait(0.25); |
amithy | 1:f793eace78a5 | 266 | |
amithy | 1:f793eace78a5 | 267 | getDHT(); |
amithy | 1:f793eace78a5 | 268 | if (dhtTemp == 0) |
amithy | 1:f793eace78a5 | 269 | { |
amithy | 1:f793eace78a5 | 270 | wait(5); |
amithy | 1:f793eace78a5 | 271 | getDHT(); |
amithy | 1:f793eace78a5 | 272 | } |
amithy | 1:f793eace78a5 | 273 | wait(0.25); |
amithy | 1:f793eace78a5 | 274 | |
amithy | 1:f793eace78a5 | 275 | |
amithy | 1:f793eace78a5 | 276 | } |
amithy | 1:f793eace78a5 | 277 | |
amithy | 1:f793eace78a5 | 278 | |
amithy | 1:f793eace78a5 | 279 | |
amithy | 1:f793eace78a5 | 280 | |
amithy | 1:f793eace78a5 | 281 | |
amithy | 1:f793eace78a5 | 282 | |
amithy | 1:f793eace78a5 | 283 | |
amithy | 1:f793eace78a5 | 284 | /* |
amithy | 1:f793eace78a5 | 285 | // SD functions |
amithy | 1:f793eace78a5 | 286 | */ |
amithy | 1:f793eace78a5 | 287 | |
amithy | 1:f793eace78a5 | 288 | void sd_begin() |
amithy | 1:f793eace78a5 | 289 | { |
amithy | 1:f793eace78a5 | 290 | DigitalOut mosi(p25); //out |
amithy | 1:f793eace78a5 | 291 | DigitalIn miso(p28); //in |
amithy | 1:f793eace78a5 | 292 | miso.mode(PullUp); |
amithy | 1:f793eace78a5 | 293 | DigitalOut sclk(p29); //out |
amithy | 1:f793eace78a5 | 294 | DigitalOut cs(p23,1); //out |
amithy | 1:f793eace78a5 | 295 | |
amithy | 1:f793eace78a5 | 296 | plantowerReset = 1; |
amithy | 1:f793eace78a5 | 297 | plantowerSet = 1; |
amithy | 1:f793eace78a5 | 298 | adp = 1; //turn on ADP194 |
amithy | 1:f793eace78a5 | 299 | sharpLED = 1; |
amithy | 1:f793eace78a5 | 300 | status_led=1; //turn on status led - red |
amithy | 1:f793eace78a5 | 301 | wait(0.25); |
amithy | 1:f793eace78a5 | 302 | } |
amithy | 1:f793eace78a5 | 303 | |
amithy | 1:f793eace78a5 | 304 | |
amithy | 1:f793eace78a5 | 305 | void sd_sleep() |
amithy | 1:f793eace78a5 | 306 | { |
amithy | 1:f793eace78a5 | 307 | wait(0.25); |
amithy | 1:f793eace78a5 | 308 | status_led=0; //turn off status led - red |
amithy | 1:f793eace78a5 | 309 | adp = 0; //turn off ADP194 |
amithy | 1:f793eace78a5 | 310 | sharpLED = 0; |
amithy | 1:f793eace78a5 | 311 | //set all SPI as outputs pulled down |
amithy | 1:f793eace78a5 | 312 | DigitalOut mosi(p25,0); |
amithy | 1:f793eace78a5 | 313 | DigitalOut miso(p28,0); |
amithy | 1:f793eace78a5 | 314 | DigitalOut sclk(p29,0); |
amithy | 1:f793eace78a5 | 315 | DigitalOut cs(p23,0); |
amithy | 1:f793eace78a5 | 316 | } |
amithy | 1:f793eace78a5 | 317 | |
amithy | 1:f793eace78a5 | 318 | void sd_header_write()//Write header sd card |
amithy | 1:f793eace78a5 | 319 | { |
amithy | 1:f793eace78a5 | 320 | sd.mount();//Mount the filesystem |
amithy | 1:f793eace78a5 | 321 | wait(0.25);//wait for stabilization |
amithy | 1:f793eace78a5 | 322 | |
amithy | 1:f793eace78a5 | 323 | FILE *fp = fopen(datapath, "a"); //will overwrite any existing datalog.txt file |
amithy | 1:f793eace78a5 | 324 | if (fp != NULL) { |
amithy | 1:f793eace78a5 | 325 | fprintf(fp,"Time,PM2_5,PM10,sharpPM,dhtTemp,dhtHum,specCO,figaroCO,figaroCO2\r\n"); |
amithy | 1:f793eace78a5 | 326 | wait(0.25); //seconds |
amithy | 1:f793eace78a5 | 327 | } |
amithy | 1:f793eace78a5 | 328 | fclose(fp); |
amithy | 1:f793eace78a5 | 329 | sd.unmount();//Unmount the filesystem |
amithy | 1:f793eace78a5 | 330 | } |
amithy | 1:f793eace78a5 | 331 | |
amithy | 1:f793eace78a5 | 332 | void saveData() |
amithy | 1:f793eace78a5 | 333 | { |
amithy | 1:f793eace78a5 | 334 | sd.mount();//Mount the filesystem |
amithy | 1:f793eace78a5 | 335 | wait(0.25);//wait for stabilization |
amithy | 1:f793eace78a5 | 336 | |
amithy | 1:f793eace78a5 | 337 | FILE *fp = fopen(datapath, "a"); //will overwrite any existing datalog.txt file |
amithy | 1:f793eace78a5 | 338 | if (fp != NULL) { |
amithy | 1:f793eace78a5 | 339 | //fprintf(fp,"%d,%.4f\r\n",temperature); |
amithy | 1:f793eace78a5 | 340 | fprintf(fp,"%d,%d,%d,%.4f,%d,%d,%.4f,%.4f,%d\r\n",timeNow,honeywellPM2_5,honeywellPM10,sharpPM,dhtTemp,dhtHum,specCO,figaroCO,figaroCO2); |
amithy | 1:f793eace78a5 | 341 | wait(0.25); //seconds |
amithy | 1:f793eace78a5 | 342 | } |
amithy | 1:f793eace78a5 | 343 | fclose(fp); |
amithy | 1:f793eace78a5 | 344 | sd.unmount();//Unmount the filesystem |
amithy | 1:f793eace78a5 | 345 | |
amithy | 1:f793eace78a5 | 346 | } |
amithy | 1:f793eace78a5 | 347 | |
amithy | 1:f793eace78a5 | 348 | /* |
amithy | 1:f793eace78a5 | 349 | // Clock functions |
amithy | 1:f793eace78a5 | 350 | */ |
amithy | 1:f793eace78a5 | 351 | |
amithy | 1:f793eace78a5 | 352 | void checkTime() |
amithy | 1:f793eace78a5 | 353 | { |
amithy | 1:f793eace78a5 | 354 | timeNow = rtc.time(); |
amithy | 1:f793eace78a5 | 355 | if (timeNow > wakeTime) { |
amithy | 1:f793eace78a5 | 356 | triggered = true; |
mbed_official | 0:bdbd3d6fc5d5 | 357 | } |
amithy | 1:f793eace78a5 | 358 | //timeNow = rtc.time(); |
amithy | 1:f793eace78a5 | 359 | //wakeTime = timeNow + LOGDELAY; |
amithy | 1:f793eace78a5 | 360 | } |
amithy | 1:f793eace78a5 | 361 | |
amithy | 1:f793eace78a5 | 362 | |
amithy | 1:f793eace78a5 | 363 | |
amithy | 1:f793eace78a5 | 364 | |
amithy | 1:f793eace78a5 | 365 | |
amithy | 1:f793eace78a5 | 366 | int main(void) |
amithy | 1:f793eace78a5 | 367 | { |
amithy | 1:f793eace78a5 | 368 | /* |
amithy | 1:f793eace78a5 | 369 | //set time |
amithy | 1:f793eace78a5 | 370 | int val = 5; |
amithy | 1:f793eace78a5 | 371 | val = ( ( val / 10 ) * 0x10 ) + ( val % 10 ); |
amithy | 1:f793eace78a5 | 372 | writeReg(DS1307,0x00,val); |
amithy | 1:f793eace78a5 | 373 | |
amithy | 1:f793eace78a5 | 374 | val = 5; |
amithy | 1:f793eace78a5 | 375 | val = ( ( val / 10 ) * 0x10 ) + ( val % 10 ); |
amithy | 1:f793eace78a5 | 376 | writeReg(DS1307,0x01,val); |
amithy | 1:f793eace78a5 | 377 | |
amithy | 1:f793eace78a5 | 378 | val = 0; |
amithy | 1:f793eace78a5 | 379 | val = ( ( val / 10 ) * 0x10 ) + ( val % 10 ); |
amithy | 1:f793eace78a5 | 380 | writeReg(DS1307,0x02,val); |
amithy | 1:f793eace78a5 | 381 | |
amithy | 1:f793eace78a5 | 382 | val = 20; |
amithy | 1:f793eace78a5 | 383 | val = ( ( val / 10 ) * 0x10 ) + ( val % 10 ); |
amithy | 1:f793eace78a5 | 384 | writeReg(DS1307,0x03,val); |
amithy | 1:f793eace78a5 | 385 | |
amithy | 1:f793eace78a5 | 386 | val = 26; |
amithy | 1:f793eace78a5 | 387 | val = ( ( val / 10 ) * 0x10 ) + ( val % 10 ); |
amithy | 1:f793eace78a5 | 388 | writeReg(DS1307,0x04,val); |
amithy | 1:f793eace78a5 | 389 | |
amithy | 1:f793eace78a5 | 390 | val = 10; |
amithy | 1:f793eace78a5 | 391 | val = ( ( val / 10 ) * 0x10 ) + ( val % 10 ); |
amithy | 1:f793eace78a5 | 392 | writeReg(DS1307,0x05,val); |
amithy | 1:f793eace78a5 | 393 | |
amithy | 1:f793eace78a5 | 394 | val = 17; |
amithy | 1:f793eace78a5 | 395 | val = ( ( val / 10 ) * 0x10 ) + ( val % 10 ); |
amithy | 1:f793eace78a5 | 396 | writeReg(DS1307,0x06,val); |
amithy | 1:f793eace78a5 | 397 | |
amithy | 1:f793eace78a5 | 398 | while(1) |
amithy | 1:f793eace78a5 | 399 | { |
amithy | 1:f793eace78a5 | 400 | sample_led = !sample_led; |
amithy | 1:f793eace78a5 | 401 | wait(1); |
amithy | 1:f793eace78a5 | 402 | } |
amithy | 1:f793eace78a5 | 403 | |
amithy | 1:f793eace78a5 | 404 | */ |
amithy | 1:f793eace78a5 | 405 | |
amithy | 1:f793eace78a5 | 406 | //pc.printf("starting up... \n"); |
amithy | 1:f793eace78a5 | 407 | /* |
amithy | 1:f793eace78a5 | 408 | sd_begin(); |
amithy | 1:f793eace78a5 | 409 | while(1) |
amithy | 1:f793eace78a5 | 410 | { |
amithy | 1:f793eace78a5 | 411 | |
amithy | 1:f793eace78a5 | 412 | |
amithy | 1:f793eace78a5 | 413 | while(1) |
amithy | 1:f793eace78a5 | 414 | { |
amithy | 1:f793eace78a5 | 415 | if(hpm.getc()==0x42) |
amithy | 1:f793eace78a5 | 416 | { |
amithy | 1:f793eace78a5 | 417 | if(hpm.getc()==0x4d) |
amithy | 1:f793eace78a5 | 418 | { |
amithy | 1:f793eace78a5 | 419 | for(int i=0;i<8;i++) |
amithy | 1:f793eace78a5 | 420 | { |
amithy | 1:f793eace78a5 | 421 | hpmbuffer[i]=hpm.getc(); |
amithy | 1:f793eace78a5 | 422 | } |
amithy | 1:f793eace78a5 | 423 | break; |
amithy | 1:f793eace78a5 | 424 | } |
amithy | 1:f793eace78a5 | 425 | } |
amithy | 1:f793eace78a5 | 426 | } |
amithy | 1:f793eace78a5 | 427 | |
amithy | 1:f793eace78a5 | 428 | FILE *fp = fopen(datapath, "a"); //will overwrite any existing datalog.txt file |
amithy | 1:f793eace78a5 | 429 | if (fp != NULL) { |
amithy | 1:f793eace78a5 | 430 | fprintf(fp,"%x,%x,%x,%x\r\n",hpmbuffer[4],hpmbuffer[5],hpmbuffer[6],hpmbuffer[7]); |
amithy | 1:f793eace78a5 | 431 | //fprintf(fp,"%d,%.4f,%.4f,%.4f,%.4f\r\n",timeNow,sharpPM,specCO,figaroCO,figaroCO2 |
amithy | 1:f793eace78a5 | 432 | } |
amithy | 1:f793eace78a5 | 433 | fclose(fp); |
amithy | 1:f793eace78a5 | 434 | */ |
amithy | 1:f793eace78a5 | 435 | |
amithy | 1:f793eace78a5 | 436 | /* |
amithy | 1:f793eace78a5 | 437 | char readme_msb, readme_lsb; |
amithy | 1:f793eace78a5 | 438 | |
amithy | 1:f793eace78a5 | 439 | char reg = 0x03; |
amithy | 1:f793eace78a5 | 440 | i2c.write(figCO2, ®, 1, true); |
amithy | 1:f793eace78a5 | 441 | i2c.read(figCO2, &readme_lsb, 1, false); |
amithy | 1:f793eace78a5 | 442 | |
amithy | 1:f793eace78a5 | 443 | reg = 0x04; |
amithy | 1:f793eace78a5 | 444 | i2c.write(figCO2, ®, 1, true); |
amithy | 1:f793eace78a5 | 445 | i2c.read(figCO2, &readme_msb, 1, false); |
amithy | 1:f793eace78a5 | 446 | |
amithy | 1:f793eace78a5 | 447 | int16_t xval = twosComp(readme_msb,readme_lsb); |
amithy | 1:f793eace78a5 | 448 | figaroCO2 = (float)xval/15987; |
amithy | 1:f793eace78a5 | 449 | |
amithy | 1:f793eace78a5 | 450 | pc.printf("figaroCO2 %f\n", figaroCO2); |
amithy | 1:f793eace78a5 | 451 | wait(1); |
amithy | 1:f793eace78a5 | 452 | */ |
mbed_official | 0:bdbd3d6fc5d5 | 453 | |
amithy | 1:f793eace78a5 | 454 | BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); |
amithy | 1:f793eace78a5 | 455 | ble.init(); |
amithy | 1:f793eace78a5 | 456 | sd_begin(); |
amithy | 1:f793eace78a5 | 457 | sd_header_write(); |
amithy | 1:f793eace78a5 | 458 | sample_led = 1; |
amithy | 1:f793eace78a5 | 459 | |
amithy | 1:f793eace78a5 | 460 | while (ble.hasInitialized() == false) { |
amithy | 1:f793eace78a5 | 461 | } |
amithy | 1:f793eace78a5 | 462 | |
amithy | 1:f793eace78a5 | 463 | while(true) { |
amithy | 1:f793eace78a5 | 464 | |
amithy | 1:f793eace78a5 | 465 | //wait(5); |
amithy | 1:f793eace78a5 | 466 | if(triggered) |
amithy | 1:f793eace78a5 | 467 | { |
amithy | 1:f793eace78a5 | 468 | sd_begin(); |
amithy | 1:f793eace78a5 | 469 | wait(1); |
amithy | 1:f793eace78a5 | 470 | /* |
amithy | 1:f793eace78a5 | 471 | getTime(); |
amithy | 1:f793eace78a5 | 472 | getDHT(); |
amithy | 1:f793eace78a5 | 473 | pc.printf("%ld,%d,%d,%d,%d,%d,%d,%d,",timeNow,rtcbuff[0],rtcbuff[1],rtcbuff[2],rtcbuff[3],rtcbuff[4],rtcbuff[5],rtcbuff[6]); |
amithy | 1:f793eace78a5 | 474 | pc.printf("%d,%d\n\r",dhtTemp,dhtHum); |
amithy | 1:f793eace78a5 | 475 | wait(5); |
amithy | 1:f793eace78a5 | 476 | */ |
amithy | 1:f793eace78a5 | 477 | |
amithy | 1:f793eace78a5 | 478 | //getData(); |
amithy | 1:f793eace78a5 | 479 | //specCO = spec.read(); |
amithy | 1:f793eace78a5 | 480 | //sd_temp_get(&p_temp); |
amithy | 1:f793eace78a5 | 481 | //temperature = float(p_temp)/4.; |
amithy | 1:f793eace78a5 | 482 | |
amithy | 1:f793eace78a5 | 483 | //pc.printf("%.4f\n",specCO); |
amithy | 1:f793eace78a5 | 484 | |
amithy | 1:f793eace78a5 | 485 | getTime(); |
amithy | 1:f793eace78a5 | 486 | wakeTime = timeNow + LOGDELAY; |
amithy | 1:f793eace78a5 | 487 | |
amithy | 1:f793eace78a5 | 488 | for(int i = 0; i < 5; i++) |
amithy | 1:f793eace78a5 | 489 | { |
amithy | 1:f793eace78a5 | 490 | getTime(); |
amithy | 1:f793eace78a5 | 491 | sample_led = 1; |
amithy | 1:f793eace78a5 | 492 | getData(); |
amithy | 1:f793eace78a5 | 493 | sample_led = 0; |
amithy | 1:f793eace78a5 | 494 | saveData(); |
amithy | 1:f793eace78a5 | 495 | } |
amithy | 1:f793eace78a5 | 496 | |
amithy | 1:f793eace78a5 | 497 | |
amithy | 1:f793eace78a5 | 498 | rtc.set_time(timeNow); |
amithy | 1:f793eace78a5 | 499 | triggered = false; |
amithy | 1:f793eace78a5 | 500 | sd_sleep(); //powers off everything |
amithy | 1:f793eace78a5 | 501 | } |
amithy | 1:f793eace78a5 | 502 | else |
amithy | 1:f793eace78a5 | 503 | { |
amithy | 1:f793eace78a5 | 504 | wakeUp.attach(&checkTime, 10); |
amithy | 1:f793eace78a5 | 505 | ble.waitForEvent(); |
amithy | 1:f793eace78a5 | 506 | __wfe(); |
amithy | 1:f793eace78a5 | 507 | wakeUp.detach(); |
amithy | 1:f793eace78a5 | 508 | } |
amithy | 1:f793eace78a5 | 509 | } |
amithy | 1:f793eace78a5 | 510 | } |