diff --git a/esp32/working_dir/main_code.ino b/esp32/working_dir/main_code.ino index dcb1895..856e8a3 100644 --- a/esp32/working_dir/main_code.ino +++ b/esp32/working_dir/main_code.ino @@ -2,21 +2,13 @@ #include "arduino_secrets.h" -#define BLYNK_TEMPLATE_ID MY_ID -#define BLYNK_TEMPLATE_NAME MY_TEMPLATE_NAME -#define BLYNK_AUTH_TOKEN MY_AUTH_TOKEN - char ssid[] = MY_SSID; char pass[] = MY_PASSWORD; -#define BLYNK_PRINT Serial - #if defined(ESP8266) -#include #include #include #elif defined(ESP32) -#include #include #include @@ -25,7 +17,7 @@ char pass[] = MY_PASSWORD; #include #include -#include +// #include #include #include @@ -58,7 +50,7 @@ BLEServer *pServer = NULL; BLECharacteristic *pCharacteristic = NULL; bool deviceConnected = false; bool oldDeviceConnected = false; -std::string receivedData = ""; +String receivedData = ""; String new_wifi_name = ""; String new_wifi_password = ""; @@ -70,25 +62,8 @@ int closing_angle = 180; int servo_shifter_timeout = 0; // in seconds // press button for n seconds to update lock status -// Define NTP Client to get time -WiFiUDP ntpUDP; -NTPClient timeClient(ntpUDP); - -// Variables to save date and time -String formattedDate; - -struct RTC { - // Final data : 23/08/26,05:38:34+20 - int year = 0; // year will be 23 - int month = 0; // month will be 08 - int date = 0; // date will be 26 - int hour = 0; // hour will be 05 - int minutes = 0; // minutes will be 38 - int seconds = 0; // seconds will be 34 -}; -RTC myRTC; - -unsigned int last_time_updated = 0; +unsigned int last_time_update = 0; +int UPDATE_DOOR_TIME = 3; // in seconds int update_time_after = 10; // in minutes class MyServerCallbacks : public BLEServerCallbacks { @@ -99,7 +74,7 @@ class MyServerCallbacks : public BLEServerCallbacks { class MyCharacteristicCallbacks : public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pCharacteristic) { - std::string value = pCharacteristic->getValue(); + String value = pCharacteristic->getValue(); if (value.length() > 0) { receivedData = value; Serial.print("Received from BLE: "); @@ -153,8 +128,8 @@ const int white_bulb = 33; #define INPUT_BUTTON 13 unsigned int door_last_open_on = 0; -int close_door_in = 5; // in seconds -bool DOOR_STATE = false; +int close_door_in = 5; // in seconds +bool DOOR_STATE = false; // by default close int door_pin = builtin_led; bool BYPASS_PREVIOUS_DOOR_STATE = false; bool ALLOW_DOOR_OPENING = true; @@ -172,6 +147,9 @@ void setup() { Serial.begin(115200); initBLE(); + servo.attach(SERVO_PIN); + servo.write(closing_angle); + pinMode(builtin_led, OUTPUT); pinMode(white_bulb, OUTPUT); @@ -188,7 +166,7 @@ void setup() { syncSPIFFS(); led.led(RED_LED, true); - connectToWifiAndBlynk(); // Attempt initial connection + connectToWifi(); // Attempt initial connection led.led(RED_LED, false); led_test(); @@ -196,20 +174,44 @@ void setup() { server.on("/", HTTP_GET, handleRoot); server.on("/update", HTTP_POST, handleUpdate); server.on("/getVariables", HTTP_GET, handleGetVariables); + server.on("/alter", HTTP_GET, []() { + alterDoorState(); + String state = DOOR_STATE ? "Open" : "Close"; + server.send(200, "text/plain", + "alterDoorState called, new state: " + state + "\n"); + }); + + server.on("/state", HTTP_GET, []() { + if (server.hasArg("value")) { + String val = server.arg("value"); + val.trim(); + val.toLowerCase(); + + if (val == "true") { + doorState(true); + String state = DOOR_STATE ? "Open" : "Close"; + server.send(200, "text/plain", + "doorState(true) called, new state: " + state + "\n"); + } else if (val == "false") { + doorState(false); + String state = DOOR_STATE ? "Open" : "Close"; + server.send(200, "text/plain", + "doorState(false) called, new state: " + state + "\n"); + } else { + server.send(400, "text/plain", + "Invalid value. Use ?value=true or ?value=false\n"); + } + } else { + server.send(400, "text/plain", "Missing 'value' parameter\n"); + } + }); // Start server server.begin(); Serial.println("Server started"); - servo.attach(SERVO_PIN); - if (IS_CONNECTED_TO_INTERNET) { - timeClient.begin(); - timeClient.setTimeOffset(18000); - } } void loop() { - if (IS_CONNECTED_TO_WIFI) - Blynk.run(); server.handleClient(); // Handle client requests @@ -240,7 +242,7 @@ void loop() { if (currentTime - lastCheckTime >= recheck_internet_connectivity_in) { lastCheckTime = currentTime; if (!checkInternetConnectivity()) - connectToWifiAndBlynk(); + connectToWifi(); } manageBackGroundJobs(); @@ -283,12 +285,17 @@ void inputManager(String command) { String ip = ipv4.toString(); println(ip); delay(2000); + } else if (isIn(command, "alter gate") || isIn(command, "Alter gate") || + isIn(command, "alter door") || isIn(command, "Alter door")) { + alterDoorState(); + } else if (isIn(command, "my ip") || isIn(command, "My ip")) { + Serial.println(WiFi.localIP()); } else { Serial.println("Nothing executed"); } } -void connectToWifiAndBlynk() { +void connectToWifi() { Serial.println("Connecting to Wi-Fi..."); IS_CONNECTED_TO_WIFI = false; @@ -304,14 +311,11 @@ void connectToWifiAndBlynk() { println("Connecting to file ssid: " + String(file_ssid) + " password: " + String(file_password)); IS_CONNECTED_TO_WIFI = validateWIFICreds(file_ssid, file_password); - if (IS_CONNECTED_TO_WIFI) - Blynk.begin(BLYNK_AUTH_TOKEN, file_ssid, file_password); led.led(builtin_led, true); } if (!IS_CONNECTED_TO_WIFI && validateWIFICreds(ssid, pass, 20)) { println("Connecting to ssid: " + String(ssid)); - Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass); IS_CONNECTED_TO_WIFI = true; led.led(builtin_led, true); } else if (!OWN_NETWORK_CREATED) { @@ -320,16 +324,6 @@ void connectToWifiAndBlynk() { OWN_NETWORK_CREATED = true; led.led(builtin_led, false); } - - if (IS_CONNECTED_TO_WIFI) { - println("Trying to connect to blynk"); - if (Blynk.connect()) { - Serial.println("Connected to Blynk"); - Serial.println(WiFi.localIP()); - } else { - Serial.println("Failed to connect to Blynk"); - } - } } bool checkInternetConnectivity() { @@ -362,42 +356,6 @@ bool checkInternetConnectivity() { return isConnected; } -BLYNK_WRITE(V1) { - int ledValue = param.asInt(); - if ((ledValue != 0) && ALLOW_DOOR_OPENING) { - doorState(true); - println("Door state changed, blynk input value: " + String(ledValue)); - delay(1000); - } else { - println("Closing by blink, State: " + String(ledValue)); - } -} - -BLYNK_WRITE(V2) { - int ledValue = param.asInt(); - digitalWrite(WHITE_LED, ledValue ? HIGH : LOW); -} - -BLYNK_WRITE(V3) { - int ledValue = param.asInt(); - digitalWrite(BLUE_LED, ledValue ? HIGH : LOW); -} - -BLYNK_WRITE(V4) { - int ledValue = param.asInt(); - digitalWrite(YELLOW_LED, ledValue ? HIGH : LOW); -} - -BLYNK_WRITE(V5) { - int ledValue = param.asInt(); - digitalWrite(white_bulb, ledValue ? HIGH : LOW); -} - -BLYNK_WRITE(V6) { - int ledValue = param.asInt(); - digitalWrite(RED_LED, ledValue ? HIGH : LOW); -} - void println(String str) { Serial.println(str); } void print(String str) { Serial.print(str); } @@ -462,7 +420,7 @@ void manageBackGroundJobs() { buttonDealer(); led.led(BLUE_LED, !ALLOW_DOOR_OPENING); led.led(WHITE_LED, OWN_NETWORK_CREATED); - fetchOnlineTime(); + // fetchOnlineTime(); // disableDoorServo(); delay(50); } @@ -549,9 +507,9 @@ bool validateWIFICreds(char ssid[], char pass[], int timeOut) { if (i < timeOut) { println("Connected."); bool status = checkInternetConnectivity(); - println("\n Disconnecting wifi..."); - WiFi.disconnect(true); - delay(2000); + // println("\n Disconnecting wifi..."); + // WiFi.disconnect(true); + // delay(2000); return status; } } @@ -682,7 +640,7 @@ void handleGetVariables() { void alterDoorState() { println("Altering door state...\n New state: " + - String(!DOOR_STATE ? "Open" : "Close")); + String(DOOR_STATE ? "Close" : "Open")); DOOR_STATE = !DOOR_STATE; doorState(DOOR_STATE); delay(1500); @@ -699,7 +657,7 @@ void doorState(bool state) { door_last_open_on = getSeconds(); println("Door state: Door state: " + String(state ? "Open" : "Close")); digitalWrite(YELLOW_LED, state); - state ? servo.write(opening_angle) : servo.write(closing_angle); + state ? openDoor() : closeDoor(); } void led_test() { @@ -725,59 +683,49 @@ void led_test() { } void buttonDealer() { - bool longPressed = false; led.led(YELLOW_LED, true); - for (int i = 0; - i < servo_shifter_timeout + 3000 && (digitalRead(INPUT_BUTTON) == HIGH); - i += 100) { - if (digitalRead(INPUT_BUTTON) == HIGH) - if (i > servo_shifter_timeout) { - longPressed = true; - led.led(BLUE_LED, ALLOW_DOOR_OPENING); // condition and check is valid - } - delay(100); - } - if (longPressed) { - ALLOW_DOOR_OPENING = !ALLOW_DOOR_OPENING; - cspi.updateSPIFFS("ALLOW_DOOR_OPENING", - (String(ALLOW_DOOR_OPENING ? "1" : "0"))); - } else - alterDoorState(); + alterDoorState(); } -void fetchOnlineTime() { - if (!IS_CONNECTED_TO_INTERNET || - (getMinutes() - last_time_updated) < update_time_after) - return; - - int i = 100; - while (!timeClient.update() && i > 0) { - timeClient.forceUpdate(); - i -= 10; - delay(100); +void openDoor() { + if (ALLOW_DOOR_OPENING) { + if (getSeconds() < last_time_update + UPDATE_DOOR_TIME) { + println("Door is already open, please wait..."); + return; + } + last_time_update = getSeconds(); + println("Opening door..."); + moveServoGradually(opening_angle, closing_angle); + DOOR_STATE = true; + } else { + println("Door opening is not allowed."); } - - formattedDate = timeClient.getFormattedDate(); - updateOnlineTime(formattedDate, myRTC); - Serial.println("Update time: " + getRTC_Time()); - // Serial.println("Updated RTC values:"); - // Serial.printf("Year: %02d ", myRTC.year); - // Serial.printf("Month: %02d ", myRTC.month); - // Serial.printf("Date: %02d ", myRTC.date); - // Serial.printf("Hour: %02d ", myRTC.hour); - // Serial.printf("Minutes: %02d ", myRTC.minutes); - // Serial.printf("Seconds: %02d\n", myRTC.seconds); - last_time_updated = getMinutes(); } -void updateOnlineTime(String formattedDate, RTC &rtc) { - sscanf(formattedDate.c_str(), "%d-%d-%dT%d:%d:%dZ", &rtc.year, &rtc.month, - &rtc.date, &rtc.hour, &rtc.minutes, &rtc.seconds); - rtc.year = rtc.year % 100; +void closeDoor() { + if (ALLOW_DOOR_OPENING) { + if (getSeconds() < last_time_update + UPDATE_DOOR_TIME) { + println("Door is already open, please wait..."); + return; + } + last_time_update = getSeconds(); + println("Closing door..."); + moveServoGradually(closing_angle, opening_angle); + DOOR_STATE = false; + } else { + println("Door closing is not allowed."); + } } -String getRTC_Time() { - return (String(myRTC.hour) + ":" + String(myRTC.minutes) + ":" + - String(myRTC.seconds) + " ___ " + String(myRTC.month) + "/" + - String(myRTC.date) + "/" + String(myRTC.year)); -} +void moveServoGradually(int targetAngle, int currentAngle) { + // Check if we need to move the servo + if (targetAngle != currentAngle) { + int step = + (targetAngle > currentAngle) ? 1 : -1; // Determine the step direction + for (int angle = currentAngle; angle != targetAngle; angle += step) { + servo.write(angle); + delay(15); // Delay to move smoothly (adjust for smoother/faster) + } + currentAngle = targetAngle; // Update current angle after moving + } +} \ No newline at end of file