Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Bounce2.h>
- #include <DHT.h>
- #include <ESP8266WiFi.h>
- #define D0 16
- #define D1 5 // I2C Bus SCL (clock)
- #define D2 4 // I2C Bus SDA (data)
- #define D3 0
- #define D4 2 // Same as "LED_BUILTIN", but inverted logic
- #define D5 14 // SPI Bus SCK (clock)
- #define D6 12 // SPI Bus MISO
- #define D7 13 // SPI Bus MOSI
- #define D8 15 // SPI Bus SS (CS)
- #define D9 3 // RX0 (Serial console)
- #define D10 1 // TX0 (Serial console)
- #define DHTPIN_R 12 // Rechter Temp_Sensor
- #define DHTPIN_L 13 // Linker Temp_Sensor
- #define BUTTON_PIN_1 14 // Taster 1
- #define BUTTON_PIN_1 15 // Taster 2
- #define DHTTYPE DHT11
- DHT dht1(DHTPIN_R, DHTTYPE);
- DHT dht2(DHTPIN_L, DHTTYPE);
- Bounce debouncer1 = Bounce();
- // ------------------------------- Einstellungen -----------------------------------------------------
- const char* ssid = ""; // WLAN Name
- const char* password = ""; // WLAN Passwort
- String r1_name = "Heizlampe"; // Name auf Button 1
- String r2_name = "Neonlampe"; // Name auf Button 2
- String r3_name = "Strahler"; // Name auf Button 3
- String r4_name = "Nachtlampe"; // Name auf Button 4
- String nacht_name = "Nachtschaltung"; // Name für Nachtschaltung
- String tag_name = "Morgenschaltung"; // Name für Morgenschaltung
- int value1 = LOW;
- int value2 = LOW;
- int value3 = LOW;
- int value4 = LOW;
- int relais1 = 5; // D1
- int relais2 = 4; // D2
- int relais3 = 0; // D3
- int relais4 = 2; // D4
- // static IP
- IPAddress ip(192,168,100,99);
- IPAddress gateway(192,168,100,1);
- IPAddress subnet(255,255,255,0);
- WiFiServer server(80);
- void setup() {
- Serial.begin(115200);
- dht1.begin();
- dht2.begin();
- delay(10);
- pinMode(relais1, OUTPUT);
- pinMode(relais2, OUTPUT);
- pinMode(relais3, OUTPUT);
- pinMode(relais4, OUTPUT);
- pinMode(BUTTON_PIN_1,INPUT_PULLUP);
- digitalWrite(relais1, HIGH);
- digitalWrite(relais2, HIGH);
- digitalWrite(relais3, HIGH);
- digitalWrite(relais4, HIGH);
- debouncer1.attach(BUTTON_PIN_1);
- debouncer1.interval(5); // interval in ms
- // Connect to WiFi network
- WiFi.config(ip, gateway, subnet);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- server.begin();
- Serial.println(WiFi.localIP());
- }
- void loop() {
- debouncer1.update();
- int value1 = debouncer1.read();
- if ( value1 == LOW ) {
- digitalWrite(relais1, LOW);
- delay(300);
- } else {
- digitalWrite(relais1, HIGH);
- delay(300);
- }
- // Check if a client has connected
- WiFiClient client = server.available();
- if (!client) {
- return;
- }
- while(!client.available()){
- delay(1);
- html();
- }
- String request = client.readStringUntil('\r');
- // Serial.println(request); // Befehl ausgeben (DEBUG anschalten)
- client.flush();
- // Web Abfrage abfangen
- if (request.indexOf("/R1=ON") != -1) {
- digitalWrite(relais1, LOW);
- value1 = HIGH;
- }
- if (request.indexOf("/R1=OFF") != -1) {
- digitalWrite(relais1, HIGH);
- value1 = LOW;
- }
- if (request.indexOf("/R2=ON") != -1) {
- digitalWrite(relais2, LOW);
- value2 = HIGH;
- }
- if (request.indexOf("/R2=OFF") != -1) {
- digitalWrite(relais2, HIGH);
- value2 = LOW;
- }
- if (request.indexOf("/R3=ON") != -1) {
- digitalWrite(relais3, LOW);
- value3 = HIGH;
- }
- if (request.indexOf("/R3=OFF") != -1) {
- digitalWrite(relais3, HIGH);
- value3 = LOW;
- }
- if (request.indexOf("/R4=ON") != -1) {
- digitalWrite(relais4, LOW);
- value4 = HIGH;
- }
- if (request.indexOf("/R4=OFF") != -1) {
- digitalWrite(relais4, HIGH);
- value4 = LOW;
- }
- if (request.indexOf("/NACHTSCHALTUNG") != -1) {
- // Reigenfolge: NEON - Strahler - Heizer
- digitalWrite(relais2, HIGH);
- value1 = LOW;
- delay(300000); // 5 min
- digitalWrite(relais3, HIGH);
- value3 = LOW;
- delay(300000); // 5 Min
- digitalWrite(relais1, HIGH);
- value2 = LOW;
- delay(1000); // 1 Sek
- digitalWrite(relais4, LOW);
- value4 = HIGH;
- }
- // 120000 = 2 min
- // 300000 = 5 min
- // 450000 = 7,5 min
- // 600000 = 10 min
- if (request.indexOf("/GUTENMORGEN") != -1) {
- // digitalWrite(relais3, LOW);
- // value1 = HIGH;
- // delay(30000); // 8 min
- //
- // digitalWrite(relais2, LOW);
- // value3 = HIGH;
- // delay(5000); // 5 Sek
- //
- // digitalWrite(relais4, HIGH);
- // value2 = HIGH;
- }
- // Return the response
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println(""); // do not forget this one
- client.println("<!DOCTYPE HTML>");
- client.println("<html><body style=\"background-color:#D7DF01\">");
- byte hr = dht1.readHumidity(); // Lesen der Luftfeuchtigkeit und speichern in die Variable h
- byte tr = dht1.readTemperature(false); // Lesen der Temperatur in °C und speichern in die Variable t
- byte hl = dht2.readHumidity(); // Lesen der Luftfeuchtigkeit und speichern in die Variable h
- byte tl = dht2.readTemperature(false); // Lesen der Temperatur in °C und speichern in die Variable t
- client.print("<br><center><h1>Wernis Haussteuerung</h1>");
- delay(1);
- Serial.println("Client disonnected");
- Serial.println("");
- }
- void html() {
- String inhalt = "";
- inhalt += "<br><center><h1>Wernis Haussteuerung</h1>";
- //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\">";
- inhalt += "<table width=\40%\" align=\"center\" style=\"float:left; margin-right:20px; border: 1px solid red;\">";
- inhalt += "<td align=\"center\" colspan=\"2\" width=\"200px\"><h2>" + nacht_name + "</h2><hr></td><tr><td align=\"center\">";
- 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>";
- inhalt += "</td><tr><td align=\"center\" colspan=\"2\" width=\"200px\"><h2>" + tag_name + "</h2><hr></td><tr><td align=\"center\">";
- 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>";
- inhalt += "</td></table>";
- inhalt += "<p style=\"width:300px; font-size:1.3em; position:absolute; border:1px solid red; padding:10px; top:5px; right:20px;\">";
- inhalt += "Luftfeuchtigkeit Rechts: ";
- inhalt += " %";
- inhalt += "<br>Temperatur Rechts: ";
- inhalt += " C";
- inhalt += "<br>Luftfeuchtigkeit Links: ";
- inhalt += " %";
- inhalt += "<br>Temperatur Links: ";
- inhalt += "temp 1";
- inhalt += "173";
- inhalt += " C";
- inhalt += "</p>";
- inhalt += "<table width=\500px\" align=\"center\" style=\"float:left; border: 1px solid red;\">";
- // ------------------------------------------------------ BUTTON 1 ----------------------------------------------------------------------
- inhalt +="<td align=\"center\" colspan=\"2\" width=\"400px\"><h2>" + r1_name + "</h2></td><tr><td align=\"right\" style=\"border-bottom:1px solid red;\">";
- if(value1 == HIGH) {
- inhalt +="<p style=\"width:40px; height:40px; background-color:red; color:#fff; border-radius:20px;\"></p>";
- } else {
- inhalt +="<p style=\"width:40px; height:40px; background-color:green; color:#fff; border-radius:20px;\"></p>";
- }
- inhalt +="</td><td align=\"center\" style=\"border-bottom:1px solid red;\">";
- 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>";
- 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 />";
- inhalt +="</td><tr>";
- // ------------------------------------------------------BUTTON 2 ---------------------------------------------------------------
- inhalt +="<td align=\"center\" colspan=\"2\" width=\"400px\"><h2>" + r2_name + "</h2></td><tr><td align=\"right\" style=\"border-bottom:1px solid red;\">";
- if(value2 == HIGH) {
- inhalt +="<p style=\"width:40px; height:40px; background-color:red; color:#fff; border-radius:20px;\"></p>";
- } else {
- inhalt +="<p style=\"width:40px; height:40px; background-color:green; color:#fff; border-radius:20px;\"></p>";
- }
- inhalt +="</td><td align=\"center\" style=\"border-bottom:1px solid red;\">";
- 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>";
- 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 />";
- inhalt +="</td><tr>";
- // ------------------------------------------------------BUTTON 3 ---------------------------------------------------------------
- inhalt +="<td align=\"center\" colspan=\"2\" width=\"400px\"><h2>" + r3_name + "</h2></td><tr><td align=\"right\" style=\"border-bottom:1px solid red;\">";
- if(value3 == HIGH) {
- inhalt +="<p style=\"width:40px; height:40px; background-color:red; color:#fff; border-radius:20px;\"></p>";
- } else {
- inhalt +="<p style=\"width:40px; height:40px; background-color:green; color:#fff; border-radius:20px;\"></p>";
- }
- inhalt +="</td><td align=\"center\" style=\"border-bottom:1px solid red;\">";
- 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>";
- 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 />";
- inhalt +="</td><tr>";
- // ------------------------------------------------------BUTTON 4 ---------------------------------------------------------------
- inhalt +="<td align=\"center\" colspan=\"2\" width=\"400px\"><h2>" + r4_name + "</h2></td><tr><td align=\"right\">";
- if(value4 == HIGH) {
- inhalt +="<p style=\"width:40px; height:40px; background-color:red; color:#fff; border-radius:20px;\"></p>";
- } else {
- inhalt +="<p style=\"width:40px; height:40px; background-color:green; color:#fff; border-radius:20px;\"></p>";
- }
- inhalt +="</td><td align=\"center\">";
- 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>";
- 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 />";
- inhalt +="</td><tr>";
- inhalt +="</td>";
- inhalt +="</table>";
- inhalt +="</html>";
- }
Advertisement
Add Comment
Please, Sign In to add comment