Hello I am having some conflict trouble with the Ethernet shield. I have written a program to read two Ic2 temperature chips and display the result on a web page. Using the Ethernet shield on its own with no SD library included the sketch works perfectly. When I add the SD library the web page is lost. I know the SD library works as I have unused it to send a web page during the examples.
What I was hoping to do is show the temperature figures on the web site and record the temperature figures periodically.
My code is below. Can someone please point me in the right direction? I am new to this and still learning so sorry if this turns out to be a silly mistake I’m making.
I have searched the forum and found a number of people with the same conflict but they have not explained how they overcome it.
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <SD.h>
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 125); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
int panel_address = 72;
int lower_address = 77;
int pump = 3;
int ledStatus = 5;
int ledState;
String HTTP_req; // stores the HTTP request
void setup()
{
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin();
Wire.begin();
pinMode (10, OUTPUT);
digitalWrite (10,HIGH);
Serial.begin(9600);
Serial.println("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("ERROR - SD card initialization failed!");
return; // init failed
}
Serial.println("SUCCESS - SD card initialized.");
pinMode (pump, OUTPUT);
pinMode (ledStatus, OUTPUT);
ledState = LOW;
}
void loop()
{
EthernetClient client = server.available(); // try to get client
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite (ledStatus, ledState);
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
HTTP_req += c; // save the HTTP request 1 char at a time
// 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: keep-alive");
client.println();
// AJAX request for Temp state
if (HTTP_req.indexOf("ajax_Temp") > -1) {
// read Temp state and send appropriate paragraph text
GetTempState(client);
}
else { // HTTP request for web page
// send web page - contains JavaScript with AJAX calls
client.println("<!DOCTYPE html>");
client.println("<html>");
client.println("<head>");
client.println("<title>Solar Panel</title>");//page heading title
client.println("<script>");
client.println("function GetTempState() {");
client.println("nocache = \"&nocache=\"\
+ Math.random() * 1000000;");
client.println("var request = new XMLHttpRequest();");
client.println("request.onreadystatechange = function() {");
client.println("if (this.readyState == 4) {");
client.println("if (this.status == 200) {");
client.println("if (this.responseText != null) {");
client.println("document.getElementById(\"Temp_txt\")\
.innerHTML = this.responseText;");
client.println("}}}}");
client.println(
"request.open(\"GET\", \"ajax_Temp\" + nocache, true);");
client.println("request.send(null);");
client.println("setTimeout('GetTempState()', 1000);");
client.println("}");
client.println("</script>");
client.println("</head>");
client.println("<body onload=\"GetTempState()\">");
client.println("<h1>Solar Panel Temperatures</h1>");
client.println(
"<p id=\"Temp_txt\">Temp state: Not requested...</p>");
client.println("</body>");
client.println("</html>");
}
// display received HTTP request on serial port
Serial.print(HTTP_req);
HTTP_req = ""; // finished with request, empty string
break;
}
// 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
} // end if (client)
}
// send the state of the Temp to the web browser
void GetTempState(EthernetClient cl)
{
Wire.beginTransmission(panel_address);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(panel_address, 1);
while (Wire.available () == 0);
int Panel = Wire.read();
delay (500);
Wire.beginTransmission(lower_address);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(lower_address, 1);
while (Wire.available () == 0);
int lowerTemp = Wire.read();
delay (500);
cl.println("<p>Panel Temp<p>");
cl.println(Panel);
cl.println ("<p>Lower Temp<p>");
cl.println(lowerTemp);
int diff = lowerTemp + 2;
cl.println ("<p>Panel Temperature pump turns on at<p>");
cl.println (diff);
cl.println ("<P>Pump Status<P>");
if (diff < Panel) {
digitalWrite (pump, HIGH);
cl.println ("<P> ON<P>");
delay(1000);
digitalWrite (pump, LOW);
delay (1000);
}
else{
digitalWrite (pump, LOW);
}
}