miércoles, 17 de mayo de 2023

ESP32 +SD+DHT11: Guardar datos de temperatura en la SD

Toma la hora actual de Internet.
 
// Libraries for SD card

#include "FS.h"
#include "SD.h"
#include <SPI.h>
 
// Libraries for DHT11
#include <DHT.h>
 
// Libraries to get time from NTP Server
#include <WiFi.h>
#include "time.h"
 
// Replace with your network credentials
const char* ssid     = "Daniel";
const char* password = "12345678";
 
// Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 30000;
 
DHT dht27(27,DHT11);
 
// Variables to hold sensor readings
float temp;
String dataMessage;
 
// NTP server to request epoch time
const char* ntpServer = "pool.ntp.org";
 
// Variable to save current epoch time
unsigned long epochTime;
 
// Function that gets current epoch time
unsigned long getTime() {
  time_t now;
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    //Serial.println("Failed to obtain time");
    return(0);
  }
  time(&now);
  return now;
}
 
// Initialize WiFi
void initWiFi() {
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi ..");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
  }
  Serial.println(WiFi.localIP());
}
 

 
// Initialize SD card
void initSDCard(){
   if (!SD.begin()) {
    Serial.println("Card Mount Failed");
    return;
  }
  uint8_t cardType = SD.cardType();
 
  if(cardType == CARD_NONE){
    Serial.println("No SD card attached");
    return;
  }
  Serial.print("SD Card Type: ");
  if(cardType == CARD_MMC){
    Serial.println("MMC");
  } else if(cardType == CARD_SD){
    Serial.println("SDSC");
  } else if(cardType == CARD_SDHC){
    Serial.println("SDHC");
  } else {
    Serial.println("UNKNOWN");
  }
  uint64_t cardSize = SD.cardSize() / (1024 * 1024);
  Serial.printf("SD Card Size: %lluMB\n", cardSize);
}
 
// Write to the SD card.
void writeFile(fs::FS &fs, const char * path, const char * message) {
  Serial.printf("Writing file: %s\n", path);
 
  File file = fs.open(path, FILE_WRITE);
  if(!file) {
    Serial.println("Failed to open file for writing");
    return;
  }
  if(file.print(message)) {
    Serial.println("File written");
  } else {
    Serial.println("Write failed");
  }
  file.close();
}
 
// Append data to the SD card
void appendFile(fs::FS &fs, const char * path, const char * message) {
  Serial.printf("Appending to file: %s\n", path);
 
  File file = fs.open(path, FILE_APPEND);
  if(!file) {
    Serial.println("Failed to open file for appending");
    return;
  }
  if(file.print(message)) {
    Serial.println("Message appended");
  } else {
    Serial.println("Append failed");
  }
  file.close();
}
 
void setup() {
  Serial.begin(115200);
   
  initWiFi();
  dht27.begin();  
  initSDCard();
  configTime(0, 0, ntpServer);
   
  // If the data.txt file doesn't exist
  // Create a file on the SD card and write the data labels
  File file = SD.open("/data.txt");
  if(!file) {
    Serial.println("File doesn't exist");
    Serial.println("Creating file...");
    writeFile(SD, "/data.txt", "Epoch Time, Temperature \r\n");
  }
  else {
    Serial.println("File already exists");  
  }
  file.close();
}
 
void loop() {
  if ((millis() - lastTime) > timerDelay) {
    //Get epoch time
    epochTime = getTime();
     
    //Get sensor readings
    temp = dht27.readTemperature();
    //temp = 1.8*bme.readTemperature() + 32;
   
     
    //Concatenate all info separated by commas
    dataMessage = String(epochTime) + "," + String(temp)+"\r\n";
    Serial.print("Saving data: ");
    Serial.println(dataMessage);
 
    //Append the data to file
    appendFile(SD, "/data.txt", dataMessage.c_str());
 
    lastTime = millis();
  }
}