This is my code:
#include <SPI.h>
#include <SD.h>
#include <Ethernet.h>
#include "DHT.h"
#include <LiquidCrystal.h>
#define DHTPIN 2
#define DHTTYPE DHT22
const int chipSelect = 4;
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(3, 9, 5, 6, 7, 8);
byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x23, 0x36};
IPAddress ip(192, 168, 1, 49);
EthernetServer server(80);
File myFile;
File webFile;
int Timer = 0;
//long lst = 0;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
pinMode(4, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
Serial.println("card initialized.");
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
lcd.begin(16, 2);
lcd.clear();
dht.begin();
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (Timer == 0)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(t);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(h);
lcd.print("%");
delay(200);
}
else
{
File myFile;
myFile = SD.open("test.txt", FILE_WRITE);
if (myFile) {
myFile.println(".......");
myFile.println("humidity(%)");
myFile.println(h, 2);
myFile.println("Temperature(*C)");
myFile.println(t, 2);
myFile.close();
Serial.println("done.");
} else {
Serial.println("error opening test.txt");
}
delay (60000);
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
while (myFile.available()) {
Serial.write(myFile.read());
}
}
EthernetClient client = server.available(); // try to get client
if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
// last line of client request is blank and ends with \n
// respond to client only after last line received
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");
//client.println("Refresh: 60"); // refresh the page automatically every 60 sec
client.println();
client.println("");
client.println("");
client.println("
");
// send web page
webFile = SD.open("test.txt"); // open web page file
if (webFile) {
while (webFile.available()) {
client.write(webFile.read()); // send web page to client
}
webFile.close();
}
break;
//client.println("Refresh: 5");
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop();// close the connection
}
}
Timer = Timer + 1;
if (Timer == 2)
{
Timer = 0;
}
}