Skip to content

lab 1 #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
16 changes: 16 additions & 0 deletions mc_labs/Bala_lab1/Pins.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef PINS_H
#define PINS_H

#define LED1_PIN 2
#define LED2_PIN 14
#define LED3_PIN 12
#define BTN_PIN 13

void pinsSetup(){
pinMode(LED1_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
pinMode(LED3_PIN, OUTPUT);
pinMode(BTN_PIN, INPUT);
}

#endif
134 changes: 134 additions & 0 deletions mc_labs/Bala_lab1/Websrv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#ifndef WEBSRV_H
#define WEBSRV_H

#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>

extern uint32_t ledDelay;

//const char* ssid = "Mi Phone2";
//const char* password = "12345678";

const char* ssid = "__MY__VAULT__";
const char* password = "79715218";

const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML>
<html>

<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-family: Arial;
text-align: center;
margin: 0px auto;
padding-top: 30px;
}

.button {
padding: 10px 20px;
font-size: 24px;
text-align: center;
outline: none;
color: #fff;
background-color: #2f4468;
border: none;
border-radius: 5px;
box-shadow: 0 6px #999;
cursor: pointer;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}

.button:hover {
background-color: #1f2e45
}

.button:active {
background-color: #1f2e45;
box-shadow: 0 4px #666;
transform: translateY(2px);
}

@media screen and (max-width: 480px) {
.button {
padding: 15px 100px 15px 10px;
font-size: 10px;
}

h1 {
font-size: 24px;
padding-top: 20px;
}
}
</style>
</head>

<body>
<h1>ESP Pushbutton Web Server</h1>
<button class="button" onclick="changeDelay();">Change Delay</button>
<p id="delayValue">Current Delay: 1000 ms</p>

<script>
function changeDelay() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "/change_delay", true);
xhr.send();
}

function getDelayValue() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "/get_delay", true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
document.getElementById('delayValue').innerText = "Current Delay: " + xhr.responseText + " ms";
}
};
xhr.send();
}

setInterval(getDelayValue, 1000);
</script>
</body>

</html>
)rawliteral";

AsyncWebServer server(80);

void changeDelay();

void setupWebServer() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send_P(200, "text/html", index_html);
});

server.on("/change_delay", HTTP_GET, [](AsyncWebServerRequest *request) {
changeDelay();
request->send(200, "text/plain", String(ledDelay).c_str());
});

server.on("/get_delay", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", String(ledDelay).c_str());
});

server.begin();
}

#endif
90 changes: 90 additions & 0 deletions mc_labs/Bala_lab1/lab1_oop.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "Pins.h"
#include "Websrv.h"
#include <ESPAsyncWebServer.h>


uint8_t lastButtonState = 1;

uint32_t lastDebounceTime = 0;
const uint32_t debounceDelay = 50;

uint32_t lastLedToggle = 0;
uint32_t ledDelay = 1000;
uint8_t ledState = 0;


void setup() {
pinsSetup();
setupWebServer();
}

void loop() {
handleButton();
handleLeds();
}

void handleButton() {
uint32_t tmp = millis();
int btnVal = digitalRead(BTN_PIN);
bool dbtn=0;
if (btnVal != lastButtonState) {
Serial.println(btnVal);
lastDebounceTime = millis();
Serial.println(lastDebounceTime);
dbtn=true;

}

if (dbtn && ((tmp - lastDebounceTime) > debounceDelay)) {
dbtn=false;
tmp = 0;
lastDebounceTime = 0;
Serial.println("deb time past");
if (btnVal == LOW && lastButtonState == HIGH) {
Serial.println("btn pressed!");
changeDelay();
}
lastButtonState = btnVal;
}
}


void handleLeds() {
unsigned long currentMillis = millis();

if (currentMillis - lastLedToggle >= ledDelay) {
Serial.println("leds");
lastLedToggle = currentMillis;

if (ledState == 0) {
Serial.println("led1");
digitalWrite(LED1_PIN, HIGH);
digitalWrite(LED3_PIN, LOW);
} else if (ledState == 1) {
Serial.println("led2");
digitalWrite(LED1_PIN, LOW);
digitalWrite(LED2_PIN, HIGH);
} else if (ledState == 2) {
Serial.println("led3");
digitalWrite(LED2_PIN, LOW);
digitalWrite(LED3_PIN, HIGH);
}

ledState++;
if (ledState > 2){
ledState = 0;
Serial.println("led > 2");
}
}
}

void changeDelay() {
if (ledDelay > 100) {
Serial.println("led change");
ledDelay -= 100;
} else {
ledDelay = 1000;
}
Serial.print("New ledDelay: ");
Serial.println(ledDelay);
}
5 changes: 5 additions & 0 deletions mc_labs/Bala_lab2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
10 changes: 10 additions & 0 deletions mc_labs/Bala_lab2/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}
Loading
Loading