GroPro_Rich/config.h
2025-02-10 06:04:18 +00:00

209 lines
8.3 KiB
C

/**
* @file config.h
* @brief Global configuration definitions for the GreenHouse Controller.
*
* This file defines default values for WiFi, MQTT, timing parameters, setpoints,
* and per-plant watering configurations. It also declares modifiable global variables,
* which are defined in one source file (typically the main sketch). Additionally,
* inline functions are provided to dynamically build MQTT topics based on the current
* configuration.
*
* Author: James C. Alexander
* Date: 2025-02-03
*/
#ifndef CONFIG_H
#define CONFIG_H
#include <WiFi.h>
#include <PubSubClient.h>
#include <Preferences.h>
// ---------------------------------------------------------------------
// Global Default Values
// ---------------------------------------------------------------------
#define DEFAULT_SSID "" ///< Default WiFi SSID.
#define DEFAULT_WIFI_PASSWORD "" ///< Default WiFi password.
#define DEFAULT_MQTT_HOST "000.000.000.000" ///< Default MQTT broker host.
#define DEFAULT_MQTT_PORT 1883 ///< Default MQTT broker port.
#define DEFAULT_MQTT_USERNAME "" ///< Default MQTT username (empty if not used).
#define DEFAULT_MQTT_PASSWORD "" ///< Default MQTT password (empty if not used).
#define DEFAULT_MQTT_TOPIC_ROOT "GroPro" ///< Default MQTT topic root.
#define DEFAULT_PUBLISH_INTERVAL 2500 ///< Default publish interval in milliseconds.
#define DEFAULT_WIFI_RECONNECT_INTERVAL 5000 ///< Default WiFi reconnect interval in milliseconds.
#define DEFAULT_MQTT_RECONNECT_INTERVAL 5000 ///< Default MQTT reconnect interval in milliseconds.
#define DEFAULT_MIXER_LEVEL_SETPOINT 50.0 ///< Default mixer tank refill setpoint (cm).
#define DEFAULT_FEEDER_LEVEL_SETPOINT 10.0 ///< Default feeder tank refill setpoint (cm).
#define DEFAULT_MIXER_FULL_LEVEL 80.0 ///< Default mixer tank full level (cm).
#define DEFAULT_MIXER_REFILL_LEVEL 40.0 ///< Default mixer tank refill level (cm).
#define DEFAULT_PH_SETTLING_TIME 5.0 ///< Default delay after mixing before checking pH (seconds).
#define DEFAULT_PH_TARGET_MAX 6.5 ///< Default maximum acceptable pH.
#define DEFAULT_PH_PUMP_DURATION 3.0 ///< Default duration to run the pH pump (seconds).
#define DEFAULT_MIXING_PUMP_DURATION 10.0 ///< Default duration to run the mixing pump (seconds).
// ---------------------------------------------------------------------
// Watering Modes and Per-Plant Watering Configuration
// ---------------------------------------------------------------------
/**
* @enum WateringMode
* @brief Defines the various watering modes.
*/
enum WateringMode {
SEEDLING_MODE = 0,
VEGETATIVE_MODE = 1,
FLOWERING_MODE = 2,
DRYING_MODE = 3
};
/**
* @struct WateringSetpoints
* @brief Holds the soil moisture setpoints for a specific watering mode.
*/
struct WateringSetpoints {
float soilMoistureLow; ///< Low threshold for soil moisture.
float soilMoistureHigh; ///< High threshold for soil moisture.
};
/**
* @struct PlantWateringConfig
* @brief Holds the complete watering configuration for a single plant.
*/
struct PlantWateringConfig {
WateringSetpoints setpoints[4]; ///< Setpoints for each watering mode.
uint8_t currentMode; ///< Active watering mode index (0-3).
};
/**
* @brief Default watering configurations for three plants.
*/
const PlantWateringConfig DEFAULT_PLANT_CONFIGS[3] = {
{ { {60, 80}, {40, 70}, {30, 60}, {20, 40} }, VEGETATIVE_MODE },
{ { {60, 80}, {40, 70}, {30, 60}, {20, 40} }, VEGETATIVE_MODE },
{ { {60, 80}, {40, 70}, {30, 60}, {20, 40} }, VEGETATIVE_MODE }
};
// ---------------------------------------------------------------------
// WiFi Configuration
// ---------------------------------------------------------------------
/**
* @brief Modifiable WiFi configuration buffers.
*/
extern char ssid[32]; ///< WiFi SSID.
extern char password[64]; ///< WiFi password.
// ---------------------------------------------------------------------
// MQTT Configuration
// ---------------------------------------------------------------------
/**
* @brief Modifiable MQTT broker settings.
*/
extern char mqtt_host[64]; ///< MQTT broker host.
extern uint16_t mqtt_port; ///< MQTT broker port.
extern char mqtt_username[32]; ///< MQTT username.
extern char mqtt_password[32]; ///< MQTT password.
extern char mqtt_topic_root[32]; ///< MQTT topic root.
// ---------------------------------------------------------------------
// Inline Functions for Dynamic Topic Construction
// ---------------------------------------------------------------------
inline String getMqttConfigTopic() {
return String(mqtt_topic_root) + "/config";
}
inline String getMqttPublishIntervalTopic() {
return String(mqtt_topic_root) + "/config/pubInt";
}
inline String getMqttWifiReconnectTopic() {
return String(mqtt_topic_root) + "/config/wifInt";
}
inline String getMqttMqttReconnectTopic() {
return String(mqtt_topic_root) + "/config/mqtInt";
}
inline String getMqttMixerLevelSetpointTopic() {
return String(mqtt_topic_root) + "/config/mixSet";
}
inline String getMqttFeederLevelSetpointTopic() {
return String(mqtt_topic_root) + "/config/feeSet";
}
inline String getMqttBypassFillInterlockTopic() {
return String(mqtt_topic_root) + "/config/bypass";
}
inline String getMqttWateringModeTopic(uint8_t plantIndex) {
return String(mqtt_topic_root) + "/config/plant" + String(plantIndex + 1) + "/wMode";
}
inline String getMqttSoilMoistureLowTopic(uint8_t plantIndex) {
return String(mqtt_topic_root) + "/config/plant" + String(plantIndex + 1) + "/sLow";
}
inline String getMqttSoilMoistureHighTopic(uint8_t plantIndex) {
return String(mqtt_topic_root) + "/config/plant" + String(plantIndex + 1) + "/sHigh";
}
inline String getAmbientTempTopic() {
return String(mqtt_topic_root) + "/climate/temp";
}
inline String getAmbientHumidityTopic() {
return String(mqtt_topic_root) + "/climate/hum";
}
inline String getPhTopic() {
return String(mqtt_topic_root) + "/mixing/ph";
}
inline String getTdsTopic() {
return String(mqtt_topic_root) + "/mixing/tds";
}
inline String getPhotoTopic() {
return String(mqtt_topic_root) + "/lighting/photo";
}
inline String getFeederLevelTopic() {
return String(mqtt_topic_root) + "/tank/feeder";
}
inline String getMixerLevelTopic() {
return String(mqtt_topic_root) + "/tank/mixer";
}
// ---------------------------------------------------------------------
// Global Configuration Variables (Stored in EEPROM)
// ---------------------------------------------------------------------
extern unsigned long publishInterval;
extern unsigned long wifiReconnectInterval;
extern unsigned long mqttReconnectInterval;
extern float mixerLevelSetpoint;
extern float feederLevelSetpoint;
// ---------------------------------------------------------------------
// Additional Global Configuration Variables
// ---------------------------------------------------------------------
extern float mixerFullLevel;
extern float mixerRefillLevel;
extern float phSettlingTime;
extern float phTargetMax;
extern float phPumpDuration;
extern float mixingPumpDuration;
// ---------------------------------------------------------------------
// Auto Mode and Interlock Flags
// ---------------------------------------------------------------------
extern bool autoMode;
extern bool bypassFillInterlock;
// ---------------------------------------------------------------------
// Tank Level Sensor Readings
// ---------------------------------------------------------------------
extern float mixerLevel;
extern float feederLevel;
// ---------------------------------------------------------------------
// Per-Plant Watering Configuration Variables
// ---------------------------------------------------------------------
extern PlantWateringConfig plantConfigs[3];
// ---------------------------------------------------------------------
// New Global Variable for Relay Logic
// ---------------------------------------------------------------------
/**
* @brief Relay activation logic flag.
* false: relays are active-low (default)
* true: relays are active-high.
*/
extern bool relayActiveHigh;
#endif // CONFIG_H