I ran your program here. I could not get it to work on either Arduino 1.8.13 + Teensyduino 1.53 or Arduino 1.8.15 + Teensyduino 1.54.
The main problem is the "while (client.connected()) {" loop. The browser doesn't disconnect, and it doesn't recognize the response as complete, so it just sits forever waiting.
Just adding a "break" at the end of transmitting the data makes it work on both versions.
I also changed the file read & write to this, which can confirm the data really is being read from the SD card if you uncomment 2 lines:
Code:
while (webFile.available()) {
int n = webFile.read();
//Serial.print(".");
//Serial.println((char)n);
client.write(n); // send web page to client
}
Here is a screenshot of it working with Arduino 1.8.15 + Teensyduino 1.54.
View attachment 25313
This is the complete program I ran.
Code:
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
File webFile; //webpage
#define SS_SD_CARD BUILTIN_SDCARD // using SD card in the main board
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 194, 72);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
//digitalWrite(SS_SD_CARD, LOW); // SD Card Enabled
delay(1000);
if (!SD.begin(SS_SD_CARD)) {
Serial.println("SD Card initialization failed!");
return;
}
Serial.println("SD Card initialization done.");
//digitalWrite(SS_SD_CARD, HIGH); // SD Card disabled
// You can use Ethernet.init(pin) to configure the CS pin
//Ethernet.init(10); // Most Arduino shields
//Ethernet.init(5); // MKR ETH shield
//Ethernet.init(0); // Teensy 2.0
Ethernet.init(10); // Teensy++ 2.0
//SPI.setSCK(14);
//Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Ethernet WebServer Example");
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// start the server
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
Serial.println("main loop");
delay(200);
// listen for incoming clients
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) {
Serial.println("send http header");
// 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();
Serial.println("about to open file.htm");
webFile = SD.open("file.htm"); // open web page file
Serial.println("about to opened file.htm");
if (webFile) {
Serial.println("open successful");
while (webFile.available()) {
int n = webFile.read();
//Serial.print(".");
//Serial.println((char)n);
client.write(n); // send web page to client
}
webFile.close();
}
else {
client.println("Can't find webpage! Check SD Card");
}
//client.println("This is a test...");
break; // <----- this is needed because browser does not close connection
}
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;
}
}
}
Serial.println("client no longer available");
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
I did have to change the IP number, and I commented out "SPI.setSCK(14);" since I tested with a
WIZ820 adapter and Wiz850io ethernet module.
Here's a photo of the hardware I used for this test.
View attachment 25312