diff --git a/src/credentials.h b/src/credentials.h index 4cc5bda..cce683e 100644 --- a/src/credentials.h +++ b/src/credentials.h @@ -5,4 +5,4 @@ #define MQTT_PASS "22pa0799" #define OTA_USER "admin" -#define OTA_PASS "Ut@meri$22" \ No newline at end of file +#define OTA_PASS "P@ssword" \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 47b3f8b..4d4cae5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,17 +10,33 @@ #define MQTT_HOST "192.168.1.146" #define MQTT_PORT 1883 +// Actuator state enum +enum ActuatorState { + IDLE, + OPENING, + CLOSING +}; + void openValve(); void closeValve(); void stopValve(); +void updateActuator(); void OnConnect(); void OnOpen(); void OnClose(); +void OnStatus(); void OnRestart(); void OnNotFound(); -String SendHTML(uint8_t ValveState); +String SendHTML(); +String GetStatusJSON(); + +// Actuator control variables +ActuatorState currentState = IDLE; +ActuatorState desiredState = IDLE; +unsigned long operationStartTime = 0; +const unsigned long OPERATION_DURATION = 60000; // 60 seconds bool ValveState = LOW; @@ -101,6 +117,7 @@ void setup_http() { server.on("/", OnConnect); server.on("/open", OnOpen); server.on("/close", OnClose); + server.on("/status", OnStatus); server.on("/restart", OnRestart); server.onNotFound(OnNotFound); server.begin(); @@ -136,41 +153,42 @@ void loop() { server.handleClient(); + // Update actuator state machine (non-blocking) + updateActuator(); + + // Handle physical buttons int buttonOpenState = digitalRead(ButtonOpen); int buttonCloseState = digitalRead(ButtonClose); if(buttonOpenState == LOW) { - buttonCloseState = !buttonOpenState; - digitalWrite(MotorPin1, LOW); - digitalWrite(MotorPin2, HIGH); - ValveState = HIGH; + openValve(); } if(buttonCloseState == LOW) { - buttonOpenState = !buttonCloseState; - digitalWrite(MotorPin1, HIGH); - digitalWrite(MotorPin2, LOW); - ValveState = LOW; - } - - if(buttonOpenState == HIGH && buttonCloseState == HIGH) { - stopValve(); + closeValve(); } - } void OnConnect() { - server.send(200, "text/html", SendHTML(ValveState)); + server.send(200, "text/html", SendHTML()); } void OnOpen() { - openValve(); - server.send(200, "text/html", SendHTML(ValveState)); + if(currentState == IDLE) { + openValve(); + } + server.send(200, "application/json", GetStatusJSON()); } void OnClose() { - closeValve(); - server.send(200, "text/html", SendHTML(ValveState)); + if(currentState == IDLE) { + closeValve(); + } + server.send(200, "application/json", GetStatusJSON()); +} + +void OnStatus() { + server.send(200, "application/json", GetStatusJSON()); } void OnRestart() { @@ -184,21 +202,23 @@ void OnNotFound(){ } void openValve() { - Serial.println("Opening valve..."); - digitalWrite(MotorPin1, LOW); - digitalWrite(MotorPin2, HIGH); - delay(duration); - stopValve(); - ValveState = HIGH; + if(currentState == IDLE) { + Serial.println("Opening valve..."); + digitalWrite(MotorPin1, LOW); + digitalWrite(MotorPin2, HIGH); + currentState = OPENING; + operationStartTime = millis(); + } } void closeValve() { - Serial.println("Closing valve..."); - digitalWrite(MotorPin1, HIGH); - digitalWrite(MotorPin2, LOW); - delay(duration); - stopValve(); - ValveState = LOW; + if(currentState == IDLE) { + Serial.println("Closing valve..."); + digitalWrite(MotorPin1, HIGH); + digitalWrite(MotorPin2, LOW); + currentState = CLOSING; + operationStartTime = millis(); + } } void stopValve() { @@ -206,28 +226,157 @@ void stopValve() { digitalWrite(MotorPin2, LOW); } -String SendHTML(uint8_t ValveState) { - String ptr = " \n"; - ptr +="
\n"; - ptr +="Valve is OPEN
CLOSE\n"; - } else { - ptr +="Valve is CLOSED
OPEN\n"; +void updateActuator() { + // Check if operation is complete + if(currentState != IDLE) { + unsigned long elapsedTime = millis() - operationStartTime; + + if(elapsedTime >= OPERATION_DURATION) { + // Operation complete + stopValve(); + + if(currentState == OPENING) { + ValveState = HIGH; + Serial.println("Valve fully open"); + } else if(currentState == CLOSING) { + ValveState = LOW; + Serial.println("Valve fully closed"); + } + + currentState = IDLE; + } } - ptr +="\n"; - ptr +="\n"; +} + +String SendHTML() { + String ptr = "\n"; + ptr += "\n"; + ptr += "\n"; + ptr += " \n"; + ptr += "