Hi, I am developing a arduino MEGA program to kept a DHT11 data in a HTML table in a micro SD with a micro SD module.
The program consist in 2 archives, the arduino code:
// Añadiendo librerias
#include <SD.h>
#include <SPI.h>
#include <DHT.h>
// Declarando variables --- DHT11
#define DHTPIN 45
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// Variables para registro de datos
File myFile;
String Plantilla;
String HTML;
const String Molde = "<tr><th class=\"Contenido\">Time</th><th class=\"Contenido\">Temp C°</th><th class=\"Contenido\">Hum %</th><th class=\"Contenido\"> PromTemp C°</th><th class=\"Contenido\">PromHum %</th></tr>\n<!-- && -->"; // Molde para reemplazar los datos registrados del DHT11
// Variables para toma de datos
float T;
float H;
float AvgT;
float AvgH;
// Variables para registro del tiempo
int Sec = 0;
int Min = 0;
int Horas = 0;
unsigned long Time;
int Buff;
bool FT = true;
String TiempoF;
void setup() {
Serial.begin(9600);
pinMode(53, OUTPUT); // Iniciando SD
Serial.println("Inciando módulo SD...");
if(!SD.begin(53)){
Serial.println("Fallo al inciar módulo SD");
}
Serial.println("Módulo SD iniado correctamente");
Serial.println("Cargando Plantilla..."); // Carga de la plantilla
myFile = SD.open("Plantilla.html");
if (myFile){
Plantilla = myFile.read();
HTML = Plantilla; // El primer registro es igual a la plantilla, posteriormente en la función HTML_Reg() se agregarán nuevos datos
myFile.close();
Serial.println("Plantilla cargada correctamente");
}
else{
Serial.println("Fallo al cargar la Plantilla");
}
dht.begin(); // Inicio del sensor DHT11
}
void loop() {
RegData();
getTime();
HTML_Reg(TiempoF, T, H, AvgT, AvgH);
Guardar();
delay(10000);
}
void HTML_Reg(String Tiempo, int Temperatura, int Humedad, int avgTemperatura, int avgHumedad){ // Creación del registro de datos a base del molde y la plantilla en HTML
HTML.replace("Time",String(Tiempo)); // Se reemplazan las claves en la plantilla por los valores registrados
HTML.replace("Temp",String(Temperatura));
HTML.replace("Hum",String(Humedad));
HTML.replace("PromTemp",String(avgTemperatura));
HTML.replace("PromHum",String(avgHumedad));
HTML.replace("<!-- && -->",Molde); // Luego del registro se vuelve a colocar el molde para posteriormente ser reemplazado
Serial.println(HTML);
Serial.println("Registro elaborado correctamente");
}
void Guardar(){ // Guardado del HTML con el registro de la función HTML_Reg()
if(SD.exists("Data.html")){SD.remove("Data.html");} // Si hay un HTML anterior, se elimina para crear un HTML con la información actualizada
myFile = SD.open("Data.html", FILE_WRITE);
if(myFile){
myFile.print(HTML);
myFile.close();
Serial.println("Guardado correctamente");
}
else{
Serial.println("Fallo al guardar el registro");
}
}
void RegData(){ // Toma de datos del DHT11
T=dht.readTemperature();
H=dht.readHumidity();
AvgT = (FT) ? T : (AvgT + T)/2;
AvgH = (FT) ? H : (AvgH + H)/2;
FT = false;
Serial.println("Toma de datos correctamente");
}
void getTime(){ // Registro del Tiempo
Time = millis();
Sec = Time/1000;
Buff = Sec/60;
Min = Buff%60;
Horas = Buff/60;
TiempoF = String(Horas) + " : " + String(Min) + " : " + String(Sec);
}
And a HTML how template for the final table:
<!DOCTYPE html>
<html>
<head>
<title>Registro de datos 001</title>
</head>
<body>
<table border="1" align="center">
<tr>
<th class="Titulo" rowspan="2">Tiempo</th>
<th class="Titulo" rowspan="2">Temperatura</th>
<th class="Titulo" rowspan="2">Humedad</th>
<th class="Titulo" colspan="2">Promedios</th>
</tr><tr>
<th class="Titulo">Temperatura</th>
<th class="Titulo">Humedad</th>
</tr>
<tr><th class="Contenido">Time</th><th class="Contenido">Temp C°</th><th class="Contenido">Hum %</th><th class="Contenido"> PromTemp C°</th><th class="Contenido">PromHum %</th></tr><!-- && -->
</table>
</body>
</html>
The arduino initialized the module SD correctly, but he not open the template neither create the final HTML. I check the archives names and be OK but I recieve this output:
Inciando módulo SD...
Módulo SD iniado correctamente
Cargando Plantilla...
Fallo al cargar la Plantilla
Toma de datos correctamente
Registro elaborado correctamente
Fallo al guardar el registro
Pls help
Note: The Arduino conections are: CS --> 53, SCK --> 52, MOSI --> 51, MISO --> 50 and the DHT Output --> 45
Note 2: Sorry for my English, i am learning.