pleasedontcode

# Relay Controller rev_03

Feb 5th, 2026
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 21.47 KB | None | 0 0
  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: # Relay Controller
  13.     - Source Code NOT compiled for: ESP8266 NodeMCU V1.0
  14.     - Source Code created on: 2026-02-05 14:55:51
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* add i2c gpio expander PCF8574 */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23.  
  24. #include "PCF8574.h"
  25. #include "Relay.h"
  26. #include "VoiceAssistant.h"
  27.  
  28. // Create PCF8574 instance with default I2C address 0x20
  29. // Address range: 0x20 to 0x27 depending on A0, A1, A2 pins
  30. PCF8574 expander(0x20);
  31.  
  32. // Create Relay instance for managing 8 relays
  33. Relay relayController(&expander, 8);
  34.  
  35. // Create VoiceAssistant instance
  36. VoiceAssistant* voiceAssistant = nullptr;
  37.  
  38. // WiFi Configuration
  39. // IMPORTANT: Replace these with your actual WiFi credentials
  40. const char* WIFI_SSID = "YourWiFiSSID";
  41. const char* WIFI_PASSWORD = "YourWiFiPassword";
  42.  
  43. // Google Cloud Speech-to-Text API Configuration
  44. // IMPORTANT: Replace these with your actual Google Cloud credentials
  45. const char* GOOGLE_API_KEY = "YOUR_GOOGLE_CLOUD_API_KEY_HERE";
  46. const char* GOOGLE_PROJECT_ID = "YOUR_GOOGLE_PROJECT_ID_HERE";
  47.  
  48. // Serial command buffer
  49. String commandBuffer = "";
  50.  
  51. // Timestamp for periodic status monitoring
  52. unsigned long lastStatusTime = 0;
  53. unsigned long statusInterval = 5000;  // Print status every 5 seconds
  54.  
  55. // Voice command request flag
  56. volatile bool voiceCommandRequested = false;
  57. unsigned long voiceCommandRequestTime = 0;
  58. const unsigned long voiceCommandDebounce = 1000;  // Debounce time for voice command requests
  59.  
  60. // Relay state change log
  61. struct RelayLog {
  62.   uint32_t timestamp;
  63.   uint8_t relayNum;
  64.   bool state;  // true = ON, false = OFF
  65.   char action[20];
  66. };
  67.  
  68. // Log buffer for relay state changes (last 10 changes)
  69. RelayLog relayLogs[10];
  70. uint8_t logIndex = 0;
  71.  
  72. /****** FUNCTION PROTOTYPES *****/
  73. void setup(void);
  74. void loop(void);
  75. void processSerialCommand(String command);
  76. void handleRelayCommand(String command);
  77. void printAvailableCommands(void);
  78. void logRelayStateChange(uint8_t relayNum, bool newState, const char* action);
  79. void printRelayLogs(void);
  80. void printRelayStatusMonitoring(void);
  81. void processVoiceCommand(void);
  82. void initializeVoiceAssistant(void);
  83.  
  84. void setup(void)
  85. {
  86.   // Initialize Serial communication
  87.   Serial.begin(115200);
  88.   delay(100);
  89.  
  90.   Serial.println("\n\n================================================");
  91.   Serial.println("     PCF8574 I2C GPIO Expander with Relays     ");
  92.   Serial.println("         + Voice Assistant Support              ");
  93.   Serial.println("================================================");
  94.   Serial.println();
  95.  
  96.   // Initialize PCF8574 with I2C address 0x20
  97.   // Using default ESP8266 pins: SDA=4 (D2), SCL=5 (D1)
  98.   Serial.println("Initializing PCF8574 I2C GPIO Expander...");
  99.  
  100.   if (expander.begin()) {
  101.     Serial.println("[SUCCESS] PCF8574 initialized successfully!");
  102.     Serial.print("[INFO] I2C Address: 0x");
  103.     Serial.println(0x20, HEX);
  104.     Serial.println("[INFO] I2C Pins: SDA=4 (D2), SCL=5 (D1)");
  105.    
  106.     // Initialize all relay pins to HIGH (default/inactive state)
  107.     expander.writePort(0xFF);
  108.     Serial.println("[INFO] All relay pins set to HIGH (inactive)");
  109.    
  110.     delay(500);
  111.    
  112.     // Initialize Relay controller
  113.     Serial.println();
  114.     Serial.println("Initializing Relay Controller...");
  115.     relayController.init();
  116.    
  117.     Serial.println();
  118.     Serial.println("[SUCCESS] System fully initialized and ready!");
  119.     Serial.println();
  120.    
  121.     // Initialize Voice Assistant with WiFi and Google Cloud API
  122.     delay(1000);
  123.     Serial.println();
  124.     initializeVoiceAssistant();
  125.    
  126.     delay(1000);
  127.    
  128.     // Print help information
  129.     printAvailableCommands();
  130.    
  131.     // Initialize log timestamps
  132.     lastStatusTime = millis();
  133.    
  134.   } else {
  135.     Serial.println("[ERROR] Failed to initialize PCF8574!");
  136.     Serial.println("[ERROR] Please check:");
  137.     Serial.println("  - I2C connections (SDA=D2/GPIO4, SCL=D1/GPIO5)");
  138.     Serial.println("  - PCF8574 I2C address (default 0x20, check A0/A1/A2 pins)");
  139.     Serial.println("  - PCF8574 power supply (VCC and GND)");
  140.     Serial.println();
  141.     Serial.println("System will continue but relay control may not work.");
  142.   }
  143.  
  144.   delay(1000);
  145. }
  146.  
  147.  
  148. void loop(void)
  149. {
  150.   // Check for serial commands
  151.   if (Serial.available()) {
  152.     char inChar = Serial.read();
  153.    
  154.     // Handle enter key (carriage return or newline)
  155.     if (inChar == '\r' || inChar == '\n') {
  156.       if (commandBuffer.length() > 0) {
  157.         Serial.println();  // Echo newline
  158.         Serial.print("[COMMAND] ");
  159.         Serial.println(commandBuffer);
  160.         processSerialCommand(commandBuffer);
  161.         commandBuffer = "";  // Clear buffer
  162.       }
  163.     }
  164.     // Handle backspace
  165.     else if (inChar == '\b') {
  166.       if (commandBuffer.length() > 0) {
  167.         commandBuffer.remove(commandBuffer.length() - 1);
  168.         Serial.print(inChar);  // Echo backspace
  169.       }
  170.     }
  171.     // Add character to buffer
  172.     else if (inChar >= 32 && inChar <= 126) {  // Printable ASCII characters
  173.       commandBuffer += inChar;
  174.       Serial.print(inChar);  // Echo character
  175.     }
  176.   }
  177.  
  178.   // Check if voice command was requested via serial
  179.   if (voiceCommandRequested && (millis() - voiceCommandRequestTime >= voiceCommandDebounce)) {
  180.     voiceCommandRequested = false;
  181.     processVoiceCommand();
  182.   }
  183.  
  184.   // Periodic status monitoring and logging
  185.   if (millis() - lastStatusTime >= statusInterval) {
  186.     lastStatusTime = millis();
  187.     printRelayStatusMonitoring();
  188.   }
  189.  
  190.   // Small delay to prevent watchdog timeout
  191.   delay(10);
  192. }
  193.  
  194.  
  195. // Initialize VoiceAssistant with WiFi and Google Cloud API credentials
  196. void initializeVoiceAssistant(void) {
  197.   Serial.println("=== Initializing Voice Assistant System ===");
  198.  
  199.   // Create VoiceAssistant instance
  200.   voiceAssistant = new VoiceAssistant(&relayController);
  201.  
  202.   if (voiceAssistant == nullptr) {
  203.     Serial.println("[ERROR] Failed to allocate memory for VoiceAssistant!");
  204.     return;
  205.   }
  206.  
  207.   // Begin voice assistant initialization with WiFi and API credentials
  208.   if (voiceAssistant->begin(WIFI_SSID, WIFI_PASSWORD, GOOGLE_API_KEY, GOOGLE_PROJECT_ID)) {
  209.     Serial.println("[SUCCESS] Voice Assistant initialized successfully!");
  210.    
  211.     // Configure audio settings (16kHz, Mono, 16-bit, 10 second timeout)
  212.     voiceAssistant->setAudioConfig(16000, 1, 16, 10000);
  213.    
  214.     // Print voice assistant status
  215.     voiceAssistant->printStatus();
  216.     voiceAssistant->printConfiguration();
  217.    
  218.     Serial.println("=== Voice Assistant Ready ===");
  219.     Serial.println("[INFO] Voice commands are now available!");
  220.     Serial.println("[INFO] Type 'voice' command to process voice commands");
  221.     Serial.println("[INFO] Type 'vtest <command>' to test voice parsing");
  222.     Serial.println();
  223.   } else {
  224.     Serial.println("[ERROR] Failed to initialize Voice Assistant!");
  225.     Serial.println("[ERROR] WiFi connection failed or API credentials invalid");
  226.     Serial.println("[INFO] System will continue with relay control only");
  227.    
  228.     // Clean up on failure
  229.     delete voiceAssistant;
  230.     voiceAssistant = nullptr;
  231.   }
  232.  
  233.   Serial.println("========================================");
  234.   Serial.println();
  235. }
  236.  
  237.  
  238. // Process voice command from microphone
  239. void processVoiceCommand(void) {
  240.   if (voiceAssistant == nullptr) {
  241.     Serial.println("[ERROR] Voice Assistant not initialized!");
  242.     return;
  243.   }
  244.  
  245.   if (!voiceAssistant->isInitialized()) {
  246.     Serial.println("[ERROR] Voice Assistant not ready!");
  247.     return;
  248.   }
  249.  
  250.   Serial.println();
  251.   Serial.println("================================================");
  252.   Serial.println("         Starting Voice Command Processing       ");
  253.   Serial.println("================================================");
  254.  
  255.   // Process voice command from microphone
  256.   VoiceCommandResult result = voiceAssistant->processVoiceCommand();
  257.  
  258.   Serial.println();
  259.   Serial.print("[VOICE] Processing Result: ");
  260.  
  261.   switch(result) {
  262.     case VOICE_CMD_SUCCESS:
  263.       Serial.println("SUCCESS");
  264.       Serial.println("[VOICE] Command executed successfully!");
  265.       break;
  266.     case VOICE_CMD_WIFI_NOT_CONNECTED:
  267.       Serial.println("ERROR - WiFi Not Connected");
  268.       Serial.println("[VOICE] Attempting to reconnect to WiFi...");
  269.       voiceAssistant->reconnectWiFi();
  270.       break;
  271.     case VOICE_CMD_API_ERROR:
  272.       Serial.println("ERROR - Google Cloud API Error");
  273.       Serial.println("[VOICE] Failed to communicate with Google Cloud Speech-to-Text API");
  274.       break;
  275.     case VOICE_CMD_AUDIO_CAPTURE_FAILED:
  276.       Serial.println("ERROR - Audio Capture Failed");
  277.       Serial.println("[VOICE] Failed to capture audio from microphone");
  278.       break;
  279.     case VOICE_CMD_PARSE_FAILED:
  280.       Serial.println("ERROR - Command Parse Failed");
  281.       Serial.println("[VOICE] Could not parse the recognized speech into a valid command");
  282.       break;
  283.     case VOICE_CMD_INVALID_RELAY:
  284.       Serial.println("ERROR - Invalid Relay Number");
  285.       Serial.println("[VOICE] The recognized relay number is out of range (0-7)");
  286.       break;
  287.     case VOICE_CMD_TIMEOUT:
  288.       Serial.println("ERROR - Command Timeout");
  289.       Serial.println("[VOICE] Voice command processing timed out");
  290.       break;
  291.     case VOICE_CMD_BUSY:
  292.       Serial.println("ERROR - System Busy");
  293.       Serial.println("[VOICE] Voice Assistant is currently busy processing a command");
  294.       break;
  295.     default:
  296.       Serial.println("UNKNOWN ERROR");
  297.       break;
  298.   }
  299.  
  300.   Serial.println();
  301.   Serial.println("================================================");
  302.   Serial.println();
  303.  
  304.   // Print current relay status
  305.   relayController.printStatus();
  306.  
  307.   // Print voice command log
  308.   voiceAssistant->printVoiceCommandLog();
  309. }
  310.  
  311.  
  312. // Process serial commands from user
  313. void processSerialCommand(String command) {
  314.   // Convert to lowercase for case-insensitive handling
  315.   command.toLowerCase();
  316.  
  317.   // Trim whitespace
  318.   command.trim();
  319.  
  320.   // Empty command
  321.   if (command.length() == 0) {
  322.     return;
  323.   }
  324.  
  325.   // Help command
  326.   if (command == "help" || command == "?") {
  327.     printAvailableCommands();
  328.     return;
  329.   }
  330.  
  331.   // Status command - print current relay states
  332.   if (command == "status" || command == "s") {
  333.     Serial.println();
  334.     relayController.printStatus();
  335.     Serial.println();
  336.     return;
  337.   }
  338.  
  339.   // Logs command - print relay change logs
  340.   if (command == "logs" || command == "l") {
  341.     Serial.println();
  342.     printRelayLogs();
  343.     Serial.println();
  344.     return;
  345.   }
  346.  
  347.   // Clear logs command
  348.   if (command == "clrlogs" || command == "cl") {
  349.     logIndex = 0;
  350.     Serial.println("[INFO] Relay logs cleared");
  351.     Serial.println();
  352.     return;
  353.   }
  354.  
  355.   // Voice Assistant commands
  356.   if (voiceAssistant != nullptr) {
  357.     // Voice command - process audio from microphone
  358.     if (command == "voice" || command == "v") {
  359.       voiceCommandRequested = true;
  360.       voiceCommandRequestTime = millis();
  361.       return;
  362.     }
  363.    
  364.     // Voice test command - test voice parsing with a string
  365.     if (command.startsWith("vtest ")) {
  366.       String testCommand = command.substring(6);
  367.       testCommand.trim();
  368.      
  369.       if (testCommand.length() > 0) {
  370.         Serial.println();
  371.         Serial.println("=== Testing Voice Command Parsing ===");
  372.         Serial.print("[VOICE] Test command: ");
  373.         Serial.println(testCommand);
  374.        
  375.         VoiceCommandResult result = voiceAssistant->executeCommandFromString(testCommand);
  376.        
  377.         Serial.println();
  378.         Serial.print("[VOICE] Parse result: ");
  379.         switch(result) {
  380.           case VOICE_CMD_SUCCESS:
  381.             Serial.println("SUCCESS");
  382.             break;
  383.           case VOICE_CMD_PARSE_FAILED:
  384.             Serial.println("PARSE FAILED");
  385.             break;
  386.           case VOICE_CMD_INVALID_RELAY:
  387.             Serial.println("INVALID RELAY");
  388.             break;
  389.           default:
  390.             Serial.println("ERROR");
  391.             break;
  392.         }
  393.        
  394.         Serial.println("[VOICE] ====================================");
  395.         Serial.println();
  396.        
  397.         relayController.printStatus();
  398.       } else {
  399.         Serial.println("[ERROR] Please provide a test command. Usage: vtest <command>");
  400.       }
  401.       return;
  402.     }
  403.    
  404.     // Voice command log command
  405.     if (command == "vlogs" || command == "vl") {
  406.       voiceAssistant->printVoiceCommandLog();
  407.       return;
  408.     }
  409.    
  410.     // Clear voice command logs
  411.     if (command == "vclrlogs" || command == "vcl") {
  412.       voiceAssistant->clearVoiceCommandLog();
  413.       Serial.println();
  414.       return;
  415.     }
  416.    
  417.     // Voice assistant status command
  418.     if (command == "vstatus" || command == "vs") {
  419.       voiceAssistant->printStatus();
  420.       return;
  421.     }
  422.    
  423.     // Voice assistant configuration command
  424.     if (command == "vconfig" || command == "vc") {
  425.       voiceAssistant->printConfiguration();
  426.       return;
  427.     }
  428.   }
  429.  
  430.   // Turn off all relays
  431.   if (command == "alloff" || command == "off") {
  432.     Serial.println();
  433.     relayController.turnOffAll();
  434.     logRelayStateChange(255, false, "ALL OFF");
  435.     relayController.printStatus();
  436.     Serial.println();
  437.     return;
  438.   }
  439.  
  440.   // Test mode - cycle through all relays
  441.   if (command == "test" || command == "t") {
  442.     Serial.println();
  443.     Serial.println("[TEST] Starting relay test sequence...");
  444.     Serial.println("[TEST] Turning ON each relay for 1 second...");
  445.     Serial.println();
  446.    
  447.     for (uint8_t i = 0; i < 8; i++) {
  448.       Serial.print("[TEST] Testing Relay ");
  449.       Serial.println(i);
  450.       relayController.turnOn(i);
  451.       delay(1000);
  452.       relayController.turnOff(i);
  453.       delay(200);
  454.     }
  455.    
  456.     Serial.println();
  457.     Serial.println("[TEST] Relay test sequence completed!");
  458.     Serial.println();
  459.     return;
  460.   }
  461.  
  462.   // Relay control commands
  463.   if (command.startsWith("on ") || command.startsWith("off ") ||
  464.       command.startsWith("toggle ") || command.startsWith("tog ")) {
  465.     handleRelayCommand(command);
  466.     return;
  467.   }
  468.  
  469.   // Check for single digit commands (quick on/off)
  470.   if (command.length() == 1 && isDigit(command[0])) {
  471.     uint8_t relayNum = command[0] - '0';
  472.     if (relayNum < 8) {
  473.       Serial.println();
  474.       relayController.toggle(relayNum);
  475.       logRelayStateChange(relayNum, relayController.getState(relayNum), "TOGGLE");
  476.       relayController.printStatus();
  477.       Serial.println();
  478.       return;
  479.     }
  480.   }
  481.  
  482.   // Unknown command
  483.   Serial.println("[ERROR] Unknown command. Type 'help' for available commands.");
  484.   Serial.println();
  485. }
  486.  
  487.  
  488. // Handle relay-specific commands (on/off/toggle)
  489. void handleRelayCommand(String command) {
  490.   String action = "";
  491.   uint8_t relayNum = 255;
  492.  
  493.   Serial.println();
  494.  
  495.   // Parse "on" commands
  496.   if (command.startsWith("on ")) {
  497.     action = "ON";
  498.     command = command.substring(3);
  499.     command.trim();
  500.    
  501.     if (command.length() > 0 && isDigit(command[0])) {
  502.       relayNum = command[0] - '0';
  503.     }
  504.   }
  505.   // Parse "off" commands
  506.   else if (command.startsWith("off ")) {
  507.     action = "OFF";
  508.     command = command.substring(4);
  509.     command.trim();
  510.    
  511.     if (command.length() > 0 && isDigit(command[0])) {
  512.       relayNum = command[0] - '0';
  513.     }
  514.   }
  515.   // Parse "toggle" or "tog" commands
  516.   else if (command.startsWith("toggle ")) {
  517.     action = "TOGGLE";
  518.     command = command.substring(7);
  519.     command.trim();
  520.    
  521.     if (command.length() > 0 && isDigit(command[0])) {
  522.       relayNum = command[0] - '0';
  523.     }
  524.   }
  525.   else if (command.startsWith("tog ")) {
  526.     action = "TOGGLE";
  527.     command = command.substring(4);
  528.     command.trim();
  529.    
  530.     if (command.length() > 0 && isDigit(command[0])) {
  531.       relayNum = command[0] - '0';
  532.     }
  533.   }
  534.  
  535.   // Execute command if relay number is valid
  536.   if (relayNum < 8) {
  537.     if (action == "ON") {
  538.       relayController.turnOn(relayNum);
  539.       logRelayStateChange(relayNum, true, "ON");
  540.     }
  541.     else if (action == "OFF") {
  542.       relayController.turnOff(relayNum);
  543.       logRelayStateChange(relayNum, false, "OFF");
  544.     }
  545.     else if (action == "TOGGLE") {
  546.       relayController.toggle(relayNum);
  547.       logRelayStateChange(relayNum, relayController.getState(relayNum), "TOGGLE");
  548.     }
  549.    
  550.     relayController.printStatus();
  551.   } else {
  552.     Serial.print("[ERROR] Invalid relay number. Use 0-7. Got: ");
  553.     Serial.println(command);
  554.   }
  555.  
  556.   Serial.println();
  557. }
  558.  
  559.  
  560. // Print available commands
  561. void printAvailableCommands(void) {
  562.   Serial.println();
  563.   Serial.println("================================================");
  564.   Serial.println("           Available Serial Commands            ");
  565.   Serial.println("================================================");
  566.   Serial.println();
  567.   Serial.println("Relay Control Commands:");
  568.   Serial.println("  on <n>      - Turn ON relay n (0-7)");
  569.   Serial.println("  off <n>     - Turn OFF relay n (0-7)");
  570.   Serial.println("  toggle <n>  - Toggle relay n (0-7)");
  571.   Serial.println("  tog <n>     - Toggle relay n (0-7) [shorthand]");
  572.   Serial.println("  <n>         - Quick toggle relay n (0-7)");
  573.   Serial.println("  alloff      - Turn OFF all relays");
  574.   Serial.println("  off         - Turn OFF all relays [shorthand]");
  575.   Serial.println();
  576.   Serial.println("Voice Assistant Commands (requires WiFi & API credentials):");
  577.   Serial.println("  voice, v    - Process voice command from microphone");
  578.   Serial.println("  vtest <cmd> - Test voice parsing with command string");
  579.   Serial.println("  vlogs, vl   - Display voice command logs");
  580.   Serial.println("  vclrlogs,vc - Clear voice command logs");
  581.   Serial.println("  vstatus, vs - Display voice assistant status");
  582.   Serial.println("  vconfig, vc - Display voice assistant configuration");
  583.   Serial.println();
  584.   Serial.println("System Commands:");
  585.   Serial.println("  status, s   - Display current relay status");
  586.   Serial.println("  logs, l     - Display relay change log");
  587.   Serial.println("  clrlogs, cl - Clear relay change log");
  588.   Serial.println("  test, t     - Run relay test sequence");
  589.   Serial.println("  help, ?     - Display this help message");
  590.   Serial.println();
  591.   Serial.println("Example: Type 'on 0' to turn ON relay 0");
  592.   Serial.println("Example: Type 'vtest turn on relay 3' to test voice parsing");
  593.   Serial.println("================================================");
  594.   Serial.println();
  595. }
  596.  
  597.  
  598. // Log relay state changes
  599. void logRelayStateChange(uint8_t relayNum, bool newState, const char* action) {
  600.   // Special handling for "ALL OFF" action
  601.   if (relayNum == 255) {
  602.     // Log all relays being turned off
  603.     for (uint8_t i = 0; i < 8; i++) {
  604.       RelayLog& log = relayLogs[logIndex % 10];
  605.       log.timestamp = millis();
  606.       log.relayNum = i;
  607.       log.state = false;
  608.       strncpy(log.action, action, sizeof(log.action) - 1);
  609.       log.action[sizeof(log.action) - 1] = '\0';
  610.       logIndex++;
  611.     }
  612.   } else {
  613.     RelayLog& log = relayLogs[logIndex % 10];
  614.     log.timestamp = millis();
  615.     log.relayNum = relayNum;
  616.     log.state = newState;
  617.     strncpy(log.action, action, sizeof(log.action) - 1);
  618.     log.action[sizeof(log.action) - 1] = '\0';
  619.     logIndex++;
  620.   }
  621. }
  622.  
  623.  
  624. // Print relay state change logs
  625. void printRelayLogs(void) {
  626.   Serial.println("=== Relay Change Log (Last 10 changes) ===");
  627.  
  628.   if (logIndex == 0) {
  629.     Serial.println("No relay changes recorded yet.");
  630.   } else {
  631.     // Calculate start index for circular buffer
  632.     uint8_t startIdx = (logIndex >= 10) ? (logIndex % 10) : 0;
  633.     uint8_t count = (logIndex >= 10) ? 10 : logIndex;
  634.    
  635.     for (uint8_t i = 0; i < count; i++) {
  636.       uint8_t idx = (startIdx + i) % 10;
  637.       RelayLog& log = relayLogs[idx];
  638.      
  639.       Serial.print("[");
  640.       Serial.print(log.timestamp / 1000);
  641.       Serial.print("s] Relay ");
  642.       Serial.print(log.relayNum);
  643.       Serial.print(": ");
  644.       Serial.print(log.state ? "ON" : "OFF");
  645.       Serial.print(" (");
  646.       Serial.print(log.action);
  647.       Serial.println(")");
  648.     }
  649.   }
  650.  
  651.   Serial.println("===========================================");
  652. }
  653.  
  654.  
  655. // Print relay status monitoring with periodic updates
  656. void printRelayStatusMonitoring(void) {
  657.   // Calculate uptime
  658.   uint32_t uptimeSeconds = millis() / 1000;
  659.   uint8_t hours = uptimeSeconds / 3600;
  660.   uint8_t minutes = (uptimeSeconds % 3600) / 60;
  661.   uint8_t seconds = uptimeSeconds % 60;
  662.  
  663.   Serial.println();
  664.   Serial.println("--------- Relay Status Monitor (Periodic) --------");
  665.   Serial.print("Uptime: ");
  666.  
  667.   if (hours < 10) Serial.print("0");
  668.   Serial.print(hours);
  669.   Serial.print(":");
  670.  
  671.   if (minutes < 10) Serial.print("0");
  672.   Serial.print(minutes);
  673.   Serial.print(":");
  674.  
  675.   if (seconds < 10) Serial.print("0");
  676.   Serial.println(seconds);
  677.  
  678.   // Print all relay states
  679.   Serial.println("Current Relay States:");
  680.   for (uint8_t i = 0; i < 8; i++) {
  681.     Serial.print("  Relay ");
  682.     Serial.print(i);
  683.     Serial.print(": ");
  684.     Serial.println(relayController.getState(i) ? "ON " : "OFF");
  685.   }
  686.  
  687.   // Print number of logged changes
  688.   Serial.print("Total logged relay changes: ");
  689.   Serial.println(logIndex);
  690.  
  691.   // Print voice assistant status if available
  692.   if (voiceAssistant != nullptr && voiceAssistant->isInitialized()) {
  693.     Serial.print("Voice Assistant WiFi: ");
  694.     Serial.println(voiceAssistant->isWiFiConnected() ? "CONNECTED" : "DISCONNECTED");
  695.     Serial.print("Voice Commands Logged: ");
  696.     Serial.println(voiceAssistant->getCommandLogCount());
  697.    
  698.     // Attempt to reconnect WiFi if disconnected
  699.     if (!voiceAssistant->isWiFiConnected()) {
  700.       Serial.println("[INFO] Attempting WiFi reconnection...");
  701.       voiceAssistant->reconnectWiFi();
  702.     }
  703.   }
  704.  
  705.   Serial.println("--------------------------------------------------");
  706.   Serial.println();
  707. }
  708.  
  709. /* END CODE */
  710.  
Advertisement
Add Comment
Please, Sign In to add comment