#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
const int sd_cs = 4;
const int ethernet_cs = 10;
long interval = 200000;
long previousMillis = 0;
byte mac[] = {
UP to YOU };
IPAddress ip(DEPENDS);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
File myFile;
char data [] = "";
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
pinMode(sd_cs, OUTPUT);
pinMode(ethernet_cs, OUTPUT);
Ethernet.begin(mac, ip);
server.begin();
Serial.print("Init Serv @ ");
Serial.println(Ethernet.localIP());
digitalWrite(ethernet_cs, HIGH);
digitalWrite(sd_cs, LOW);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
myFile = SD.open("temps.txt", FILE_WRITE);
myFile.close();
}
void loop() {
checkClient();
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
recordConditions();
}
}
void checkClient() // listen for incoming clients
{
digitalWrite(ethernet_cs, LOW);
digitalWrite(sd_cs, HIGH);
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("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// add a meta refresh tag, so the browser pulls again every 5 seconds:
client.println("<meta http-equiv=\"refresh\" content=\"5\">");
digitalWrite(ethernet_cs, HIGH);
digitalWrite(sd_cs, LOW);
myFile = SD.open("temps.txt");
if (myFile) {
Serial.println("temps.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
char data = myFile.read();
digitalWrite(ethernet_cs, LOW);
digitalWrite(sd_cs, HIGH);
client.print(data);
Serial.write(data);
}
// close the file:
digitalWrite(ethernet_cs, HIGH);
digitalWrite(sd_cs, LOW);
myFile.close();
}
else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
digitalWrite(ethernet_cs, LOW);
digitalWrite(sd_cs, HIGH);
client.println(" done");
Serial.println(" done");
client.println("<br />");
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");
}
}
}
No comments:
Post a Comment