Hello,
I would like to put a Feather Adalogger in deep sleep after it has read the data from a temperature and humidity sensor every 5 minutes.
I found the Low_Power library and Adafruit_SleepyDog .
While searching further I found various information and code, but I'm not shure about the best way to put into sleep mode or standby the feather.
#include <SD.h>
#include "RTClib.h"
#include "DHT.h"
#define VBATPIN A7 // Battery Voltage on Pin A7
#define DHTPIN 5 // what pin the sensor is connected to
#define DHTTYPE DHT11 // Which type of DHT sensor you're using:
RTC_DS3231 rtc;
// initialize the sensor:
DHT dht(DHTPIN, DHTTYPE);
const int chipSelect = 4; // SPI chip select for SD card
const int cardDetect = 7; // pin that detects whether the card is there
const int writeLed = 8; // LED indicator for writing to card
const int errorLed = 13; // LED indicator for error
long lastWriteTime = 0; // timestamp for last write attempt
long interval = 5000; // time between readings
char fileName[] = "datalog.csv"; // filename to save on SD card
void setup() {
Serial.begin(9600); // initialize serial communication
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, 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__)));
}
// initialize LED and cardDetect pins:
pinMode(writeLed, OUTPUT);
pinMode(errorLed, OUTPUT);
pinMode(cardDetect, INPUT_PULLUP);
// startSDCard() blocks everything until the card is present
// and writable:
if (startSDCard() == true) {
Serial.println("card initialized.");
delay(100);
// open the log file:
File logFile = SD.open(fileName, FILE_WRITE);
// write header columns to file:
if (logFile) {
logFile.println("Date,Hour,Batt");
logFile.close();
}
}
// initialize the sensor:
dht.begin();
}
void loop() {
// if the cart's not there, don't do anything more:
if (digitalRead(cardDetect) == LOW) {
digitalWrite(errorLed, HIGH);
return;
}
// turn of the error LED:
digitalWrite(errorLed, LOW);
DateTime now = rtc.now();
float measuredvbat = analogRead(VBATPIN);
measuredvbat *= 2; // we divided by 2, so multiply back
measuredvbat *= 3.3; // Multiply by 3.3V, our reference voltage
measuredvbat /= 1024; // convert to voltage
// read sensors every 10 seconds
if (millis() - lastWriteTime >= interval) {
File logFile = SD.open(fileName, FILE_WRITE); // open the log file
if (logFile) { // if you can write to the log file,
digitalWrite(writeLed, HIGH); // turn on the write LED
int day = now.day();
int month = now.month();
int year = now.year();
int hour = now.hour();
int minute = now.minute();
int second = now.second();
// print to the log file:
logFile.print(day);
logFile.print("/");
logFile.print(month);
logFile.print("/");
logFile.print(year);
logFile.print(", ");
logFile.print(hour);
logFile.print(":");
logFile.print(minute);
logFile.print(":");
logFile.print(second);
logFile.print(", ");
logFile.println(measuredvbat); // Print battery voltage
logFile.print(", ");
logFile.print(humidity);
logFile.print(",");
logFile.println(temperature);
logFile.flush();
// for debugging only:
Serial.print(day);
Serial.print('/');
Serial.print(month);
Serial.print('/');
Serial.print(year);
Serial.print(",");
Serial.print(hour);
Serial.print(':');
Serial.print(minute);
Serial.print(':');
Serial.print(second);
Serial.print(",");
Serial.print(measuredvbat);
Serial.print(",");
Serial.print(humidity);
Serial.print(",");
Serial.println(temperature);
Serial.print('\n');
// read sensor:
float humidity = dht.readTemperature();
float temperature = dht.readHumidity();
// update the last attempted save time:
lastWriteTime = millis();
}
digitalWrite(writeLed, LOW); // turn off the write LED
}
}
boolean startSDCard() {
// Wait until the card is inserted:
while (digitalRead(cardDetect) == LOW) {
Serial.println("Waiting for card...");
digitalWrite(errorLed, HIGH);
delay(750);
}
// wait until the card initialized successfully:
while (!SD.begin(chipSelect)) {
digitalWrite(errorLed, HIGH); // turn on error LED
Serial.println("Card failed");
delay(750);
}
return true;
}
Thanks for your feedback