/* Web Server A simple web server that shows the value of the analog input pins. using an Arduino Wiznet Ethernet shield. Circuit: * Ethernet shield attached to pins 10, 11, 12, 13 * Analog inputs attached to pins A0 through A5 (optional) created 18 Dec 2009 by David A. Mellis modified 9 Apr 2012 by Tom Igoe ergänzt um Poldis interektiven Webserver am 19.4.2013 von A. Lompe */ #include #include // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //IPAddress ip(192,168,0, 177); byte ip[] = { 192, 168, 0, 222 }; byte gateway[] = { 192, 168, 0, 1 }; byte subnet[] = { 255, 255, 255, 0 }; // Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): EthernetServer server(80); int temp = 0; int Pin2 = 2; // Pin 2 statt 4 falls die SD-Karte verwendet werden soll int Pin3 = 3; boolean flag = true; //damit nur bei Änderungen die Seite geladen wird (kann ggf. entfallen) String Meldung = String(""); // string for fetching data from address boolean Pin2ON = false; boolean Pin3ON = false; // Status flag void setup() { pinMode(Pin3, OUTPUT); pinMode(Pin2, OUTPUT); // Open serial communications and wait for port to open: Serial.begin(115200); // geht auch langsamer while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } // start the Ethernet connection and the server: Ethernet.begin(mac, ip, gateway, subnet); server.begin(); Serial.print("server is at "); Serial.println(Ethernet.localIP()); } void loop() { // listen for incoming clients EthernetClient client = server.available(); if (client) { Serial.println("new client"); // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); //-------------------------------------------------------------------------------------- //read char by char HTTP request if (Meldung.length() < 12) { //store characters to string //Meldung.append(c); //removed by Katsu Meldung = Meldung + c; // insert by Katsu // very simple but it works... } //-----------------------------------------------------------------------------------------*/ // 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) { //-------------------------------------------------------------------------------------------- if(Meldung.indexOf("2=ein") > -1) { digitalWrite(Pin2, HIGH); Serial.println("Pin 2 eingeschaltet!"); Pin2ON = true; flag = true; } if(Meldung.indexOf("2=aus") > -1){ digitalWrite(Pin2, LOW); Serial.println("Pin 2 ausgeschaltet!"); Pin2ON = false; flag = true; } if(Meldung.indexOf("3=ein") > -1) { digitalWrite(Pin3, HIGH); Serial.println("Pin 3 eingeschaltet!"); Pin3ON = true; flag = true; } if(Meldung.indexOf("3=aus") > -1){ digitalWrite(Pin3, LOW); Serial.println("Pin 3 ausgeschaltet!"); Pin3ON = false; flag = true; } if(Meldung.indexOf("all=Al") > -1){ digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW); Serial.println("Alles ausgeschaltet!"); Pin2ON = false; Pin3ON = false; flag = true; } //-----------------------------------------------------------------------------------------------*/ Meldung=""; // send a standard http response header client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connnection: close"); client.println(); client.println(""); client.println(""); client.print(""); client.print("Arduino Webserver"); // add a meta refresh tag, so the browser pulls again every 5 seconds: // Diese Zeile für den automatischen Abruf wieder einfügen; simuliert dann aber einen Mausklick auf das erste input-Feld. // kann man sicher ändern, ich weiß aber im Moment nicht wie. // client.println(""); // output the value of each analog input pin client.println(""); client.print(""); for (int analogChannel = 0; analogChannel < 6; analogChannel++) { int sensorReading = analogRead(analogChannel); client.print("analog input "); client.print(analogChannel); client.print(" is "); client.print(sensorReading); client.println("
"); } //--------------------------HTML------------------------ if (flag) { //---Überschrift--- client.println("

"); client.print("

"); client.print("Arduino Webserver 1."); client.print(temp); client.println("

"); client.println("

"); //---Überschrift--- //---Ausgänge schalten--- client.println("
"); client.print(""); client.println("Ausgänge schalten:
"); client.println("
"); client.println(""); client.println(""); client.print(""); client.print(""); client.print(""); client.print(""); client.println(""); client.print(""); client.print(""); client.print(""); client.print(""); client.println("
"); client.print(""); client.println("Ausgang 2
"); client.print("
"); client.print(""); client.println("
"); client.print("
"); client.print(""); client.println("
"); if (Pin2ON) { client.print("ON"); } else { client.print("OFF"); } client.println("
"); client.print(""); client.println("Ausgang 3
"); client.println("
"); client.println("
"); if (Pin3ON) { client.print("ON"); } else { client.print("OFF"); } client.println("
"); client.println("
"); client.print("
"); client.print(""); client.println("
"); //*/ client.println(""); } // Ende von: if (flag) //---Ausgänge schalten--- //-------------------------------------------------------------------------------------------- client.println(""); //*/ temp = temp +1; // Damit sich auch was bewegt auf dem Bildschirm break; //wozu? } 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("client disonnected"); } }