pleasedontcode

Phase Timer rev_03

Aug 18th, 2025
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Phase Timer
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-08-18 13:24:46
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* esp12f timer.with mobile operation wifi.when touch */
  21.     /* start button from mobile app relay pin no 14 high */
  22.     /* and hold until set delay 1 time finish.after delay */
  23.     /* 1 finished delay 2 start and hold until set time */
  24.     /* finished,need to show count down time on mobile. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27.  
  28. /* START CODE */
  29.  
  30. /****** DEFINITION OF LIBRARIES *****/
  31. #include <WiFi.h>
  32. #include <WebServer.h>
  33. #include <Arduino.h>
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38.  
  39. // System configuration (customize to your network)
  40. const char* ssid = "YOUR_SSID";       // TODO: replace with your WiFi network name
  41. const char* password = "YOUR_PASSWORD"; // TODO: replace with your WiFi password
  42.  
  43. // Relay and timing configuration
  44. const int RELAY_PIN = 14;                       // Relay control pin (HIGH to activate)
  45. const unsigned long DELAY1_MS = 5000;           // Phase 1 duration in milliseconds
  46. const unsigned long DELAY2_MS = 15000;          // Phase 2 duration in milliseconds
  47.  
  48. // State machine for timer control
  49. enum State { IDLE, PHASE1, PHASE2 };
  50. State currentState = IDLE;
  51. unsigned long phaseStart = 0; // timestamp when current phase started
  52.  
  53. WebServer server(80);
  54.  
  55. // Root HTML page for mobile access
  56. const char* indexPage = "<!DOCTYPE html>"
  57. "<html><head><title>Relay Timer</title>"
  58. "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
  59. "</head><body>"
  60. "<h1>Relay Timer</h1>"
  61. "<button onclick=\"startTimer()\">Start</button>"
  62. "<p id=\"state\">State: Idle</p>"
  63. "<p>Remaining: <span id=\"remaining\">0</span> ms</p>"
  64. "<p>Relay Pin: <span id=\"relay\">LOW</span></p>"
  65. "<p><a href=\\\"/status\\\">Refresh Status</a></p>"
  66. "<script>function startTimer(){ fetch('/start').then(r=>r.text()).then(()=>{document.getElementById('state').innerText='State: PHASE1';}); }"
  67. "async function refresh(){ try { const r = await fetch('/status'); const j = await r.json(); document.getElementById('state').innerText='State: '+j.state; document.getElementById('remaining').innerText=j.remaining_ms; document.getElementById('relay').innerText=j.relay; } catch(e){} }"
  68. "setInterval(refresh, 1000); refresh();"
  69. "</script>"
  70. "</body></html>";
  71.  
  72. void handleRoot() {
  73.     server.send(200, "text/html", indexPage);
  74. }
  75.  
  76. void handleStart() {
  77.     if (currentState != IDLE) {
  78.         server.send(200, "text/plain", "Already running");
  79.         return;
  80.     }
  81.     // Activate relay and start Phase 1
  82.     digitalWrite(RELAY_PIN, HIGH);
  83.     currentState = PHASE1;
  84.     phaseStart = millis();
  85.     server.send(200, "text/plain", "Started");
  86. }
  87.  
  88. void handleStatus() {
  89.     String stateName = "IDLE";
  90.     unsigned long remaining = 0;
  91.     if (currentState == PHASE1) {
  92.         stateName = "PHASE1";
  93.         unsigned long elapsed = millis() - phaseStart;
  94.         if (DELAY1_MS > elapsed) remaining = DELAY1_MS - elapsed; else remaining = 0;
  95.     } else if (currentState == PHASE2) {
  96.         stateName = "PHASE2";
  97.         unsigned long elapsed = millis() - phaseStart;
  98.         if (DELAY2_MS > elapsed) remaining = DELAY2_MS - elapsed; else remaining = 0;
  99.     }
  100.     const char* relayState = (digitalRead(RELAY_PIN) == HIGH) ? "HIGH" : "LOW";
  101.     String json = String("{\"state\":\"") + stateName + String("\",\"remaining_ms\":") + String(remaining) + String(",\"relay\":\"") + relayState + String("\"}");
  102.     server.send(200, "application/json", json);
  103. }
  104.  
  105. void setup(void){
  106.     Serial.begin(115200);
  107.     pinMode(RELAY_PIN, OUTPUT);
  108.     digitalWrite(RELAY_PIN, LOW);
  109.  
  110.     // Connect to WiFi
  111.     Serial.print("Connecting to "); Serial.println(ssid);
  112.     WiFi.begin(ssid, password);
  113.     while (WiFi.status() != WL_CONNECTED) {
  114.         delay(500);
  115.         Serial.print(".");
  116.     }
  117.     Serial.println();
  118.     Serial.println("WiFi connected");
  119.     Serial.print("IP address: "); Serial.println(WiFi.localIP());
  120.  
  121.     // Setup HTTP routes
  122.     server.on("/", handleRoot);
  123.     server.on("/start", handleStart);
  124.     server.on("/status", handleStatus);
  125.     server.begin();
  126.     Serial.println("HTTP server started");
  127. }
  128.  
  129. void loop(void){
  130.     server.handleClient();
  131.  
  132.     unsigned long now = millis();
  133.     switch (currentState) {
  134.         case PHASE1:
  135.             if (now - phaseStart >= DELAY1_MS) {
  136.                 currentState = PHASE2;
  137.                 phaseStart = now;
  138.             }
  139.             break;
  140.         case PHASE2:
  141.             if (now - phaseStart >= DELAY2_MS) {
  142.                 currentState = IDLE;
  143.                 digitalWrite(RELAY_PIN, LOW);
  144.             }
  145.             break;
  146.         case IDLE:
  147.         default:
  148.             break;
  149.     }
  150. }
  151.  
  152. /* END CODE */
  153.  
Advertisement
Add Comment
Please, Sign In to add comment