/* Derived from Ed Mallon's starter logging code. C. Fastie modified to run without sleeping anything. added BME280 sensor, added seconds to time readout added unixtime */ #include #include #include #include #include Adafruit_BME280 bme; // The BME280 will be an I2C device float hgInches = 29.95; // Enter the sealevel barometric pressure here (xx.xx inches Hg) #define SEALEVELPRESSURE_HPA (hgInches/0.02952998751) // hPa=(inches Hg)/0.02952998751 SdFat SD; #define MOSIpin 11 #define MISOpin 12 RTC_DS1307 RTC; #define DS1307_I2C_ADDRESS 0x68 char TmeStrng[] = "0000/00/00,00:00:00"; //19 ascii characters void setup() { Serial.begin(9600); // Open serial communications and wait for port to open: Wire.begin(); // start the i2c interface RTC.begin(); // start the RTC RTC.adjust(DateTime((__DATE__), (__TIME__))); //sets the RTC to the computer time when the sketch is loaded Serial.print("Find SD card: "); if (!SD.begin(10)) { Serial.println("Card failed"); return; } Serial.println(" SD card OK"); //————-print a header to the data file———- File dataFile = SD.open("datalog.txt", FILE_WRITE); if (dataFile) { // if the file is available, write to it: dataFile.println("Nano Logger"); dataFile.close(); } else { Serial.println("file error"); // if the file isn’t open, pop up an error: } bool status; status = bme.begin(0x76); // 0x76 is the I2C address of the clone sensors I have if (!status) { Serial.println("No BME280"); while (1); } } // end of setup void loop() { float BMEt = (bme.readTemperature()); float BMEh = (bme.readHumidity()); float BMEp = (bme.readPressure() / 100.0F); float BMEa = (bme.readAltitude(SEALEVELPRESSURE_HPA)); DateTime now = RTC.now(); //this reads the time from the RTC sprintf(TmeStrng, "%04d/%02d/%02d,%02d:%02d:%02d", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second()); // [added seconds] Serial.print("RTC utc Time: "); Serial.print(now.unixtime()); Serial.println(); Serial.print("RTC time: "); Serial.println(TmeStrng); Serial.print("BME280 temp: "); Serial.print(BMEt); Serial.println(" C"); Serial.print("BME280 Humidity: "); Serial.print(BMEh); Serial.println(" %"); Serial.print("Pressure: "); Serial.print(BMEp); Serial.println(" hPa"); Serial.print("Elevation: "); Serial.print(BMEa); Serial.println(" m"); Serial.println(); //Construct a data string to write to µSD card String dataString = ""; //this erases the previous string dataString += TmeStrng; dataString += ","; dataString += String(now.unixtime()); dataString += ","; dataString += String(BMEt); dataString += ","; dataString += String(BMEh); dataString += ","; dataString += String(BMEp); dataString += ","; dataString += String(BMEa); //——– write the data to the SD card ——– File dataFile = SD.open("datalog.txt", FILE_WRITE); // if the file is available, write to it: if (dataFile) { dataFile.println(dataString); dataFile.close(); } else { Serial.println("file error"); // if the file isn’t open, pop up an error: } delay(3000); // write data every 3 seconds } // END of the MAIN LOOP