#include #include #include #include #include #include #include #include #include #include #include //Attach the serial display's RX line to digital pin 2 SoftwareSerial LCD(3,5); // pin 2 = TX, pin 3 = RX (unused) TimedAction resetToActive = TimedAction (1000, resetMsg); int curMin = 0; int curHour = 0; int curSec = 0; TimedAction t_updateSec = TimedAction (998, updateSec); // network configuration. gateway and subnet are optional. // the media access control (ethernet hardware) address for the shield: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEB }; //the IP address for the shield: byte ip[] = { 192, 168, 178, 42 }; // the router's gateway address: byte gateway[] = { 192, 168, 178, 1 }; // the subnet: byte subnet[] = { 255, 255, 0, 0 }; String curLine; String curGet; String completeRequest; // telnet defaults to port 80 EthernetServer server = EthernetServer(80); void setup() { pinMode (8, OUTPUT); pinMode (4, OUTPUT); if(!SD.begin (4)) { digitalWrite (8, HIGH); delay (500); digitalWrite (8, LOW); delay (500); digitalWrite (8, HIGH); delay (500); digitalWrite (8, LOW); delay (500); } // initialize the ethernet device Ethernet.begin(mac, ip, gateway, subnet); // start listening for clients server.begin(); LCD.begin(9600); //set up serial port for 9600 baud delay(600); //wait for LCD to boot up LCD.write (0x7C); LCD.write (157); clearLCD(); gotoPos (1); LCD.write ("ACTIVE"); Serial.begin (9600); } void loop() { resetToActive.check(); t_updateSec.check(); // listen for incoming clients EthernetClient client = server.available(); if (client) { resetToActive.disable(); // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); // 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 // Serial.print (c); curLine += c; completeRequest += c; if (c == '\n' && currentLineIsBlank) { // Let's parse // send a standard http response header // REDIRECTIION // client.println("HTTP/1.1 301 Moved Permanently"); // client.println("Location: http://jan.apple-iv.net/vt"); // client.stop(); Serial.println (completeRequest.indexOf('Cache')); if (completeRequest.indexOf ('Cache')) { client.println ("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); client.println(); client.println(""); client.println(""); // add a meta refresh tag, so the browser pulls again every 5 seconds: //client.println(""); // output the value of each analog input pin if (curGet == "/") { client.print ("

Index

"); } else if (curGet.substring (0, 6) == "/lamp/") { client.print ("Aktion: lamp -- Param: "); client.print (curGet.substring (6)); String param = curGet.substring (6); if (param.charAt (0) == '1') { digitalWrite (8, HIGH); clearLCD(); gotoPos (2); LCD.write ("ACTION: LAMP"); gotoPos (4, 2); LCD.write ("PARAM: 1"); } else { digitalWrite (8, LOW); clearLCD(); gotoPos (2); LCD.write ("ACTION: LAMP"); gotoPos (4, 2); LCD.write ("PARAM: 0"); } } else if (curGet.substring (0, 6) == "/time/") { // /time/A/B/C // --4---67891111 // 0123 int h; int m; int s; h = (int)curGet.substring (6,7).charAt (0) - 65; m = (int)curGet.substring (8,9).charAt (0) - 65; s = (int)curGet.charAt (10) - 65; clearAndReset (); LCD.write ("UPDATE TIME: "); LCD.print ((int)h); LCD.write ("-"); LCD.print ((int)m); LCD.write ("-"); LCD.print ((int)s); curHour = h; curMin = m; curSec = s; client.print ((int)curGet.charAt (10) - 65); client.write (" "); client.print (curGet.charAt (10)); } else if (curGet.substring (0, 8) == "/curtime") { clearLCD(); gotoPos (2); LCD.write ("CURERENT TIME:"); gotoPos (4,2); if (curHour < 10) LCD.write ("0"); LCD.print (curHour); LCD.write (":"); if (curMin < 10) LCD.write ("0"); LCD.print (curMin); LCD.write (":"); if (curSec < 10) LCD.write ("0"); LCD.print (curSec); client.print ("OK"); } else if (curGet.substring (0, 6) == "/test/") { File fh = SD.open ("/test.txt"); client.print (fh); client.print (fh.size()); while (fh.available()) client.print (fh.read()); fh.close(); client.print ("WHATSUP"); } else client.print ("

Undefined

"); client.print("
"); client.println(""); curLine = ""; completeRequest = ""; } break; } if (c == '\n') { if (curLine.substring (0, 3) == "GET") { curGet = curLine.substring (4, curLine.length() - 11); } curLine = ""; // 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(); gotoPos (1, 2); curLine = ""; completeRequest = ""; resetToActive.reset(); resetToActive.enable(); } } void clearLCD () { LCD.write (254); LCD.write (0x01); delay (10); } void resetPos () { LCD.write (254); LCD.write (128); } void clearAndReset () { clearLCD(); resetPos(); } void gotoPos (int num) { gotoPos (num, 1); } void gotoPos (int num, int line) { int base = 127; if (line == 2) base = 191; if (num < 1) num = 1; if (num > 16) num = 16; LCD.write (254); LCD.write (base + num); delay (10); // fix wrong chars } void resetMsg () { clearLCD(); gotoPos (2); LCD.write ("LISTENING (80)"); gotoPos (2, 2); LCD.write ("192.168.178.42"); resetToActive.disable(); } void updateSec () { curSec ++; if (curSec == 60) { curMin ++; curSec = 0; if (curMin == 60) { curHour ++; curMin = 0; if (curHour == 24) curHour = 0; } } if (curHour == 5 && curMin == 16) { digitalWrite (8, HIGH); } /* clearAndReset(); LCD.print (curHour); LCD.write (":"); LCD.print (curMin); LCD.write (":"); LCD.print (curSec); */ }