HCesp/Config.cpp
2026-05-01 21:59:15 +09:00

189 lines
6.4 KiB
C++

#include "HermitCrab.h"
#include "AHT2x.h"
#include "Config.h"
#include "TimeManager.h"
#include <Preferences.h>
#ifndef SIGNATURE1
#define SIGNATURE1 ((uint16_t) 0xC8AB)
#endif
#ifndef SIGNATURE2
#define SIGNATURE2 ((uint16_t) 0x4E81)
#endif
Preferences preferences;
CONFIG_TYPE config;
// Function to initialize default values for the config structure
void CONFIG_TYPE::init() {
m_nSignature1 = SIGNATURE1;
m_nSignature2 = SIGNATURE2;
// Block 1 - Control and Environment
bSmartControl = false;
bNightControl = false;
bControlTemperature = true;
bControlHumidity = true;
bCheckAC = true;
bAC2_OnOff = false;
nNightStartHour = 18;
nNightStartMin = 0;
nNightEndHour = 6;
nNightEndMin = 0;
ac1ControlMode = PHASE_CONTROL;
ac2ControlMode = ZCD_CONTROL;
// Block 2 - Sensor and Temperature/Humidity Settings
nTempTarget = 260;
nTempTargetNight = 260;
nTemp1Offset = 0;
nTemp2Offset = 0;
nTemp3Offset = 0;
nHumidTarget = 800;
nHumidTargetNight = 800;
nHumid1Offset = 0;
nHumid2Offset = 0;
nTemp1SensorType = TEMP_SENSOR_TYPE::BLE_TUYA;
nTemp2SensorType = TEMP_SENSOR_TYPE::AHT20;
nTempSafety = 290;
bNTCNegativePolarity = true;
nBLEScanInterval = 143;
bBLETest = false;
Kp_Temp1 = 3.0f; // Load Kp for Temperature control
Kd_Temp1 = 750.0f; // Load Kd for Temperature control
LR_Temp1 = 0.05f; // Load learning rate for Temperature
Kp_Humidity = 2.0f; // Load Kp for Humidity control
Kd_Humidity = 150.0f; // Load Kd for Humidity control
LR_Humidity = 0.08f; // Load learning rate for Humidity
Kp_Temp2 = 3.0f; // Load Kp for Temperature control
Kd_Temp2 = 750.0f; // Load Kd for Temperature control
LR_Temp2 = 0.05f; // Load learning rate for Temperature
Kp_Humid2 = 2.0f; // Load Kp for Humidity control
Kd_Humid2 = 150.0f; // Load Kd for Humidity control
LR_Humid2 = 0.08f; // Load learning rate for Humidity
Kp_Temp3 = 3.0f; // Load Kp for Temperature control
Kd_Temp3 = 750.0f; // Load Kd for Temperature control
LR_Temp3 = 0.05f; // Load learning rate for Temperature
Kp_Humid3 = 2.0f; // Load Kp for Humidity control
Kd_Humid3 = 150.0f; // Load Kd for Humidity control
LR_Humid3 = 0.08f; // Load learning rate for Humidity
// Block 3 - AC1 and AC2
// Day/Night Min/Max/Start On/Off THigh/Low Time B/E Period Hum H/L
config.ac1 = {CONTROL_TEMP_HEAT_PID, 0, 1,1, 0, 1000, 0, 250,0, 920,880, 0,24*60-1, 60, 5, 820,780 };
config.ac2 = {CONTROL_TEMP_HEAT, 0, 0,0, 0, 1000, 0, 250,0, 920,880, 0,25*60-1, 60, 5, 820,780 };
// Block 4 = Mist and Fan
// Day/Night Min/Max/Start On/Off THigh/Low Time B/E Period Hum H/L
config.mist = {CONTROL_HUMIDITY_INC_PID, 0, 1,1, 0, 1000, 0, 250,0, 920,880, 0,25*60-1, 60, 5, 820,780 };
config.fan = {CONTROL_TEMP_COOL, 0, 1,1, 0, 1000, 0, 250,0, 920,880, 0,25*60-1, 60, 5, 820,780 };
// Block 5 - Motor and Light
// Day/Night Min/Max/Start On/Off THigh/Low Time B/E Period Hum H/L
config.motor = {CONTROL_PERIOD, 0, 1,1, 0, 1000, 0, 250,0, 920,880, 0,25*60-1, 60, 5, 820,780 };
config.light = {CONTROL_DAY_NIGHT, 0, 1,1, 0, 1000, 0, 250,0, 920,880, 0,25*60-1, 60, 5, 820,780 };
// Block 6 - Environment and Operations
bConfigSaved = false;
bStatusSaved = false;
bSendStatusSerial = false;
//
// Reserved
//
// TBD
for (int i = 0; i < 17; i = i + 8) {
m_nChipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
}
m_nDeviceType = THIS_DEVICE_TYPE;
m_nPublicPort = 3939;
//
// WiFi Client Only
//
m_nDisplayTime = 1800;
m_nDisplayTempHigh = 40;
m_nDisplayTempLow = 20;
m_nTempHigh = 25;
m_fShowRealTime = 0x000F;
m_fShowHistory = 0x000F;
m_nEpochTime = 0;
m_nTimeOffset = 9 * 60 * 60;
m_bFahrenheit = false;
// WiFi - SSID and Password
strcpy(ssid, "RECALL");
strcpy(pw, "BBBB9999");
//Block 10
strcpy(m_sDeviceName, "Beta X");
strcpy(m_sMake, "VisionSoft");
// Block 11
strcpy(m_sModel, "HermitCrab");
strncpy(m_sVersion, HC__VERSION, 11);
ESP_LOGI(TAG,"Config Initialized");
}
// Function to load the configuration block from preferences
bool CONFIG_TYPE::load() {
preferences.begin("HermitCrab", false); // Open preferences in read-write mode
// Check if config has been saved previously
size_t len = preferences.getBytesLength("config_data");
if (len != sizeof(config)) {
ESP_LOGI(TAG,"\nPreferences - First Time");
// First time, initialize default config
ESP_LOGI(TAG, "Config Size Error: %d out of %d\n", len, sizeof(config));
init();
save(); // Save defaults
preferences.end();
return true; // Indicates that it was initialized
}
// Load the structure as a block of data
preferences.getBytes("config_data", &config, sizeof(config));
if (m_nSignature1 != SIGNATURE1 ||
m_nSignature2 != SIGNATURE2) {
ESP_LOGI(TAG, "Config Load: Signature Mismatch %X %X - %X %X\n",
m_nSignature1, m_nSignature2,
SIGNATURE1, SIGNATURE2);
}
preferences.end();
ESP_LOGI(TAG,"Config Loaded");
bConfigSaved = false;
bStatusSaved = false;
for (int i = 0; i < 17; i = i + 8) {
m_nChipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
}
strncpy(m_sVersion, HC__VERSION, 11);
m_sVersion[11] = 0;
m_nDeviceType = THIS_DEVICE_TYPE;
return true; // Config loaded successfully
}
// Function to save the entire configuration block to preferences
void CONFIG_TYPE::save() {
// Update before save
config.statusSave = status;
config.bStatusSaved = true;
// Save the entire config structure as one block
preferences.begin("HermitCrab", false); // Open preferences in read-write mode
preferences.putBytes("config_data", &config, sizeof(config));
preferences.end(); // Close preferences
bConfigSaved = true;
DPRINTLN("[Config] Saved");
}