thommy1972de

Test1

Aug 2nd, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.24 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Bounce2.h>
  3. #include <DHT.h>
  4. #include <ESP8266WiFi.h>
  5.  
  6. #define D0 16
  7. #define D1 5 // I2C Bus SCL (clock)
  8. #define D2 4 // I2C Bus SDA (data)
  9. #define D3 0
  10. #define D4 2 // Same as "LED_BUILTIN", but inverted logic
  11. #define D5 14 // SPI Bus SCK (clock)
  12. #define D6 12 // SPI Bus MISO
  13. #define D7 13 // SPI Bus MOSI
  14. #define D8 15 // SPI Bus SS (CS)
  15. #define D9 3 // RX0 (Serial console)
  16. #define D10 1 // TX0 (Serial console)
  17.  
  18. #define DHTPIN_R 12  // Rechter Temp_Sensor
  19. #define DHTPIN_L 13  // Linker Temp_Sensor
  20. #define BUTTON_PIN_1 14   // Taster 1
  21. #define BUTTON_PIN_1 15   // Taster 2
  22.  
  23. #define DHTTYPE DHT11
  24. DHT dht1(DHTPIN_R, DHTTYPE);
  25. DHT dht2(DHTPIN_L, DHTTYPE);
  26. Bounce debouncer1 = Bounce();
  27. // ------------------------------- Einstellungen -----------------------------------------------------
  28. const char* ssid = "";                     // WLAN Name
  29. const char* password = "";      // WLAN Passwort
  30.  
  31. String r1_name = "Heizlampe";                 // Name auf Button 1
  32. String r2_name = "Neonlampe";                 // Name auf Button 2
  33. String r3_name = "Strahler";                  // Name auf Button 3
  34. String r4_name = "Nachtlampe";                // Name auf Button 4
  35. String nacht_name = "Nachtschaltung";         // Name für Nachtschaltung
  36. String tag_name = "Morgenschaltung";          // Name für Morgenschaltung
  37.  
  38. int value1 = LOW;
  39. int value2 = LOW;
  40. int value3 = LOW;
  41. int value4 = LOW;
  42.  
  43. int relais1 = 5; // D1
  44. int relais2 = 4; // D2
  45. int relais3 = 0; // D3
  46. int relais4 = 2; // D4
  47.  
  48. // static IP
  49. IPAddress ip(192,168,100,99);
  50. IPAddress gateway(192,168,100,1);
  51. IPAddress subnet(255,255,255,0);
  52. WiFiServer server(80);
  53.  
  54. void setup() {
  55.   Serial.begin(115200);
  56.   dht1.begin();
  57.   dht2.begin();
  58.   delay(10);
  59.  
  60.   pinMode(relais1, OUTPUT);
  61.   pinMode(relais2, OUTPUT);
  62.   pinMode(relais3, OUTPUT);
  63.   pinMode(relais4, OUTPUT);
  64.   pinMode(BUTTON_PIN_1,INPUT_PULLUP);
  65.  
  66.   digitalWrite(relais1, HIGH);
  67.   digitalWrite(relais2, HIGH);
  68.   digitalWrite(relais3, HIGH);
  69.   digitalWrite(relais4, HIGH);
  70.  
  71.   debouncer1.attach(BUTTON_PIN_1);
  72.   debouncer1.interval(5); // interval in ms
  73.  
  74.  
  75.  
  76.   // Connect to WiFi network
  77.   WiFi.config(ip, gateway, subnet);
  78.   WiFi.begin(ssid, password);
  79.  
  80.   while (WiFi.status() != WL_CONNECTED) {
  81.     delay(500);
  82.     Serial.print(".");
  83.   }
  84.   server.begin();
  85.   Serial.println(WiFi.localIP());
  86.  
  87. }
  88.  
  89. void loop() {
  90.  
  91.   debouncer1.update();
  92.   int value1 = debouncer1.read();
  93.  
  94.  if ( value1 == LOW ) {
  95.     digitalWrite(relais1, LOW);
  96.     delay(300);
  97.   } else {
  98.     digitalWrite(relais1, HIGH);
  99.     delay(300);
  100.   }
  101.  
  102.  
  103.   // Check if a client has connected
  104.   WiFiClient client = server.available();
  105.   if (!client) {
  106.     return;
  107.   }
  108.   while(!client.available()){
  109.     delay(1);
  110.     html();
  111.   }
  112.   String request = client.readStringUntil('\r');
  113.   // Serial.println(request); // Befehl ausgeben (DEBUG anschalten)
  114.   client.flush();
  115.  
  116.  
  117.   // Web Abfrage abfangen
  118.  
  119.   if (request.indexOf("/R1=ON") != -1)  {
  120.     digitalWrite(relais1, LOW);
  121.     value1 = HIGH;
  122.   }
  123.   if (request.indexOf("/R1=OFF") != -1)  {
  124.     digitalWrite(relais1, HIGH);
  125.     value1 = LOW;
  126.   }
  127.  
  128.   if (request.indexOf("/R2=ON") != -1)  {
  129.     digitalWrite(relais2, LOW);
  130.     value2 = HIGH;
  131.   }
  132.  
  133.   if (request.indexOf("/R2=OFF") != -1)  {
  134.     digitalWrite(relais2, HIGH);
  135.     value2 = LOW;
  136.   }
  137.  
  138.   if (request.indexOf("/R3=ON") != -1)  {
  139.     digitalWrite(relais3, LOW);
  140.     value3 = HIGH;
  141.   }
  142.  
  143.   if (request.indexOf("/R3=OFF") != -1)  {
  144.     digitalWrite(relais3, HIGH);
  145.     value3 = LOW;
  146.   }
  147.  
  148.     if (request.indexOf("/R4=ON") != -1)  {
  149.     digitalWrite(relais4, LOW);
  150.     value4 = HIGH;
  151.   }
  152.  
  153.   if (request.indexOf("/R4=OFF") != -1)  {
  154.     digitalWrite(relais4, HIGH);
  155.     value4 = LOW;
  156.   }
  157.  
  158.  
  159.   if (request.indexOf("/NACHTSCHALTUNG") != -1)  {
  160.  
  161.     // Reigenfolge: NEON - Strahler - Heizer
  162.    
  163.     digitalWrite(relais2, HIGH);
  164.     value1 = LOW;
  165.  
  166.     delay(300000); // 5 min
  167.     digitalWrite(relais3, HIGH);
  168.     value3 = LOW;
  169.    
  170.     delay(300000); // 5 Min
  171.     digitalWrite(relais1, HIGH);
  172.     value2 = LOW;
  173.    
  174.     delay(1000); // 1 Sek
  175.     digitalWrite(relais4, LOW);
  176.     value4 = HIGH;
  177.   }
  178.  
  179. // 120000 = 2 min
  180. // 300000 = 5 min
  181. // 450000 = 7,5 min
  182. // 600000 = 10 min
  183.  
  184.  
  185.     if (request.indexOf("/GUTENMORGEN") != -1)  {
  186.      
  187. //    digitalWrite(relais3, LOW);
  188. //    value1 = HIGH;
  189. //    delay(30000); // 8 min
  190. //    
  191. //    digitalWrite(relais2, LOW);
  192. //    value3 = HIGH;
  193. //    delay(5000); // 5 Sek
  194. //    
  195. //    digitalWrite(relais4, HIGH);
  196. //    value2 = HIGH;
  197.  
  198.  
  199.   }
  200.  
  201.  
  202.  
  203.  
  204.   // Return the response
  205.   client.println("HTTP/1.1 200 OK");
  206.   client.println("Content-Type: text/html");
  207.   client.println(""); //  do not forget this one
  208.   client.println("<!DOCTYPE HTML>");
  209.   client.println("<html><body style=\"background-color:#D7DF01\">");
  210.  
  211.   byte hr = dht1.readHumidity();    // Lesen der Luftfeuchtigkeit und speichern in die Variable h
  212.   byte tr = dht1.readTemperature(false); // Lesen der Temperatur in °C und speichern in die Variable t
  213.   byte hl = dht2.readHumidity();    // Lesen der Luftfeuchtigkeit und speichern in die Variable h
  214.   byte tl = dht2.readTemperature(false); // Lesen der Temperatur in °C und speichern in die Variable t
  215.  
  216.   client.print("<br><center><h1>Wernis Haussteuerung</h1>");
  217.  
  218.   delay(1);
  219.   Serial.println("Client disonnected");
  220.   Serial.println("");
  221.  
  222. }
  223.  
  224. void html() {
  225.   String inhalt = "";
  226.   inhalt += "<br><center><h1>Wernis Haussteuerung</h1>";
  227.   //inhalt +="<img style=\"width:300px; position:absolute; border:1px solid #fff;  top:150px; right:30px; opacity: 0.7\" src=\"http://www.zwpc.de/werni_gross.jpg\">";
  228.   inhalt += "<table width=\40%\" align=\"center\" style=\"float:left; margin-right:20px; border: 1px solid red;\">";
  229.   inhalt += "<td align=\"center\" colspan=\"2\" width=\"200px\"><h2>" + nacht_name + "</h2><hr></td><tr><td align=\"center\">";
  230.   inhalt += "<a href=\"/NACHTSCHALTUNG\"\"><button style=\"border-radius:20px; font-size:1em; border:none; width:150px; padding:10px; margin-bottom:5px; background-color:#2E2EFE; color:#fff\">Geh Schlafen...</button></a><br>";
  231.   inhalt += "</td><tr><td align=\"center\" colspan=\"2\" width=\"200px\"><h2>" + tag_name + "</h2><hr></td><tr><td align=\"center\">";
  232.   inhalt += "<a href=\"/GUTENMORGEN\"\"><button style=\"border-radius:20px; font-size:1em; border:none; width:150px; padding:10px; margin-bottom:5px; background-color:#2E2EFE; color:#fff\">Guten Morgen !</button></a><br>";
  233.   inhalt += "</td></table>";
  234.   inhalt += "<p style=\"width:300px; font-size:1.3em; position:absolute; border:1px solid red; padding:10px; top:5px; right:20px;\">";
  235.   inhalt += "Luftfeuchtigkeit Rechts: ";
  236.   inhalt += " %";
  237.   inhalt += "<br>Temperatur Rechts: ";
  238.   inhalt += " C";
  239.   inhalt += "<br>Luftfeuchtigkeit Links: ";
  240.   inhalt += " %";
  241.   inhalt += "<br>Temperatur Links: ";
  242.   inhalt += "temp 1";
  243.   inhalt += "173";
  244.   inhalt += " C";
  245.   inhalt += "</p>";
  246.   inhalt += "<table width=\500px\" align=\"center\" style=\"float:left; border: 1px solid red;\">";
  247. // ------------------------------------------------------ BUTTON 1 ----------------------------------------------------------------------
  248.   inhalt +="<td align=\"center\" colspan=\"2\" width=\"400px\"><h2>" + r1_name + "</h2></td><tr><td align=\"right\" style=\"border-bottom:1px solid red;\">";
  249.   if(value1 == HIGH) {
  250.     inhalt +="<p style=\"width:40px; height:40px; background-color:red; color:#fff; border-radius:20px;\"></p>";
  251.   } else {
  252.     inhalt +="<p style=\"width:40px; height:40px; background-color:green; color:#fff; border-radius:20px;\"></p>";
  253.   }
  254.   inhalt +="</td><td align=\"center\" style=\"border-bottom:1px solid red;\">";
  255.   inhalt +="<a href=\"/R1=ON\"\"><button style=\"border-radius:20px; font-size:1em; border:none; width:150px; padding:5px; margin-bottom:5px; background-color:#2E2EFE; color:#fff\">Einschalten</button></a><br>";
  256.   inhalt +="<a href=\"/R1=OFF\"\"><button style=\"border-radius:20px; font-size:1em; border:none; width:150px; padding:5px; background-color:#2E2EFE; color:#fff\">Ausschalten</button></a><br />";  
  257.   inhalt +="</td><tr>";
  258.   // ------------------------------------------------------BUTTON 2 ---------------------------------------------------------------
  259.   inhalt +="<td align=\"center\" colspan=\"2\" width=\"400px\"><h2>" + r2_name + "</h2></td><tr><td align=\"right\" style=\"border-bottom:1px solid red;\">";
  260.   if(value2 == HIGH) {
  261.     inhalt +="<p style=\"width:40px; height:40px; background-color:red; color:#fff; border-radius:20px;\"></p>";
  262.   } else {
  263.     inhalt +="<p style=\"width:40px; height:40px; background-color:green; color:#fff; border-radius:20px;\"></p>";
  264.   }
  265.   inhalt +="</td><td align=\"center\" style=\"border-bottom:1px solid red;\">";
  266.   inhalt +="<a href=\"/R2=ON\"\"><button style=\"border-radius:20px; font-size:1em; border:none; width:150px; padding:5px; margin-bottom:5px; background-color:#2E2EFE; color:#fff\">Einschalten</button></a><br>";
  267.   inhalt +="<a href=\"/R2=OFF\"\"><button style=\"border-radius:20px; font-size:1em; border:none; width:150px; padding:5px; background-color:#2E2EFE; color:#fff\">Ausschalten</button></a><br />";  
  268.   inhalt +="</td><tr>";
  269.   // ------------------------------------------------------BUTTON 3 ---------------------------------------------------------------
  270.   inhalt +="<td align=\"center\" colspan=\"2\" width=\"400px\"><h2>" + r3_name + "</h2></td><tr><td align=\"right\" style=\"border-bottom:1px solid red;\">";
  271.   if(value3 == HIGH) {
  272.     inhalt +="<p style=\"width:40px; height:40px; background-color:red; color:#fff; border-radius:20px;\"></p>";
  273.   } else {
  274.     inhalt +="<p style=\"width:40px; height:40px; background-color:green; color:#fff; border-radius:20px;\"></p>";
  275.   }
  276.   inhalt +="</td><td align=\"center\" style=\"border-bottom:1px solid red;\">";
  277.   inhalt +="<a href=\"/R3=ON\"\"><button style=\"border-radius:20px; font-size:1em; border:none; width:150px; padding:5px; margin-bottom:5px; background-color:#2E2EFE; color:#fff\">Einschalten</button></a><br>";
  278.   inhalt +="<a href=\"/R3=OFF\"\"><button style=\"border-radius:20px; font-size:1em; border:none; width:150px; padding:5px; background-color:#2E2EFE; color:#fff\">Ausschalten</button></a><br />";  
  279.   inhalt +="</td><tr>";  
  280.   // ------------------------------------------------------BUTTON 4 ---------------------------------------------------------------
  281.   inhalt +="<td align=\"center\" colspan=\"2\" width=\"400px\"><h2>" + r4_name + "</h2></td><tr><td align=\"right\">";
  282.   if(value4 == HIGH) {
  283.     inhalt +="<p style=\"width:40px; height:40px; background-color:red; color:#fff; border-radius:20px;\"></p>";
  284.   } else {
  285.     inhalt +="<p style=\"width:40px; height:40px; background-color:green; color:#fff; border-radius:20px;\"></p>";
  286.   }
  287.   inhalt +="</td><td align=\"center\">";
  288.   inhalt +="<a href=\"/R4=ON\"\"><button style=\"border-radius:20px; font-size:1em; border:none; width:150px; padding:5px; margin-bottom:5px; background-color:#2E2EFE; color:#fff\">Einschalten</button></a><br>";
  289.   inhalt +="<a href=\"/R4=OFF\"\"><button style=\"border-radius:20px; font-size:1em; border:none; width:150px; padding:5px; background-color:#2E2EFE; color:#fff\">Ausschalten</button></a><br />";  
  290.   inhalt +="</td><tr>";
  291.   inhalt +="</td>";
  292.   inhalt +="</table>";
  293.   inhalt +="</html>";
  294. }
Advertisement
Add Comment
Please, Sign In to add comment