rtc project add power features
This commit is contained in:
parent
baf4833bb7
commit
83d2719ed6
|
@ -1,4 +1,8 @@
|
|||
#include <SD.h>
|
||||
|
||||
#include <SPI.h>
|
||||
#include <DHT.h>
|
||||
#include <DS3231.h>
|
||||
#include <avr/sleep.h>
|
||||
#include <avr/power.h>
|
||||
#include <avr/wdt.h>
|
||||
|
@ -12,6 +16,14 @@
|
|||
// watchdog timer configuration.
|
||||
#define MAX_SLEEP_ITERATIONS LOGGING_FREQ_SECONDS / 8
|
||||
|
||||
#define DHTPIN 2
|
||||
#define DHTTYPE DHT22
|
||||
const int chipSelect = 4;
|
||||
|
||||
//real time clock ds3231
|
||||
DS3231 rtc(SDA, SCL);
|
||||
DHT dht(DHTPIN, DHTTYPE);
|
||||
|
||||
int sleepIterations = 0;
|
||||
volatile bool watchdogActivated = false;
|
||||
|
||||
|
@ -84,9 +96,69 @@ void setup_interrupts(void) {
|
|||
}
|
||||
|
||||
void setup(void) {
|
||||
Serial.begin(9600);
|
||||
setup_interrupts();
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
dht.begin();
|
||||
rtc.begin();
|
||||
Serial.begin(9600);
|
||||
while (!Serial) { }
|
||||
|
||||
Serial.print("Initializing SD card...");
|
||||
|
||||
if (!SD.begin(chipSelect)) {
|
||||
Serial.println("Card failed, or not present");
|
||||
while (1);
|
||||
}
|
||||
Serial.println("card initialized.");
|
||||
setup_interrupts();
|
||||
|
||||
}
|
||||
|
||||
void visualize(float h, float t, String data) {
|
||||
|
||||
Serial.print("Hezetasuna:");
|
||||
Serial.print(h);
|
||||
Serial.println("");
|
||||
|
||||
Serial.print("Tenperatura:");
|
||||
Serial.print(t);
|
||||
Serial.println("");
|
||||
|
||||
Serial.print("dataString:");
|
||||
Serial.println(data + "\n");
|
||||
}
|
||||
void getAndWrite(){
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
float h = dht.readHumidity();
|
||||
float t = dht.readTemperature();
|
||||
double unixTime = rtc.getUnixTime(rtc.getTime());
|
||||
|
||||
if (isnan(t) || isnan(h)){
|
||||
Serial.println("Failed to read from DHT");
|
||||
return;
|
||||
}
|
||||
String dataString = "";
|
||||
dataString = String(h) + ";" + String(t) + ";" + String(unixTime);
|
||||
|
||||
visualize(h, t, dataString);
|
||||
writeSD(dataString);
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
|
||||
}
|
||||
|
||||
void writeSD(String dataString) {
|
||||
File dataFile = SD.open("datalog.txt", FILE_WRITE);
|
||||
|
||||
// if the file is available, write to it:
|
||||
if (dataFile) {
|
||||
dataFile.println(dataString);
|
||||
dataFile.close();
|
||||
// print to the serial port too:
|
||||
Serial.println(dataString);
|
||||
}
|
||||
// if the file isn't open, pop up an error:
|
||||
else {
|
||||
Serial.println("error opening datalog.txt");
|
||||
}
|
||||
}
|
||||
|
||||
void loop(void)
|
||||
|
@ -103,10 +175,8 @@ void loop(void)
|
|||
sleepIterations = 0;
|
||||
// Log the sensor data (waking the CC3000, etc. as needed)
|
||||
/*logSensorReading();*/
|
||||
Serial.println("gimme 5v");
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
delay(1000); // wait for a second
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
getAndWrite();
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue