Hallo,
ich habe mir mit Hilfe des ENC28J60 und der EthernetENC Bibliothek (Nutzt die selbe API wie die Arduino Ethernet Bibliothek) einen Webserver auf einem Arduino Nano realisiert.
Es ergibt sich folgendes Problem:
- Wenn ich mit einem Browser die URL aurfurfe werden mir per Console zwei Verbindungen angezeigt. Hierbei ist es egal welcher Browser verwendet wird. Ebenso wird kein Code abgearbeitet.
- Mach ich einen Aufruf per CURL wird alles abgearbeitet.
void loop() {
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
String header = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
header += 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) {
// 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();
// Sonstige Programmfunktionen werden hier abgearbeitet
// Funktioniert nur bei CURL
if(compURL(header, "GET /on")) {
lightState = true;
staticOn();
}
break;
}
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;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
// Serial.println(header);
Serial.println("client disconnected");
}
}
int compURL(String _header, String _searchStr) {
int _idx = 0;
_idx = _header.indexOf(_searchStr);
if(_idx >= 0) {
return int(_idx + _searchStr.length());
} else {
return 0;
}
}
Konsolenausgabe bei CURL
new client
switched static on
client disconnected
Konsolenausgabe bei Aufruf mit dem Browser
new client
client disconnected
Bei CURL wird "switched static on" ausgegeben, bei dem Aufruf über den Browser nicht.
Aufruf: http://192.168.10.151/on
Ich vermute es hat etwas hiermit zu tun:
// 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) {
// send a standard http response header
...
break;
}
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;
}
Jemand eine Idee?
Danke