So I'm playing around to make a webserver. I found a nice tutorial on the net Arduino Ethernet Shield Webserver probably familiar for some of you guys and girls.
But when I want to use the ethernet shield together with the SD carder reader some things go wrong. The idea is to write a page in HTML and copy this to the SD card. I have done this by copy paste next run a webserver which takes the webfile and make it visible. Unfortunately this does'nt seem to work. Then after reading here on the forum and search on google it seems while initializing the sd card you need to disable the W5500 chip. I have tried that but I think I stil do something wrong. This is " my code" which I try to use could anybody give me some advise on what i am doing wrong?
#include <SPI.h>
#include <Ethernet2.h>
#include <SD.h>
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x85, 0xB2 };
IPAddress ip(192, 168, 178, 48); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
File webFile;
void setup()
{
pinMode(10, OUTPUT);
digitalWrite(10,HIGH);
Serial.begin(9600); // for debugging
// initialize SD card
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.");
// check for index.htm file
if (!SD.exists("index.htm")) {
Serial.println("ERROR - Can't find index.htm file!");
return; // can't find index file
}
Serial.println("SUCCESS - Found index.htm file.");
delay(2000);
Ethernet.begin(mac, ip); //Start de Ethernet connectie.
server.begin(); //Start de server.
Serial.print("[-- De server is actief op IP: ");
Serial.print(Ethernet.localIP());
Serial.println(" --]");
}
void loop()
{
EthernetClient client = server.available(); // try to get client
if (client) { // got client?
boolean currentLineIsBlank = true;
Serial.println("SUCCESS got client");
while (client.connected()) {
Serial.println("SUCCESS connected");
if (client.available()) { // client data available to read
Serial.println("SUCCESS client data available");
char c = client.read(); // read 1 byte (character) from client
Serial.println("SUCCESS read Char");
// 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
Serial.println("SUCCESS - send standard response file.");
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
// send web page
webFile = SD.open("index.htm"); // open web page file
Serial.println("SUCCESS opened webfile");
if (webFile) {
while(webFile.available()) {
client.write(webFile.read()); // send web page to client
}
webFile.close();
}
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)
}
So after it tells me the IP adress where the server is active nothing happens anymore. I have placed extra serial prints in order to see where things go wrong.
ps actually I do not understand your comment exactly even the examples from the shield use that line that seems wrong to you but in fact those examples work fine with me.
Where should I put that global declaration you use capital name for the Client but in the example all lower cases.
Sorry but what is your suggestion on how to make it work?
If I use webserver examples without using a SD-card it works fine.
If I use examples to test or datalog data to my SD-card it also works fine
When I want to combine the two it doesnt work
I've misread what you wrote, my apologies, I thought you were trying to connect to another server rather than process an incoming request. Just ignore that posting.
I'm using the ethernet shield Rev2 and a Mega2560.
I just found out if I make pin 4 high it initialize my sd card correctly and before ethernet.begin I make it low so than it finds my server. Now I need to find a way to be able to make it work such that it shows my webpage.
This is what I have now note the pin 4 toggle between initializing the SD card and using the server.
The smal step I made now is that when entering the server via my web browser it finds the page but doesnt show the correct contents, but this is a step closer to the correct result
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x85, 0xB2 };
IPAddress ip(192, 168, 178, 48); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
File webFile;
void setup()
{
pinMode(4, OUTPUT);
digitalWrite(4,HIGH);
Serial.begin(9600); // for debugging
// initialize SD card
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.");
// check for index.htm file
if (!SD.exists("index.htm")) {
Serial.println("ERROR - Can't find index.htm file!");
return; // can't find index file
}
Serial.println("SUCCESS - Found index.htm file.");
delay(2000);
digitalWrite(4,LOW);
Ethernet.begin(mac, ip); //Start de Ethernet connection.
server.begin(); //Start de server.
Serial.print("Active IP: ");
Serial.print(Ethernet.localIP());
}
void loop()
{
EthernetClient client = server.available(); // try to get client
if (client) { // got client?
boolean currentLineIsBlank = true;
Serial.println("SUCCESS got client");
while (client.connected()) {
Serial.println("SUCCESS connected");
if (client.available()) { // client data available to read
Serial.println("SUCCESS client data available");
char c = client.read(); // read 1 byte (character) from client
Serial.println("SUCCESS read Char");
// 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
Serial.println("SUCCESS - send standard response file.");
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
// send web page
webFile = SD.open("index.htm"); // open web page file
Serial.println("SUCCESS opened webfile");
if (webFile) {
while(webFile.available()) {
client.write(webFile.read()); // send web page to client
}
webFile.close();
}
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)
}
I noticed this in the Arduino library documentation:
"The Arduino board communicates with the shield using the SPI bus. This is on digital pins 11, 12, and 13 on the Uno and pins 50, 51, and 52 on the Mega. On both boards, pin 10 is used as SS. On the Mega, the hardware SS pin, 53, is not used to select the Ethernet controller chip, but it must be kept as an output or the SPI interface won't work."
No idea if it is relevant or not, but you may wan to try setting pin 53 as output just to check.
But should I make make pin 53 High continiously or toggle. Pin 53 is not even connected to the Ethernet Shield. I'll try to upload a pic give me a moment.
I start to think that I need to toggle with pin 4, I dont know for sure its been a long time I played with the arduino's . Eventhough this kind of issues are "annoying" they are cool as well ......just to learn stuff