diff --git a/mc_labs/mc_lab_01/Dubyk_Yura_Lab_01/Dubyk_Yura_Lab_01.ino b/mc_labs/mc_lab_01/Dubyk_Yura_Lab_01/Dubyk_Yura_Lab_01.ino index 451b102..9202568 100644 --- a/mc_labs/mc_lab_01/Dubyk_Yura_Lab_01/Dubyk_Yura_Lab_01.ino +++ b/mc_labs/mc_lab_01/Dubyk_Yura_Lab_01/Dubyk_Yura_Lab_01.ino @@ -2,7 +2,7 @@ #include #include -#define ESP_WIFI_MODE 2 // WIFI_STA // WIFI_AP //WIFI_AP_STA +#define ESP_WIFI_MODE 2 // WIFI_STA // WIFI_AP //WIFI_AP_STA #define DELAY_BETWEEN_BUTTONS 300 #define DEBOUNCE_DELAY 40 @@ -14,14 +14,13 @@ const uint8_t LED2 = LED_BUILTIN; const uint8_t LED3 = D6; const uint8_t btnGPIO = D7; -uint8_t ledsArray[] = { LED1, LED2, LED3 }; -uint8_t reverseLedsArray[] = { LED3, LED2, LED1 }; +uint8_t ledsArray[] = {LED1, LED2, LED3}; +uint8_t reverseLedsArray[] = {LED3, LED2, LED1}; uint8_t currentLedsArray[3]; uint8_t ledLen = sizeof(ledsArray) / sizeof(ledsArray[0]); uint32_t timestamp; uint32_t lastDebounceTime = 0; uint8_t currentLed = 0; -uint8_t prevLed; uint32_t buttonCounter = 0; uint32_t prevButtonCounter = 0; @@ -190,17 +189,21 @@ const char index_html[] PROGMEM = R"rawliteral( )rawliteral"; -void notFound(AsyncWebServerRequest *request) { +void notFound(AsyncWebServerRequest *request) +{ request->send(404, "text/plain", "Not found"); } -void initWiFi() { +void initWiFi() +{ - if (ESP_WIFI_MODE == 1) { + if (ESP_WIFI_MODE == 1) + { WiFi.mode(WIFI_STA); // Connect to Wi-Fi network with SSID and password WiFi.begin(ssid, password); - while (WiFi.status() != WL_CONNECTED) { + while (WiFi.status() != WL_CONNECTED) + { delay(1000); Serial.println("Connecting to WiFi.."); } @@ -210,7 +213,9 @@ void initWiFi() { Serial.print("RRSI: "); Serial.println(WiFi.RSSI()); - } else { // (ESP_WIFI_MODE == WIFI_AP) + } + else + { // (ESP_WIFI_MODE == WIFI_AP) WiFi.mode(WIFI_AP); Serial.println("Setting AP (Access Point)…"); // Remove the password parameter, if you want the AP (Access Point) to be open @@ -221,60 +226,65 @@ void initWiFi() { Serial.println(IP); } // Send web page to client - server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { - request->send_P(200, "text/html", index_html); - }); + server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) + { request->send_P(200, "text/html", index_html); }); - server.on("/on_alg1", HTTP_GET, [](AsyncWebServerRequest *request) { + server.on("/on_alg1", HTTP_GET, [](AsyncWebServerRequest *request) + { siteBtnPressed = true; - request->send(200, "text/plain", "ok"); - }); + request->send(200, "text/plain", "ok"); }); - server.on("/off_alg1", HTTP_GET, [](AsyncWebServerRequest *request) { + server.on("/off_alg1", HTTP_GET, [](AsyncWebServerRequest *request) + { siteBtnPressed = false; - request->send(200, "text/plain", "ok"); - }); + request->send(200, "text/plain", "ok"); }); - server.on("/status_led_1", HTTP_GET, [](AsyncWebServerRequest *request) { - request->send(200, "text/plain", String(digitalRead(LED1)).c_str()); - }); + server.on("/status_led_1", HTTP_GET, [](AsyncWebServerRequest *request) + { request->send(200, "text/plain", String(digitalRead(LED1)).c_str()); }); - server.on("/status_led_2", HTTP_GET, [](AsyncWebServerRequest *request) { - request->send(200, "text/plain", String(digitalRead(LED2)).c_str()); - }); + server.on("/status_led_2", HTTP_GET, [](AsyncWebServerRequest *request) + { request->send(200, "text/plain", String(digitalRead(LED2)).c_str()); }); - server.on("/status_led_3", HTTP_GET, [](AsyncWebServerRequest *request) { - request->send(200, "text/plain", String(digitalRead(LED3)).c_str()); - }); + server.on("/status_led_3", HTTP_GET, [](AsyncWebServerRequest *request) + { request->send(200, "text/plain", String(digitalRead(LED3)).c_str()); }); server.onNotFound(notFound); server.begin(); } -void pinsSetup() { +void pinsSetup() +{ pinMode(LED1, OUTPUT); pinMode(LED2, OUTPUT); pinMode(LED3, OUTPUT); pinMode(btnGPIO, INPUT); } -IRAM_ATTR void ISRbtnChange() { +IRAM_ATTR void ISRbtnChange() +{ buttonCounter++; } -void btnChange() { - if (buttonCounter > prevButtonCounter) { - if (millis() - lastDebounceTime >= DEBOUNCE_DELAY) { +void btnChange() +{ + if (buttonCounter > prevButtonCounter) + { + if (millis() - lastDebounceTime >= DEBOUNCE_DELAY) + { lastDebounceTime = millis(); prevButtonCounter = buttonCounter; presentState = digitalRead(btnGPIO); - if (presentState == LOW) { + if (presentState == LOW) + { btnPressed = true; - } else { + } + else + { btnPressed = false; } - if (buttonCounter > 10000) { + if (buttonCounter > 10000) + { prevButtonCounter = 0; buttonCounter = 0; } @@ -282,17 +292,22 @@ void btnChange() { } } -void chechSiteButton() { - if (siteBtnPressed) { +void chechSiteButton() +{ + if (siteBtnPressed) + { btnPressed = true; msgAboutButtonSended = true; - } else if (!siteBtnPressed && msgAboutButtonSended) { + } + else if (!siteBtnPressed && msgAboutButtonSended) + { btnPressed = false; msgAboutButtonSended = false; } } -void setup() { +void setup() +{ Serial.begin(115200); pinsSetup(); attachInterrupt(digitalPinToInterrupt(btnGPIO), ISRbtnChange, CHANGE); @@ -300,36 +315,44 @@ void setup() { initWiFi(); } -void choose_led_array() { - if (btnPressed) { - for (uint8_t i = 0; i < ledLen; i++) { +void choose_led_array() +{ + if (btnPressed) + { + for (uint8_t i = 0; i < ledLen; i++) + { currentLedsArray[i] = reverseLedsArray[i]; } - } else { - for (uint8_t i = 0; i < ledLen; i++) { + } + else + { + for (uint8_t i = 0; i < ledLen; i++) + { currentLedsArray[i] = ledsArray[i]; } } } -void do_algorithm() { +void do_algorithm() +{ choose_led_array(); - if (prevLed != 999) { - prevLed = currentLed; - } else prevLed = 0; digitalWrite(currentLedsArray[currentLed], HIGH); currentLed++; - if (currentLed >= ledLen) { + if (currentLed >= ledLen) + { currentLed = 0; } } -void loop() { +void loop() +{ chechSiteButton(); btnChange(); - if (millis() - timestamp >= DELAY_BETWEEN_BUTTONS) { + if (millis() - timestamp >= DELAY_BETWEEN_BUTTONS) + { timestamp = millis(); - for (int i = 0; i < ledLen; i++) { + for (int i = 0; i < ledLen; i++) + { digitalWrite(ledsArray[i], LOW); } do_algorithm(); diff --git a/mc_labs/mc_lab_02/Dubyk_Yura_Lab_02/Dubyk_Yura_Lab_02.ino b/mc_labs/mc_lab_02/Dubyk_Yura_Lab_02/Dubyk_Yura_Lab_02.ino new file mode 100644 index 0000000..5ffe0b7 --- /dev/null +++ b/mc_labs/mc_lab_02/Dubyk_Yura_Lab_02/Dubyk_Yura_Lab_02.ino @@ -0,0 +1,387 @@ +#include +#include +#include + +#define ESP_WIFI_MODE 2 // WIFI_STA // WIFI_AP //WIFI_AP_STA +#define DELAY_BETWEEN_BUTTONS 300 +#define DEBOUNCE_DELAY 40 + +const char *ssid = "WEMOS_ESP8266_Yura"; +const char *password = "IoT"; + +const uint8_t LED1 = D5; +const uint8_t LED2 = LED_BUILTIN; +const uint8_t LED3 = D6; +const uint8_t btnGPIO = D7; + +uint8_t ledsArray[] = {LED1, LED2, LED3}; +uint8_t reverseLedsArray[] = {LED3, LED2, LED1}; +uint8_t currentLedsArray[3]; +uint8_t ledLen = sizeof(ledsArray) / sizeof(ledsArray[0]); +uint32_t timestamp; +uint32_t lastDebounceTime = 0; +uint8_t currentLed = 0; +uint32_t buttonCounter = 0; +uint32_t prevButtonCounter = 0; + +bool presentState; +bool btnPressed = false; +bool siteBtnPressed = false; +bool msgAboutButtonSended = true; +bool algo2Started = false; +bool prevAlgo2State = false; + +// Create AsyncWebServer object on port 80 +AsyncWebServer server(80); + +const char index_html[] PROGMEM = R"rawliteral( + + + + + + + + + +

ESP Pushbutton Web Server

+ + + + +
+
+
+
+
+ + + + + +)rawliteral"; + +void notFound(AsyncWebServerRequest *request) +{ + request->send(404, "text/plain", "Not found"); +} + +void initWiFi() +{ + + if (ESP_WIFI_MODE == 1) + { + WiFi.mode(WIFI_STA); + // Connect to Wi-Fi network with SSID and password + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) + { + delay(1000); + Serial.println("Connecting to WiFi.."); + } + Serial.println(); + Serial.print("ESP IP Address: http://"); + Serial.println(WiFi.localIP()); + + Serial.print("RRSI: "); + Serial.println(WiFi.RSSI()); + } + else + { // (ESP_WIFI_MODE == WIFI_AP) + WiFi.mode(WIFI_AP); + Serial.println("Setting AP (Access Point)…"); + // Remove the password parameter, if you want the AP (Access Point) to be open + WiFi.softAP(ssid, NULL); + + IPAddress IP = WiFi.softAPIP(); + Serial.print("AP IP address: "); + Serial.println(IP); + } + // Send web page to client + server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) + { request->send_P(200, "text/html", index_html); }); + + server.on("/on_alg1", HTTP_GET, [](AsyncWebServerRequest *request) + { + siteBtnPressed = true; + request->send(200, "text/plain", "ok"); }); + + server.on("/off_alg1", HTTP_GET, [](AsyncWebServerRequest *request) + { + siteBtnPressed = false; + request->send(200, "text/plain", "ok"); }); + + server.on("/status_led_1", HTTP_GET, [](AsyncWebServerRequest *request) + { request->send(200, "text/plain", String(digitalRead(LED1)).c_str()); }); + + server.on("/status_led_2", HTTP_GET, [](AsyncWebServerRequest *request) + { request->send(200, "text/plain", String(digitalRead(LED2)).c_str()); }); + + server.on("/status_led_3", HTTP_GET, [](AsyncWebServerRequest *request) + { request->send(200, "text/plain", String(digitalRead(LED3)).c_str()); }); + + server.on("/algo2", HTTP_GET, [](AsyncWebServerRequest *request) + { + algo2Started = (!algo2Started) ? (true) : (false); + request->send(200, "text/plain", "ok"); }); + + server.onNotFound(notFound); + server.begin(); +} + +void pinsSetup() +{ + pinMode(LED1, OUTPUT); + pinMode(LED2, OUTPUT); + pinMode(LED3, OUTPUT); + pinMode(btnGPIO, INPUT); +} + +IRAM_ATTR void ISRbtnChange() +{ + buttonCounter++; +} + +void btnChange() +{ + if (buttonCounter > prevButtonCounter) + { + if (millis() - lastDebounceTime >= DEBOUNCE_DELAY) + { + lastDebounceTime = millis(); + prevButtonCounter = buttonCounter; + presentState = digitalRead(btnGPIO); + btnPressed = (presentState == LOW) ? true : false; + if (buttonCounter > 10000) + { + prevButtonCounter = 0; + buttonCounter = 0; + } + } + } +} + +void checkSiteButton() +{ + if (siteBtnPressed) + { + btnPressed = true; + msgAboutButtonSended = true; + } + else if (!siteBtnPressed && msgAboutButtonSended) + { + btnPressed = false; + msgAboutButtonSended = false; + } +} + +void setup() +{ + Serial.begin(115200); + pinsSetup(); + attachInterrupt(digitalPinToInterrupt(btnGPIO), ISRbtnChange, CHANGE); + timestamp = millis(); + initWiFi(); +} + +void choose_led_array() +{ + if (btnPressed) + { + for (uint8_t i = 0; i < ledLen; i++) + { + currentLedsArray[i] = reverseLedsArray[i]; + } + } + else + { + for (uint8_t i = 0; i < ledLen; i++) + { + currentLedsArray[i] = ledsArray[i]; + } + } +} + +void do_algorithm() +{ + choose_led_array(); + digitalWrite(currentLedsArray[currentLed], HIGH); + currentLed++; + if (currentLed >= ledLen) + { + currentLed = 0; + } +} + +void loop() +{ + if (Serial.available() > 0) + { + byte some = Serial.read(); + Serial.println(some); + if (some == 0xA1) + btnPressed = true; + if (some == '\n') + btnPressed = false; + } + + if (prevAlgo2State != algo2Started) + { + if (algo2Started) + Serial.write(0xC1); + else + Serial.write(0xD1); + prevAlgo2State = algo2Started; + } + + checkSiteButton(); + btnChange(); + + if (millis() - timestamp >= DELAY_BETWEEN_BUTTONS) + { + timestamp = millis(); + for (int i = 0; i < ledLen; i++) + { + digitalWrite(ledsArray[i], LOW); + } + do_algorithm(); + } +} \ No newline at end of file diff --git a/mc_labs/mc_lab_02/Dubyk_Yura_Lab_02/Lab_02_EPS32_2nd_board/Lab_02_EPS32_2nd_board.ino b/mc_labs/mc_lab_02/Dubyk_Yura_Lab_02/Lab_02_EPS32_2nd_board/Lab_02_EPS32_2nd_board.ino new file mode 100644 index 0000000..8776a61 --- /dev/null +++ b/mc_labs/mc_lab_02/Dubyk_Yura_Lab_02/Lab_02_EPS32_2nd_board/Lab_02_EPS32_2nd_board.ino @@ -0,0 +1,149 @@ +#include + +#define RX2 16 +#define TX2 17 +#define LED1 SCK +#define LED2 MISO +#define LED3 MOSI +#define btnGPIO1 SDA +#define btnGPIO2 A10 +#define DELAY_BETWEEN_LEDS 400 +#define DEBOUNCE_DELAY 80 +#define UART_DELAY 50 + +uint32_t timestamp; +uint32_t timestamp2 = 0; +uint32_t timestamp3 = 0; +uint32_t timestamp4 = 0; +uint32_t count = 0; +uint32_t buttonCounter = 0; +uint32_t prevCount = 0; +uint8_t ledsArray[] = {LED1, LED2, LED3}; +uint8_t ledLen = sizeof(ledsArray) / sizeof(ledsArray[0]); +uint8_t currentLed = 0; +uint32_t prevButtonCounter = 0; +uint8_t lastButtonState = LOW; +uint8_t buttonState; + +bool algo_active = true; +bool presentState; +bool btnPressed = false; +bool msgSended = false; + +void pinsSetup() +{ + pinMode(LED1, OUTPUT); + pinMode(LED2, OUTPUT); + pinMode(LED3, OUTPUT); + pinMode(btnGPIO1, INPUT); + pinMode(btnGPIO2, INPUT); +} + +IRAM_ATTR void ISRbtn1LOW() +{ + count++; +} + +IRAM_ATTR void ISRbtnChange() +{ + buttonCounter++; +} + +void btnChange() +{ + if (buttonCounter > prevButtonCounter) + { + if (millis() - timestamp3 >= DEBOUNCE_DELAY) + { + timestamp3 = millis(); + prevButtonCounter = buttonCounter; + presentState = digitalRead(btnGPIO2); + btnPressed = (presentState == LOW) ? true : false; + if (buttonCounter > 10000) + { + prevButtonCounter = 0; + buttonCounter = 0; + } + } + } +} + +void setup() +{ + Serial.begin(115200); + pinsSetup(); + attachInterrupt(digitalPinToInterrupt(btnGPIO2), ISRbtnChange, CHANGE); + timestamp = millis(); + Serial2.begin(115200, SERIAL_8N1, RX2, TX2); +} + +void do_algo() +{ + digitalWrite(ledsArray[currentLed], HIGH); + currentLed++; + if (currentLed >= ledLen) + { + currentLed = 0; + } +} + +void checkButton() +{ + int reading = digitalRead(btnGPIO1); + if (reading != lastButtonState) + { + timestamp2 = millis(); + } + if (millis() - timestamp2 >= DEBOUNCE_DELAY) + { + if (reading != buttonState) + { + buttonState = reading; + if (buttonState == HIGH) + { + algo_active = !algo_active; + } + } + } + lastButtonState = reading; +} + +void loop() +{ + checkButton(); + if (millis() - timestamp4 >= UART_DELAY) + { + timestamp4 = millis(); + if (btnPressed) + { + Serial2.write(0xA1); + msgSended = false; + } + else if (!btnPressed && !msgSended) + { + Serial2.write('\n'); + msgSended = true; + } + } + if (Serial2.available() > 0) + { + byte some = Serial2.read(); + if (some == 0xC1) + algo_active = false; + if (some == 0xD1) + algo_active = true; + } + btnChange(); + if (algo_active) + { + if (millis() - timestamp >= DELAY_BETWEEN_LEDS) + { + timestamp = millis(); + for (int i = 0; i < ledLen; i++) + { + digitalWrite(ledsArray[i], LOW); + } + do_algo(); + } + } +} diff --git a/mc_labs/mc_lab_02/Dubyk_Yura_Lab_02/Schematic_Lab2.png b/mc_labs/mc_lab_02/Dubyk_Yura_Lab_02/Schematic_Lab2.png new file mode 100644 index 0000000..6a4a41f Binary files /dev/null and b/mc_labs/mc_lab_02/Dubyk_Yura_Lab_02/Schematic_Lab2.png differ