|
|
|
@ -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() { |
|
|
|
if(currentState == IDLE) { |
|
|
|
openValve(); |
|
|
|
server.send(200, "text/html", SendHTML(ValveState)); |
|
|
|
} |
|
|
|
server.send(200, "application/json", GetStatusJSON()); |
|
|
|
} |
|
|
|
|
|
|
|
void OnClose() { |
|
|
|
if(currentState == IDLE) { |
|
|
|
closeValve(); |
|
|
|
server.send(200, "text/html", SendHTML(ValveState)); |
|
|
|
} |
|
|
|
server.send(200, "application/json", GetStatusJSON()); |
|
|
|
} |
|
|
|
|
|
|
|
void OnStatus() { |
|
|
|
server.send(200, "application/json", GetStatusJSON()); |
|
|
|
} |
|
|
|
|
|
|
|
void OnRestart() { |
|
|
|
@ -184,21 +202,23 @@ void OnNotFound(){ |
|
|
|
} |
|
|
|
|
|
|
|
void openValve() { |
|
|
|
if(currentState == IDLE) { |
|
|
|
Serial.println("Opening valve..."); |
|
|
|
digitalWrite(MotorPin1, LOW); |
|
|
|
digitalWrite(MotorPin2, HIGH); |
|
|
|
delay(duration); |
|
|
|
stopValve(); |
|
|
|
ValveState = HIGH; |
|
|
|
currentState = OPENING; |
|
|
|
operationStartTime = millis(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void closeValve() { |
|
|
|
if(currentState == IDLE) { |
|
|
|
Serial.println("Closing valve..."); |
|
|
|
digitalWrite(MotorPin1, HIGH); |
|
|
|
digitalWrite(MotorPin2, LOW); |
|
|
|
delay(duration); |
|
|
|
stopValve(); |
|
|
|
ValveState = LOW; |
|
|
|
currentState = CLOSING; |
|
|
|
operationStartTime = millis(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void stopValve() { |
|
|
|
@ -206,28 +226,157 @@ void stopValve() { |
|
|
|
digitalWrite(MotorPin2, LOW); |
|
|
|
} |
|
|
|
|
|
|
|
String SendHTML(uint8_t ValveState) { |
|
|
|
String ptr = "<!DOCTYPE html> <html>\n"; |
|
|
|
ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n"; |
|
|
|
ptr +="<title>Linear Actuator</title>\n"; |
|
|
|
ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n"; |
|
|
|
ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n"; |
|
|
|
ptr +=".button {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n"; |
|
|
|
ptr +=".button-open {background-color: #1abc9c;}\n"; |
|
|
|
ptr +=".button-open:active {background-color: #16a085;}\n"; |
|
|
|
ptr +=".button-close {background-color: #34495e;}\n"; |
|
|
|
ptr +=".button-close:active {background-color: #2c3e50;}\n"; |
|
|
|
ptr +="p {font-size: 14px;color: #888;margin-bottom: 10px;}\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; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
String SendHTML() { |
|
|
|
String ptr = "<!DOCTYPE html>\n"; |
|
|
|
ptr += "<html>\n"; |
|
|
|
ptr += "<head>\n"; |
|
|
|
ptr += " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n"; |
|
|
|
ptr += " <title>Linear Actuator Control</title>\n"; |
|
|
|
ptr += " <style>\n"; |
|
|
|
ptr += " * { margin: 0; padding: 0; box-sizing: border-box; }\n"; |
|
|
|
ptr += " html { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; display: flex; height: 100vh; }\n"; |
|
|
|
ptr += " body { display: flex; align-items: center; justify-content: center; width: 100%; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); margin: 0; }\n"; |
|
|
|
ptr += " .container { background: white; padding: 40px; border-radius: 12px; box-shadow: 0 20px 60px rgba(0,0,0,0.3); text-align: center; max-width: 400px; }\n"; |
|
|
|
ptr += " h1 { color: #333; margin-bottom: 30px; font-size: 28px; }\n"; |
|
|
|
ptr += " .status-display { background: #f8f9fa; padding: 20px; border-radius: 8px; margin-bottom: 30px; border-left: 4px solid #667eea; }\n"; |
|
|
|
ptr += " .status-text { font-size: 18px; color: #555; margin-bottom: 10px; }\n"; |
|
|
|
ptr += " .status-value { font-size: 24px; font-weight: bold; color: #667eea; }\n"; |
|
|
|
ptr += " .progress-bar { width: 100%; height: 8px; background: #e0e0e0; border-radius: 4px; overflow: hidden; margin-top: 10px; display: none; }\n"; |
|
|
|
ptr += " .progress-bar.active { display: block; }\n"; |
|
|
|
ptr += " .progress-fill { height: 100%; background: linear-gradient(90deg, #667eea 0%, #764ba2 100%); width: 0%; transition: width 0.2s ease; }\n"; |
|
|
|
ptr += " .button { display: inline-block; padding: 15px 40px; font-size: 18px; font-weight: bold; border: none; border-radius: 8px; cursor: pointer; transition: all 0.3s ease; color: white; margin-top: 20px; width: 100%; }\n"; |
|
|
|
ptr += " .button.open { background: linear-gradient(135deg, #1abc9c 0%, #16a085 100%); }\n"; |
|
|
|
ptr += " .button.open:hover:not(:disabled) { transform: translateY(-2px); box-shadow: 0 10px 20px rgba(26, 188, 156, 0.3); }\n"; |
|
|
|
ptr += " .button.close { background: linear-gradient(135deg, #e74c3c 0%, #c0392b 100%); }\n"; |
|
|
|
ptr += " .button.close:hover:not(:disabled) { transform: translateY(-2px); box-shadow: 0 10px 20px rgba(231, 76, 60, 0.3); }\n"; |
|
|
|
ptr += " .button:disabled { opacity: 0.6; cursor: not-allowed; transform: none; }\n"; |
|
|
|
ptr += " .info-text { font-size: 12px; color: #999; margin-top: 15px; }\n"; |
|
|
|
ptr += " </style>\n"; |
|
|
|
ptr += "</head>\n"; |
|
|
|
ptr += "<body>\n"; |
|
|
|
ptr += " <div class=\"container\">\n"; |
|
|
|
ptr += " <h1>Linear Actuator</h1>\n"; |
|
|
|
if(ValveState == HIGH) { |
|
|
|
ptr +="<p>Valve is OPEN</p><a class=\"button button-close\" href=\"/close\">CLOSE</a>\n"; |
|
|
|
} else { |
|
|
|
ptr +="<p>Valve is CLOSED</p><a class=\"button button-open\" href=\"/open\">OPEN</a>\n"; |
|
|
|
} |
|
|
|
ptr += " <div class=\"status-display\">\n"; |
|
|
|
ptr += " <div class=\"status-text\">Current Status</div>\n"; |
|
|
|
ptr += " <div class=\"status-value\" id=\"status\">CLOSED</div>\n"; |
|
|
|
ptr += " <div class=\"progress-bar\" id=\"progressBar\">\n"; |
|
|
|
ptr += " <div class=\"progress-fill\" id=\"progressFill\"></div>\n"; |
|
|
|
ptr += " </div>\n"; |
|
|
|
ptr += " </div>\n"; |
|
|
|
ptr += " <button class=\"button open\" id=\"actionButton\" onclick=\"handleAction()\">OPEN</button>\n"; |
|
|
|
ptr += " <div class=\"info-text\">Operation time: <span id=\"opTime\">0</span>s / 60s</div>\n"; |
|
|
|
ptr += " </div>\n"; |
|
|
|
ptr += "\n"; |
|
|
|
ptr += " <script>\n"; |
|
|
|
ptr += " let isOperating = false;\n"; |
|
|
|
ptr += " let statusCheckInterval;\n"; |
|
|
|
ptr += "\n"; |
|
|
|
ptr += " function updateStatus() {\n"; |
|
|
|
ptr += " fetch('/status')\n"; |
|
|
|
ptr += " .then(response => response.json())\n"; |
|
|
|
ptr += " .then(data => {\n"; |
|
|
|
ptr += " const statusEl = document.getElementById('status');\n"; |
|
|
|
ptr += " const btnEl = document.getElementById('actionButton');\n"; |
|
|
|
ptr += " const progBar = document.getElementById('progressBar');\n"; |
|
|
|
ptr += " const progFill = document.getElementById('progressFill');\n"; |
|
|
|
ptr += " const opTimeEl = document.getElementById('opTime');\n"; |
|
|
|
ptr += "\n"; |
|
|
|
ptr += " if(data.state === 'OPENING' || data.state === 'CLOSING') {\n"; |
|
|
|
ptr += " isOperating = true;\n"; |
|
|
|
ptr += " btnEl.disabled = true;\n"; |
|
|
|
ptr += " progBar.classList.add('active');\n"; |
|
|
|
ptr += " statusEl.textContent = data.state;\n"; |
|
|
|
ptr += " const progress = (data.elapsedTime / data.totalTime) * 100;\n"; |
|
|
|
ptr += " progFill.style.width = progress + '%';\n"; |
|
|
|
ptr += " opTimeEl.textContent = Math.round(data.elapsedTime / 1000);\n"; |
|
|
|
ptr += " } else {\n"; |
|
|
|
ptr += " isOperating = false;\n"; |
|
|
|
ptr += " btnEl.disabled = false;\n"; |
|
|
|
ptr += " progBar.classList.remove('active');\n"; |
|
|
|
ptr += " progFill.style.width = '0%';\n"; |
|
|
|
ptr += " opTimeEl.textContent = '0';\n"; |
|
|
|
ptr += " \n"; |
|
|
|
ptr += " if(data.valveState === 1) {\n"; |
|
|
|
ptr += " statusEl.textContent = 'OPEN';\n"; |
|
|
|
ptr += " btnEl.textContent = 'CLOSE';\n"; |
|
|
|
ptr += " btnEl.className = 'button close';\n"; |
|
|
|
ptr += " } else {\n"; |
|
|
|
ptr += " statusEl.textContent = 'CLOSED';\n"; |
|
|
|
ptr += " btnEl.textContent = 'OPEN';\n"; |
|
|
|
ptr += " btnEl.className = 'button open';\n"; |
|
|
|
ptr += " }\n"; |
|
|
|
ptr += " }\n"; |
|
|
|
ptr += " })\n"; |
|
|
|
ptr += " .catch(err => console.error('Status update failed:', err));\n"; |
|
|
|
ptr += " }\n"; |
|
|
|
ptr += "\n"; |
|
|
|
ptr += " function handleAction() {\n"; |
|
|
|
ptr += " if(isOperating) return;\n"; |
|
|
|
ptr += " \n"; |
|
|
|
ptr += " const btnEl = document.getElementById('actionButton');\n"; |
|
|
|
ptr += " const endpoint = btnEl.textContent === 'OPEN' ? '/open' : '/close';\n"; |
|
|
|
ptr += " \n"; |
|
|
|
ptr += " fetch(endpoint)\n"; |
|
|
|
ptr += " .then(() => {\n"; |
|
|
|
ptr += " isOperating = true;\n"; |
|
|
|
ptr += " btnEl.disabled = true;\n"; |
|
|
|
ptr += " updateStatus();\n"; |
|
|
|
ptr += " })\n"; |
|
|
|
ptr += " .catch(err => console.error('Action failed:', err));\n"; |
|
|
|
ptr += " }\n"; |
|
|
|
ptr += "\n"; |
|
|
|
ptr += " // Initial status check\n"; |
|
|
|
ptr += " updateStatus();\n"; |
|
|
|
ptr += "\n"; |
|
|
|
ptr += " // Poll status every 250ms when operating, every 1s when idle\n"; |
|
|
|
ptr += " statusCheckInterval = setInterval(() => {\n"; |
|
|
|
ptr += " updateStatus();\n"; |
|
|
|
ptr += " }, 250);\n"; |
|
|
|
ptr += " </script>\n"; |
|
|
|
ptr += "</body>\n"; |
|
|
|
ptr += "</html>\n"; |
|
|
|
return ptr; |
|
|
|
} |
|
|
|
|
|
|
|
String GetStatusJSON() { |
|
|
|
String json = "{" |
|
|
|
"\"state\":\""; |
|
|
|
|
|
|
|
if(currentState == OPENING) { |
|
|
|
json += "OPENING"; |
|
|
|
} else if(currentState == CLOSING) { |
|
|
|
json += "CLOSING"; |
|
|
|
} else { |
|
|
|
json += "IDLE"; |
|
|
|
} |
|
|
|
|
|
|
|
json += "\","; |
|
|
|
json += "\"valveState\":" + String(ValveState ? 1 : 0) + ","; |
|
|
|
json += "\"elapsedTime\":" + String(millis() - operationStartTime) + ","; |
|
|
|
json += "\"totalTime\":" + String(OPERATION_DURATION); |
|
|
|
json += "}"; |
|
|
|
|
|
|
|
return json; |
|
|
|
} |
|
|
|
|