From be41b58be7ff9f167401b759363aa35b74c5cd85 Mon Sep 17 00:00:00 2001 From: shubak123 Date: Thu, 6 Mar 2025 23:47:51 +0200 Subject: [PATCH 1/2] Create lab-one.ino --- lab-one.ino | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 lab-one.ino diff --git a/lab-one.ino b/lab-one.ino new file mode 100644 index 0000000..5e64231 --- /dev/null +++ b/lab-one.ino @@ -0,0 +1,157 @@ +#include +#include +#include +#define LEDRED 16 +#define LEDYELLOW 4 +#define LEDGREEN 5 +#define BUTTONE 14 +#define LEDS_NUM 6 + +WebServer server(80); + +bool web_click_block = false; +uint16_t countclick = 0; +bool last_buttone_state = HIGH; +uint32_t last_buttone_press = 0; +const uint32_t debounce_dalay = 50; +// qwiq action +bool buttone_state = HIGH; +uint32_t last_debounce_time = 0; +const uint32_t double_click_dalay = 300; +bool waiting_second_click = false; + +const uint32_t block_timeout = 5000; +uint32_t last_action_time = 0; + +enum lesStates +{ + TWO_FIRST_LEDS = 0b011, + FIRST_LED = 0b001, + SECOND_LED = 0b010, + THIRD_LED = 0b100, + TWO_SECOND_LEDS = 0b110, + TWO_THIRD_LEDS = 0b101 +}; + +// зроби форк і закинь в репрозиторій на мс парт 1 +byte leds_states[LEDS_NUM] = + { + TWO_FIRST_LEDS, + FIRST_LED, + TWO_SECOND_LEDS, + SECOND_LED, + TWO_THIRD_LEDS, + THIRD_LED +}; + + +void update_led_state() +{ + byte current_led_state = leds_states[(countclick / 2) % sizeof(leds_states)]; + digitalWrite(LEDRED, (current_led_state & 0b001) ? HIGH : LOW); + digitalWrite(LEDYELLOW, (current_led_state & 0b010) ? HIGH : LOW); + digitalWrite(LEDGREEN, (current_led_state & 0b100) ? HIGH : LOW); +} + + + +void handle_web_click() +{ + if (!web_click_block) + { + countclick += 2; + update_led_state(); + } +} + +// http://192.168.4.1 +void handleroot() +{ + String html = R"rawliteral( + + + + + + ESP32 LED Control + + + +

Click

+ + + + )rawliteral"; + + server.send(200, "text/html", html); +} + +void setup() +{ + Serial.begin(115200); + + pinMode(BUTTONE, INPUT); + pinMode(LEDRED, OUTPUT); + pinMode(LEDYELLOW, OUTPUT); + pinMode(LEDGREEN, OUTPUT); + + + WiFi.softAP("XDlab1LoL", "shubak1982"); + server.begin(); + server.on("/", HTTP_GET, handleroot); + server.on("/click", HTTP_GET, handle_web_click); + +} + +void handleButton() +{ + bool reading = digitalRead(BUTTONE); + if (reading != last_buttone_state) + { + last_debounce_time = millis(); + } + if ((millis() - last_debounce_time) > debounce_dalay) + { + if (reading != buttone_state) + { + buttone_state = reading; + if (buttone_state == LOW) + { + uint32_t curraent_milis = millis(); + if (waiting_second_click && (curraent_milis - last_buttone_press < double_click_dalay)) + { + countclick += 2; + web_click_block = true; + waiting_second_click = false; + update_led_state(); + } + else + { + waiting_second_click = true; + last_buttone_press = curraent_milis; + } + last_action_time = millis(); + } + } + } + last_buttone_state = reading; + if (web_click_block && (millis() - last_action_time > block_timeout)) + { + web_click_block = false; + } +} + +void loop() +{ + server.handleClient(); + handleButton(); +} From 733817ba0db2a34a42bdf928cfa909823ec7fc70 Mon Sep 17 00:00:00 2001 From: shubak123 Date: Thu, 3 Apr 2025 13:50:15 +0300 Subject: [PATCH 2/2] Add files via upload --- lab2/esp322lab/esp322lab.ino | 313 +++++++++++++++++++++++++++++++ lab2/esp8266lab2/esp8266lab2.ino | 179 ++++++++++++++++++ 2 files changed, 492 insertions(+) create mode 100644 lab2/esp322lab/esp322lab.ino create mode 100644 lab2/esp8266lab2/esp8266lab2.ino diff --git a/lab2/esp322lab/esp322lab.ino b/lab2/esp322lab/esp322lab.ino new file mode 100644 index 0000000..c44816c --- /dev/null +++ b/lab2/esp322lab/esp322lab.ino @@ -0,0 +1,313 @@ +#include +#include + +#define TX_PIN 17 +#define RX_PIN 16 +#define BUTTON 19 +#define LEDRED 18 +#define LEDYELLOW 5 +#define LEDGREEN 4 + +#define LEDS_NUM 6 +char Lcommand ='L'; +String algos = "algo"; +bool ledgo = true; +uint16_t countclick = 0; +uint16_t count_tab = 2; +int length = 3; +int threshold = -1; +unsigned long buttonPressMillis = 0; +const long buttonDelay = 1000; +bool is_algo_1 = false; +bool is_algo_2 = false; +bool isButtonPressedOnce = false; +bool currentButtonState = HIGH; +bool lastButtonState = HIGH; +byte byte_mask_led1 =0b001; +byte byte_mask_led2=0b010; +byte byte_mask_led3 =0b100; + + +enum ledStates { + TWO_FIRST_LEDS = 0b011, + FIRST_LED = 0b001, + SECOND_LED = 0b010, + THIRD_LED = 0b100, + TWO_SECOND_LEDS = 0b110, + TWO_THIRD_LEDS = 0b101 +}; + +byte leds_states[LEDS_NUM] = { + TWO_FIRST_LEDS, + FIRST_LED, + TWO_SECOND_LEDS, + SECOND_LED, + TWO_THIRD_LEDS, + THIRD_LED +}; + + +const char* ssid = "ESP32_AP"; +const char* password = "12345678"; +String ledState = "000"; + +WebServer server(80); + +void setup() { + + Serial.begin(115200); + Serial2.begin(28800, SERIAL_8O1, RX_PIN, TX_PIN); + pinMode(BUTTON, INPUT_PULLUP); + pinMode(LEDRED, OUTPUT); + pinMode(LEDYELLOW, OUTPUT); + pinMode(LEDGREEN, OUTPUT); + Serial.println("ESP32 is ready!"); + + WiFi.softAP(ssid, password); + Serial.println("Access Point started"); + Serial.print("IP Address: "); + Serial.println(WiFi.softAPIP()); + + + server.on("/", HTTP_GET, []() { + String html = R"rawliteral( + + + + + ESP32 LED Control + + + +

ESP32 LED Control

+

LED State: ---

+ +
+
+
+ + + + + + +)rawliteral"; + server.send(200, "text/html", html); + }); + + server.on("/state", HTTP_GET, []() { + server.send(200, "text/plain", ledState); + }); + server.on("/click", HTTP_GET, handle_web_click_algo1); + server.on("/click1", HTTP_GET, handle_web_click_algo2); + + server.begin(); +} + +void handle_web_click_algo1() { + is_algo_1 = true; + is_algo_2 = false; +} + +void handle_web_click_algo2() { + is_algo_1 = false; + is_algo_2 = true; + Serial2.println(algos); + Serial.println(algos); +} + +void serv() { + currentButtonState = digitalRead(BUTTON); + + + if ((currentButtonState == LOW && lastButtonState == HIGH) || is_algo_1) { + byte current_led_state = leds_states[(countclick / count_tab) % LEDS_NUM]; + ledState = String((current_led_state & byte_mask_led1) ? "1" : "0") + + String((current_led_state & byte_mask_led2) ? "1" : "0") + + String((current_led_state & byte_mask_led3) ? "1" : "0"); + countclick += count_tab; + + + isButtonPressedOnce = true; + } + + if (currentButtonState == HIGH && lastButtonState == LOW) { + isButtonPressedOnce = false; + } + + + if (Serial2.available() > 0 || is_algo_2) { + ledState = String(digitalRead(LEDRED)) + String(digitalRead(LEDYELLOW)) + String(digitalRead(LEDGREEN)); + } + + lastButtonState = currentButtonState; +} + + + +void handle_buttone() { + unsigned long currentMillis = millis(); + + if ((digitalRead(BUTTON) == LOW || is_algo_1) && (currentMillis - buttonPressMillis) >= buttonDelay){ + byte asciiValue = Lcommand; + Serial2.println(asciiValue, OCT); + Serial.println("Sent command: L"); + ledgo = false; + + + buttonPressMillis = currentMillis; + + algorithm(); + is_algo_1 = true; + is_algo_2=false; + } +} + +void handle_comand() { + if (Serial2.available() > 0) { + is_algo_2= true; + is_algo_1 = false; + String command = Serial2.readStringUntil('\n'); + command.trim(); + + + long octalValue = strtol(command.c_str(), NULL, 8); + char outputChar = (char)octalValue; + + + + + if (outputChar == Lcommand) { + ledgo = true; + } + + if (command.length() == length && command.indexOf('1') != threshold || command.indexOf('0') != threshold) { + + + if (command[0] == '0' || command[0] == '1') { + digitalWrite(LEDRED, (command[0] == '1') ? HIGH : LOW); + } + if (command[1] == '0' || command[1] == '1') { + digitalWrite(LEDYELLOW, (command[1] == '1') ? HIGH : LOW); + } + if (command[2] == '0' || command[2] == '1') { + digitalWrite(LEDGREEN, (command[2] == '1') ? HIGH : LOW); + } + + Serial.print("LEDs updated: "); + Serial.print(command[0]); + Serial.print(command[1]); + Serial.println(command[2]); + } + } +} + +void algorithm() { + + byte current_led_state = leds_states[(countclick / count_tab) % LEDS_NUM]; + + Serial2.print((current_led_state & byte_mask_led1) ? "1" : "0"); + Serial2.print((current_led_state & byte_mask_led2) ? "1" : "0"); + Serial2.println((current_led_state & byte_mask_led3) ? "1" : "0"); + + + Serial.print("Sent LED state: "); + Serial.print((current_led_state & byte_mask_led1) ? "1" : "0"); + Serial.print((current_led_state & byte_mask_led2) ? "1" : "0"); + Serial.println((current_led_state & byte_mask_led3) ? "1" : "0"); + +} + +void update_led_state() { + ledgo = true; + if (Serial2.available() > 0) { + + String command = Serial2.readStringUntil('\n'); + command.trim(); + + + if (command.length() != length) { + Serial.println("Invalid command length! Expected 3 characters."); + + } + for (int i = 0; i < length; i++) { + if (command[i] != '0' && command[i] != '1') { + Serial.println("Invalid command format! Expected only '0' or '1'."); + + } + } + + + digitalWrite(LEDRED, (command[0] == '1') ? HIGH : LOW); + digitalWrite(LEDYELLOW, (command[1] == '1') ? HIGH : LOW); + digitalWrite(LEDGREEN, (command[2] == '1') ? HIGH : LOW); + + Serial.print("LEDs updated: "); + Serial.print(command[0]); + Serial.print(command[1]); + Serial.println(command[2]); + } +} + +void loop() { + server.handleClient(); + handle_comand(); + handle_buttone(); + + if (ledgo) { + + update_led_state(); + + } else { + digitalWrite(LEDRED, LOW); + digitalWrite(LEDYELLOW, LOW); + digitalWrite(LEDGREEN, LOW); + } + serv(); + is_algo_1=false; +} \ No newline at end of file diff --git a/lab2/esp8266lab2/esp8266lab2.ino b/lab2/esp8266lab2/esp8266lab2.ino new file mode 100644 index 0000000..7ede874 --- /dev/null +++ b/lab2/esp8266lab2/esp8266lab2.ino @@ -0,0 +1,179 @@ +#include + + +#define RX_PIN 5 // RX (D1) +#define TX_PIN 4 // TX (D2) + +#define LEDRED 14 // D5 +#define LEDYELLOW 12 // D6 +#define LEDGREEN 13 // D7 +#define LEDS_NUM 6 +#define BUTTON 2 +int threshold = -1; +int length = 3; +bool ledgo = true; +uint16_t countclick = 0; +uint16_t count_tab =2; +char Lcommand ='L'; +unsigned long buttonPressMillis = 0; +const long buttonDelay = 1000; +SoftwareSerial espSerial(RX_PIN, TX_PIN); +String algos ="algo"; +byte byte_mask_led1 =0b001; +byte byte_mask_led2=0b010; +byte byte_mask_led3 =0b100; +void setup() { + Serial.begin(115200); + espSerial.begin(28800,SWSERIAL_8O1); + + pinMode(LEDRED, OUTPUT); + pinMode(LEDYELLOW, OUTPUT); + pinMode(LEDGREEN, OUTPUT); + + digitalWrite(LEDRED, LOW); + digitalWrite(LEDYELLOW, LOW); + digitalWrite(LEDGREEN, LOW); + + Serial.println("ESP8266 готовий!"); +} + +enum ledStates { + TWO_FIRST_LEDS = 0b011, + FIRST_LED = 0b001, + SECOND_LED = 0b010, + THIRD_LED = 0b100, + TWO_SECOND_LEDS = 0b110, + TWO_THIRD_LEDS = 0b101 +}; + +byte leds_states[LEDS_NUM] = { + TWO_FIRST_LEDS, + FIRST_LED, + TWO_SECOND_LEDS, + SECOND_LED, + TWO_THIRD_LEDS, + THIRD_LED +}; + +void update_led_state() { + ledgo = true; + if (espSerial.available() > 0) { + + String command = espSerial.readStringUntil('\n'); + command.trim(); + + + if (command.length() != length) { + Serial.println("Invalid command length! Expected 3 characters."); + + } + + for (int i = 0; i < length; i++) { + if (command[i] != '0' && command[i] != '1') { + Serial.println("Invalid command format! Expected only '0' or '1'."); + + } + } + + digitalWrite(LEDRED, (command[0] == '1') ? HIGH : LOW); + digitalWrite(LEDYELLOW, (command[1] == '1') ? HIGH : LOW); + digitalWrite(LEDGREEN, (command[2] == '1') ? HIGH : LOW); + + + Serial.print("LEDs updated: "); + Serial.print(command[0]); + Serial.print(command[1]); + Serial.println(command[2]); + } +} + +void handle_buttone() { + unsigned long currentMillis = millis(); + + if (digitalRead(BUTTON) == LOW && (currentMillis - buttonPressMillis) >= buttonDelay) { + + byte asciiValue = Lcommand; + espSerial.println(asciiValue, OCT); + Serial.println("Sent command: L"); + ledgo = false; + + buttonPressMillis = currentMillis; + + + algorithm(); + } +} + + +void handle_comand() { + if (espSerial.available()) { + String command = espSerial.readStringUntil('\n'); + command.trim(); + + Serial.print("Отримано: "); + + long octalValue = strtol(command.c_str(), NULL, 8); + char outputChar = (char)octalValue; + Serial.println(outputChar); + + if (outputChar == Lcommand) { + ledgo = true; + } + if (command == algos) { + byte asciiValue = Lcommand; + espSerial.println(asciiValue, OCT); + Serial.println("Sent command: L"); + ledgo = false; + + algorithm(); + } + + if (command.length() == length && command.indexOf('1') != threshold || command.indexOf('0') != threshold) { + + + + if (command[0] == '0' || command[0] == '1') { + digitalWrite(LEDRED, (command[0] == '1') ? HIGH : LOW); + } + if (command[1] == '0' || command[1] == '1') { + digitalWrite(LEDYELLOW, (command[1] == '1') ? HIGH : LOW); + } + if (command[2] == '0' || command[2] == '1') { + digitalWrite(LEDGREEN, (command[2] == '1') ? HIGH : LOW); + } + + Serial.print("LEDs updated: "); + Serial.print(command[0]); + Serial.print(command[1]); + Serial.println(command[2]); + } + } +} + +void algorithm() { + + byte current_led_state = leds_states[(countclick / count_tab) % LEDS_NUM]; + espSerial.print((current_led_state & byte_mask_led1) ? "1" : "0"); + espSerial.print((current_led_state & byte_mask_led2) ? "1" : "0"); + espSerial.println((current_led_state & byte_mask_led3) ? "1" : "0"); + + Serial.print("Sent LED state: "); + Serial.print((current_led_state & byte_mask_led1) ? "1" : "0"); + Serial.print((current_led_state & byte_mask_led2) ? "1" : "0"); + Serial.println((current_led_state & byte_mask_led3) ? "1" : "0"); + countclick += count_tab; +} + +void loop() { + handle_comand(); + handle_buttone(); + if (ledgo) { + + update_led_state(); + + } else { + digitalWrite(LEDRED, LOW); + digitalWrite(LEDYELLOW, LOW); + digitalWrite(LEDGREEN, LOW); + } +}