Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions mc_labs/mc_lab_01/Zaplatynsky_Igor_lab1/btn.h
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions mc_labs/mc_lab_01/Zaplatynsky_Igor_lab1/indexHTML.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef INDEX_HTML_H
#define INDEX_HTML_H

const char htmlPage[] PROGMEM = R"rawliteral(
<html>
<body>
<h1>LED Control</h1>
<button ondblclick="toggle()">Double Click to Toggle LEDs</button>
<p>Status: <span id="status">Checking...</span></p>
<script>
function toggle() {
fetch('/toggle').then(res => res.text()).then(data => {
document.getElementById('status').innerText = data;
});
}
function updateStatus() {
fetch('/status').then(res => res.text()).then(data => {
document.getElementById('status').innerText = data;
setTimeout(updateStatus, 2000);
});
}
updateStatus();
</script>
</body>
</html>
)rawliteral";

#endif
73 changes: 73 additions & 0 deletions mc_labs/mc_lab_01/Zaplatynsky_Igor_lab1/mc-lab1.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#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);
}
}
30 changes: 30 additions & 0 deletions mc_labs/mc_lab_01/Zaplatynsky_Igor_lab1/wifi.h
Original file line number Diff line number Diff line change
@@ -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
Loading