/* RWed 06/2014 Webswitch Ethernet Arduino Mega 2560 - LCD TFT 1.8 ok sclk 52 -> SCL mosi 51 -> SDA cs 53 dc 48 rst 49 - Ethernetshield WS5100 ok - RTC DS3231 ok - Drehencoder - Solid State 8x ok S1 22 S2 24 S3 26 S4 28 S5 30 S6 32 S7 34 S8 36 - EEprom Status 1-8 ok */ #define debug #define Version "Ethernet-Steckdose v 0.33" #define S1 22 // Pin Solid State 1 #define S2 24 #define S3 26 #define S4 28 #define S5 30 #define S6 32 #define S7 34 #define S8 36 #define StatusLED 38 // Status-LED client connect #define cs 53 // tft CS #define dc 48 // TFT DC #define rst 49 // TFT RST #include #include #include #include // LCD Ausgabe Querformat !! #include #include TFT TFTscreen = TFT(cs, dc, rst); #define DS1307_ADDRESS 0x68 // DS3231 byte zero = 0x00; //workaround for issue #527 String text1="Canon 5350"; // Bezeichnung Solid State 1 Web, max. 10 Zeichen String text2="USB HD 1TB"; String text3=" - S 3 - "; String text4=" - S 4 - "; String text5=" - S 5 - "; String text6=" - S 6 - "; String text7=" - S 7 - "; String text8=" - S 8 - "; String lastboot=""; // Zeit / Datum Neustart String lastaction="-"; // letzter Schaltvorgang String lasttime="-"; // zeit / datum letzter Schaltvorgang int wsec; int wmin; int whour; int wweekday; int wday; int wmonth; int wyear; byte vmin=99; // Vergleich Minute byte s1=0; // Voreinstellung S1 Reboot byte s2=0; byte s3=0; byte s4=0; byte s5=0; byte s6=0; byte s7=0; byte s8=0; byte relais; // Bitstatus Solid State 1-8 byte relais_e; // Bitstatus EEprom Solid State 1-8 char lcdzeit[6]; char lcdsolid[11]; char tfttemp[20]; String tfttemp2=""; String wtag ="wt"; String zeit="ss:mm"; String datum="tt/mm/jj"; byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xCf }; // nur EINMAL im Netzwerk vergeben IPAddress ip(192,168,0,159); // IP EthernetServer server(80); // Port, Std.: 80 void setup() { leseeeprom(); // Status 1-8 lesen relais=relais_e; // Status 1-8 ├╝bernehmen setzeeeprom(); // Status 1-8 setzen switchsolid(); // S1-S8 schalten Serial.begin(9600); Serial.println(Version); Serial.println(ip); TFTscreen.begin(); // TFT init TFTscreen.background(0, 0, 0); TFTscreen.setTextSize(1); TFTscreen.stroke(0,100,255); TFTscreen.text(Version,5,0); pinMode(S1, OUTPUT); // S1 Ausgang pinMode(S2, OUTPUT); pinMode(S3, OUTPUT); pinMode(S4, OUTPUT); pinMode(S5, OUTPUT); pinMode(S6, OUTPUT); pinMode(S7, OUTPUT); pinMode(S8, OUTPUT); pinMode(StatusLED, OUTPUT); // LED Ausgang Wire.begin(); Ethernet.begin(mac, ip); server.begin(); // Eth-Server starten rtclesen(); // RTC lesen setTime(whour,wmin,wsec,wday,wmonth,wyear); // interne Uhr (Time.h) setzen zeitstring(); // String Zeit datumstring(); // String Datum wtstring(); // String WTag lastboot=zeit+" "+datum+" "+wtag; // String Bootzeit /-datum TFTscreen.setTextSize(1); TFTscreen.stroke(80,80,80); String tftboot=zeit+" "; // Ausgabe TFT Bootzeit /-datum if(day()<10)tftboot=tftboot+"0"; tftboot=tftboot+day(); tftboot=tftboot+"/"; if(month()<10)tftboot=tftboot+"0"; tftboot=tftboot+month()+" "+wtag; tftboot.toCharArray(tfttemp, 20); TFTscreen.text(tfttemp,70,120); } void loop() { if(vmin!=minute()) // Vergleich Minute, neue Zeit ? { rtclesen(); setTime(whour,wmin,wsec,wday,wmonth,wyear); zeitstring(); datumstring(); wtstring(); Serial.println(zeit); Serial.print(datum); Serial.print(" "); Serial.println(wtag); tfttime(); vmin=minute(); } EthernetClient client = server.available(); // listen for incoming clients if (client) { Serial.println("new client"); boolean currentLineIsBlank = true; String buffer = ""; while (client.connected()) { if (client.available()) digitalWrite(StatusLED, HIGH); // Status LED ein TFTscreen.setTextSize(2); // TFT Status ein TFTscreen.stroke(255,0,0); TFTscreen.text("*",145,90); { char c = client.read(); buffer+=c; if (c == '\n' && currentLineIsBlank) { client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); client.println(); client.println(""); client.println(""); client.print(""); client.print("Ethernet-Steckdose"); client.println(""); // Ausgabe Statuszeile client.print(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println("
"); client.print(""); client.print(Version); client.print("
"); client.print(""); client.print("boot: "); client.print(lastboot); client.println("
"); client.print(""); client.print(ip); client.println(""); client.print(""); client.print(zeit); client.println(""); client.print(""); client.print(datum); client.print(" "); client.print(""); client.print(wtag); client.println("
"); client.println("

"); // Ausgabe Status S1-S8 client.print(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.print(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); // Ausgabe Formdaten client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println("
"); client.print(""); if(bitRead(relais_e,0)==1)client.print(""); client.print("S 1"); client.println(""); client.print(""); if(bitRead(relais_e,1)==1)client.print(""); client.print("S 2"); client.println(""); client.print(""); if(bitRead(relais_e,2)==1)client.print(""); client.print("S 3"); client.println(""); client.print(""); if(bitRead(relais_e,3)==1)client.print(""); client.print("S 4"); client.println(""); client.print(""); if(bitRead(relais_e,4)==1)client.print(""); client.print("S 5"); client.println(""); client.print(""); if(bitRead(relais_e,5)==1)client.print(""); client.print("S 6"); client.println(""); client.print(""); if(bitRead(relais_e,6)==1)client.print(""); client.print("S 7"); client.println(""); client.print(""); if(bitRead(relais_e,7)==1)client.print(""); client.print("S 8"); client.println("
"); client.print(text1); client.println(""); client.print(text2); client.println(""); client.print(text3); client.println(""); client.print(text4); client.println(""); client.print(text5); client.println(""); client.print(text6); client.println(""); client.print(text7); client.println(""); client.print(text8); client.println("
"); if (digitalRead(S1)) { client.print("ON"); client.print("

"); client.println("
"); } else{ client.print("OFF"); client.print("

"); client.println("
"); } client.println("
"); if (digitalRead(S2)) { client.print("ON"); client.print("

"); client.println("
"); } else{ client.print("OFF"); client.print("

"); client.println("
"); } client.println("
"); if (digitalRead(S3)) { client.print("ON"); client.print("

"); client.println("
"); } else{ client.print("OFF"); client.print("

"); client.println("
"); } client.println("
"); if (digitalRead(S4)) { client.print("ON"); client.print("

"); client.println("
"); } else{ client.print("OFF"); client.print("

"); client.println("
"); } client.println("
"); if (digitalRead(S5)) { client.print("ON"); client.print("

"); client.println("
"); } else{ client.print("OFF"); client.print("

"); client.println("
"); } client.println("
"); if (digitalRead(S6)) { client.print("ON"); client.print("

"); client.println("
"); } else{ client.print("OFF"); client.print("

"); client.println("
"); } client.println("
"); if (digitalRead(S7)) { client.print("ON"); client.print("

"); client.println("
"); } else{ client.print("OFF"); client.print("

"); client.println("
"); } client.println("
"); if (digitalRead(S8)) { client.print("ON"); client.print("

"); client.println("
"); } else{ client.print("OFF"); client.print("

"); client.println("
"); } client.println("
"); client.println("

"); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println("
"); client.print(""); client.println("
"); client.println("
"); client.println("
"); client.println("
"); // Status letzte Aktion if(lasttime!="-") { client.print(""); client.print(" > "); if(lastaction.indexOf("on")>=0)client.print(""); if(lastaction.indexOf("off")>=0)client.print(""); client.print(lastaction); client.print(""); client.print(" <"); } client.println(""); if(lasttime!="-") { client.print(""); client.print(lasttime); } client.println("
"); client.println(""); break; } if (c == '\n') { currentLineIsBlank = true; buffer=""; } else if (c == '\r') { // Auswertung Formdaten if(buffer.indexOf("GET /?eeprom=SAVE")>=0) // SCHREIBEN EEprom 1-8 { Serial.println("Web: SAVE EPPROM"); lastaction="SAVE"; schreibeeeprom(); leseeeprom(); if(relais_e!=relais) { lastaction=("save-error"); } else { relais=relais_e; } tftstatus(); logtime(); Serial.println(lasttime); } if(buffer.indexOf("GET /?eeprom=LOAD")>=0) // Lesen EEprom 1-8 { Serial.println("Web: LOAD EPPROM"); lastaction="LOAD"; leseeeprom(); relais=relais_e; setzeeeprom(); switchsolid(); // tftstatus(); logtime(); Serial.println(lasttime); } if(buffer.indexOf("GET /?allon=1")>=0) // alle ein ? { Serial.println("Web: all ON"); s1=1; s2=1; s3=1; s4=1; s5=1; s6=1; s7=1; s8=1; lastaction="all on"; switchsolid(); logtime(); Serial.println(lasttime); } if(buffer.indexOf("GET /?allon=0")>=0) // alle aus ? { Serial.println("Web: all off"); s1=0; s2=0; s3=0; s4=0; s5=0; s6=0; s7=0; s8=0; lastaction="all off"; switchsolid(); logtime(); Serial.println(lasttime); } if(buffer.indexOf("GET /?s1=ON")>=0) // S1 ein ? { Serial.print("Web: S1 ON "); Serial.println(text1); s1=1; lastaction="S1 on"; switchsolid(); logtime(); Serial.println(lasttime); } if(buffer.indexOf("GET /?s1=OFF")>=0) // S1 aus ? { Serial.print("Web: S1 off "); Serial.println(text1); s1=0; lastaction="S1 off"; switchsolid(); logtime(); Serial.println(lasttime); } if(buffer.indexOf("GET /?s2=ON")>=0) { Serial.print("Web: S2 ON "); Serial.println(text2); s2=1; lastaction="S2 on"; switchsolid(); logtime(); Serial.println(lasttime); } if(buffer.indexOf("GET /?s2=OFF")>=0) { Serial.print("Web: S2 off "); Serial.println(text2); s2=0; lastaction="S2 off"; switchsolid(); logtime(); Serial.println(lasttime); } if(buffer.indexOf("GET /?s3=ON")>=0) { Serial.print("Web: S3 ON "); Serial.println(text3); s3=1; lastaction="S3 on"; switchsolid(); logtime(); Serial.println(lasttime); } if(buffer.indexOf("GET /?s3=OFF")>=0) { Serial.print("Web: S3 off "); Serial.println(text3); s3=0; lastaction="S3 off"; switchsolid(); logtime(); Serial.println(lasttime); } if(buffer.indexOf("GET /?s4=ON")>=0) { Serial.print("Web: S4 ON "); Serial.println(text4); s4=1; lastaction="S4 on"; switchsolid(); logtime(); Serial.println(lasttime); } if(buffer.indexOf("GET /?s4=OFF")>=0) { Serial.print("Web: S4 off "); Serial.println(text4); s4=0; lastaction="S4 off"; switchsolid(); logtime(); Serial.println(lasttime); } if(buffer.indexOf("GET /?s5=ON")>=0) { Serial.print("Web: S5 ON "); Serial.println(text5); s5=1; lastaction="S5 on"; switchsolid(); logtime(); Serial.println(lasttime); } if(buffer.indexOf("GET /?s5=OFF")>=0) { Serial.print("Web: S5 off "); Serial.println(text5); s5=0; lastaction="S5 off"; switchsolid(); logtime(); Serial.println(lasttime); } if(buffer.indexOf("GET /?s6=ON")>=0) { Serial.print("Web: S6 ON "); Serial.println(text6); s6=1; lastaction="S6 on"; switchsolid(); logtime(); Serial.println(lasttime); } if(buffer.indexOf("GET /?s6=OFF")>=0) { Serial.print("Web: S6 off "); Serial.println(text6); s6=0; lastaction="S6 off"; switchsolid(); logtime(); Serial.println(lasttime); } if(buffer.indexOf("GET /?s7=ON")>=0) { Serial.print("Web: S7 ON "); Serial.println(text7); s7=1; lastaction="S7 on"; switchsolid(); logtime(); Serial.println(lasttime); } if(buffer.indexOf("GET /?s7=OFF")>=0) { Serial.print("Web: S7 off "); Serial.println(text7); s7=0; lastaction="S7 off"; switchsolid(); logtime(); Serial.println(lasttime); } if(buffer.indexOf("GET /?s8=ON")>=0) { Serial.print("Web: S8 ON "); Serial.println(text8); s8=1; lastaction="S8 on"; switchsolid(); logtime(); Serial.println(lasttime); } if(buffer.indexOf("GET /?s8=OFF")>=0) { Serial.print("Web: S8 off "); Serial.println(text8); s8=0; lastaction="S8 off"; switchsolid(); logtime(); Serial.println(lasttime); } } else { currentLineIsBlank = false; } } } delay(1); // give the web browser time to receive the data client.stop(); digitalWrite(StatusLED, LOW); // Status LED aus TFTscreen.setTextSize(2); // TFT Status aus TFTscreen.stroke(0,0,0); TFTscreen.text("*",145,90); 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 tfttime() // Ausgabe Zeit TFT { TFTscreen.setTextSize(2); TFTscreen.stroke(0,0,0); TFTscreen.text(lcdzeit,5,112); zeit.toCharArray(lcdzeit, 6); TFTscreen.setTextSize(2); TFTscreen.stroke(0,255,255); TFTscreen.text(lcdzeit,5,112); TFTscreen.stroke(0,100,255); TFTscreen.setTextSize(1); datum.toCharArray(tfttemp, 20); TFTscreen.text(tfttemp,70,112); TFTscreen.stroke(255,255,255); wtag.toCharArray(tfttemp, 3); TFTscreen.text(tfttemp,140,112); } void tftstatus() // Status-Ausgabe TFT { TFTscreen.setTextSize(1); text1.toCharArray(lcdsolid, 11); TFTscreen.stroke(0,0,0); if(bitRead(relais_e,0)==1)TFTscreen.stroke(0,255,255); TFTscreen.text(">",0,15); TFTscreen.stroke(50,50,255); if(s1==1)TFTscreen.stroke(50,255,50); TFTscreen.text(lcdsolid,10,15); text2.toCharArray(lcdsolid, 11); TFTscreen.stroke(0,0,0); if(bitRead(relais_e,1)==1)TFTscreen.stroke(0,255,255); TFTscreen.text(">",0,25); TFTscreen.stroke(50,50,255); if(s2==1)TFTscreen.stroke(50,255,50); TFTscreen.text(lcdsolid,10,25); text3.toCharArray(lcdsolid, 11); TFTscreen.stroke(0,0,0); if(bitRead(relais_e,2)==1)TFTscreen.stroke(0,255,255); TFTscreen.text(">",0,35); TFTscreen.stroke(50,50,255); if(s3==1)TFTscreen.stroke(50,255,50); TFTscreen.text(lcdsolid,10,35); text4.toCharArray(lcdsolid, 11); TFTscreen.stroke(0,0,0); if(bitRead(relais_e,3)==1)TFTscreen.stroke(0,255,255); TFTscreen.text(">",0,45); TFTscreen.stroke(50,50,255); if(s4==1)TFTscreen.stroke(50,255,50); TFTscreen.text(lcdsolid,10,45); text5.toCharArray(lcdsolid, 11); TFTscreen.stroke(0,0,0); if(bitRead(relais_e,4)==1)TFTscreen.stroke(0,255,255); TFTscreen.text(">",80,15); TFTscreen.stroke(50,50,255); if(s5==1)TFTscreen.stroke(50,255,50); TFTscreen.text(lcdsolid,90,15); text6.toCharArray(lcdsolid, 11); TFTscreen.stroke(0,0,0); if(bitRead(relais_e,5)==1)TFTscreen.stroke(0,255,255); TFTscreen.text(">",80,25); TFTscreen.stroke(50,50,255); if(s6==1)TFTscreen.stroke(50,255,50); TFTscreen.text(lcdsolid,90,25); text7.toCharArray(lcdsolid, 11); TFTscreen.stroke(0,0,0); if(bitRead(relais_e,6)==1)TFTscreen.stroke(0,255,255); TFTscreen.text(">",80,35); TFTscreen.stroke(50,50,255); if(s7==1)TFTscreen.stroke(50,255,50); TFTscreen.text(lcdsolid,90,35); text8.toCharArray(lcdsolid, 11); TFTscreen.stroke(0,0,0); if(bitRead(relais_e,7)==1)TFTscreen.stroke(0,255,255); TFTscreen.text(">",80,45); TFTscreen.stroke(50,50,255); if(s8==1)TFTscreen.stroke(50,255,50); TFTscreen.text(lcdsolid,90,45); if(lastaction!="-") { TFTscreen.setTextSize(3); TFTscreen.stroke(0,0,0); tfttemp2.toCharArray(tfttemp, 11); TFTscreen.text(tfttemp,20,65); TFTscreen.stroke(0,255,255); if(lastaction.indexOf("on")>=0)TFTscreen.stroke(50,255,50); if(lastaction.indexOf("off")>=0)TFTscreen.stroke(50,50,255); lastaction.toCharArray(tfttemp, 11); TFTscreen.text(tfttemp,20,65); tfttemp2=lastaction; } } void logtime() // Logzeit /-datum { zeitstring(); datumstring(); wtstring(); lasttime=zeit+" "+datum+" "+wtag; } void switchsolid() // S1-8 schalten { Serial.println("Switch Solid State Relais"); Serial.println(" SSSSSSSS"); Serial.println(" 12345678"); Serial.println(" ========"); String ssr="old: "; ssr=ssr+(digitalRead(S1))+(digitalRead(S2))+(digitalRead(S3))+(digitalRead(S4))+(digitalRead(S5))+(digitalRead(S6))+(digitalRead(S7))+(digitalRead(S8)); Serial.println(ssr); if(s1==1) { digitalWrite(S1, HIGH); bitWrite(relais,0,1); } else { digitalWrite(S1, LOW); bitWrite(relais,0,0); } if(s2==1) { digitalWrite(S2, HIGH); bitWrite(relais,1,1); } else { digitalWrite(S2, LOW); bitWrite(relais,1,0); } if(s3==1) { digitalWrite(S3, HIGH); bitWrite(relais,2,1); } else { digitalWrite(S3, LOW); bitWrite(relais,2,0); } if(s4==1) { digitalWrite(S4, HIGH); bitWrite(relais,3,1); } else { digitalWrite(S4, LOW); bitWrite(relais,3,0); } if(s5==1) { digitalWrite(S5, HIGH); bitWrite(relais,4,1); } else { digitalWrite(S5, LOW); bitWrite(relais,4,0); } if(s6==1) { digitalWrite(S6, HIGH); bitWrite(relais,5,1); } else { digitalWrite(S6, LOW); bitWrite(relais,5,0); } if(s7==1) { digitalWrite(S7, HIGH); bitWrite(relais,6,1); } else { digitalWrite(S7, LOW); bitWrite(relais,6,0); } if(s8==1) { digitalWrite(S8, HIGH); bitWrite(relais,7,1); } else { digitalWrite(S8, LOW); bitWrite(relais,7,0); } ssr="new: "; ssr=ssr+s1+s2+s3+s4+s5+s6+s7+s8; Serial.println(ssr); Serial.print("Bitstatus Relais: "); // Serial.println(relais,BIN); for(byte x=0;x<8;x++) { Serial.print(bitRead(relais,x)); } Serial.println(); tftstatus(); // Ausgabe Status TFT } void zeitstring() // Zeit generieren { zeit=""; if(hour()<10)zeit="0"; zeit=zeit+hour()+":"; if(minute()<10)zeit=zeit+"0"; zeit=zeit+minute(); } void datumstring() // Datum generieren { datum=""; if(day()<10)datum="0"; datum=datum+day()+"/"; if(month()<10)datum=datum+"0"; datum=datum+month()+"/"+year(); } void wtstring() // Wochentag generieren { 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() // RTC lesen { 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 leseeeprom() // Auslesen EEprom, S1-8 -> Byte relais_e { relais_e=0; relais_e=EEPROM.read(0); Serial.print("EEPROM lesen Bitstatus Relais: "); // Serial.println(relais,BIN); for(byte x=0;x<8;x++) { Serial.print(bitRead(relais_e,x)); } Serial.println(); delay(100); } void schreibeeeprom()// Schreiben EEprom, S1-8 -> Byte Relais { EEPROM.write(0,relais); Serial.print("EEPROM schreiben Bitstatus Relais: "); // Serial.println(relais,BIN); for(byte x=0;x<8;x++) { Serial.print(bitRead(relais,x)); } Serial.println(); delay(100); } void setzeeeprom() // Status S1-8 setzen { s1=bitRead(relais,0); s2=bitRead(relais,1); s3=bitRead(relais,2); s4=bitRead(relais,3); s5=bitRead(relais,4); s6=bitRead(relais,5); s7=bitRead(relais,6); s8=bitRead(relais,7); }