Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <ESP8266WebServer.h>
- #include <arduino_secrets.h>
- const char* ssid = SECRET_SSID;
- const char* password = SECRET_PASSWORD;
- const int switch1Pin = D1;
- const int switch2Pin = D2;
- const int emergencyPin = D3;
- const int relayOpenPin = D5; // Relé pre otvorenie brány
- const int relayClosePin = D6; // Relé pre zatvorenie brány
- const int ledOpenPin = D7; // LED pre otvorenú bránu
- const int ledClosePin = D8; // LED pre zatvorenú bránu
- ESP8266WebServer server(80);
- // Deklarácie funkcií
- void handleRoot();
- void handleOpenGate();
- void handleCloseGate();
- void checkSwitches();
- void handleEmergency();
- void setup() {
- Serial.begin(115200);
- pinMode(switch1Pin, INPUT_PULLUP); //INPUT_PULLUP
- pinMode(switch2Pin, INPUT_PULLUP);
- pinMode(emergencyPin, INPUT_PULLUP);
- pinMode(relayOpenPin, OUTPUT);
- pinMode(relayClosePin, OUTPUT);
- pinMode(ledOpenPin, OUTPUT);
- pinMode(ledClosePin, OUTPUT);
- // Pripojenie k Wi-Fi
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.println("Connecting to WiFi...");
- }
- Serial.println("Connected to WiFi");
- // Nastavenie HTTP servera
- server.on("/", handleRoot);
- server.on("/open", handleOpenGate);
- server.on("/close", handleCloseGate);
- server.begin();
- Serial.println("HTTP server started");
- }
- void loop() {
- int emergencyState = digitalRead(emergencyPin);
- // Kontrola núdzového spínača
- if (emergencyState == LOW) { // Núdzové tlačidlo stlačené
- handleEmergency();
- }
- server.handleClient();
- }
- void checkSwitches() {
- int switch1State = digitalRead(switch1Pin);
- int switch2State = digitalRead(switch2Pin);
- // Zobrazenie stavov v Serial monitore
- Serial.print("Switch 1: ");
- Serial.print(switch1State == LOW ? "LOW" : "HIGH");
- Serial.print(", Switch 2: ");
- Serial.print(switch2State == LOW ? "LOW" : "HIGH");
- Serial.print(", Gate: ");
- Serial.println(digitalRead(relayOpenPin) == LOW ? "CLOSED" : "OPEN");
- if ((switch1State == LOW && switch2State == LOW) ||
- (switch1State == HIGH && switch2State == LOW) ||
- (switch1State == HIGH && switch2State == HIGH)) {
- digitalWrite(relayOpenPin, HIGH); // Otvorí bránu
- digitalWrite(relayClosePin, LOW);
- digitalWrite(ledOpenPin, HIGH); // Zapne LED pre otvorenú bránu
- digitalWrite(ledClosePin, LOW); // Vypne LED pre zatvorenú bránu
- } else {
- digitalWrite(relayOpenPin, LOW); // Zatvorí bránu
- digitalWrite(relayClosePin, HIGH);
- digitalWrite(ledOpenPin, LOW); // Vypne LED pre otvorenú bránu
- digitalWrite(ledClosePin, HIGH); // Zapne LED pre zatvorenú bránu
- }
- }
- void handleRoot() {
- String html = "<html><head><style>";
- html += "body { font-family: Arial, sans-serif; text-align: center; padding: 50px; }";
- html += "h1 { color: #333; }";
- html += "a { display: inline-block; margin: 20px; padding: 20px; font-size: 20px; color: white; background-color: #007BFF; text-decoration: none; border-radius: 5px; }";
- html += "a:hover { background-color: #0056b3; }";
- html += "</style></head><body>";
- html += "<h1>Gate Control</h1>";
- html += "<p><a href=\"/open\">Open Gate</a></p>";
- html += "<p><a href=\"/close\">Close Gate</a></p>";
- html += "</body></html>";
- server.send(200, "text/html", html);
- }
- void handleOpenGate() {
- digitalWrite(relayOpenPin, HIGH);
- digitalWrite(relayClosePin, LOW);
- digitalWrite(ledOpenPin, HIGH); // Zapne LED pre otvorenú bránu
- digitalWrite(ledClosePin, LOW); // Vypne LED pre zatvorenú bránu
- server.send(200, "text/plain", "Gate is OPEN");
- checkSwitches(); // Kontrola spínačov po otvorení brány
- }
- void handleCloseGate() {
- digitalWrite(relayOpenPin, LOW);
- digitalWrite(relayClosePin, HIGH);
- digitalWrite(ledOpenPin, LOW); // Vypne LED pre otvorenú bránu
- digitalWrite(ledClosePin, HIGH); // Zapne LED pre zatvorenú bránu
- server.send(200, "text/plain", "Gate is CLOSED");
- checkSwitches(); // Kontrola spínačov po zatvorení brány
- }
- void handleEmergency() {
- // Ak je brána otvorená, zatvorí ju, inak ju otvorí
- if (digitalRead(relayOpenPin) == HIGH) {
- handleCloseGate();
- } else {
- handleOpenGate();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement