Working http server with sdcard
This commit is contained in:
@@ -1,12 +1,12 @@
|
|||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#include <Ethernet.h>
|
#include <Ethernet.h>
|
||||||
#include <SD.h>
|
#include <SdFat.h>
|
||||||
#include <String.h>
|
#include <String.h>
|
||||||
|
|
||||||
byte mac[] = {
|
byte mac[] = {
|
||||||
0xA8, 0x61, 0x0A, 0xAE, 0x6F, 0x4A
|
0xA8, 0x61, 0x0A, 0xAE, 0x6F, 0x4A
|
||||||
};
|
};
|
||||||
|
SdFat SD;
|
||||||
EthernetServer server(80);
|
EthernetServer server(80);
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
@@ -17,15 +17,17 @@ void setup() {
|
|||||||
}
|
}
|
||||||
Serial.println("Arduino Ethernet WebServer");
|
Serial.println("Arduino Ethernet WebServer");
|
||||||
|
|
||||||
Serial.print("Initializing SD card...");
|
Serial.print("Starting SD...");
|
||||||
if (!SD.begin(4)) {
|
if (!SD.begin(4)) {
|
||||||
Serial.println("initialization failed!");
|
Serial.println("failed");
|
||||||
while (true) {
|
while (true) {
|
||||||
delay(1);
|
delay(15);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else Serial.println("ok");
|
||||||
|
|
||||||
Serial.println("Initialize Ethernet with DHCP:");
|
Serial.println("Initialize Ethernet with DHCP:");
|
||||||
|
|
||||||
if (Ethernet.begin(mac) == 0) {
|
if (Ethernet.begin(mac) == 0) {
|
||||||
Serial.println("Failed to configure Ethernet using DHCP");
|
Serial.println("Failed to configure Ethernet using DHCP");
|
||||||
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
|
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
|
||||||
@@ -34,7 +36,7 @@ void setup() {
|
|||||||
Serial.println("Ethernet cable is not connected.");
|
Serial.println("Ethernet cable is not connected.");
|
||||||
}
|
}
|
||||||
while (true) {
|
while (true) {
|
||||||
delay(1);
|
delay(15);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,37 +49,50 @@ void setup() {
|
|||||||
void loop() {
|
void loop() {
|
||||||
EthernetClient client = server.available();
|
EthernetClient client = server.available();
|
||||||
if (client) {
|
if (client) {
|
||||||
String res = client.readStringUntil('\n');
|
SdFile file;
|
||||||
int str_len = res.length() + 1;
|
{
|
||||||
char char_array[str_len];
|
char* path;
|
||||||
res.toCharArray(char_array, str_len);
|
{
|
||||||
char* method = strtok(char_array, " ");
|
String res = client.readStringUntil(" HTTP/1.1");
|
||||||
char* path = strtok(0, " ");
|
char char_array[res.length() + 1];
|
||||||
Serial.println(method);
|
res.toCharArray(char_array, res.length() + 1);
|
||||||
Serial.println(path);
|
strtok(char_array, " ");
|
||||||
if (client.available()) {
|
path = strtok(0, " ");
|
||||||
if (method != "GET") {
|
|
||||||
sendMethodNotAllowed(client);
|
|
||||||
} else {
|
|
||||||
sendHeaders(client);
|
|
||||||
}
|
}
|
||||||
|
if (strcmp(path, "/") == 0) {
|
||||||
|
path = "/index.html";
|
||||||
|
}
|
||||||
|
Serial.println(path);
|
||||||
|
if (!file.open(path)) {
|
||||||
|
sendNotFound(client);
|
||||||
|
delay(15);
|
||||||
|
client.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
char buf[25];
|
||||||
|
sendHeaders(client);
|
||||||
|
while (file.available())
|
||||||
|
{
|
||||||
|
file.read(buf, sizeof(buf));
|
||||||
|
client.write(buf, sizeof(buf));
|
||||||
}
|
}
|
||||||
delay(15);
|
delay(15);
|
||||||
client.stop();
|
client.stop();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendHeaders(EthernetClient client) {
|
void sendHeaders(EthernetClient client) {
|
||||||
client.println("HTTP/1.1 200 OK");
|
client.println("HTTP/1.1 200 OK");
|
||||||
client.println("Content-Type: text/html");
|
client.println("Cache-Control: max-age=31536000");
|
||||||
client.println("Connection: close"); // the connection will be closed after completion of the response
|
client.println("Connection: close");
|
||||||
client.println();
|
client.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendMethodNotAllowed(EthernetClient client) {
|
void sendNotFound(EthernetClient client) {
|
||||||
client.println("HTTP/1.1 405 Method Not Allowed");
|
client.println("HTTP/1.1 404 Not Found");
|
||||||
client.println("Content-Type: text/html");
|
client.println("Content-Type: text/html");
|
||||||
client.println("Allow: GET");
|
|
||||||
client.println("Connection: close"); // the connection will be closed after completion of the response
|
client.println("Connection: close"); // the connection will be closed after completion of the response
|
||||||
client.println();
|
client.println();
|
||||||
client.println("<!doctype html>");
|
client.println("<!doctype html>");
|
||||||
@@ -85,8 +100,8 @@ void sendMethodNotAllowed(EthernetClient client) {
|
|||||||
client.println("<title>Arduino Dashboard</title>");
|
client.println("<title>Arduino Dashboard</title>");
|
||||||
client.println("</head>");
|
client.println("</head>");
|
||||||
client.println("<body>");
|
client.println("<body>");
|
||||||
client.println("<h1>405 Method Not Allowed</h1>");
|
client.println("<h1>404 Not Found</h1>");
|
||||||
client.println("<p>the method received in the request-line is known by the origin server but not supported by the target resource.</p>");
|
client.println("<p>The requested URL was not found on this server.</p>");
|
||||||
client.println("</body>");
|
client.println("</body>");
|
||||||
client.println("</html>");
|
client.println("</html>");
|
||||||
client.println();
|
client.println();
|
||||||
|
|||||||
Reference in New Issue
Block a user