From 14191302944244caabd56dc1382450646365125b Mon Sep 17 00:00:00 2001 From: HenryV4 <150185495+HenryV4@users.noreply.github.com> Date: Sat, 15 Mar 2025 14:44:23 +0200 Subject: [PATCH] Zaplatynsky_Igor_LAB1 --- mc_labs/mc_lab_01/Zaplatynsky_Igor_lab1/btn.h | 25 +++++++ .../Zaplatynsky_Igor_lab1/indexHTML.h | 28 +++++++ .../Zaplatynsky_Igor_lab1/mc-lab1.ino | 73 +++++++++++++++++++ .../mc_lab_01/Zaplatynsky_Igor_lab1/wifi.h | 30 ++++++++ 4 files changed, 156 insertions(+) create mode 100644 mc_labs/mc_lab_01/Zaplatynsky_Igor_lab1/btn.h create mode 100644 mc_labs/mc_lab_01/Zaplatynsky_Igor_lab1/indexHTML.h create mode 100644 mc_labs/mc_lab_01/Zaplatynsky_Igor_lab1/mc-lab1.ino create mode 100644 mc_labs/mc_lab_01/Zaplatynsky_Igor_lab1/wifi.h diff --git a/mc_labs/mc_lab_01/Zaplatynsky_Igor_lab1/btn.h b/mc_labs/mc_lab_01/Zaplatynsky_Igor_lab1/btn.h new file mode 100644 index 0000000..4b17c93 --- /dev/null +++ b/mc_labs/mc_lab_01/Zaplatynsky_Igor_lab1/btn.h @@ -0,0 +1,25 @@ +#ifndef BTN_H +#define BTN_H + +#define BUTTON_PIN 12 +const int doubleClickTime = 500; + +bool detectDoubleClick() { + static unsigned long firstClickTime = 0; + static bool buttonReleased = true; + + if (digitalRead(BUTTON_PIN) == LOW && buttonReleased) { + buttonReleased = false; + if (millis() - firstClickTime < doubleClickTime) { + firstClickTime = 0; + return true; + } else { + firstClickTime = millis(); + } + } else if (digitalRead(BUTTON_PIN) == HIGH) { + buttonReleased = true; + } + return false; +} + +#endif diff --git a/mc_labs/mc_lab_01/Zaplatynsky_Igor_lab1/indexHTML.h b/mc_labs/mc_lab_01/Zaplatynsky_Igor_lab1/indexHTML.h new file mode 100644 index 0000000..b593fc6 --- /dev/null +++ b/mc_labs/mc_lab_01/Zaplatynsky_Igor_lab1/indexHTML.h @@ -0,0 +1,28 @@ +#ifndef INDEX_HTML_H +#define INDEX_HTML_H + +const char htmlPage[] PROGMEM = R"rawliteral( + + +

LED Control

+ +

Status: Checking...

+ + + +)rawliteral"; + +#endif diff --git a/mc_labs/mc_lab_01/Zaplatynsky_Igor_lab1/mc-lab1.ino b/mc_labs/mc_lab_01/Zaplatynsky_Igor_lab1/mc-lab1.ino new file mode 100644 index 0000000..b71803c --- /dev/null +++ b/mc_labs/mc_lab_01/Zaplatynsky_Igor_lab1/mc-lab1.ino @@ -0,0 +1,73 @@ +#include +#include +#include "btn.h" +#include "wifi.h" +#include "indexHTML.h" + +#define LED1 14 // D5 (Red LED) +#define LED2 2 // D4 (Yellow LED) +#define LED3 13 // D7 (Green LED) + +ESP8266WebServer server(80); +bool running = false; +int lastLED = 0; + +void setup() { + Serial.begin(115200); + + pinMode(BUTTON_PIN, INPUT_PULLUP); + pinMode(LED1, OUTPUT); + pinMode(LED2, OUTPUT); + pinMode(LED3, OUTPUT); + + turnOffLEDs(); + connectToWiFi(); + + // Define Web Routes + server.on("/toggle", HTTP_GET, []() { + running = !running; + server.send(200, "text/plain", running ? "ON" : "OFF"); + }); + + server.on("/status", HTTP_GET, []() { + server.send(200, "text/plain", running ? "ON" : "OFF"); + }); + + server.on("/", HTTP_GET, []() { + server.send(200, "text/html", htmlPage); + }); + + server.begin(); +} + +void loop() { + server.handleClient(); + + if (detectDoubleClick()) { + running = !running; + } + + if (running) { + runLEDSequence(); + } +} + +void runLEDSequence() { + static unsigned long lastMillis = 0; + const int interval = 500; + int ledPins[] = {LED1, LED3, LED2}; + + if (millis() - lastMillis > interval) { + lastMillis = millis(); + digitalWrite(ledPins[lastLED], LOW); + lastLED = (lastLED + 1) % 3; + digitalWrite(ledPins[lastLED], HIGH); + } +} + +void turnOffLEDs() { + int ledPins[] = {LED1, LED3, LED2}; + for (int i = 0; i < 3; i++) { + digitalWrite(ledPins[i], LOW); + } +} diff --git a/mc_labs/mc_lab_01/Zaplatynsky_Igor_lab1/wifi.h b/mc_labs/mc_lab_01/Zaplatynsky_Igor_lab1/wifi.h new file mode 100644 index 0000000..447daca --- /dev/null +++ b/mc_labs/mc_lab_01/Zaplatynsky_Igor_lab1/wifi.h @@ -0,0 +1,30 @@ +#ifndef WIFI_H +#define WIFI_H + +const char* ssid = "Kria"; +const char* password = "Igorko)))"; + +void connectToWiFi() { + Serial.print("Connecting to WiFi: "); + Serial.println(ssid); + + WiFi.begin(ssid, password); + unsigned long startAttemptTime = millis(); + + while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < 15000) { + delay(1000); + Serial.print("."); + } + + Serial.println(); + + if (WiFi.status() != WL_CONNECTED) { + Serial.println("Failed to connect to WiFi."); + } else { + Serial.println("WiFi connected!"); + Serial.print("Access the site at: http://"); + Serial.println(WiFi.localIP()); + } +} + +#endif