SHARE
TWEET

Interwebs control panel

Suppenbiatch Feb 27th, 2020 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SPI.h>
  2. #include <Ethernet2.h>
  3.  
  4. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  5. IPAddress ip( 192, 168, 178, 10 );
  6. const int MAX_PAGENAME_LEN = 10;
  7. const int anzahl = 16;
  8. char buffer[MAX_PAGENAME_LEN + 1];
  9. unsigned long wait_turn_pc_off;
  10. unsigned long wait_run_pc;
  11. bool ispcon = false;
  12. bool iswlanon = true;
  13. bool ischargeon = false;
  14. bool turnpcoff = false;
  15. bool turnpcon = false;
  16.  
  17.  
  18. EthernetServer server(8080);
  19. EthernetClient client;
  20. void setup() {
  21.   Ethernet.begin(mac, ip);
  22.   server.begin();
  23.   delay(1000);
  24.   for (int i = 30; i <= ((anzahl - 1) + 30); i++) {
  25.     pinMode(i, OUTPUT);
  26.     digitalWrite(i, HIGH);
  27.   }
  28.   digitalWrite(38, LOW);
  29. }
  30. void loop() {
  31.   client = server.available();
  32.   if (client) {
  33.     int type = 0;
  34.     while (client.connected()) {
  35.       if (client.available()) {
  36.         memset(buffer, 0, sizeof(buffer));
  37.         if (client.readBytesUntil('/', buffer, MAX_PAGENAME_LEN)) {
  38.           if (strcmp(buffer, "GET ") == 0 )
  39.             type = 1;
  40.           else if (strcmp(buffer, "POST ") == 0)
  41.             type = 2;
  42.           memset(buffer, 0, sizeof(buffer));
  43.           if (client.readBytesUntil( '/', buffer, MAX_PAGENAME_LEN )) {
  44.             if (strcasecmp(buffer, "relais") == 0) {
  45.               showRelais(type == 2);
  46.             } else if (strcasecmp(buffer, "pc") == 0) {
  47.               showPC(type == 2);
  48.             } else if (strcasecmp(buffer, "runpc") == 0) {
  49.               showRunPC(type == 2);
  50.             } else if (strcasecmp(buffer, "vars") == 0) {
  51.               showVars(type == 2);
  52.             } else if (strcasecmp(buffer, "charge") == 0) {
  53.               showCharge(type == 2);
  54.             } else if (strcasecmp(buffer, "wlan") == 0) {
  55.               showWLAN(type == 2);
  56.             } else if (strcasecmp(buffer, "vars2") == 0) {
  57.               showVars2(type == 2);
  58.             } else if (strcasecmp(buffer, "pcOFF") == 0) {
  59.               showPowerOffPC(type == 2);
  60.             } else if (strcasecmp(buffer, "killALL") == 0) {
  61.               killALL(type == 2);
  62.             } else {
  63.               unknownPage(buffer);
  64.  
  65.             }
  66.           }
  67.           client.println(F("</body></html>"));
  68.         }
  69.         break;
  70.       }
  71.     }
  72.     delay(1);
  73.     client.stop();
  74.   }
  75.  
  76.   if (turnpcoff && millis() - wait_turn_pc_off > 125) {
  77.     pinMode(42, OUTPUT);
  78.     digitalWrite(42, HIGH);
  79.  
  80.   }
  81.   if (turnpcoff && millis() - wait_turn_pc_off > 150000) {
  82.     turnpcoff = false;
  83.     pinMode(30, OUTPUT);
  84.     pinMode(31, OUTPUT);
  85.     pinMode(32, OUTPUT);
  86.     digitalWrite(30, HIGH);
  87.     //delay(750);
  88.     digitalWrite(31, HIGH);
  89.     //delay(750);
  90.     digitalWrite(32, HIGH);
  91.   }
  92.   if (turnpcon && millis() - wait_run_pc > 125) {
  93.     turnpcon = false;
  94.     pinMode(42, OUTPUT);
  95.     digitalWrite(42, HIGH);
  96.   }
  97. }
  98.  
  99. void showPC(boolean isPost) {
  100.   if (isPost) {
  101.     client.find("\n\r");
  102.     while (client.findUntil("pinD", "\n\r")) {
  103.       int pin = client.parseInt();
  104.       int val = client.parseInt();
  105.       if (pin == 28) {
  106.         if (val == 1) {
  107.           turnpcoff = false;
  108.           ispcon = false;
  109.           pinMode(30, OUTPUT);
  110.           pinMode(31, OUTPUT);
  111.           pinMode(32, OUTPUT);
  112.           digitalWrite(30, HIGH);
  113.           //delay(750);
  114.           digitalWrite(31, HIGH);
  115.           //delay(750);
  116.           digitalWrite(32, HIGH);
  117.         } else {
  118.           ispcon = false;
  119.           turnpcoff = false;
  120.           pinMode(30, OUTPUT);
  121.           pinMode(31, OUTPUT);
  122.           pinMode(32, OUTPUT);
  123.           digitalWrite(30, LOW);
  124.           //delay(750);
  125.           digitalWrite(31, LOW);
  126.           //delay(250);
  127.           digitalWrite(32, LOW);
  128.         }
  129.       } else if (pin == 27) {
  130.         if (val == 1 && !ispcon) {
  131.           ispcon = true;
  132.           iswlanon = true;
  133.           turnpcoff = false;
  134.           pinMode(30, OUTPUT);
  135.           pinMode(31, OUTPUT);
  136.           pinMode(32, OUTPUT);
  137.           pinMode(42, OUTPUT);
  138.           pinMode(38, OUTPUT);
  139.           digitalWrite(30, LOW);
  140.           //delay(750);
  141.           digitalWrite(31, LOW);
  142.           //delay(750);
  143.           digitalWrite(32, LOW);
  144.           digitalWrite(38, LOW);
  145.           //delay(2500);
  146.           digitalWrite(42, LOW);
  147.           turnpcon = true;
  148.           wait_run_pc = millis();
  149.  
  150.         } else if (val == 0 && ispcon) {
  151.           turnpcoff = true;
  152.           ispcon = false;
  153.           digitalWrite(42, LOW);
  154.           wait_turn_pc_off = millis();
  155.         }
  156.       } else if (pin == 29) {
  157.         for (int i = 30; i <= ((anzahl - 1) + 30); i++) {
  158.           pinMode(i, OUTPUT);
  159.           digitalWrite(i, HIGH);
  160.           //delay(125);
  161.         }
  162.         turnpcoff = false;
  163.         ispcon = false;
  164.         ischargeon = false;
  165.         iswlanon = false;
  166.       } else if (pin == 38) {
  167.         pinMode(pin, OUTPUT);
  168.         iswlanon = !val;
  169.         digitalWrite(pin, val);
  170.       }
  171.     }
  172.   }
  173.   sendHeader("PC Control");
  174.  
  175.   client.println(F("<table border='1'>"));
  176.   client.print(F("<tr><td>Computer"));
  177.   client.print(F(" </td><td>"));
  178.   htmlButton("RUN", "pinD", 27, "1", "'/pc/'");
  179.   client.print(F(" </td><td>"));
  180.   htmlButton("SHUTDOWN", "pinD", 27, "0", "'/pc/'");
  181.   client.print(F(" </td><td>"));
  182.   htmlButton("POWER ON", "pinD", 28, "0", "'/pc/'");
  183.   client.print(F(" </td><td>"));
  184.   htmlButton("POWER OFF", "pinD", 28, "1", "'/pc/'");
  185.   client.print(F(" </td></tr>"));
  186.   client.println(F("</table>"));
  187.  
  188.   client.println(F("<table border='1'>"));
  189.   client.print(F("<tr><td>WLAN"));
  190.   client.print(F(" </td><td>"));
  191.   htmlButton("POWER ON", "pinD", 38, "0", "'/pc/'");
  192.   client.print(F(" </td><td>"));
  193.   htmlButton("POWER OFF", "pinD", 38, "1", "'/pc/'");
  194.   client.print(F(" </td></tr>"));
  195.   client.println(F("</table>"));
  196.  
  197.   client.println(F("<table border='1'>"));
  198.   client.print(F(" <tr><td>Kill All Power"));
  199.   client.print(F(" </td><td>"));
  200.   htmlButton("POWER OFF", "pinD", 29, "1", "'/pc/'");
  201.   client.print(F(" </td>"));
  202.   client.print(F("</tr>"));
  203.   client.println(F("</table>"));
  204.  
  205. }
  206.  
  207. void showRunPC(boolean isPost) {
  208.   sendHeader("PC Autostart");
  209.   if (!ispcon) {
  210.     ispcon = true;
  211.     turnpcoff = false;
  212.     iswlanon = true;
  213.     pinMode(30, OUTPUT);
  214.     pinMode(31, OUTPUT);
  215.     pinMode(32, OUTPUT);
  216.     pinMode(42, OUTPUT);
  217.     pinMode(38, OUTPUT);
  218.     digitalWrite(30, LOW);
  219.     //delay(750);
  220.     digitalWrite(31, LOW);
  221.     //delay(750);
  222.     digitalWrite(32, LOW);
  223.     digitalWrite(38, LOW);
  224.     //delay(2500);
  225.     digitalWrite(42, LOW);
  226.     turnpcon = true;
  227.     wait_run_pc = millis();
  228.     client.println(F(" DONE "));
  229.   } else {
  230.     client.println(F(" PC is already on ... "));
  231.   }
  232.  
  233. }
  234.  
  235.  
  236. void showCharge(boolean isPost) {
  237.   sendHeader("Charging Port");
  238.   if (!ischargeon) {
  239.     ischargeon = true;
  240.     pinMode(33, OUTPUT);
  241.     digitalWrite(33, LOW);
  242.     client.println(F(" Charging Port is ON "));
  243.   } else {
  244.     ischargeon = false;
  245.     pinMode(33, OUTPUT);
  246.     digitalWrite(33, HIGH);
  247.     client.println(F(" Charging Port is OFF "));
  248.   }
  249. }
  250.  
  251. void showWLAN(boolean isPost) {
  252.   sendHeader("WLAN Port");
  253.   if (!iswlanon) {
  254.     iswlanon = true;
  255.     pinMode(38, OUTPUT);
  256.     digitalWrite(38, LOW);
  257.     client.println(F(" WLAN is ON "));
  258.   } else {
  259.     iswlanon = false;
  260.     pinMode(38, OUTPUT);
  261.     digitalWrite(38, HIGH);
  262.     client.println(F(" WLAN is OFF "));
  263.   }
  264. }
  265.  
  266. void showVars(boolean isPost) {
  267.   sendHeader("Variablen");
  268.   if (isPost) {
  269.     client.find("\n\r");
  270.     while (client.findUntil("pinD", "\n\r")) {
  271.       int pin = client.parseInt();
  272.       int val = client.parseInt();
  273.       if (pin == 26) {
  274.         if (val == 0) {
  275.           ispcon = !ispcon;
  276.         } else {
  277.           turnpcoff = !turnpcoff;
  278.           if (turnpcoff == true) {
  279.             ispcon = false;
  280.           } else {
  281.             ispcon = true;
  282.           }
  283.         }
  284.       } if (pin == 27) {
  285.         ischargeon = !ischargeon;
  286.       } if (pin == 28) {
  287.         iswlanon = !iswlanon;
  288.       }
  289.     }
  290.   }
  291.   client.println(F("<table border='1'>"));
  292.   client.print(F("<tr><td>isPCon = "));
  293.   client.print(ispcon);
  294.   client.print(F("</td><td>"));
  295.   htmlButton("CHANGE", "pinD", 26, "0", "'/vars/'");
  296.   client.print(F("</td></tr><tr><td>"));
  297.   client.print(F("turnPCoff = "));
  298.   client.print(turnpcoff);
  299.   client.print(F("</td><td>"));
  300.   htmlButton("CHANGE", "pinD", 26, "1", "'/vars/'");
  301.   client.print(F("</td></tr><tr><td>"));
  302.   client.print(F("Chargin Port = "));
  303.   client.print((ischargeon));
  304.   client.print(F("</td><td>"));
  305.   htmlButton("CHANGE", "pinD", 27, "0", "'/vars/'");
  306.   client.print(F("</td></tr><tr><td>"));
  307.   client.print(F("isWLANon = "));
  308.   client.print(iswlanon);
  309.   client.print(F("</td><td>"));
  310.   htmlButton("CHANGE", "pinD", 28, "0", "'/vars/'");
  311.   client.print(F("</td></tr>"));
  312.   client.println(F("</table>"));
  313.  
  314. }
  315.  
  316.  
  317. void showVars2(boolean isPost) {
  318.   sendHeader("vars");
  319.   client.print(F("//t"));
  320.   client.print(ispcon);
  321.   client.print(F("//t"));
  322.   client.print(turnpcoff);
  323.   client.print(F("//t"));
  324.   client.print(ischargeon);
  325.   client.print(F("//t"));
  326.   client.print(iswlanon);
  327.   client.print(F("//t"));
  328. }
  329.  
  330. void showPowerOffPC(boolean isPost) {
  331.   sendHeader("PC POWER OFF");
  332.   ispcon = false;
  333.   turnpcoff = false;
  334.   pinMode(30, OUTPUT);
  335.   pinMode(31, OUTPUT);
  336.   pinMode(32, OUTPUT);
  337.   digitalWrite(30, HIGH);
  338.   //delay(750);
  339.   digitalWrite(31, HIGH);
  340.   //delay(750);
  341.   digitalWrite(32, HIGH);
  342. }
  343.  
  344. void killALL(boolean isPost) {
  345.   sendHeader("Kill ALL POWER");
  346.   ispcon = false;
  347.   turnpcoff = false;
  348.   iswlanon = false;
  349.   ischargeon = false;
  350.   for (int i = 30; i <= ((anzahl - 1) + 30); i++) {
  351.     pinMode(i, OUTPUT);
  352.     digitalWrite(i, HIGH);
  353.     //delay(125);
  354.   }
  355.  
  356. }
  357.  
  358. void showRelais(boolean isPost) {
  359.   if (isPost) {
  360.     client.find("\n\r");
  361.     while (client.findUntil("pinD", "\n\r")) {
  362.       int pin = 29 + client.parseInt();
  363.       int val = client.parseInt();
  364.       if (pin >= 30) {
  365.         pinMode(pin, OUTPUT);
  366.         digitalWrite(pin, val);
  367.       } else if (pin == 29) {
  368.         for (int i = 30; i <= ((anzahl - 1) + 30); i++) {
  369.           pinMode(i, OUTPUT);
  370.           digitalWrite(i, HIGH);
  371.           //delay(125);
  372.         }
  373.         turnpcoff = false;
  374.         ispcon = false;
  375.         ischargeon = false;
  376.         iswlanon = false;
  377.       }
  378.       if (pin == 33) {
  379.         ischargeon = !val;
  380.       }
  381.       if (pin == 38) {
  382.         iswlanon = !val;
  383.       }
  384.     }
  385.   }
  386.   sendHeader("Relais");
  387.  
  388.   client.println(F("<table border='1'>"));
  389.   client.print(F(" <tr><td>Kill All Power"));
  390.   client.print(F(" </td><td>"));
  391.   htmlButton("POWER OFF", "pinD", 0, "1", "'/relais/'");
  392.   client.print(F(" </td>"));
  393.   client.print(F("</tr>"));
  394.   client.println(F("</table>"));
  395.  
  396.   client.println(F("<table border='1'>"));
  397.   for (int i = 1; i < 5; i++) {
  398.     client.print(F("<tr><td>Relais "));
  399.     client.print(i);
  400.     client.print(F(" </td><td>"));
  401.     htmlButton("EIN", "pinD", i, "0", "'/relais/'");
  402.     client.print(F(" </td><td>"));
  403.     htmlButton("AUS", "pinD", i, "1", "'/relais/'");
  404.     client.print(F(" </td><td>Relais "));
  405.     client.print(i + 4);
  406.     client.print(F(" </td><td>"));
  407.     htmlButton("EIN", "pinD", i + 4, "0", "'/relais/'");
  408.     client.print(F(" </td><td>"));
  409.     htmlButton("AUS", "pinD", i + 4, "1", "'/relais/'");
  410.     client.print(F(" </td><td>Relais "));
  411.     client.print(i + 8);
  412.     client.print(F(" </td><td>"));
  413.     htmlButton("EIN", "pinD", i + 8, "0", "'/relais/'");
  414.     client.print(F(" </td><td>"));
  415.     htmlButton("AUS", "pinD", i + 8, "1", "'/relais/'");
  416.     client.print(F(" </td><td>Relais "));
  417.     client.print(i + 12);
  418.     client.print(F(" </td><td>"));
  419.     htmlButton("EIN", "pinD", i + 12, "0", "'/relais/'");
  420.     client.print(F(" </td><td>"));
  421.     htmlButton("AUS", "pinD", i + 12, "1", "'/relais/'");
  422.     client.print(F(" </td>"));
  423.   }
  424.   client.print(F("</tr>"));
  425.   client.println(F("</table>"));
  426. }
  427.  
  428. void htmlButton(char *label, char *name, int nameId, char *value, char *page) {
  429.   client.print(F("<form action='"));
  430.   client.print(page);
  431.   client.print(F("' method='POST'><p><input type='hidden' name='"));
  432.  
  433.   client.print(name);
  434.   client.print(nameId);
  435.   client.print(F("' value='"));
  436.   client.print(value);
  437.   client.print(F("'><input type='submit' value='"));
  438.   client.print(label);
  439.   client.print(F("'/></form>"));
  440. }
  441.  
  442. void unknownPage(char *page) {
  443.   sendHeader("Unbekannte Seite");
  444.   client.println(F("<h1>LOST</h1>"));
  445.   client.println(F("<h2>Unbekannte Seite</h2>"));
  446.   client.println(page);
  447. }
  448.  
  449. void sendHeader(char *title) {
  450.   // Standard HTTP-Response-Header senden
  451.   client.println(F("HTTP/1.1 200 OK"));
  452.   client.println(F("Content-Type: text/html"));
  453.   client.println();
  454.   client.print(F("<html><head><title>"));
  455.   client.println(title);
  456.   client.println(F("</title></head><body>"));
  457.  
  458. }
  459.  
  460. //http://pastebin.com/uurPzSZ6
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top