void Webserver() { String readString = ""; char c; char br[5] = "
"; char dp[3] = ": "; EthernetClient client = server.available(); if (client) { // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { c = client.read(); readString += c; if (c == '\n' && currentLineIsBlank) { // Datei Upload if (readString.indexOf("multipart/form-data;") > 0) { readString = ""; currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char w = client.read(); readString += w; if (w == '\n' && currentLineIsBlank) { // Dateiname extrahieren, Datei anlegen int a = readString.indexOf("filename=") + 10; readString = readString.substring(a); a = readString.indexOf("\""); readString.remove(a); if (SD.exists(readString)) { Serial.print(F("Upload: ")); Serial.print(readString); Serial.println(F(" ist bereits vorhanden, wird berschrieben.")); SD.remove(readString); } sdFile = SD.open(readString, FILE_WRITE); int i = 0; // stay in this loop until the file has been received while (client.connected()) { if (client.available()) { w = client.read(); // get file byte if (w == 0x2D) { // 1. w = client.read(); if (w == 0x2D) { // 2. w = client.read(); if (w == 0x2D) { // 3. w = client.read(); if (w == 0x2D) { // 4. w = client.read(); if (w == 0x2D) { // 5. w = client.read(); if (w == 0x2D) { // 6. sdFile.close(); if (SD.exists(readString)) { Serial.print(F("Upload: ")); Serial.print(readString); Serial.println(F(" OK")); } else { Serial.print(F("Upload: ")); Serial.print(readString); Serial.println(F(" Fehlgeschlagen")); } goto Configuration; //client.stop(); //break; } else { sdFile.print("-"); sdFile.print("-"); sdFile.print("-"); sdFile.print("-"); sdFile.print("-"); sdFile.print(w); } } else { sdFile.print("-"); sdFile.print("-"); sdFile.print("-"); sdFile.print("-"); sdFile.print(w); } } else { sdFile.print("-"); sdFile.print("-"); sdFile.print("-"); sdFile.print(w); } } else { sdFile.print("-"); sdFile.print("-"); sdFile.print(w); } } else { sdFile.print("-"); sdFile.print(w); } } else { sdFile.print(w); } } } } // detect the end of the incoming HTTP header if (w == '\n') { // starting a new line currentLineIsBlank = true; } else if (w != '\r') { // got a character on the current line currentLineIsBlank = false; } } } } // Download von Dateien von der SD-Karte if (readString.indexOf("?download") > 0 ) { int a = readString.indexOf("?download") + 10; int b = readString.indexOf(" HTTP/"); readString = readString.substring(a, b); client.println(F("HTTP/1.1 200 OK")); client.println(F("Content-Type: text/html")); client.println(); client.println(F("")); client.println(F("")); client.println(F("")); client.print(F("ATS-Konfiguration (")); client.print(readString); client.println(F(")")); client.println(F("")); client.println(F("
"));
            readString.toUpperCase();
            sdFile = SD.open(readString);
            if (!sdFile) {
              client.println(F("

File not Found!

")); Serial.println(F("Download: ")); Serial.print(readString); Serial.println(F(" nicht gefunden!")); client.stop(); break; } else { Serial.print(F("Download: ")); Serial.println(readString); while (sdFile.available()) { int x = sdFile.read(); if (x == 60) client.print("<"); if (x == 62) client.print(">"); if (x != 60 && x != 62) client.print(char(x)); } sdFile.close(); } client.println(F("
")); client.stop(); break; } // Reboot Arduino if (readString.indexOf("?reset") > 0 ) { PgmPrintln("Anforderung per Webfrontend zum Reset."); sendEmail(16); client.println(F("HTTP/1.1 200 OK")); client.println(F("Content-Type: text/html")); client.println(); client.print(F("ATS

Rebooting...

")); delay(1); client.stop(); delay(2000); softReset(); break; } // Datei von SD-Karte lschen if (readString.indexOf("?deletefile") > 0 ) { //Serial.print(readString); int a = readString.indexOf("?deletefile") + 12; int b = readString.indexOf("&"); int c = readString.indexOf(" HTTP"); String filename = readString.substring(a, b); if (readString.substring(b + 1, c) == "delete=OK") { filename.toUpperCase(); if (filename != "CONFIG.INI") { if (SD.exists(filename)) { SD.remove(filename); if (!SD.exists(filename)) { Serial.print(F("Delete: ")); Serial.print(filename); Serial.println(F(" OK")); } else { Serial.print(F("Delete: ")); Serial.print(filename); Serial.println(F(" Fehlgeschlagen")); } } else { Serial.println(F("Delete: ")); Serial.print(filename); Serial.println(F(" nicht gefunden!")); } } else { Serial.println(F("Delete: config.ini darf nicht gelscht werden!")); } } goto Configuration; break; } // Datei lschen / Sicherheitsabfrage if (readString.indexOf("?askdelete") > 0 ) { int a = readString.indexOf("?askdelete") + 11; int b = readString.indexOf(" HTTP/"); readString = readString.substring(a, b); client.println(F("HTTP/1.1 200 OK")); client.println(F("Content-Type: text/html")); client.println(); client.print(F("ATS-Konfiguration")); client.print(F("")); client.print(F("
")); client.print(F("")); client.print(F("")); client.print(F("")); client.print(F("")); client.print(F("")); client.print(F("")); client.print(F("")); client.print(F("

Achtung!

Soll ")); client.print(readString); client.print(F(" wirklich gelöscht werden?

")); client.print(F("")); delay(1); client.stop(); break; } // Konfiguration anzeigen if (readString.indexOf("?config") > 0 ) { Configuration: client.println(F("HTTP/1.1 200 OK")); client.println(F("Content-Type: text/html")); client.println(); client.println(F("")); client.println(F("")); ParseContent(&client, Config_File); break; } // Notfall Formular um Dateien hoch zu laden if (readString.indexOf("?upload") > 0 ) { client.println(F("HTTP/1.1 200 OK")); client.println(F("Content-Type: text/html")); client.println(); client.print(F("ATS

Datei auf SD-Karte speichern

")); client.print(F("
")); client.print(F("  ")); client.print(F("  ")); client.print(F("  ")); client.print(F("
")); delay(1); client.stop(); break; } if (readString.indexOf("?power") > 0 ) { // Abfrage der aktuellen Leistung client.println(F("HTTP/1.1 200 OK")); client.println(F("Content-Type: text/plain")); client.println(); client.println(ConvSeperator(StrLeistung)); delay(1); client.stop(); break; } if (readString.indexOf("?mainbet") > 0 ) { // Netzbetrieb PgmPrintln("Anforderung per Webfrontend zum Netzbetrieb"); Status = 0; // "Netzbetrieb" Alert(HCServer, HCRequest, HCStop); if (!Generator(OFF)) { SwitchNetzbetrieb(); sendEmail(2); } else { Status = 4; // "Strung Manuell" sendEmail(3); } Print_Status[Status]; } if (readString.indexOf("?genbet") > 0 ) { // Generatorbetrieb, Stromausfall simulieren PgmPrintln("Anforderung per Webfrontend zum Generatorbetrieb"); Status = 2; // Manueller Betrieb Alert(HCServer, HCRequest, HCStart); if (Generator(ON)) { SwitchGeneratorbetrieb(); sendEmail(0); } else { Status = 4; // "Strung Manuell" Alert(HCServer, HCRequest, HCError); sendEmail(1); } Print_Status[Status]; } if (readString.indexOf("?genstart") > 0 ) { // Generator starten PgmPrintln("Anforderung per Webfrontend zum Generatorstart"); Alert(HCServer, HCRequest, HCStart); if (Generator(ON)) { Status = 18; // "Generator luft" sendEmail(4); } else { Status = 4; // "Strung Manuell" Alert(HCServer, HCRequest, HCError); sendEmail(5); } Print_Status[Status]; } if (readString.indexOf("?genstop") > 0 ) { // Generator stoppen PgmPrintln("Anforderung per Webfrontend zum Generatorstopp"); Alert(HCServer, HCRequest, HCStop); if (!Generator(OFF)) { Status = 0; // "Netzbetrieb" sendEmail(6); } else { Status = 4; // Strung Manuell sendEmail(7); } Print_Status[Status]; } if (readString.indexOf("?sendmail") > 0 ) { // Status-Seite als E-Mail versenden PgmPrintln("Anforderung per Webfrontend zum Status-Mail-Versand"); sendEmail(14); } // HTTP-Request lschen um Speicher frei zu geben. readString = ""; // Webseite an Client ausliefern client.println(F("HTTP/1.1 200 OK")); client.println(F("Content-Type: text/html")); client.println(); client.println(F("")); client.println(F("")); ParseContent(&client, ATS_File); 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; } } } delay(1); client.stop(); } } void DeliverSDRoot(EthernetClient *client) { File root; root = SD.open("/"); while (true) { File entry = root.openNextFile(); if (! entry) { // no more files break; } if (!entry.isDirectory()) { client->print(F("")); String filename = String(entry.name()); filename.toLowerCase(); client->print(F("print(host); client->print(F(".")); client->print(domain); client->print(F("/?download=")); client->print(filename); client->print(F("\">")); client->print(filename); client->print(F("")); client->print(F("")); client->print(entry.size(), DEC); client->print(F("")); client->print(F("print(host); client->print(F(".")); client->print(domain); client->print(F("/?askdelete=")); client->print(filename); if (filename == "config.ini") { client->print(F("\">")); } else { client->print(F("\">X")); } client->print(F("")); client->println(F("")); } entry.close(); } delay(1); } void DeliverContent(EthernetClient *client, char *filename) { char c; char temp[200] = {}; int i = 0; sdFile = SD.open(filename); if (!sdFile) { Serial.println(F("DeliverContent")); Serial.print(filename); Serial.println(F(" nicht gefunden!")); return false; } while (sdFile.available()) { c = sdFile.read(); if ((c == '\n')) { // Zeile fertig eingelesen int len = strlen(temp); temp[len] = '\0'; client->print(temp); // ... und an Webclient ausliefern i = 0; memset(temp, '\0', sizeof(temp)); } else { temp[i] = c; i++; } } sdFile.close(); } void ParseContent(EthernetClient *client, char *filename) { char c; char temp[200] = {}; String strLine = ""; int i = 0; // Ist die Datei berhaupt da? sdFile = SD.open(filename); if (!sdFile) { Serial.println(F("ParseWebContent")); Serial.print(filename); Serial.println(F(" nicht gefunden!")); return false; } while (sdFile.available()) { c = sdFile.read(); if ((c == '\n')) { // Zeile fertig eingelesen int len = strlen(temp); temp[len] = '\0'; strLine = String(temp); // SD-Karteninhalt ausgeben if (strLine.indexOf("") > 0) { DeliverSDRoot(client); } strLine.replace("%ram%", String(ram)); strLine.replace("%ver%", version); strLine.replace("%z%", showTime()); strLine.replace("%d%", showDate()); strLine.replace("%sta%", Web_Status(Status)); if (zuendung) { if (Status == 1 || Status == 18) { strLine.replace("%gen%", "Ein"); } else { strLine.replace("%gen%", "Standby"); } } else { strLine.replace("%gen%", "Aus"); } strLine.replace("%sys%", ConvSeperator(t_data.SysTemperatur)); strLine.replace("%mot%", ConvSeperator(t_data.MotorTemperatur)); strLine.replace("%aut%", ConvSeperator(t_data.AussenTemperatur)); strLine.replace("%bat%", ConvSeperator(GeneratorBatterie)); strLine.replace("%p%", ConvSeperator(StrLeistung)); strLine.replace("%1v%", String(p_data.L1_Spannung, 0)); strLine.replace("%2v%", String(p_data.L2_Spannung, 0)); strLine.replace("%3v%", String(p_data.L3_Spannung, 0)); strLine.replace("%1c%", ConvSeperator(String(p_data.L1_Strom, 1))); strLine.replace("%2c%", ConvSeperator(String(p_data.L2_Strom, 1))); strLine.replace("%3c%", ConvSeperator(String(p_data.L3_Strom, 1))); strLine.replace("%lm%", String(lostmain)); strLine.replace("%bm%", String(backmain)); strLine.replace("%gt%", String(glowtime)); strLine.replace("%it%", String(ignitiontime * 100)); strLine.replace("%st%", String(starttrying)); strLine.replace("%startd%", String(genstartdelay)); strLine.replace("%stopd%", String(genstoppdelay)); strLine.replace("%sd%", String(switchdelay)); strLine.replace("%mv%", ConvSeperator(String(minbatvoltage, 1))); strLine.replace("", "ö"); strLine.replace("", "ä"); strLine.replace("", "ü"); strLine.trim(); client->println(strLine); // ... und an Webclient ausliefern i = 0; memset(temp, '\0', sizeof(temp)); } else { temp[i] = c; i++; } } sdFile.close(); }