我买了一个 Arduino Ethernet Shield,但我在使用 SD 卡上的文件的 Web 服务器时遇到了问题。有一个服务器在端口 80 上运行服务数据,但是当我加载它时,我似乎得到了类似的数据
文件·找不到
文件未找到
文件未找到
文件未找到
标题>04 NotÁFhund<šhtml¦ 找不到文件
文件未找到
文件未找到
<”html> * 文件„未找到ž/p> žtitle>404 Not F·und
或者它开始下载与上面类似的东西(我假设标题像实际内容一样损坏)
如果我不使用 SD 卡,而只提供写入草图的预先编写的网页,那么只要 SD 卡不在插槽中,页面就会正确显示。此外,SD 库说它存在时看不到“index.html”
我在 Arduino Uno 上使用 Transcend Micro SDHC 4GB (FAT32) 和 Ethernet Shield R3,并尝试格式化 Micro SD 卡。我的草图如下。
#include <SPI.h>
#include <Ethernet.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, 130); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
File webFile;
void setup()
{
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
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.html")) {
Serial.println("ERROR - Can't find index.html!");
}
Serial.print("Running on ");
Serial.println(Ethernet.localIP());
}
void loop()
{
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();
client.println("<html><head><title>404 Not Found</title></head><body><p>File not found</p></body></html>");
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)
}