/* Webswitch Ethernet - Arduino Mega 2560 - LCD TFT 1.8 sclk 52 -> SCL mosi 51 -> SDA cs 53 dc 48 rst 49 - Ethernetshield WS5100 ok - RTC DS3231 ok - Drehencoder - Solid State 8x */ #include #include #include #include #define DS1307_ADDRESS 0x68 // DS3231 byte zero = 0x00; //workaround for issue #527 int wsec; int wmin; int whour; int wweekday; int wday; int wmonth; int wyear; byte vmin=99; // Vergleich Minute String wtag ="wt"; String zeit="ss:mm"; String datum="tt/mm/jj"; byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xCf }; IPAddress ip(192,168,0,159); EthernetServer server(80); void setup() { Serial.begin(9600); // DS1307 Wire.begin(); Ethernet.begin(mac, ip); server.begin(); Serial.print("server is at "); Serial.println(Ethernet.localIP()); } void loop() { if(vmin!=minute()) { rtclesen(); setTime(whour,wmin,wsec,wday,wmonth,wyear); zeitstring(); datumstring(); wtstring(); Serial.println(zeit); Serial.print(datum); Serial.print(" "); Serial.println(wtag); vmin=minute(); // setDateTime(); } // 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); 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"); // the connection will be closed after completion of the response client.println("Refresh: 5"); // refresh the page automatically every 5 sec client.println(); client.println(""); client.println(""); client.print(""); client.print("Ethernet-Steckdose"); client.println(""); client.print(""); client.print(""); // output the value of each analog input pin client.print(ip); client.println("
"); client.print(""); client.print(zeit); client.print(" "); client.print(""); client.print(datum); client.print(" "); client.print(""); client.print(wtag); client.println("
"); client.println(""); 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("client disconnected"); } } byte decToBcd(byte val){ // Convert normal decimal numbers to binary coded decimal return ( (val/10*16) + (val%10) ); } byte bcdToDec(byte val) { // Convert binary coded decimal to normal decimal numbers return ( (val/16*10) + (val%16) ); } void zeitstring() { zeit=""; if(hour()<9)zeit="0"; zeit=zeit+hour()+":"; if(minute()<9)zeit=zeit+"0"; zeit=zeit+minute(); } void datumstring() { datum=""; if(day()<9)datum="0"; datum=datum+day()+"/"; if(month()<9)datum=datum+"0"; datum=datum+month()+"/"+year(); } void wtstring() { switch (wweekday) { case 1: wtag="Mo"; break; case 2: wtag="Di"; break; case 3: wtag="Mi"; break; case 4: wtag="Do"; break; case 5: wtag="Fr"; break; case 6: wtag="Sa"; break; case 7: wtag="So"; break; default: wtag="xx"; break; } } void rtclesen() { Serial.println("Lese RTC..."); Serial.print("alte Zeit: "); if(hour()<9)Serial.print("0"); Serial.print(hour()); Serial.print(":"); if(minute()<9)Serial.print("0"); Serial.print(minute()); Serial.print(":"); if(second()<9)Serial.print("0"); Serial.println(second()); // DS1307 // Reset the register pointer Wire.beginTransmission(DS1307_ADDRESS); Wire.write(zero); Wire.endTransmission(); Wire.requestFrom(DS1307_ADDRESS, 7); wsec = bcdToDec(Wire.read()); wmin = bcdToDec(Wire.read()); whour = bcdToDec(Wire.read() & 0b111111); //24 hour time wweekday = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday wday = bcdToDec(Wire.read()); wmonth = bcdToDec(Wire.read()); wyear = bcdToDec(Wire.read()); wyear=wyear+2000; setTime(whour,wmin,wsec,wday,wmonth,wyear); zeitstring(); Serial.print("neue Zeit: "); Serial.print(zeit); Serial.print(":"); if(second()<9)Serial.print("0"); Serial.println(second()); } void setDateTime(){ Wire.beginTransmission(DS1307_ADDRESS); Wire.write(zero); //stop Oscillator Wire.write(decToBcd(second())); Wire.write(decToBcd(minute())); Wire.write(decToBcd(hour())); Wire.write(decToBcd(weekday())); Wire.write(decToBcd(day())); Wire.write(decToBcd(month())); Wire.write(decToBcd(year()-2000)); Wire.write(0x10); // Q-Out 1 Hz Wire.endTransmission(); }