Hello,
I would like to record sensor data autonomously on an SD card with an Adalogger M0 and a BME 280 and battery. The system should get a power switch. As soon as the switch is pressed, the sketch should run. Unfortunately, it only works if I upload the sketch directly from the PC. Then I plug in my battery and remove the USB cable. The data will continue to be recorded. But only as long as a power supply is available. If I disconnect and reconnect the power supply, the sketch no longer runs. Is there a way that the sketch restarts as soon as it gets suspense again? What needs to be changed in the sketch?
Here is my Code:
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <DS3231.h>
#include "RTClib.h"
#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10
#define SEALEVELPRESSURE_HPA (1013.25)
unsigned long delayTime = 5000;
File myFile;
String Temperature, Pressure, Altitude, Humidity, Messung;
String Date;
RTC_DS1307 rtc;
//Adafruit BME 280
Adafruit_BME280 bme; // I2C
// Real Time Clock
DS3231 clock;
RTCDateTime dt;
void setup() {
Serial.begin(57600);
pinMode(13, OUTPUT); //Testzweck: Um eine vollständige Speicherung zu erkennen
//************************ RTC Initialisierung ************************\\
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
else
{
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
//******************************* BME280 Initialisierung ****************************\\
if (! bme.begin(0x77, &Wire))
{
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
bme.setSampling(Adafruit_BME280::MODE_NORMAL,
Adafruit_BME280::SAMPLING_X8, // temperature
Adafruit_BME280::SAMPLING_X16, // pressure
Adafruit_BME280::SAMPLING_X1, // humidity
Adafruit_BME280::FILTER_X8,
Adafruit_BME280::STANDBY_MS_0_5 );
// suggested rate is 25Hz
// 1 + (2 * T_ovs) + (2 * P_ovs + 0.5) + (2 * H_ovs + 0.5)
// T_ovs = 2
// P_ovs = 16
// H_ovs = 1
// = 40ms (25Hz)
// with standby time that should really be 24.16913... Hz
//***************************** SD card Initialisierung ****************************\\
while (!Serial)
{
Serial.print("Initializing SD card...");
if (!SD.begin(4))
{
Serial.println("initialisation failed!");
while(1);
}
Serial.println("inistalisation done");
}
myFile = SD.open("Messung.txt", FILE_WRITE);
if (myFile)
{
myFile.println("Date | Time | Temperature (°C) | Pressure (hPa) | Humidity (%) \r\n");
myFile.close();
}
else
{
Serial.println("error opening Messung.txt");
}
//delayTime = 5000;
}
void loop()
{
bme.takeForcedMeasurement(); // has no effect in normal mode
printRTC();
printBME280();
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13,LOW);
delay(delayTime);
}
void printRTC()
{
DateTime time = rtc.now();
Serial.print (String("DateTime::TIMESTAMP_DATE:\t")+ time.timestamp(DateTime::TIMESTAMP_DATE));
Serial.print(String("DateTime::TIMESTAMP_TIME:\t")+ time.timestamp(DateTime::TIMESTAMP_TIME));
Serial.println("\n");
Serial.println("Beginne zu speichern");
myFile = SD.open("Messung.txt", FILE_WRITE);
if(myFile){
myFile.print(time.timestamp(DateTime::TIMESTAMP_DATE));
myFile.print(" | ");
myFile.print(time.timestamp(DateTime::TIMESTAMP_TIME));
myFile.print(" | ");
myFile.close();
Serial.println("gespeichert");
}
else{
Serial.println("error opening Sensordaten.txt nach RTC");
}
}
void printBME280() {
String Temperature = String(bme.readTemperature(), 2);
String Pressure = String(bme.readPressure() / 100.0F,2);
String Humidity = String(bme.readHumidity(),2);
Messung = Temperature + " | " + Pressure + " | " + Humidity;
Serial.println("Save Sensordata");
Serial.println(Messung);
myFile = SD.open("Messung.txt", FILE_WRITE);
if(myFile) {
Serial.println("Writing to Messung.txt...");
myFile.print(Messung);
myFile.print("\n");
myFile.close();
Serial.println("done");
}
else{
Serial.println("error opening Sensordaten.txt nach Sensor");
}
}