diff --git a/mc_labs/Bala_lab1/Pins.h b/mc_labs/Bala_lab1/Pins.h new file mode 100644 index 0000000..97ee918 --- /dev/null +++ b/mc_labs/Bala_lab1/Pins.h @@ -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 \ No newline at end of file diff --git a/mc_labs/Bala_lab1/Websrv.h b/mc_labs/Bala_lab1/Websrv.h new file mode 100644 index 0000000..89bd40b --- /dev/null +++ b/mc_labs/Bala_lab1/Websrv.h @@ -0,0 +1,134 @@ +#ifndef WEBSRV_H +#define WEBSRV_H + +#include +#include +#include + +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( + + + + + + + + + +

ESP Pushbutton Web Server

+ +

Current Delay: 1000 ms

+ + + + + + )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 \ No newline at end of file diff --git a/mc_labs/Bala_lab1/lab1_oop.ino b/mc_labs/Bala_lab1/lab1_oop.ino new file mode 100644 index 0000000..e824f6a --- /dev/null +++ b/mc_labs/Bala_lab1/lab1_oop.ino @@ -0,0 +1,90 @@ +#include "Pins.h" +#include "Websrv.h" +#include + + +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); +} \ No newline at end of file diff --git a/mc_labs/Bala_lab2/.gitignore b/mc_labs/Bala_lab2/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/mc_labs/Bala_lab2/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/mc_labs/Bala_lab2/.vscode/extensions.json b/mc_labs/Bala_lab2/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/mc_labs/Bala_lab2/.vscode/extensions.json @@ -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" + ] +} diff --git a/mc_labs/Bala_lab2/data/index.html b/mc_labs/Bala_lab2/data/index.html new file mode 100644 index 0000000..81ae93a --- /dev/null +++ b/mc_labs/Bala_lab2/data/index.html @@ -0,0 +1,195 @@ + + + + + + + + + +

ESP Pushbutton Web Server

+
+ + +
+ +

Current Delay: 1000 ms

+
+
+
+
+
+ + + + + \ No newline at end of file diff --git a/mc_labs/Bala_lab2/include/README b/mc_labs/Bala_lab2/include/README new file mode 100644 index 0000000..49819c0 --- /dev/null +++ b/mc_labs/Bala_lab2/include/README @@ -0,0 +1,37 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the convention is to give header files names that end with `.h'. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/mc_labs/Bala_lab2/lib/README b/mc_labs/Bala_lab2/lib/README new file mode 100644 index 0000000..9379397 --- /dev/null +++ b/mc_labs/Bala_lab2/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into the executable file. + +The source code of each library should be placed in a separate directory +("lib/your_library_name/[Code]"). + +For example, see the structure of the following example libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +Example contents of `src/main.c` using Foo and Bar: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +The PlatformIO Library Dependency Finder will find automatically dependent +libraries by scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/mc_labs/Bala_lab2/platformio.ini b/mc_labs/Bala_lab2/platformio.ini new file mode 100644 index 0000000..dd18654 --- /dev/null +++ b/mc_labs/Bala_lab2/platformio.ini @@ -0,0 +1,21 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:d1_mini_lite] +platform = espressif8266 +board = d1_mini_lite +framework = arduino +monitor_speed = 115200 +board_build.filesystem = littlefs +lib_extra_dirs = ~/Documents/Arduino/libraries +lib_deps = + ESPAsyncWebServer + me-no-dev/ESPAsyncTCP@^2.0.0 + plerup/EspSoftwareSerial@^8.2.0 diff --git a/mc_labs/Bala_lab2/src/Pins.h b/mc_labs/Bala_lab2/src/Pins.h new file mode 100644 index 0000000..747c846 --- /dev/null +++ b/mc_labs/Bala_lab2/src/Pins.h @@ -0,0 +1,18 @@ +#ifndef PINS_H +#define PINS_H + +#define LED1_PIN 2 +#define LED2_PIN 14 +#define LED3_PIN 12 +#define BTN_PIN 16 +#define TX 13 +#define RX 15 + +void setupPins(){ + pinMode(LED1_PIN, OUTPUT); + pinMode(LED2_PIN, OUTPUT); + pinMode(LED3_PIN, OUTPUT); + pinMode(BTN_PIN, INPUT); +} + +#endif \ No newline at end of file diff --git a/mc_labs/Bala_lab2/src/Websrv.h b/mc_labs/Bala_lab2/src/Websrv.h new file mode 100644 index 0000000..c7a2c79 --- /dev/null +++ b/mc_labs/Bala_lab2/src/Websrv.h @@ -0,0 +1,68 @@ +#ifndef WEBSRV_H +#define WEBSRV_H + +#include +#include +#include +#include +#include "Pins.h" + +extern uint32_t ledDelay; + +const char* ssid = "Mi Phone2"; +const char* password = "12345678"; + +AsyncWebServer server(80); + +void changeDelay(); +void sendCOM(); +extern void handleButton(); + +void handleLEDStatus(AsyncWebServerRequest *request, int pin) { + request->send(200, "text/plain", String(digitalRead(pin)).c_str()); +} + +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()); + + if (!LittleFS.begin()) { + Serial.println("LittleFS Mount Failed"); + return; + } + + server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { + request->send(LittleFS, "/index.html", "text/html"); + }); + + server.on("/change_delay", HTTP_GET, [](AsyncWebServerRequest *request) { + changeDelay(); + request->send(200, "text/plain", String(ledDelay).c_str()); + }); + + server.on("/send_com", HTTP_GET, [](AsyncWebServerRequest *request) { + sendCOM(); + 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.on("/status_led_1", HTTP_GET, [](AsyncWebServerRequest *request) { handleLEDStatus(request, LED1_PIN); }); + server.on("/status_led_2", HTTP_GET, [](AsyncWebServerRequest *request) { handleLEDStatus(request, LED2_PIN); }); + server.on("/status_led_3", HTTP_GET, [](AsyncWebServerRequest *request) { handleLEDStatus(request, LED3_PIN); }); + + server.begin(); +} + +#endif \ No newline at end of file diff --git a/mc_labs/Bala_lab2/src/lab2.cpp b/mc_labs/Bala_lab2/src/lab2.cpp new file mode 100644 index 0000000..eeda035 --- /dev/null +++ b/mc_labs/Bala_lab2/src/lab2.cpp @@ -0,0 +1,140 @@ +#include +#include +#include "Pins.h" +#include "Websrv.h" + +void handleLeds(); +void handleButton(); +void changeDelay(); +void sendCOM(); +void checkSerial(); +void changeLed(uint8_t pin1, uint8_t pin2); + +uint8_t lastButtonState = 1; +uint32_t lastDebounceTime = 0; +const uint32_t debounceDelay = 50; + +enum LedState +{ + LED1_ON, + LED2_ON, + LED3_ON +}; + +LedState ledState = LED1_ON; + +uint32_t lastLedToggle = 0; +uint32_t ledDelay = 1000; + +SoftwareSerial mySerial(TX, RX, SWSERIAL_7E1); + +void setup() +{ + mySerial.begin(57600); + Serial.begin(115200); + setupPins(); + setupWebServer(); +} + +void loop() +{ + handleButton(); + handleLeds(); + checkSerial(); +} + +void handleButton() +{ + uint32_t tmp = millis(); + int btnVal = digitalRead(BTN_PIN); + bool dbtn = 0; + if (btnVal != lastButtonState) + { + lastDebounceTime = millis(); + dbtn = true; + } + + if (dbtn && ((tmp - lastDebounceTime) > debounceDelay)) + { + dbtn = false; + tmp = 0; + lastDebounceTime = 0; + if (btnVal == LOW && lastButtonState == HIGH) + { + changeDelay(); + } + lastButtonState = btnVal; + } +} + +void sendCOM() +{ + mySerial.write('A'); + Serial.println("Sent: A"); +} + +void checkSerial() +{ + if (mySerial.available()) + { + uint8_t receivedChar = mySerial.read(); + Serial.print("Received: "); + Serial.println((char)receivedChar); + + if (receivedChar == 'A') + { + Serial.println("Command Received: A"); + changeDelay(); + } + } +} + + + +void handleLeds() +{ + unsigned long currentMillis = millis(); + + if (currentMillis - lastLedToggle >= ledDelay) + { + lastLedToggle = currentMillis; + + switch (ledState) + { + case LED1_ON: + changeLed(LED3_PIN, LED1_PIN); + ledState = LED2_ON; + break; + + case LED2_ON: + changeLed(LED1_PIN, LED2_PIN); + ledState = LED3_ON; + break; + + case LED3_ON: + changeLed(LED2_PIN, LED3_PIN); + ledState = LED1_ON; + Serial.println("led > 2"); + break; + } + } +} + +void changeLed(uint8_t pin1, uint8_t pin2) { + digitalWrite(pin1, LOW); + digitalWrite(pin2, HIGH); +} + +void changeDelay() +{ + if (ledDelay > 100) + { + ledDelay -= 100; + } + else + { + ledDelay = 1000; + } + Serial.print("New ledDelay: "); + Serial.println(ledDelay); +} \ No newline at end of file diff --git a/mc_labs/Bala_lab2/test/README b/mc_labs/Bala_lab2/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/mc_labs/Bala_lab2/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html