#include <stdlib.h>
#include <DHT.h>
#include "RTClib.h"
#include <SD.h>
#define DHTPIN 23
#define LDR_PIN 34
#define SDA_CLOCK 21
#define SCL_CLOCK 22
#define CS_PIN 5 // Pino CS conectado ao GPIO 5
#define MOSI_PIN 17 // Pino MOSI conectado ao GPIO 27 DI
#define MISO_PIN 19 // Pino MISO conectado ao GPIO 19 DO
#define SCK_PIN 18 // Pino SCK conectado ao GPIO 18
const char* fileName = "/data.txt";
const char* header = "TIME,TEMP,UM,LUX";
// LDR Characteristics
const float GAMMA = 0.7;
const float RL10 = 50;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
DHT dht(DHTPIN, DHT22);
RTC_DS1307 rtc;
File file;
void setup() {
Serial.begin(115200);
pinMode(LDR_PIN, INPUT);
analogReadResolution(12);
dht.begin(DHTPIN);
if (! rtc.begin()) {
Serial.println("RTC Não estabelecido");
Serial.flush();
abort();
}
SPI.begin(SCK_PIN, MISO_PIN, MOSI_PIN, CS_PIN);
if (!SD.begin(CS_PIN)) {
Serial.println("Card initialization failed!");
while (true);
}
if (SD.exists(fileName)) {
Serial.println("Arquivo já existe");
file = SD.open(fileName, FILE_READ);
if (file) {
String firstLine = file.readStringUntil('\n');
file.close();
if (firstLine != header) {
Serial.println("Cabeçalho será adicionado");
file = SD.open(fileName, FILE_WRITE);
if (file) {
file.println(header);
file.close();
Serial.println("Cabeçalho escrito.");
} else {
Serial.println("Error ao abrir para criar o cabeçalho!");
}
} else {
Serial.println("Cabeçalho já existe");
}
} else {
Serial.println("Error ao abrir arquivo para criar cabeçalho");
}
} else {
Serial.println("Arquivo não existe, criando novo arquivo");
file = SD.open(fileName, FILE_WRITE);
if (file) {
file.println(header);
file.close();
Serial.println("Cabeçalho criado");
} else {
Serial.println("Erro ao criar arquivo");
}
}
}
float readLux(int analogValue) {
float voltage = analogValue / 4095.0 * 3.3;
float resistance = 2000 * voltage / (1 - voltage / 3.3);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
return lux;
}
char* getFormattedDateTime(const DateTime& now) {
int tamanho = snprintf(NULL, 0, "%d/%02d/%02d (%s) %02d:%02d:%02d", now.year(), now.month(), now.day(), daysOfTheWeek[now.dayOfTheWeek()],now.hour(), now.minute(), now.second());
char* dateTimeString = (char*)malloc((tamanho + 1) * sizeof(char));
if (dateTimeString == NULL) {
Serial.println("Erro ao alocar memória.");
return NULL;
}
snprintf(dateTimeString, tamanho + 1, "%d/%02d/%02d (%s) %02d:%02d:%02d", now.year(), now.month(), now.day(), daysOfTheWeek[now.dayOfTheWeek()],now.hour(), now.minute(), now.second());
return dateTimeString;
}
unsigned long int setime = 0;
void loop() {
if(millis() - setime >= 1000) {
setime = millis();
file = SD.open(fileName, FILE_APPEND);
DateTime now = rtc.now();
char* formattedDateTime = getFormattedDateTime(now);
int analogValue = analogRead(LDR_PIN);
float h = dht.readHumidity();
float t = dht.readTemperature();
float lux = readLux(analogValue);
String dataLine = String(formattedDateTime) + "," + t + "," + h + "," + lux;
Serial.println(dataLine);
if (file) {
file.println(dataLine);
} else {
Serial.println("Erro ao escrever infomrações no arquivo");
}
if (formattedDateTime != NULL) {
free(formattedDateTime);
}
}
}