t

Dependencies:   SDFileSystem BLE_API BufferedSerial DHT22_3 mbed-dev_2 nRF51822 nrf51_rtc

Fork of SDFileSystem_HelloWorld by mbed official

Committer:
amithy
Date:
Fri Nov 10 20:54:05 2017 +0000
Revision:
1:d6e971d1bca9
Parent:
0:bdbd3d6fc5d5
Child:
3:42fbb9bc132b
t

Who changed what in which revision?

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