← Router Modes | Contents | Next: Main Application →
5. Module APIs and Data Structures
5.1 RouterController API
Files:
- components/acrouter_hal/include/RouterController.h
- components/acrouter_hal/src/RouterController.cpp
Design pattern: Singleton
Get Instance
RouterController& router = RouterController::getInstance();
Initialization
begin()
bool begin(DimmerHAL* dimmer, DimmerChannel channel = DimmerChannel::CHANNEL_1);
Description: Initialize controller with dimmer binding.
Parameters:
- dimmer - pointer to initialized DimmerHAL
- channel - dimmer channel (CHANNEL_1 or CHANNEL_2)
Returns: true if successful, false on error
Example:
DimmerHAL& dimmer = DimmerHAL::getInstance();
RouterController& router = RouterController::getInstance();
dimmer.begin();
if (router.begin(&dimmer, DimmerChannel::CHANNEL_1)) {
Serial.println("RouterController initialized");
} else {
Serial.println("RouterController init failed");
}
Mode Management
setMode()
void setMode(RouterMode mode);
Description: Set controller operating mode.
Parameters:
- mode - one of 6 modes (OFF=0, AUTO=1, ECO=2, OFFGRID=3, MANUAL=4, BOOST=5)
Example:
router.setMode(RouterMode::AUTO); // Automatic mode
router.setMode(RouterMode::MANUAL); // Manual mode
getMode()
RouterMode getMode() const;
Description: Get current operating mode.
Returns: Current RouterMode
Example:
RouterMode current = router.getMode();
if (current == RouterMode::AUTO) {
Serial.println("Running in AUTO mode");
}
Control Parameters
setControlGain()
void setControlGain(float gain);
Description: Set proportional controller coefficient (Kp).
Parameters:
- gain - gain coefficient (10.0 - 1000.0)
Effect:
- Lower Kp � faster response, possible oscillations
- Higher Kp � slower response, more stable
Example:
router.setControlGain(200.0f); // Standard value
router.setControlGain(100.0f); // Faster response
router.setControlGain(400.0f); // Slower, more stable
getControlGain()
float getControlGain() const;
Returns: Current Kp value
setBalanceThreshold()
void setBalanceThreshold(float threshold);
Description: Set balance threshold in Watts.
Parameters:
- threshold - threshold in W (typically 5.0 - 50.0)
Effect:
- If |P_grid| < threshold, system is considered balanced
- Lower threshold � more accurate balance, more switching
- Higher threshold � less switching, less accurate balance
Example:
router.setBalanceThreshold(10.0f); // �10 W considered balanced
router.setBalanceThreshold(20.0f); // �20 W for larger loads
getBalanceThreshold()
float getBalanceThreshold() const;
Returns: Current balance threshold (W)
Manual Control (MANUAL mode)
setManualLevel()
void setManualLevel(uint8_t percent);
Description: Set fixed dimmer level for MANUAL mode.
Parameters:
- percent - dimmer level 0-100%
Example:
router.setMode(RouterMode::MANUAL);
router.setManualLevel(75); // Set to 75%
getManualLevel()
uint8_t getManualLevel() const;
Returns: Set level for MANUAL mode
Main Update Loop
update()
void update(const PowerMeterADC::Measurements& measurements);
Description: Main control function. Called automatically from PowerMeterADC callback every 200 ms.
Parameters:
- measurements - structure with power measurements
Note: Usually called automatically, manual calling not required.
Internal logic:
void update(const Measurements& meas) {
switch (mode) {
case OFF: processOffMode(); break;
case AUTO: processAutoMode(P_grid); break;
case ECO: processEcoMode(P_grid); break;
case OFFGRID: processOffgridMode(meas); break;
case MANUAL: processManualMode(); break;
case BOOST: processBoostMode(); break;
}
}
Emergency Stop
emergencyStop()
void emergencyStop();
Description: Immediate dimmer shutdown (0%) and transition to OFF mode.
When to use:
- System overheating
- Sensor error
- Critical situation
Example:
if (temperature > 80.0f) {
router.emergencyStop();
Serial.println("EMERGENCY STOP: Overheating!");
}
Get Status
getStatus()
RouterStatus getStatus() const;
Description: Get complete controller state information.
Returns: RouterStatus structure
Example:
RouterStatus status = router.getStatus();
Serial.printf("Mode: %d\n", static_cast(status.mode));
Serial.printf("State: %d\n", static_cast(status.state));
Serial.printf("Dimmer: %d%%\n", status.dimmer_percent);
Serial.printf("P_grid: %.1f W\n", status.power_grid);
Serial.printf("Kp: %.1f\n", status.control_gain);
Data Structures
RouterStatus
struct RouterStatus {
RouterMode mode; // Current mode
RouterState state; // State (IDLE, INCREASING, etc.)
uint8_t dimmer_percent; // Dimmer level (0-100%)
float target_level; // Target level (float for smoothness)
float power_grid; // Current grid power (W)
float control_gain; // Kp coefficient
float balance_threshold; // Balance threshold (W)
uint32_t last_update_ms; // Last update timestamp
bool valid; // Status is valid
};
RouterMode
enum class RouterMode : uint8_t {
OFF = 0, // Disabled
AUTO, // Automatic Solar Router
ECO, // Economic (anti-export)
OFFGRID, // Autonomous
MANUAL, // Manual
BOOST // Forced 100%
};
RouterState
enum class RouterState : uint8_t {
IDLE, // No action
INCREASING, // Increasing dimmer (exporting)
DECREASING, // Decreasing dimmer (importing)
AT_MAXIMUM, // At maximum (100%)
AT_MINIMUM, // At minimum (0%)
ERROR // Error state
};
5.2 PowerMeterADC API
Files:
- components/acrouter_hal/include/PowerMeterADC.h
- components/acrouter_hal/src/PowerMeterADC.cpp
Design pattern: Singleton
Get Instance
PowerMeterADC& powerMeter = PowerMeterADC::getInstance();
Initialization
begin()
bool begin();
Description: Initialize DMA ADC and create FreeRTOS processing task.
Returns: true if successful
Example:
PowerMeterADC& powerMeter = PowerMeterADC::getInstance();
if (powerMeter.begin()) {
Serial.println("PowerMeterADC initialized");
} else {
Serial.println("PowerMeterADC init failed");
}
Measurement Control
start()
bool start();
Description: Start continuous DMA ADC measurements.
Returns: true if successfully started
Example:
if (powerMeter.start()) {
Serial.println("Measurements started");
}
stop()
void stop();
Description: Stop measurements.
Example:
powerMeter.stop();
Serial.println("Measurements stopped");
isRunning()
bool isRunning() const;
Description: Check if measurements are running.
Returns: true if measurements are active
Results Callback
setCallback()
void setCallback(std::function callback);
Description: Set callback function to be called every 200 ms with RMS results.
Parameters:
- callback - function of type void callback(const Measurements& meas)
Example:
// Option 1: Lambda
powerMeter.setCallback([](const PowerMeterADC::Measurements& meas) {
Serial.printf("V: %.1f V, I_grid: %.2f A, P_grid: %.1f W\n",
meas.voltage_rms,
meas.current_rms[PowerMeterADC::CURRENT_GRID],
meas.power_active[PowerMeterADC::CURRENT_GRID]);
});
// Option 2: Global function
void onMeasurements(const PowerMeterADC::Measurements& meas) {
// Process measurements
}
powerMeter.setCallback(onMeasurements);
// Option 3: Class method
powerMeter.setCallback([this](const auto& meas) {
this->handleMeasurements(meas);
});
Sensor Calibration
setVoltageCalibration()
void setVoltageCalibration(float multiplier, float offset = 0.0f);
Description: Set calibration coefficients for voltage sensor.
Parameters:
- multiplier - multiplier (V/V_adc)
- offset - offset (V)
Formula: V_real = V_adc * multiplier + offset
Example:
// ZMPT107: 230V � 1V (scale 1:230)
powerMeter.setVoltageCalibration(230.0f, 0.0f);
// With offset correction
powerMeter.setVoltageCalibration(235.0f, -2.5f);
setCurrentCalibration()
void setCurrentCalibration(CurrentChannel channel, float multiplier, float offset = 0.0f);
Description: Set calibration coefficients for current sensor.
Parameters:
- channel - channel (CURRENT_LOAD, CURRENT_GRID, CURRENT_SOLAR)
- multiplier - multiplier (A/V_adc)
- offset - offset (A)
Formula: I_real = I_adc * multiplier + offset
Example:
// SCT-013-030: 30A � 1V
powerMeter.setCurrentCalibration(PowerMeterADC::CURRENT_GRID, 30.0f, 0.0f);
// ACS-712-20A: 0A=2.5V, sensitivity 100mV/A
powerMeter.setCurrentCalibration(PowerMeterADC::CURRENT_LOAD, 10.0f, -25.0f);
Get Data
getPowerData()
Measurements getPowerData() const;
Description: Get latest measurements (thread-safe).
Returns: Measurements structure
Example:
PowerMeterADC::Measurements data = powerMeter.getPowerData();
if (data.valid) {
Serial.printf("Voltage: %.1f V\n", data.voltage_rms);
Serial.printf("Current Grid: %.2f A\n", data.current_rms[PowerMeterADC::CURRENT_GRID]);
Serial.printf("Power Grid: %.1f W\n", data.power_active[PowerMeterADC::CURRENT_GRID]);
}
getVoltageRMS()
float getVoltageRMS() const;
Description: Get voltage RMS only.
Returns: Voltage in Volts
getCurrentRMS()
float getCurrentRMS(CurrentChannel channel) const;
Description: Get RMS current for specified channel.
Parameters:
- channel - CURRENT_LOAD, CURRENT_GRID or CURRENT_SOLAR
Returns: Current in Amperes
Example:
float i_grid = powerMeter.getCurrentRMS(PowerMeterADC::CURRENT_GRID);
float i_solar = powerMeter.getCurrentRMS(PowerMeterADC::CURRENT_SOLAR);
Serial.printf("Grid: %.2f A, Solar: %.2f A\n", i_grid, i_solar);
getPower()
float getPower(CurrentChannel channel) const;
Description: Get active power for specified channel.
Parameters:
- channel - CURRENT_LOAD, CURRENT_GRID or CURRENT_SOLAR
Returns: Power in Watts
Example:
float p_grid = powerMeter.getPower(PowerMeterADC::CURRENT_GRID);
if (p_grid < 0) {
Serial.println("Exporting to grid");
} else {
Serial.println("Importing from grid");
}
Data Structures
Measurements
struct Measurements {
float voltage_rms; // RMS voltage (V)
float current_rms[3]; // RMS currents [LOAD, GRID, SOLAR] (A)
float power_active[3]; // Active power [LOAD, GRID, SOLAR] (W)
float power_reactive[3]; // Reactive power (VAR) - Phase 2
float power_apparent[3]; // Apparent power (VA) - Phase 2
float power_factor[3]; // Power factor - Phase 2
uint32_t timestamp_ms; // Timestamp
bool valid; // Data is valid
};
CurrentChannel
enum CurrentChannel : uint8_t {
CURRENT_LOAD = 0, // Load current (dimmer)
CURRENT_GRID = 1, // Grid current (import/export)
CURRENT_SOLAR = 2, // Solar panel current
CURRENT_COUNT = 3
};
5.3 DimmerHAL API
Files:
- components/acrouter_hal/include/DimmerHAL.h
- components/acrouter_hal/src/DimmerHAL.cpp
Design pattern: Singleton
Get Instance
DimmerHAL& dimmer = DimmerHAL::getInstance();
Initialization
begin()
bool begin();
Description: Initialize dimmer with zero-crossing detector.
Returns: true if successful
Example:
DimmerHAL& dimmer = DimmerHAL::getInstance();
if (dimmer.begin()) {
Serial.println("DimmerHAL initialized");
} else {
Serial.println("DimmerHAL init failed");
}
Power Control
setPower()
void setPower(DimmerChannel channel, uint8_t percent);
Description: Set dimmer power instantly.
Parameters:
- channel - CHANNEL_1 or CHANNEL_2
- percent - power 0-100%
Example:
dimmer.setPower(DimmerChannel::CHANNEL_1, 75); // 75%
dimmer.setPower(DimmerChannel::CHANNEL_2, 50); // 50% on second channel
setPowerSmooth()
void setPowerSmooth(DimmerChannel channel, uint8_t percent, uint32_t transition_ms);
Description: Set power with smooth transition.
Parameters:
- channel - CHANNEL_1 or CHANNEL_2
- percent - target power 0-100%
- transition_ms - transition time in milliseconds
Example:
// Smoothly transition to 80% over 2 seconds
dimmer.setPowerSmooth(DimmerChannel::CHANNEL_1, 80, 2000);
// Smooth shutdown over 1 second
dimmer.setPowerSmooth(DimmerChannel::CHANNEL_1, 0, 1000);
getPower()
uint8_t getPower(DimmerChannel channel) const;
Description: Get current channel power.
Returns: Power 0-100%
Example:
uint8_t current_power = dimmer.getPower(DimmerChannel::CHANNEL_1);
Serial.printf("Dimmer 1: %d%%\n", current_power);
Quick Control
turnOff()
void turnOff(DimmerChannel channel);
Description: Quickly turn off channel (0%).
Example:
dimmer.turnOff(DimmerChannel::CHANNEL_1);
turnOffAll()
void turnOffAll();
Description: Turn off all channels.
Example:
dimmer.turnOffAll(); // Emergency shutdown
Power Curve
setCurve()
void setCurve(DimmerChannel channel, DimmerCurve curve);
Description: Set power curve type.
Parameters:
- channel - CHANNEL_1 or CHANNEL_2
- curve - LINEAR, RMS or LOGARITHMIC
Recommendations:
- RMS - for resistive loads (heaters, water heaters) - recommended
- LINEAR - linear dependency
- LOGARITHMIC - for LED lamps
Example:
// RMS curve for heater (recommended)
dimmer.setCurve(DimmerChannel::CHANNEL_1, DimmerCurve::RMS);
// Linear curve
dimmer.setCurve(DimmerChannel::CHANNEL_1, DimmerCurve::LINEAR);
Status and Information
getStatus()
DimmerStatus getStatus(DimmerChannel channel) const;
Description: Get detailed channel status.
Returns: DimmerStatus structure
Example:
DimmerStatus status = dimmer.getStatus(DimmerChannel::CHANNEL_1);
Serial.printf("Initialized: %s\n", status.initialized ? "Yes" : "No");
Serial.printf("Active: %s\n", status.active ? "Yes" : "No");
Serial.printf("Power: %d%%\n", status.power_percent);
Serial.printf("Target: %d%%\n", status.target_percent);
isInitialized()
bool isInitialized() const;
Description: Check if dimmer is initialized.
Returns: true if initialized
Data Structures
DimmerChannel
enum class DimmerChannel : uint8_t {
CHANNEL_1 = 0, // First channel (GPIO 19)
CHANNEL_2 = 1 // Second channel (GPIO 23)
};
DimmerCurve
enum class DimmerCurve : uint8_t {
LINEAR, // Linear
RMS, // RMS-compensated (recommended)
LOGARITHMIC // Logarithmic
};
DimmerStatus
struct DimmerStatus {
bool initialized; // Initialized
bool active; // Active
uint8_t power_percent; // Current power (0-100%)
uint8_t target_percent; // Target power (during transition)
DimmerCurve curve; // Curve type
uint32_t last_update_ms; // Last update time
};
5.4 ConfigManager API
Files:
- components/utils/include/ConfigManager.h
- components/utils/src/ConfigManager.cpp
Design pattern: Singleton
NVS namespace: "acrouter"
Get Instance
ConfigManager& config = ConfigManager::getInstance();
Initialization
begin()
bool begin();
Description: Initialize NVS and load configuration.
Returns: true if successful
Example:
ConfigManager& config = ConfigManager::getInstance();
if (config.begin()) {
Serial.println("ConfigManager initialized");
Serial.printf("Router mode: %d\n", config.getConfig().router_mode);
}
Get Configuration
getConfig()
const SystemConfig& getConfig() const;
Description: Get reference to current configuration.
Returns: Const reference to SystemConfig
Example:
const SystemConfig& cfg = config.getConfig();
Serial.printf("Mode: %d\n", cfg.router_mode);
Serial.printf("Kp: %.1f\n", cfg.control_gain);
Serial.printf("Threshold: %.1f W\n", cfg.balance_threshold);
Set Parameters
setRouterMode()
bool setRouterMode(uint8_t mode);
Description: Set router mode (automatically saved to NVS).
Parameters:
- mode - mode 0-5
Returns: true if successfully saved
Example:
config.setRouterMode(1); // AUTO mode
setControlGain()
bool setControlGain(float gain);
Description: Set Kp coefficient.
Parameters:
- gain - coefficient (10.0 - 1000.0)
Example:
config.setControlGain(200.0f);
setBalanceThreshold()
bool setBalanceThreshold(float threshold);
Description: Set balance threshold.
Parameters:
- threshold - threshold in Watts (0.0 - 100.0)
Example:
config.setBalanceThreshold(10.0f);
setManualLevel()
bool setManualLevel(uint8_t level);
Description: Set level for MANUAL mode.
Parameters:
- level - level 0-100%
Example:
config.setManualLevel(75);
setVoltageCoefficient()
bool setVoltageCoefficient(float coef);
Description: Set voltage calibration coefficient.
Example:
config.setVoltageCoefficient(230.0f);
setCurrentCoefficient()
bool setCurrentCoefficient(float coef);
Description: Set current calibration coefficient.
Example:
config.setCurrentCoefficient(30.0f); // SCT-013-030
Batch Operations
saveAll()
bool saveAll();
Description: Save all parameters to NVS.
Note: Usually not required as each set*() method automatically saves.
loadAll()
bool loadAll();
Description: Reload configuration from NVS.
resetToDefaults()
bool resetToDefaults();
Description: Reset all settings to factory defaults and save to NVS.
Example:
if (config.resetToDefaults()) {
Serial.println("Config reset to defaults");
}
Data Structures
SystemConfig
struct SystemConfig {
// Router parameters
uint8_t router_mode; // RouterMode (0-5)
float control_gain; // Kp (10.0 - 1000.0)
float balance_threshold; // Balance threshold (W)
uint8_t manual_level; // MANUAL level (0-100%)
// Sensor calibration
float voltage_coef; // Voltage coefficient
float current_coef; // Current coefficient (A/V)
float current_threshold; // Minimum current (A)
float power_threshold; // Minimum power (W)
};
5.5 HardwareConfigManager API
Files:
- components/utils/include/HardwareConfigManager.h
- components/utils/src/HardwareConfigManager.cpp
Design pattern: Singleton
NVS namespace: "hw_config"
Get Instance
HardwareConfigManager& hwConfig = HardwareConfigManager::getInstance();
Initialization
begin()
bool begin();
Description: Initialize and load hardware configuration from NVS.
Example:
HardwareConfigManager& hwConfig = HardwareConfigManager::getInstance();
if (hwConfig.begin()) {
Serial.println("HardwareConfigManager initialized");
}
ADC Channel Configuration
setADCChannel()
bool setADCChannel(uint8_t channel, const ADCChannelConfig& config);
Description: Configure ADC channel.
Parameters:
- channel - channel number (0-3)
- config - channel configuration
Returns: true if successful
Example:
ADCChannelConfig voltage_cfg;
voltage_cfg.gpio = 35;
voltage_cfg.type = SensorType::VOLTAGE_AC;
voltage_cfg.multiplier = 230.0f;
voltage_cfg.offset = 0.0f;
voltage_cfg.enabled = true;
hwConfig.setADCChannel(0, voltage_cfg);
getADCChannel()
ADCChannelConfig getADCChannel(uint8_t channel) const;
Description: Get ADC channel configuration.
Example:
ADCChannelConfig cfg = hwConfig.getADCChannel(0);
Serial.printf("ADC0: GPIO%d, Type=%d, Mult=%.1f\n",
cfg.gpio, static_cast(cfg.type), cfg.multiplier);
Dimmer Configuration
setDimmerChannel()
bool setDimmerChannel(uint8_t channel, const DimmerChannelConfig& config);
Description: Configure dimmer channel.
Parameters:
- channel - 0 (Channel 1) or 1 (Channel 2)
- config - configuration
Example:
DimmerChannelConfig dim_cfg;
dim_cfg.gpio = 19;
dim_cfg.enabled = true;
hwConfig.setDimmerChannel(0, dim_cfg);
Zero-Cross Configuration
setZeroCross()
bool setZeroCross(uint8_t gpio, bool enabled = true);
Description: Configure zero-cross detector pin.
Example:
hwConfig.setZeroCross(18, true); // GPIO18, enabled
Relay Configuration
setRelayChannel()
bool setRelayChannel(uint8_t channel, const RelayChannelConfig& config);
Description: Configure relay channel.
Parameters:
- channel - 0 or 1
- config - relay configuration
Example:
RelayChannelConfig relay_cfg;
relay_cfg.gpio = 15;
relay_cfg.active_high = true; // Active HIGH
relay_cfg.enabled = true;
hwConfig.setRelayChannel(0, relay_cfg);
LED Configuration
setStatusLED()
bool setStatusLED(uint8_t gpio);
Description: Configure GPIO for status LED.
Example:
hwConfig.setStatusLED(17);
setLoadLED()
bool setLoadLED(uint8_t gpio);
Description: Configure GPIO for load indicator LED.
Validation
validate()
bool validate(String* error_msg = nullptr) const;
Description: Validate configuration for errors.
Parameters:
- error_msg - pointer to String for error message (optional)
Returns: true if configuration is valid
Checks:
- GPIO conflicts (one pin = one function)
- Pin validity for ADC (ADC1 only: 32-39)
- Input-only pins (34,35,36,39 cannot be outputs)
Example:
String error;
if (!hwConfig.validate(&error)) {
Serial.printf("Validation failed: %s\n", error.c_str());
} else {
Serial.println("Configuration is valid");
}
Batch Operations
saveAll()
bool saveAll();
Description: Save entire configuration to NVS.
loadAll()
bool loadAll();
Description: Load configuration from NVS.
resetToDefaults()
bool resetToDefaults();
Description: Reset to factory defaults.
printConfig()
void printConfig() const;
Description: Print configuration to Serial.
Example:
hwConfig.printConfig();
// Output:
// === Hardware Configuration ===
// ADC0: GPIO35, VOLTAGE_AC, mult=230.0, enabled
// ADC1: GPIO39, CURRENT_LOAD, mult=30.0, enabled
// ...
Data Structures
ADCChannelConfig
struct ADCChannelConfig {
uint8_t gpio; // GPIO pin (32-39 for ADC1)
SensorType type; // Sensor type
float multiplier; // Calibration multiplier
float offset; // Offset
bool enabled; // Channel enabled
};
DimmerChannelConfig
struct DimmerChannelConfig {
uint8_t gpio; // GPIO pin
bool enabled; // Channel enabled
};
RelayChannelConfig
struct RelayChannelConfig {
uint8_t gpio; // GPIO pin
bool active_high; // true=Active HIGH, false=Active LOW
bool enabled; // Channel enabled
};
SensorType
enum class SensorType : uint8_t {
NONE = 0, // Not used
VOLTAGE_AC, // ZMPT107 (AC voltage)
CURRENT_LOAD, // ACS-712 (dimmer load current)
CURRENT_GRID, // SCT-013 (grid current)
CURRENT_SOLAR, // SCT-013 (solar current)
CURRENT_AUX1, // Auxiliary channel 1
CURRENT_AUX2 // Auxiliary channel 2
};
5.6 WiFiManager API
Files:
- components/comm/include/WiFiManager.h
- components/comm/src/WiFiManager.cpp
Design pattern: Singleton
Get Instance
WiFiManager& wifi = WiFiManager::getInstance();
Initialization
begin()
bool begin();
Description: Initialize WiFi. Automatically starts AP if no STA credentials.
Returns: true if successful
Example:
WiFiManager& wifi = WiFiManager::getInstance();
if (wifi.begin()) {
Serial.println("WiFiManager initialized");
WiFiStatus status = wifi.getStatus();
Serial.printf("AP IP: %s\n", status.ap_ip.toString().c_str());
}
AP Mode
startAP()
bool startAP(const char* ssid = nullptr, const char* password = nullptr);
Description: Start Access Point.
Parameters:
- ssid - network SSID (nullptr = auto: "ACRouter_XXXXXX")
- password - password (nullptr = "12345678")
Example:
// With auto-generated SSID
wifi.startAP();
// With custom SSID
wifi.startAP("MyACRouter", "mypassword123");
stopAP()
void stopAP();
Description: Stop Access Point.
isAPActive()
bool isAPActive() const;
Returns: true if AP is active
STA Mode
connectSTA()
bool connectSTA(const char* ssid, const char* password);
Description: Connect to WiFi network.
Parameters:
- ssid - network SSID
- password - password
Returns: true if connected
Example:
if (wifi.connectSTA("HomeNetwork", "password123")) {
Serial.println("Connected to WiFi");
WiFiStatus status = wifi.getStatus();
Serial.printf("STA IP: %s\n", status.sta_ip.toString().c_str());
}
disconnectSTA()
void disconnectSTA();
Description: Disconnect from STA network.
isSTAConnected()
bool isSTAConnected() const;
Returns: true if connected to STA
Network Scanning
scanNetworks()
std::vector scanNetworks();
Description: Scan available WiFi networks.
Returns: Vector of WiFiNetwork structures
Example:
std::vector networks = wifi.scanNetworks();
Serial.printf("Found %d networks:\n", networks.size());
for (const auto& net : networks) {
Serial.printf(" %s (RSSI: %d, Encrypted: %s)\n",
net.ssid.c_str(),
net.rssi,
net.encrypted ? "Yes" : "No");
}
Status
getStatus()
WiFiStatus getStatus() const;
Description: Get complete WiFi status.
Example:
WiFiStatus status = wifi.getStatus();
Serial.printf("State: %d\n", static_cast(status.state));
Serial.printf("STA connected: %s\n", status.sta_connected ? "Yes" : "No");
Serial.printf("AP active: %s\n", status.ap_active ? "Yes" : "No");
if (status.sta_connected) {
Serial.printf("STA IP: %s\n", status.sta_ip.toString().c_str());
Serial.printf("RSSI: %d dBm\n", status.rssi);
}
if (status.ap_active) {
Serial.printf("AP IP: %s\n", status.ap_ip.toString().c_str());
Serial.printf("Clients: %d\n", status.sta_clients);
}
Data Structures
WiFiState
enum class WiFiState : uint8_t {
IDLE, // Not initialized
AP_ONLY, // AP only
STA_CONNECTING, // Connecting to STA
STA_CONNECTED, // Connected to STA
AP_STA, // Both modes
STA_FAILED // STA failed
};
WiFiStatus
struct WiFiStatus {
WiFiState state;
bool sta_connected;
bool ap_active;
IPAddress sta_ip;
IPAddress ap_ip;
String sta_ssid;
String ap_ssid;
int8_t rssi;
uint8_t sta_clients; // Clients on AP
};
WiFiNetwork
struct WiFiNetwork {
String ssid;
int8_t rssi;
bool encrypted;
};
5.7 WebServerManager API
Files:
- components/comm/include/WebServerManager.h
- components/comm/src/WebServerManager.cpp
Design pattern: Singleton
Get Instance
WebServerManager& webServer = WebServerManager::getInstance();
Initialization
begin()
bool begin(uint16_t http_port = 80);
Description: Initialize HTTP server.
Parameters:
- http_port - HTTP port (default 80)
Example:
WebServerManager& webServer = WebServerManager::getInstance();
if (webServer.begin(80)) {
Serial.println("WebServer started on port 80");
}
Control
start()
void start();
Description: Start web server.
stop()
void stop();
Description: Stop web server.
isRunning()
bool isRunning() const;
Returns: true if server is running
Request Handling
handleClient()
void handleClient();
Description: Handle incoming HTTP requests. Call from loop().
Example:
void loop() {
webServer.handleClient();
// ... rest of code
}
REST API Endpoints
For complete endpoint list see Section 8: GET endpoints and Section 9: POST endpoints.
Main groups:
- Web pages:
/,/wifi,/settings/hardware - Status:
/api/status,/api/metrics,/api/config - Control:
/api/mode,/api/manual,/api/calibrate - WiFi:
/api/wifi/* - Hardware:
/api/hardware/* - System:
/api/system/reboot