Advertisement
Nemo1979

09102024-1

Oct 9th, 2024
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.78 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266WebServer.h>
  3. #include <arduino_secrets.h>
  4. // Definovanie pinov
  5. const int sensorPin = D1;
  6. const int relayOpenPin = D5;
  7. const int relayClosePin = D6;
  8. const int buttonPin = D4; // Pin pre hardvérové tlačidlo
  9.  
  10. // Stavové premenné
  11. bool gateOpen = false;
  12. bool gateClose = false;
  13. bool emergencyStop = false;
  14. bool wasOpening = false;
  15. bool wasClosing = false;
  16.  
  17. // Nastavenie WiFi
  18. const char* ssid = YOUR_SSID;
  19. const char* password = YOUR_PASSWORD;
  20.  
  21. ESP8266WebServer server(80);
  22.  
  23. // HTML obsah ako raw literal string
  24. const char index_html[] PROGMEM = R"rawliteral(
  25. <html lang='sk'>
  26.  
  27. <head>
  28.    <meta charset='UTF-8'>
  29.        <meta name='viewport' content='width=device-width, initial-scale=1.0'>
  30.        <title>Ovládanie brány</title>
  31.    <style>
  32.  
  33.        body { font-family: Arial, sans-serif; text-align: center; margin-top: 50px; }
  34.        .container { width: 90%; max-width: 600px; margin: auto; }
  35.        button { padding: 20px; font-size: 24px; margin: 20px 0; width: 100%; }
  36.        .emergency {
  37.            background-color: red;
  38.            color: white;
  39.        }
  40.    </style>
  41. </head>
  42. <body>
  43.    <h1>Ovládanie brány</h1>
  44.    <button onclick="openGate()">Otvoriť bránu</button>
  45.    <button onclick="closeGate()">Zatvoriť bránu</button>
  46.    <button class="emergency" onclick="emergencyStop()">Núdzové zastavenie</button>
  47.  
  48.    <script>
  49.        function openGate() {
  50.            fetch('/open');
  51.        }
  52.  
  53.        function closeGate() {
  54.            fetch('/close');
  55.        }
  56.  
  57.        function emergencyStop() {
  58.            fetch('/emergency');
  59.        }
  60.    </script>
  61. </body>
  62. </html>
  63. )rawliteral";
  64.  
  65. void setup() {
  66.   Serial.begin(115200);
  67.  
  68.   // Nastavenie pinov
  69.   pinMode(sensorPin, INPUT);
  70.   pinMode(relayOpenPin, OUTPUT);
  71.   pinMode(relayClosePin, OUTPUT);
  72.   pinMode(buttonPin, INPUT_PULLUP); // Nastavenie tlačidla s interným pull-up rezistorom
  73.  
  74.   // Pripojenie k WiFi
  75.   WiFi.begin(ssid, password);
  76.   while (WiFi.status() != WL_CONNECTED) {
  77.     delay(1000);
  78.     Serial.println("Connecting to WiFi...");
  79.   }
  80.   Serial.println("Connected to WiFi");
  81.  
  82.   // Nastavenie servera
  83.   server.on("/", HTTP_GET, [] {
  84.     server.send_P(200, "text/html", index_html);
  85.   });
  86.  
  87.   server.on("/open", HTTP_GET, [] {
  88.     if (!emergencyStop) {
  89.       digitalWrite(relayOpenPin, HIGH);
  90.       while (digitalRead(sensorPin) == LOW) {
  91.         delay(100); // Čakanie na otvorenie brány
  92.       }
  93.       digitalWrite(relayOpenPin, LOW);
  94.       gateOpen = true;
  95.       gateClose = false;
  96.       wasOpening = true;
  97.       wasClosing = false;
  98.       server.send(200, "text/plain", "Brána sa otvára");
  99.     } else {
  100.       server.send(200, "text/plain", "Núdzové zastavenie je aktívne");
  101.     }
  102.   });
  103.  
  104.   server.on("/close", HTTP_GET, [] {
  105.     if (!emergencyStop) {
  106.       digitalWrite(relayClosePin, HIGH);
  107.       while (digitalRead(sensorPin) == HIGH) {
  108.         delay(100); // Čakanie na zatvorenie brány
  109.       }
  110.       digitalWrite(relayClosePin, LOW);
  111.       gateClose = true;
  112.       gateOpen = false;
  113.       wasClosing = true;
  114.       wasOpening = false;
  115.       server.send(200, "text/plain", "Brána sa zatvára");
  116.     } else {
  117.       server.send(200, "text/plain", "Núdzové zastavenie je aktívne");
  118.     }
  119.   });
  120.  
  121.   server.on("/emergency", HTTP_GET, [] {
  122.     emergencyStop = !emergencyStop;
  123.     if (emergencyStop) {
  124.       digitalWrite(relayOpenPin, LOW);
  125.       digitalWrite(relayClosePin, LOW);
  126.       gateOpen = false;
  127.       gateClose = false;
  128.       server.send(200, "text/plain", "Núdzové zastavenie aktivované");
  129.     } else {
  130.       if (wasOpening) {
  131.         digitalWrite(relayOpenPin, HIGH);
  132.         while (digitalRead(sensorPin) == LOW) {
  133.           delay(100); // Čakanie na otvorenie brány
  134.         }
  135.         digitalWrite(relayOpenPin, LOW);
  136.         gateOpen = true;
  137.         gateClose = false;
  138.         server.send(200, "text/plain", "Pokračovanie otvárania brány");
  139.       } else if (wasClosing) {
  140.         digitalWrite(relayClosePin, HIGH);
  141.         while (digitalRead(sensorPin) == HIGH) {
  142.           delay(100); // Čakanie na zatvorenie brány
  143.         }
  144.         digitalWrite(relayClosePin, LOW);
  145.         gateClose = true;
  146.         gateOpen = false;
  147.         server.send(200, "text/plain", "Pokračovanie zatvárania brány");
  148.       } else {
  149.         server.send(200, "text/plain", "Núdzové zastavenie deaktivované");
  150.       }
  151.     }
  152.   });
  153.  
  154.   server.begin();
  155. }
  156.  
  157. void loop() {
  158.   server.handleClient();
  159.  
  160.   // Čítanie stavu tlačidla
  161.   if (digitalRead(buttonPin) == LOW) {
  162.     delay(50); // Debounce delay
  163.     if (digitalRead(buttonPin) == LOW) {
  164.       emergencyStop = !emergencyStop;
  165.       if (emergencyStop) {
  166.         digitalWrite(relayOpenPin, LOW);
  167.         digitalWrite(relayClosePin, LOW);
  168.         gateOpen = false;
  169.         gateClose = false;
  170.         Serial.println("Núdzové zastavenie aktivované tlačidlom");
  171.       } else {
  172.         if (wasOpening) {
  173.           digitalWrite(relayOpenPin, HIGH);
  174.           while (digitalRead(sensorPin) == LOW) {
  175.             delay(100); // Čakanie na otvorenie brány
  176.           }
  177.           digitalWrite(relayOpenPin, LOW);
  178.           gateOpen = true;
  179.           gateClose = false;
  180.           Serial.println("Pokračovanie otvárania brány");
  181.         } else if (wasClosing) {
  182.           digitalWrite(relayClosePin, HIGH);
  183.           while (digitalRead(sensorPin) == HIGH) {
  184.             delay(100); // Čakanie na zatvorenie brány
  185.           }
  186.           digitalWrite(relayClosePin, LOW);
  187.           gateClose = true;
  188.           gateOpen = false;
  189.           Serial.println("Pokračovanie zatvárania brány");
  190.         } else {
  191.           Serial.println("Núdzové zastavenie deaktivované tlačidlom");
  192.         }
  193.       }
  194.       delay(1000); // Debounce delay
  195.     }
  196.   }
  197. }
  198.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement