Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <ESP8266WebServer.h>
- #include <arduino_secrets.h>
- // Definovanie pinov
- const int sensorPin = D1;
- const int relayOpenPin = D5;
- const int relayClosePin = D6;
- const int buttonPin = D4; // Pin pre hardvérové tlačidlo
- // Stavové premenné
- bool gateOpen = false;
- bool gateClose = false;
- bool emergencyStop = false;
- bool wasOpening = false;
- bool wasClosing = false;
- // Nastavenie WiFi
- const char* ssid = YOUR_SSID;
- const char* password = YOUR_PASSWORD;
- ESP8266WebServer server(80);
- // HTML obsah ako raw literal string
- const char index_html[] PROGMEM = R"rawliteral(
- <html lang='sk'>
- <head>
- <meta charset='UTF-8'>
- <meta name='viewport' content='width=device-width, initial-scale=1.0'>
- <title>Ovládanie brány</title>
- <style>
- body { font-family: Arial, sans-serif; text-align: center; margin-top: 50px; }
- .container { width: 90%; max-width: 600px; margin: auto; }
- button { padding: 20px; font-size: 24px; margin: 20px 0; width: 100%; }
- .emergency {
- background-color: red;
- color: white;
- }
- </style>
- </head>
- <body>
- <h1>Ovládanie brány</h1>
- <button onclick="openGate()">Otvoriť bránu</button>
- <button onclick="closeGate()">Zatvoriť bránu</button>
- <button class="emergency" onclick="emergencyStop()">Núdzové zastavenie</button>
- <script>
- function openGate() {
- fetch('/open');
- }
- function closeGate() {
- fetch('/close');
- }
- function emergencyStop() {
- fetch('/emergency');
- }
- </script>
- </body>
- </html>
- )rawliteral";
- void setup() {
- Serial.begin(115200);
- // Nastavenie pinov
- pinMode(sensorPin, INPUT);
- pinMode(relayOpenPin, OUTPUT);
- pinMode(relayClosePin, OUTPUT);
- pinMode(buttonPin, INPUT_PULLUP); // Nastavenie tlačidla s interným pull-up rezistorom
- // Pripojenie k WiFi
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.println("Connecting to WiFi...");
- }
- Serial.println("Connected to WiFi");
- // Nastavenie servera
- server.on("/", HTTP_GET, [] {
- server.send_P(200, "text/html", index_html);
- });
- server.on("/open", HTTP_GET, [] {
- if (!emergencyStop) {
- digitalWrite(relayOpenPin, HIGH);
- while (digitalRead(sensorPin) == LOW) {
- delay(100); // Čakanie na otvorenie brány
- }
- digitalWrite(relayOpenPin, LOW);
- gateOpen = true;
- gateClose = false;
- wasOpening = true;
- wasClosing = false;
- server.send(200, "text/plain", "Brána sa otvára");
- } else {
- server.send(200, "text/plain", "Núdzové zastavenie je aktívne");
- }
- });
- server.on("/close", HTTP_GET, [] {
- if (!emergencyStop) {
- digitalWrite(relayClosePin, HIGH);
- while (digitalRead(sensorPin) == HIGH) {
- delay(100); // Čakanie na zatvorenie brány
- }
- digitalWrite(relayClosePin, LOW);
- gateClose = true;
- gateOpen = false;
- wasClosing = true;
- wasOpening = false;
- server.send(200, "text/plain", "Brána sa zatvára");
- } else {
- server.send(200, "text/plain", "Núdzové zastavenie je aktívne");
- }
- });
- server.on("/emergency", HTTP_GET, [] {
- emergencyStop = !emergencyStop;
- if (emergencyStop) {
- digitalWrite(relayOpenPin, LOW);
- digitalWrite(relayClosePin, LOW);
- gateOpen = false;
- gateClose = false;
- server.send(200, "text/plain", "Núdzové zastavenie aktivované");
- } else {
- if (wasOpening) {
- digitalWrite(relayOpenPin, HIGH);
- while (digitalRead(sensorPin) == LOW) {
- delay(100); // Čakanie na otvorenie brány
- }
- digitalWrite(relayOpenPin, LOW);
- gateOpen = true;
- gateClose = false;
- server.send(200, "text/plain", "Pokračovanie otvárania brány");
- } else if (wasClosing) {
- digitalWrite(relayClosePin, HIGH);
- while (digitalRead(sensorPin) == HIGH) {
- delay(100); // Čakanie na zatvorenie brány
- }
- digitalWrite(relayClosePin, LOW);
- gateClose = true;
- gateOpen = false;
- server.send(200, "text/plain", "Pokračovanie zatvárania brány");
- } else {
- server.send(200, "text/plain", "Núdzové zastavenie deaktivované");
- }
- }
- });
- server.begin();
- }
- void loop() {
- server.handleClient();
- // Čítanie stavu tlačidla
- if (digitalRead(buttonPin) == LOW) {
- delay(50); // Debounce delay
- if (digitalRead(buttonPin) == LOW) {
- emergencyStop = !emergencyStop;
- if (emergencyStop) {
- digitalWrite(relayOpenPin, LOW);
- digitalWrite(relayClosePin, LOW);
- gateOpen = false;
- gateClose = false;
- Serial.println("Núdzové zastavenie aktivované tlačidlom");
- } else {
- if (wasOpening) {
- digitalWrite(relayOpenPin, HIGH);
- while (digitalRead(sensorPin) == LOW) {
- delay(100); // Čakanie na otvorenie brány
- }
- digitalWrite(relayOpenPin, LOW);
- gateOpen = true;
- gateClose = false;
- Serial.println("Pokračovanie otvárania brány");
- } else if (wasClosing) {
- digitalWrite(relayClosePin, HIGH);
- while (digitalRead(sensorPin) == HIGH) {
- delay(100); // Čakanie na zatvorenie brány
- }
- digitalWrite(relayClosePin, LOW);
- gateClose = true;
- gateOpen = false;
- Serial.println("Pokračovanie zatvárania brány");
- } else {
- Serial.println("Núdzové zastavenie deaktivované tlačidlom");
- }
- }
- delay(1000); // Debounce delay
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement