Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Phase Timer
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-08-18 13:24:46
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* esp12f timer.with mobile operation wifi.when touch */
- /* start button from mobile app relay pin no 14 high */
- /* and hold until set delay 1 time finish.after delay */
- /* 1 finished delay 2 start and hold until set time */
- /* finished,need to show count down time on mobile. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <WiFi.h>
- #include <WebServer.h>
- #include <Arduino.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- // System configuration (customize to your network)
- const char* ssid = "YOUR_SSID"; // TODO: replace with your WiFi network name
- const char* password = "YOUR_PASSWORD"; // TODO: replace with your WiFi password
- // Relay and timing configuration
- const int RELAY_PIN = 14; // Relay control pin (HIGH to activate)
- const unsigned long DELAY1_MS = 5000; // Phase 1 duration in milliseconds
- const unsigned long DELAY2_MS = 15000; // Phase 2 duration in milliseconds
- // State machine for timer control
- enum State { IDLE, PHASE1, PHASE2 };
- State currentState = IDLE;
- unsigned long phaseStart = 0; // timestamp when current phase started
- WebServer server(80);
- // Root HTML page for mobile access
- const char* indexPage = "<!DOCTYPE html>"
- "<html><head><title>Relay Timer</title>"
- "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
- "</head><body>"
- "<h1>Relay Timer</h1>"
- "<button onclick=\"startTimer()\">Start</button>"
- "<p id=\"state\">State: Idle</p>"
- "<p>Remaining: <span id=\"remaining\">0</span> ms</p>"
- "<p>Relay Pin: <span id=\"relay\">LOW</span></p>"
- "<p><a href=\\\"/status\\\">Refresh Status</a></p>"
- "<script>function startTimer(){ fetch('/start').then(r=>r.text()).then(()=>{document.getElementById('state').innerText='State: PHASE1';}); }"
- "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){} }"
- "setInterval(refresh, 1000); refresh();"
- "</script>"
- "</body></html>";
- void handleRoot() {
- server.send(200, "text/html", indexPage);
- }
- void handleStart() {
- if (currentState != IDLE) {
- server.send(200, "text/plain", "Already running");
- return;
- }
- // Activate relay and start Phase 1
- digitalWrite(RELAY_PIN, HIGH);
- currentState = PHASE1;
- phaseStart = millis();
- server.send(200, "text/plain", "Started");
- }
- void handleStatus() {
- String stateName = "IDLE";
- unsigned long remaining = 0;
- if (currentState == PHASE1) {
- stateName = "PHASE1";
- unsigned long elapsed = millis() - phaseStart;
- if (DELAY1_MS > elapsed) remaining = DELAY1_MS - elapsed; else remaining = 0;
- } else if (currentState == PHASE2) {
- stateName = "PHASE2";
- unsigned long elapsed = millis() - phaseStart;
- if (DELAY2_MS > elapsed) remaining = DELAY2_MS - elapsed; else remaining = 0;
- }
- const char* relayState = (digitalRead(RELAY_PIN) == HIGH) ? "HIGH" : "LOW";
- String json = String("{\"state\":\"") + stateName + String("\",\"remaining_ms\":") + String(remaining) + String(",\"relay\":\"") + relayState + String("\"}");
- server.send(200, "application/json", json);
- }
- void setup(void){
- Serial.begin(115200);
- pinMode(RELAY_PIN, OUTPUT);
- digitalWrite(RELAY_PIN, LOW);
- // Connect to WiFi
- Serial.print("Connecting to "); Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println();
- Serial.println("WiFi connected");
- Serial.print("IP address: "); Serial.println(WiFi.localIP());
- // Setup HTTP routes
- server.on("/", handleRoot);
- server.on("/start", handleStart);
- server.on("/status", handleStatus);
- server.begin();
- Serial.println("HTTP server started");
- }
- void loop(void){
- server.handleClient();
- unsigned long now = millis();
- switch (currentState) {
- case PHASE1:
- if (now - phaseStart >= DELAY1_MS) {
- currentState = PHASE2;
- phaseStart = now;
- }
- break;
- case PHASE2:
- if (now - phaseStart >= DELAY2_MS) {
- currentState = IDLE;
- digitalWrite(RELAY_PIN, LOW);
- }
- break;
- case IDLE:
- default:
- break;
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment