I am trying to create a web interface for an Arduino. It should listen to requests like /home, /led_status, /LED=ON and /LED=OFF. It, however, does not work like I hoped. Whenever I try to visit those requests (E.g. 192.168.2.200/home) via the browser, the Arduino will always go into the else statement, meaning indexOf does not seem to work. If I'll send a request via Postman, however, it does not go in the else statement.
Though this is very annoying, the SD card connection seems to be corrupted after the point where it gets in the else statement. If I click the reset button, I'll get an predefined error saying that the SD card initalization failed. By unplugging and plugging in the SD, and then click reset, the system functions as normal.
How can I resolve this? I found problems like overheating, bad soldered board, bad ethernet controller, but each does not seem to apply.
My sketch:
#include <SPI.h>
#include <Ethernet.h>
#include <SdFat.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 2, 200);
EthernetServer server(80);
String request;
SdFat SD;
SdFile myFile;
void setup() {
Ethernet.begin(mac, ip);
server.begin();
Serial.begin(9600);;
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
pinMode(7, OUTPUT);
//pinMode(13, OUTPUT);
Serial.println("Initializing SD card");
if (!SD.begin(4, SPI_FULL_SPEED)) {
Serial.println("ERROR - SD card initialization failed");
//digitalWrite(13, HIGH);
return;
}
Serial.println("SUCCESS - SD card initialized.");
if (!SD.exists("index.htm")) {
Serial.println("ERROR - Can't find index");
//digitalWrite(13, HIGH);
return;
}
Serial.println("SUCCESS - Found index");
}
void loop() {
EthernetClient client = server.available();
if (client) {
boolean lineBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
request += c;
if (c == '\n' && lineBlank) {
if (request.indexOf("led_status") > -1) {
OK(client);
GetLedState(client);
client.println("State: ");
client.println(digitalRead(7));
Serial.println("Led Switch");
Serial.println("LED");
} else if (request.indexOf("LED=ON") > -1) {
OK(client);
digitalWrite(7, HIGH);
client.println("Turned ON");
Serial.println("LEDON");
} else if (request.indexOf("LED=OFF") > -1) {
OK(client);
digitalWrite(7, LOW);
client.println("Turned OFF");
Serial.println("LEDOFF");
} else if (request.indexOf("favicon") > -1) {
client.println("Favicon");
} else if (request.indexOf("home") > -1) {
Serial.println("Home");
myFile.open("index.htm", O_READ);
OK(client);
while (myFile.available()) {
client.write(myFile.read());
}
myFile.close();
} else {
Serial.println("Else");
}
Serial.println();
Serial.println();
Serial.print(request);
Serial.println();
Serial.println();
request = "";
break;
}
if (c == '\n') {
lineBlank = true;
} else if (c != '\r') {
lineBlank = false;
}
}
}
delay(1);
client.stop();
}
}
void OK(EthernetClient client) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: keep-alive");
client.println();
}
void GetLedState(EthernetClient cl) {
if (digitalRead(7)) {
Serial.println("Led state: ON");
} else {
Serial.println("Led state: OFF");
}
}
My Arduino setup: (Just imagine there is a ethernet shield on top of the arduino, cables are plugged in the shield, sd card in the sd card slot)
