#include #include // Einfacher Webserver // Im Browser http:/// eingeben // z.B. http://192.168.0.42/ // Einrichtung: siehe Echo-Programm // MAC-Adresse byte mac[] = { 0xAF, 0xFE, 0xAB, 0xBA, 0xDE, 0xAF }; // IP-Adresse IPAddress ip(192, 168, 0, 42); // Ob DHCP benutzt werden soll const bool USE_DHCP = true; // Server EthernetServer server(80); // HTTP-Protokoll LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display void setup() { // Serielle Schnittstelle zur Ausgabe der IP nutzen Serial.begin(9600); // Ethernet-Verbindung und Server starten Serial.println("IP-Adresse ueber DHCP anfordern..."); if (!USE_DHCP || Ethernet.begin(mac) == 0) { Serial.println("DHCP-Anfrage fehlgeschlagen - manuelle Konfiguration"); Ethernet.begin(mac, ip); } server.begin(); Serial.print("Arduino hat die IP-Adresse "); Serial.println(Ethernet.localIP()); } // Puffer fuer das empfangene Kommando vom Browser const int COMMAND_SIZE = 2048; char command[COMMAND_SIZE]; EthernetClient client; void handle_get(const char path[]) { Serial.print("GET: "); Serial.println(path); // TODO: LCD-Backlight an/ausschalten // TODO: path auf LCD ausgeben } void loop() { // Anfrage bearbeiten client = server.available(); if (client) { int currentLineIsBlank = 1; int cmd = 0; while (client.connected()) { // Client verbunden if (client.available()) { // Zeichen verfuegbar char c = client.read(); Serial.write(c); if (cmd < COMMAND_SIZE - 1) command[cmd++] = c; if (c == '\n' && currentLineIsBlank) { command[cmd] = '\0'; // zerlegen des strings char *method = strtok(command, " "); char *path = strtok(NULL, " "); if (strcmp(method, "GET") == 0) { handle_get(path); } // Antwort schicken client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); client.println(); client.println(""); client.println("Mein Arduino"); client.println(""); break; } if (c == '\n') { currentLineIsBlank = 1; } else if (c != '\r') { currentLineIsBlank = 0; } } } delay(1); client.stop(); } }