Advertisement
Nemo1979

Untitled

Sep 23rd, 2024 (edited)
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.21 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266WebServer.h>
  3. #include <arduino_secrets.h>
  4.  
  5. const char* ssid = SECRET_SSID;
  6. const char* password = SECRET_PASSWORD;
  7.  
  8. const int switch1Pin = D1;
  9. const int switch2Pin = D2;
  10. const int emergencyPin = D3;
  11. const int relayOpenPin = D5; // Relé pre otvorenie brány
  12. const int relayClosePin = D6; // Relé pre zatvorenie brány
  13. const int ledOpenPin = D7; // LED pre otvorenú bránu
  14. const int ledClosePin = D8; // LED pre zatvorenú bránu
  15.  
  16. ESP8266WebServer server(80);
  17.  
  18. // Deklarácie funkcií
  19. void handleRoot();
  20. void handleOpenGate();
  21. void handleCloseGate();
  22. void checkSwitches();
  23. void handleEmergency();
  24.  
  25. void setup() {
  26.   Serial.begin(115200);
  27.   pinMode(switch1Pin, INPUT_PULLUP); //INPUT_PULLUP
  28.   pinMode(switch2Pin, INPUT_PULLUP);
  29.   pinMode(emergencyPin, INPUT_PULLUP);
  30.   pinMode(relayOpenPin, OUTPUT);
  31.   pinMode(relayClosePin, OUTPUT);
  32.   pinMode(ledOpenPin, OUTPUT);
  33.   pinMode(ledClosePin, OUTPUT);
  34.  
  35.   // Pripojenie k Wi-Fi
  36.   WiFi.begin(ssid, password);
  37.   while (WiFi.status() != WL_CONNECTED) {
  38.     delay(1000);
  39.     Serial.println("Connecting to WiFi...");
  40.   }
  41.   Serial.println("Connected to WiFi");
  42.  
  43.   // Nastavenie HTTP servera
  44.   server.on("/", handleRoot);
  45.   server.on("/open", handleOpenGate);
  46.   server.on("/close", handleCloseGate);
  47.   server.begin();
  48.   Serial.println("HTTP server started");
  49. }
  50.  
  51. void loop() {
  52.   int emergencyState = digitalRead(emergencyPin);
  53.  
  54.   // Kontrola núdzového spínača
  55.   if (emergencyState == LOW) { // Núdzové tlačidlo stlačené
  56.     handleEmergency();
  57.   }
  58.  
  59.   server.handleClient();
  60. }
  61.  
  62. void checkSwitches() {
  63.   int switch1State = digitalRead(switch1Pin);
  64.   int switch2State = digitalRead(switch2Pin);
  65.  
  66.   // Zobrazenie stavov v Serial monitore
  67.   Serial.print("Switch 1: ");
  68.   Serial.print(switch1State == LOW ? "LOW" : "HIGH");
  69.   Serial.print(", Switch 2: ");
  70.   Serial.print(switch2State == LOW ? "LOW" : "HIGH");
  71.   Serial.print(", Gate: ");
  72.   Serial.println(digitalRead(relayOpenPin) == LOW ? "CLOSED" : "OPEN");
  73.  
  74.   if ((switch1State == LOW && switch2State == LOW) ||
  75.       (switch1State == HIGH && switch2State == LOW) ||
  76.       (switch1State == HIGH && switch2State == HIGH)) {
  77.     digitalWrite(relayOpenPin, HIGH); // Otvorí bránu
  78.     digitalWrite(relayClosePin, LOW);
  79.     digitalWrite(ledOpenPin, HIGH); // Zapne LED pre otvorenú bránu
  80.     digitalWrite(ledClosePin, LOW); // Vypne LED pre zatvorenú bránu
  81.   } else {
  82.     digitalWrite(relayOpenPin, LOW); // Zatvorí bránu
  83.     digitalWrite(relayClosePin, HIGH);
  84.     digitalWrite(ledOpenPin, LOW); // Vypne LED pre otvorenú bránu
  85.     digitalWrite(ledClosePin, HIGH); // Zapne LED pre zatvorenú bránu
  86.   }
  87. }
  88.  
  89. void handleRoot() {
  90.   String html = "<html><head><style>";
  91.   html += "body { font-family: Arial, sans-serif; text-align: center; padding: 50px; }";
  92.   html += "h1 { color: #333; }";
  93.   html += "a { display: inline-block; margin: 20px; padding: 20px; font-size: 20px; color: white; background-color: #007BFF; text-decoration: none; border-radius: 5px; }";
  94.   html += "a:hover { background-color: #0056b3; }";
  95.   html += "</style></head><body>";
  96.   html += "<h1>Gate Control</h1>";
  97.   html += "<p><a href=\"/open\">Open Gate</a></p>";
  98.   html += "<p><a href=\"/close\">Close Gate</a></p>";
  99.   html += "</body></html>";
  100.   server.send(200, "text/html", html);
  101. }
  102.  
  103. void handleOpenGate() {
  104.   digitalWrite(relayOpenPin, HIGH);
  105.   digitalWrite(relayClosePin, LOW);
  106.   digitalWrite(ledOpenPin, HIGH); // Zapne LED pre otvorenú bránu
  107.   digitalWrite(ledClosePin, LOW); // Vypne LED pre zatvorenú bránu
  108.   server.send(200, "text/plain", "Gate is OPEN");
  109.   checkSwitches(); // Kontrola spínačov po otvorení brány
  110. }
  111.  
  112. void handleCloseGate() {
  113.   digitalWrite(relayOpenPin, LOW);
  114.   digitalWrite(relayClosePin, HIGH);
  115.   digitalWrite(ledOpenPin, LOW); // Vypne LED pre otvorenú bránu
  116.   digitalWrite(ledClosePin, HIGH); // Zapne LED pre zatvorenú bránu
  117.   server.send(200, "text/plain", "Gate is CLOSED");
  118.   checkSwitches(); // Kontrola spínačov po zatvorení brány
  119. }
  120.  
  121. void handleEmergency() {
  122.   // Ak je brána otvorená, zatvorí ju, inak ju otvorí
  123.   if (digitalRead(relayOpenPin) == HIGH) {
  124.     handleCloseGate();
  125.   } else {
  126.     handleOpenGate();
  127.   }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement