Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ========================================================================
- ESP32 SCHEDULED MOTOR CONTROL WITH DEEP SLEEP & EEPROM BACKUP
- ========================================================================
- OVERVIEW:
- Automatically runs a motor at scheduled hours with deep sleep power saving.
- Survives power failures with EEPROM backup and includes GPIO reset functionality.
- HARDWARE REQUIREMENTS:
- • ESP32 development board
- • Motor/relay connected to GPIO33 (MOTOR_PIN)
- • Status LED connected to GPIO32 (LED_PIN)
- • Reset button: GPIO14 (WAKEUP_GPIO) - connect to 3.3V for reset
- FEATURES:
- ✓ Web-based configuration interface (no coding required)
- ✓ Hourly scheduling (select any combination of 24 hours)
- ✓ Adjustable motor run time (5-300 seconds per activation)
- ✓ Ultra-low power deep sleep between activations
- ✓ Automatic time synchronization from web browser
- ✓ EEPROM backup for power failure recovery
- ✓ Hardware reset via GPIO14 to clear all settings
- ✓ Visual feedback with LED status indicators
- FIRST TIME SETUP:
- 1. Upload this sketch to ESP32
- 2. Connect to WiFi network "ESP32-Motor-Timer" (password: 12345678)
- 3. Open web browser to 192.168.4.1
- 4. Follow 3-step setup wizard:
- - Sync current time from your device
- - Set motor run time (5-300 seconds)
- - Select operation hours (checkboxes for each hour 00:00-23:00)
- 5. Click "Start Operation" - ESP32 enters automatic mode
- NORMAL OPERATION:
- • ESP32 sleeps in deep sleep mode (uses <1mA power)
- • Wakes up automatically at scheduled times
- • Runs motor for configured duration
- • LED blinks during motor operation
- • Returns to deep sleep until next scheduled time
- POWER FAILURE RECOVERY:
- • Settings automatically saved to EEPROM after first configuration
- • If power is lost, ESP32 recovers settings from EEPROM on restart
- • Time estimation helps resume operation even without WiFi
- • System continues running with last known schedule
- MANUAL RESET (Clear All Settings):
- 1. Connect GPIO14 to 3.3V (use jumper wire or switch)
- 2. Press ESP32 reset button OR power cycle
- 3. ESP32 will detect GPIO14 HIGH and clear all EEPROM settings
- 4. Automatically enters setup mode for reconfiguration
- 5. Disconnect GPIO14 from 3.3V after setup
- TROUBLESHOOTING:
- • No WiFi connection? ESP32 creates its own hotspot for setup
- • Setup timeout (4 minutes)? System tries to use EEPROM backup
- • Wrong schedule? Use GPIO14 reset to reconfigure
- • Motor not working? Check GPIO33 connection and power supply
- • LED not blinking? Check GPIO32 connection
- TECHNICAL SPECIFICATIONS:
- • Deep sleep power consumption: <1mA
- • Setup timeout: 4 minutes
- • WiFi network: "ESP32-Motor-Timer"
- • WiFi password: "12345678"
- • Web interface: 192.168.4.1
- • Motor pin: GPIO33 (active HIGH)
- • LED pin: GPIO32 (active HIGH)
- • Reset pin: GPIO14 (trigger HIGH)
- • EEPROM backup: 512 bytes
- • Time zone: GMT+1 (Portugal)
- • Schedule resolution: 1 hour
- • Run time range: 5-300 seconds
- • Maximum daily activations: 24
- WIRING DIAGRAM:
- ESP32 GPIO33 → Motor/Relay IN (or transistor base)
- ESP32 GPIO32 → LED + Resistor → GND
- ESP32 GPIO14 → Reset Switch → 3.3V (normally open)
- ESP32 GND → Motor/LED GND
- ESP32 VIN → External power supply (if needed)
- USAGE EXAMPLES:
- • Irrigation system: Water plants 3 times daily for 30 seconds each
- • Feeding system: Dispense food every 4 hours for 10 seconds
- • Ventilation: Run fan for 2 minutes every hour during day
- • Lighting: Turn on grow lights for 15 minutes at specific times
- SAFETY NOTES:
- • Motor voltage must match ESP32 output capability (3.3V logic)
- • Use appropriate relay or transistor for high-power motors
- • Ensure adequate power supply for motor and ESP32
- • GPIO14 must be disconnected during normal operation
- • Deep sleep mode saves battery but requires wake-up triggers
- VERSION HISTORY:
- v1.0 - Basic timer functionality
- v2.0 - Added EEPROM backup and power failure recovery
- v3.0 - Added GPIO14 hardware reset functionality
- ========================================================================
- */
- #include "soc/soc.h" // Brownout error fix
- #include "soc/rtc_cntl_reg.h" // Brownout error fix
- #include "driver/rtc_io.h" // https://github.com/pycom/esp-idf-2.0/blob/master/components/driver/include/driver/rtc_io.h
- #include <ESP32Time.h>
- #include <WiFi.h>
- #include <EEPROM.h>
- #define uS_TO_S_FACTOR 1000000ULL
- #define DEFAULT_MOTOR_ON_TIME 5
- #define MIN_MOTOR_ON_TIME 5
- #define MAX_MOTOR_ON_TIME 300
- #define MOTOR_PIN 33
- #define LED_PIN 32
- #define WAKEUP_GPIO GPIO_NUM_14 // Only RTC IO are allowed
- #define EEPROM_SIZE 512
- // EEPROM Memory Map
- #define EEPROM_MAGIC_ADDR 0
- #define EEPROM_SCHEDULE_ADDR 4
- #define EEPROM_RUNTIME_ADDR 28
- #define EEPROM_LAST_SYNC_ADDR 32
- #define EEPROM_MAGIC_NUMBER 0xDEADBEEF
- #define MAX_TIME_STALENESS 7 * 24 * 3600
- const char *ssid = "ESP32-Motor-Timer";
- const char *password = "12345678";
- ESP32Time rtc(3600);
- WiFiServer server(80);
- // RTC memory variables
- RTC_DATA_ATTR bool timeWasSet = false;
- RTC_DATA_ATTR bool scheduleWasSet = false;
- RTC_DATA_ATTR bool runTimeWasSet = false;
- RTC_DATA_ATTR int bootCount = 0;
- RTC_DATA_ATTR unsigned long webServerStartTime = 0;
- RTC_DATA_ATTR bool usingBackupSettings = false;
- RTC_DATA_ATTR bool hourlySchedule[24] = {0};
- RTC_DATA_ATTR int motorRunTime = DEFAULT_MOTOR_ON_TIME;
- struct EEPROMSettings {
- uint32_t magic;
- bool schedule[24];
- int runTime;
- uint32_t lastSyncTime;
- };
- void setup() {
- Serial.begin(115200);
- delay(1000);
- EEPROM.begin(EEPROM_SIZE);
- bootCount++;
- Serial.println("Boot count: " + String(bootCount));
- pinMode(MOTOR_PIN, OUTPUT);
- pinMode(LED_PIN, OUTPUT);
- digitalWrite(MOTOR_PIN, LOW);
- printWakeupReason();
- // Check if woken up by GPIO14 - clear EEPROM and reset
- if (esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_EXT0) {
- Serial.println("=== GPIO RESET TRIGGERED ===");
- Serial.println("Clearing EEPROM settings...");
- clearEEPROMSettings();
- // Reset RTC memory flags
- timeWasSet = false;
- scheduleWasSet = false;
- runTimeWasSet = false;
- usingBackupSettings = false;
- Serial.println("EEPROM cleared - entering setup mode");
- webServerStartTime = millis();
- setupWebServer();
- return;
- }
- // Normal wakeup logic
- if (timeWasSet && scheduleWasSet && runTimeWasSet && esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_TIMER) {
- handleScheduledWakeup();
- } else {
- if (tryLoadBackupSettings()) {
- Serial.println("=== POWER FAILURE RECOVERY ===");
- Serial.println("Loaded backup settings from EEPROM");
- printCurrentSettings();
- timeWasSet = false;
- usingBackupSettings = true;
- EEPROMSettings settings;
- loadSettingsFromEEPROM(settings);
- uint32_t currentEpoch = settings.lastSyncTime + (millis() / 1000);
- rtc.setTime(currentEpoch);
- timeWasSet = true;
- Serial.println("Estimated time: " + rtc.getTime("%A, %B %d %Y %H:%M:%S"));
- scheduleNextWakeup();
- } else {
- Serial.println("No backup settings found - entering setup mode");
- webServerStartTime = millis();
- setupWebServer();
- }
- }
- }
- void loop() {
- if (!timeWasSet || !scheduleWasSet || !runTimeWasSet) {
- if (millis() - webServerStartTime > 240000) {
- if (tryLoadBackupSettings()) {
- Serial.println("Timeout reached - falling back to EEPROM settings");
- usingBackupSettings = true;
- EEPROMSettings settings;
- loadSettingsFromEEPROM(settings);
- uint32_t estimatedTime = settings.lastSyncTime + (millis() / 1000);
- rtc.setTime(estimatedTime);
- timeWasSet = true;
- scheduleNextWakeup();
- } else {
- Serial.println("No backup settings - entering 3-hour sleep");
- setupSleepWakeup();
- esp_deep_sleep_start();
- }
- }
- handleWebClient();
- }
- }
- void clearEEPROMSettings() {
- // Clear magic number to invalidate settings
- uint32_t clearMagic = 0x00000000;
- EEPROM.put(EEPROM_MAGIC_ADDR, clearMagic);
- EEPROM.commit();
- Serial.println("EEPROM settings cleared");
- }
- bool tryLoadBackupSettings() {
- EEPROMSettings settings;
- if (loadSettingsFromEEPROM(settings)) {
- for (int i = 0; i < 24; i++) {
- hourlySchedule[i] = settings.schedule[i];
- }
- motorRunTime = settings.runTime;
- scheduleWasSet = true;
- runTimeWasSet = true;
- return true;
- }
- return false;
- }
- bool loadSettingsFromEEPROM(EEPROMSettings &settings) {
- uint32_t magic;
- EEPROM.get(EEPROM_MAGIC_ADDR, magic);
- if (magic != EEPROM_MAGIC_NUMBER) {
- Serial.println("No valid EEPROM settings found");
- return false;
- }
- EEPROM.get(EEPROM_MAGIC_ADDR, settings.magic);
- for (int i = 0; i < 24; i++) {
- settings.schedule[i] = EEPROM.read(EEPROM_SCHEDULE_ADDR + i);
- }
- EEPROM.get(EEPROM_RUNTIME_ADDR, settings.runTime);
- EEPROM.get(EEPROM_LAST_SYNC_ADDR, settings.lastSyncTime);
- if (settings.runTime < MIN_MOTOR_ON_TIME || settings.runTime > MAX_MOTOR_ON_TIME) {
- Serial.println("Invalid run time in EEPROM: " + String(settings.runTime));
- return false;
- }
- Serial.println("Valid EEPROM settings loaded");
- return true;
- }
- void saveSettingsToEEPROM() {
- Serial.println("Saving settings to EEPROM...");
- EEPROMSettings settings;
- settings.magic = EEPROM_MAGIC_NUMBER;
- for (int i = 0; i < 24; i++) {
- settings.schedule[i] = hourlySchedule[i];
- }
- settings.runTime = motorRunTime;
- settings.lastSyncTime = rtc.getEpoch();
- EEPROM.put(EEPROM_MAGIC_ADDR, settings.magic);
- for (int i = 0; i < 24; i++) {
- EEPROM.write(EEPROM_SCHEDULE_ADDR + i, settings.schedule[i]);
- }
- EEPROM.put(EEPROM_RUNTIME_ADDR, settings.runTime);
- EEPROM.put(EEPROM_LAST_SYNC_ADDR, settings.lastSyncTime);
- EEPROM.commit();
- Serial.println("Settings saved to EEPROM successfully");
- printCurrentSettings();
- }
- void printCurrentSettings() {
- Serial.println("Current Settings:");
- Serial.println("- Motor run time: " + String(motorRunTime) + " seconds");
- Serial.print("- Schedule: ");
- int count = 0;
- for (int h = 0; h < 24; h++) {
- if (hourlySchedule[h]) {
- if (count > 0) Serial.print(", ");
- Serial.print(String(h < 10 ? "0" : "") + String(h) + ":00");
- count++;
- }
- }
- if (count == 0) {
- Serial.println("No hours scheduled");
- } else {
- Serial.println(" (" + String(count) + " times/day)");
- Serial.println("- Total daily runtime: " + String(count * motorRunTime) + " seconds");
- }
- }
- void handleScheduledWakeup() {
- Serial.println("\n=== Scheduled Wake-up ===");
- Serial.println("Current time: " + rtc.getTime("%A, %B %d %Y %H:%M:%S"));
- Serial.println("Motor run time: " + String(motorRunTime) + " seconds");
- if (usingBackupSettings) {
- Serial.println("Running on backup settings from EEPROM");
- }
- runMotor();
- scheduleNextWakeup();
- }
- void runMotor() {
- Serial.println("Starting motor for " + String(motorRunTime) + " seconds...");
- digitalWrite(LED_PIN, HIGH);
- digitalWrite(MOTOR_PIN, HIGH);
- for (int i = motorRunTime; i > 0; i--) {
- Serial.println("Motor running... " + String(i) + "s remaining");
- delay(1000);
- int blinkInterval = (motorRunTime < 10) ? 1 : 5;
- if (i % blinkInterval == 0) {
- digitalWrite(LED_PIN, LOW);
- delay(100);
- digitalWrite(LED_PIN, HIGH);
- }
- }
- digitalWrite(MOTOR_PIN, LOW);
- digitalWrite(LED_PIN, LOW);
- Serial.println("Motor stopped.");
- }
- void setupSleepWakeup() {
- // Setup timer wakeup
- esp_sleep_enable_timer_wakeup(3 * 3600 * uS_TO_S_FACTOR);
- // Setup GPIO wakeup for reset functionality
- esp_sleep_enable_ext0_wakeup(WAKEUP_GPIO, 1); // 1 = High trigger
- rtc_gpio_pullup_dis(WAKEUP_GPIO);
- rtc_gpio_pulldown_en(WAKEUP_GPIO);
- Serial.println("Setup ESP32 to wake up on GPIO14 trigger (reset mode)");
- }
- void scheduleNextWakeup() {
- struct tm timeinfo = rtc.getTimeStruct();
- int currentHour = timeinfo.tm_hour;
- int currentMinute = timeinfo.tm_min;
- unsigned long sleepTime = calculateSleepTime(currentHour, currentMinute);
- if (sleepTime > 0) {
- Serial.println("Next wake-up in " + String(sleepTime / 3600) + " hours and " +
- String((sleepTime % 3600) / 60) + " minutes");
- esp_sleep_enable_timer_wakeup(sleepTime * uS_TO_S_FACTOR);
- // Always enable GPIO wakeup for reset functionality
- esp_sleep_enable_ext0_wakeup(WAKEUP_GPIO, 1);
- rtc_gpio_pullup_dis(WAKEUP_GPIO);
- rtc_gpio_pulldown_en(WAKEUP_GPIO);
- Serial.println("Entering deep sleep... (GPIO14 HIGH = reset)");
- Serial.flush();
- esp_deep_sleep_start();
- } else {
- Serial.println("No scheduled hours found - entering 24-hour sleep");
- esp_sleep_enable_timer_wakeup(24 * 3600 * uS_TO_S_FACTOR);
- esp_sleep_enable_ext0_wakeup(WAKEUP_GPIO, 1);
- rtc_gpio_pullup_dis(WAKEUP_GPIO);
- rtc_gpio_pulldown_en(WAKEUP_GPIO);
- esp_deep_sleep_start();
- }
- }
- unsigned long calculateSleepTime(int currentHour, int currentMinute) {
- int nextHour = -1;
- for (int h = currentHour + 1; h < 24; h++) {
- if (hourlySchedule[h]) {
- nextHour = h;
- break;
- }
- }
- if (nextHour == -1) {
- for (int h = 0; h < 24; h++) {
- if (hourlySchedule[h]) {
- nextHour = h + 24;
- break;
- }
- }
- }
- if (nextHour == -1) return 0;
- int currentTotalMinutes = currentHour * 60 + currentMinute;
- int nextTotalMinutes = (nextHour % 24) * 60;
- if (nextHour >= 24) {
- nextTotalMinutes += 24 * 60;
- }
- int sleepMinutes = nextTotalMinutes - currentTotalMinutes;
- return sleepMinutes * 60;
- }
- void setupWebServer() {
- Serial.println("\n=== Setting up Web Server for Configuration ===");
- Serial.println("Connect to WiFi network: " + String(ssid));
- Serial.println("Password: " + String(password));
- Serial.println("TIMEOUT: 4 minutes");
- Serial.println("RESET: Connect GPIO14 to 3.3V and reset ESP32");
- WiFi.softAP(ssid, password);
- IPAddress IP = WiFi.softAPIP();
- Serial.println("Web interface: http://" + IP.toString());
- server.begin();
- for (int i = 0; i < 10; i++) {
- digitalWrite(LED_PIN, HIGH);
- delay(200);
- digitalWrite(LED_PIN, LOW);
- delay(200);
- }
- }
- void handleWebClient() {
- WiFiClient client = server.available();
- if (client) {
- Serial.println("Client connected");
- String currentLine = "";
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- if (c == '\n') {
- if (currentLine.length() == 0) {
- sendWebPage(client);
- break;
- } else {
- currentLine = "";
- }
- } else if (c != '\r') {
- currentLine += c;
- }
- if (currentLine.endsWith("POST /syncTime")) {
- handleTimeSyncRequest(client);
- } else if (currentLine.endsWith("POST /setRunTime")) {
- handleRunTimeRequest(client);
- } else if (currentLine.endsWith("POST /setSchedule")) {
- handleScheduleRequest(client);
- }
- }
- }
- client.stop();
- }
- }
- // Simplified web page (keeping core functionality)
- void sendWebPage(WiFiClient &client) {
- unsigned long elapsed = millis() - webServerStartTime;
- unsigned long remaining = (240000 - elapsed) / 1000;
- client.println("HTTP/1.1 200 OK");
- client.println("Content-type:text/html");
- client.println();
- client.println("<!DOCTYPE html><html>");
- client.println("<head><title>ESP32 Motor Timer Setup</title>");
- client.println("<meta name='viewport' content='width=device-width, initial-scale=1'>");
- client.println("<style>");
- client.println("body{font-family:Arial;text-align:center;padding:20px;background:#f0f0f0;}");
- client.println(".container{max-width:600px;margin:0 auto;background:white;padding:30px;border-radius:10px;}");
- client.println("input,button{padding:10px;margin:5px;font-size:16px;border:1px solid #ddd;border-radius:5px;}");
- client.println("button{background:#4CAF50;color:white;border:none;cursor:pointer;padding:15px 30px;}");
- client.println(".timeout{color:red;font-weight:bold;}");
- client.println(".step{margin:20px 0;padding:15px;background:#f9f9f9;border-radius:5px;}");
- client.println(".schedule-grid{display:grid;grid-template-columns:repeat(4,1fr);gap:10px;margin:20px 0;}");
- client.println(".hour-checkbox{display:flex;align-items:center;padding:8px;background:white;border:1px solid #ddd;border-radius:5px;}");
- client.println(".reset-info{background:#FFE6E6;padding:10px;border-radius:5px;margin:10px 0;border:1px solid #FF9999;}");
- client.println("</style></head>");
- client.println("<body>");
- client.println("<div class='container'>");
- client.println("<h1>ESP32 Motor Timer Setup</h1>");
- client.println("<div class='reset-info'>");
- client.println("<strong>🔄 Reset Instructions:</strong><br>");
- client.println("To clear settings: Connect GPIO14 to 3.3V and reset ESP32");
- client.println("</div>");
- client.println("<div class='timeout'>Timeout: <span id='countdown'>" + String(remaining) + "</span> seconds</div>");
- // Step 1: Time sync
- client.println("<div class='step'>");
- client.println("<h3>Step 1: Set Current Time</h3>");
- if (timeWasSet) {
- client.println("<p>✅ Time set: " + rtc.getTime("%H:%M:%S %d/%m/%Y") + "</p>");
- } else {
- client.println("<h4 id='currentTime'></h4>");
- client.println("<form action='/syncTime' method='POST'>");
- client.println("<input type='hidden' name='epochTime' id='hiddenEpochTime'>");
- client.println("<button type='submit'>Sync Time</button>");
- client.println("</form>");
- }
- client.println("</div>");
- // Step 2: Motor run time
- client.println("<div class='step'>");
- client.println("<h3>Step 2: Set Motor Run Time</h3>");
- if (runTimeWasSet) {
- client.println("<p>✅ Run time set: " + String(motorRunTime) + " seconds</p>");
- } else {
- client.println("<form action='/setRunTime' method='POST'>");
- client.println("<input type='range' name='runTime' min='5' max='300' value='" + String(motorRunTime) + "'>");
- client.println("<span id='runTimeDisplay'>" + String(motorRunTime) + "s</span>");
- client.println("<button type='submit'>Set Run Time</button>");
- client.println("</form>");
- }
- client.println("</div>");
- // Step 3: Schedule
- client.println("<div class='step'>");
- client.println("<h3>Step 3: Select Operation Hours</h3>");
- if (scheduleWasSet) {
- client.println("<p>✅ Schedule set!</p>");
- } else {
- client.println("<form action='/setSchedule' method='POST'>");
- client.println("<div class='schedule-grid'>");
- for (int h = 0; h < 24; h++) {
- client.println("<div class='hour-checkbox'>");
- client.println("<input type='checkbox' name='hour" + String(h) + "' value='1'>");
- client.println("<label>" + String(h < 10 ? "0" : "") + String(h) + ":00</label>");
- client.println("</div>");
- }
- client.println("</div>");
- client.println("<button type='submit'>Set Schedule</button>");
- client.println("</form>");
- }
- client.println("</div>");
- if (timeWasSet && scheduleWasSet && runTimeWasSet) {
- client.println("<div class='step'>");
- client.println("<h3>✅ Setup Complete!</h3>");
- client.println("<p>ESP32 will now enter scheduled operation mode.</p>");
- client.println("<button onclick='startOperation()'>Start Operation</button>");
- client.println("</div>");
- }
- client.println("</div>");
- // Simplified JavaScript
- client.println("<script>");
- client.println("var countdown=" + String(remaining) + ";");
- client.println("function updateTime(){");
- client.println("var now=new Date();");
- client.println("if(document.getElementById('currentTime'))");
- client.println("document.getElementById('currentTime').innerHTML='Current Time: '+now.toLocaleString();");
- client.println("if(document.getElementById('hiddenEpochTime'))");
- client.println("document.getElementById('hiddenEpochTime').value=Math.floor(now.getTime()/1000);");
- client.println("}");
- client.println("function updateCountdown(){");
- client.println("countdown--;document.getElementById('countdown').innerHTML=countdown;");
- client.println("if(countdown<=0)document.body.innerHTML='<h2>Timeout reached</h2>';");
- client.println("}");
- client.println("function startOperation(){");
- client.println("document.body.innerHTML='<h2>Starting operation...</h2>';");
- client.println("}");
- client.println("setInterval(updateTime,1000);setInterval(updateCountdown,1000);updateTime();");
- client.println("</script></body></html>");
- }
- void handleTimeSyncRequest(WiFiClient &client) {
- String requestBody = "";
- while (client.available()) {
- requestBody += (char)client.read();
- }
- int epochIndex = requestBody.indexOf("epochTime=");
- if (epochIndex != -1) {
- long epochTime = requestBody.substring(epochIndex + 10).toInt();
- rtc.setTime(epochTime);
- timeWasSet = true;
- Serial.println("Time synchronized: " + rtc.getTime("%A, %B %d %Y %H:%M:%S"));
- }
- client.println("HTTP/1.1 302 Found");
- client.println("Location: /");
- client.println();
- }
- void handleRunTimeRequest(WiFiClient &client) {
- String requestBody = "";
- while (client.available()) {
- requestBody += (char)client.read();
- }
- int runTimeIndex = requestBody.indexOf("runTime=");
- if (runTimeIndex != -1) {
- int newRunTime = requestBody.substring(runTimeIndex + 8).toInt();
- if (newRunTime >= MIN_MOTOR_ON_TIME && newRunTime <= MAX_MOTOR_ON_TIME) {
- motorRunTime = newRunTime;
- runTimeWasSet = true;
- Serial.println("Motor run time set to: " + String(motorRunTime) + " seconds");
- }
- }
- client.println("HTTP/1.1 302 Found");
- client.println("Location: /");
- client.println();
- }
- void handleScheduleRequest(WiFiClient &client) {
- String requestBody = "";
- while (client.available()) {
- requestBody += (char)client.read();
- }
- for (int h = 0; h < 24; h++) {
- hourlySchedule[h] = false;
- }
- for (int h = 0; h < 24; h++) {
- String hourParam = "hour" + String(h) + "=1";
- if (requestBody.indexOf(hourParam) != -1) {
- hourlySchedule[h] = true;
- }
- }
- scheduleWasSet = true;
- // Save to EEPROM when configuration is complete
- if (timeWasSet && scheduleWasSet && runTimeWasSet) {
- saveSettingsToEEPROM();
- delay(2000);
- scheduleNextWakeup();
- }
- client.println("HTTP/1.1 302 Found");
- client.println("Location: /");
- client.println();
- }
- void printWakeupReason() {
- esp_sleep_wakeup_cause_t wakeup_reason = esp_sleep_get_wakeup_cause();
- switch (wakeup_reason) {
- case ESP_SLEEP_WAKEUP_TIMER:
- Serial.println("Wake-up: Scheduled timer");
- break;
- case ESP_SLEEP_WAKEUP_EXT0:
- Serial.println("Wake-up: GPIO14 external signal (RESET MODE)");
- break;
- default:
- Serial.println("Wake-up: Power on or reset");
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement