// Code developed by Dr Kris Chan & Dr Bruce Main, King's College London 10.11.2018 // ////////////////////////////////////////////////////////////////////////////////////// /* LIBRARIES */ #include // SPI communication library (for clock & SD card) #include "ds3234.h" // clock library #include // SD library #include // sleep library #include // power library (for sleep) #include // Additional library for BME680 #include "Adafruit_BME680.h" // BME680 library #include // Other general functions library #include // I2C communication library (for BME680) /* Constants */ const int CS = 10; //sd card SPI chip select int ss = 9; //clock SPI chip select int AlarmPin = 2; //pin which alarm communicates with unsigned int wADC; //Arduino internal temperature call //BME680 housekeeping Adafruit_BME680 bme; // BME680 setup float bmetemp, bmegas, bmepres, bmehum; // create float (x.xx) variables to store BME values temporarily //SD HOUSEKEEPING SdFat sd; SdFile file; char newfile[] = "BME680test.csv"; //name of file //ALARM HOUSEKEEPING struct ts t; void setup() { Serial.begin(9600); while (!Serial); Serial.println(F("BME680 test")); if (!bme.begin()) { Serial.println("Could not find a valid BME680 sensor, check wiring!"); while (1); } // Set up oversampling and filter initialization bme.setTemperatureOversampling(BME680_OS_8X); bme.setHumidityOversampling(BME680_OS_2X); bme.setPressureOversampling(BME680_OS_4X); bme.setIIRFilterSize(BME680_FILTER_SIZE_3); bme.setGasHeater(320, 150); // 320*C for 150 ms SPI.beginTransaction(SPISettings(6000000, MSBFIRST, SPI_MODE3)); DS3234_init(ss, DS3234_INTCN); delay(10); DS3234_get(ss, &t); delay(10); while (!sd.begin(CS, SPI_HALF_SPEED)) {} // open the file for write at end like the Native SD library file.open(newfile, O_WRITE | O_CREAT | O_APPEND); file.close(); Serial.println("setupend"); //would print to the Arduino screen if connected delay(50); } void loop() { if (! bme.performReading()) { Serial.println("Failed to perform reading :("); return; } Serial.println("loop"); //would print to the Arduino screen if connected delay(50); //get BME680 readings bmetemp = bme.temperature; bmepres = bme.pressure /100.0; bmehum = bme.humidity; bmegas = bme.gas_resistance / 1000.0; //run GetTemp subroutine (returns internal uncalibrated Arduino temperature) GetTemp(); //get time and temperature from clock SPI.beginTransaction(SPISettings(6000000, MSBFIRST, SPI_MODE3)); delay(20); DS3234_get(ss, &t); float dstemp = (DS3234_get_treg(ss)); //prepare data to be written to SD String dataString = ""; dataString += t.year; dataString += ","; dataString += t.mon; dataString += ","; dataString += t.mday; dataString += ","; dataString += t.hour; dataString += ","; dataString += t.min; dataString += ","; dataString += wADC; dataString += ","; dataString += bmetemp; dataString += ","; dataString += bmehum; dataString += ","; dataString += bmepres; dataString += ","; dataString += bmegas; dataString += ","; dataString += dstemp; //print the data to be written to SD Serial.println(dataString); Serial.println(""); delay(50); //write the data to the file while (!sd.begin(CS,SPI_HALF_SPEED)) { } file.open(newfile, O_WRITE | O_APPEND); //Opens the file delay(5); file.println(dataString); //prints data string to the file delay(5); file.close(); //closes the file delay(20); } //OUTSIDE OF LOOOP: GetTemp subroutine (gets Arduino's uncalibrated temperature) double GetTemp(void) { ADMUX = (_BV(REFS1) | _BV(REFS0) | _BV(MUX3)); ADCSRA |= _BV(ADEN); delay(20); ADCSRA |= _BV(ADSC); while (bit_is_set(ADCSRA,ADSC)); wADC = ADCW; }