Hola gente, estoy haciendo un proyecto para un monitoreo de temperatura con un RTC DS1307.
El problema es el modulo que me reseta la hora que tenia.
Al momento de cargar el codigo a la placa la hora queda programada con la hora de mi computadora, hasta aqui todo bien.
El problema viene cuando desconecto el arduino de la alimentacion o se le aplica un reset a la placa.
La hora que me muestra el RTC es la ultima hora en la que la placa fue programada, y no me sigue con el conteo normal.
Aqui les muestro el codigo.
#include <Ethernet.h>
#include <SPI.h>
#include <Wire.h>
#include "RTClib.h"
#include <SD.h>
int lm35 = A0; // Entrada analógica A0 para leer sensor
float temp=0;
int lastTime = -1;
const int chipSelect = 4;
#define ref 1.1 //voltaje de referencia
RTC_DS1307 RTC;
byte mac[] = {
0x90, 0xA2, 0xDA, 0x0E, 0xCB, 0xFF };
IPAddress ip(192,168,1,20);
EthernetServer server(80);
void setup(){
Serial.begin(9600);
Ethernet.begin(mac, ip);
server.begin();
Wire.begin();
RTC.begin();
RTC.adjust(DateTime(__DATE__, __TIME__));
Serial.print("Inicia SD card...");
pinMode(10, OUTPUT);
Serial.print("server ip ");
Serial.println(Ethernet.localIP());
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
}
/*programa principal*/
void loop(){
DateTime now = RTC.now(); // get time from RTC
int time = now.second();
if (abs(time - lastTime) > 5)
{
File dataFile = SD.open("datalog.CSV", FILE_WRITE);
if (dataFile) {
dataFile.print(now.day(), DEC);
dataFile.print('/');
dataFile.print(now.month(), DEC);
dataFile.print('/');
dataFile.print(now.year(), DEC);
dataFile.print(" , ");
dataFile.print(now.hour(), DEC);
dataFile.print(':');
dataFile.print(now.minute(), DEC);
dataFile.print(" , ");
dataFile.println((float)temp);
dataFile.close();
// print to the serial port too:
Serial.print("Temperatura = ");
Serial.println(temp);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.CSV");
}
lastTime = time;
}
/*Start HTTP server*/
EthernetClient client = server.available();
if (client) {
Serial.println("new client"); // an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
analogReference(INTERNAL);
temp=analogRead(lm35); // lectura del sensor
temp=(temp*ref*1000.0)/1023.0; // conversion de la lectura
temp=(temp*100.0)/1000.0;
delay(200);
client.print(now.day(), DEC);
client.print('/');
client.print(now.month(), DEC);
client.print('/');
client.print(now.year(), DEC);
client.print(' ');
client.print(now.hour(), DEC);
client.print(':');
client.print(now.minute(), DEC);
client.println("
");
client.print("Arduino Webserver");
client.println("
");
client.print("Temperatura(C): ");
client.print(temp);
client.println("
");
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
Saludos