Relay Component Documentation
Path: src/Relay.h
Revision History: Initial documentation
The Relay component provides a simple interface for controlling digital relay outputs with Modbus integration. It enables turning physical relays on and off both programmatically and via Modbus commands, making it suitable for industrial control applications with remote management capabilities.
REQUIREMENTS
Section titled “REQUIREMENTS”- A digital output pin for controlling the relay
- ESP-32 microcontroller
- Modbus-485 infrastructure for remote control
FEATURES
Section titled “FEATURES”- Control of digital relay outputs (ON/OFF)
- Modbus integration for remote control
- State persistence and notification
- Digital output pin management
- Clean API for both direct and Modbus control
DEPENDENCIES
Section titled “DEPENDENCIES”Component
- Base class for all system componentsArduinoLog
- Logging utilityApp
- Application frameworkenums.h
- System-wide enumeration definitionsconfig.h
- System configurationModbus
- Modbus protocol implementationModbusTCP
- Modbus TCP implementationconfig-modbus.h
- Modbus configuration
BEHAVIOUR
Section titled “BEHAVIOUR”The Relay component maintains a simple state machine transitioning between ON and OFF states, controllable both via direct commands and Modbus.
PERFORMANCE
Section titled “PERFORMANCE”- Consider implementing batch operations for applications requiring multiple relay controls
- Evaluate if digital write operations should be debounced or rate-limited in high-frequency scenarios
SECURITY
Section titled “SECURITY”- Add optional authorization for relay state changes
- Implement validation logic for Modbus commands based on system state or conditions
COMPLIANCE
Section titled “COMPLIANCE”- Ensure relay state changes are properly logged for audit in systems requiring operation traceability
- Consider adding timing constraints for safety-critical applications
RECOMMENDATIONS
Section titled “RECOMMENDATIONS”- For critical applications, implement a watchdog mechanism to set relays to a safe state if communication is lost
- When controlling high-power relays, consider implementing safety checks before switching states
EXAMPLE
Section titled “EXAMPLE”This example shows how to instantiate and register a Relay component within the main application:
#ifdef PIN_RELAY_0 relay_0 = new Relay( this, // owner PIN_RELAY_0, // pin ID_RELAY_0, // id RELAY_0_MB_ADDR // modbusAddress ); if (relay_0) { components.push_back(relay_0); Log.infoln(F("Relay_0 initialized. Pin:%d, ID:%d, MB:%d"), PIN_RELAY_0, ID_RELAY_0, RELAY_0_MB_ADDR); } else { Log.errorln(F("Relay_0 initialization failed.")); }#endif