#include "HermitCrab.h" #include "AHT2x.h" #include "Config.h" #include "TimeManager.h" #include #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; bEnableIO = true; bAC2_OnOff = false; nNightStartHour = 18; nNightStartMin = 0; nNightEndHour = 6; nNightEndMin = 0; // Block 2 - Sensor and Temperature/Humidity Settings nTempTarget = 260; nTempTargetNight = 260; nTemp1Offset = 0; nTemp2Offset = 0; nTemp3Offset = 0; nHumidTarget = 880; nHumidTargetNight = 880; nHumid1Offset = 0; nHumid2Offset = 0; nTemp1SensorType = TEMP_SENSOR_TYPE::BLE_TUYA; nTemp2SensorType = TEMP_SENSOR_TYPE::AHT20; 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 // 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, 920,880 }; config.ac2 = {CONTROL_TEMP_HEAT, 0, 0,0, 0, 1000, 0, 250,0, 920,880, 0,25*60-1, 60, 5, 920,880 }; // 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, 920,880 }; config.fan = {CONTROL_TEMP_COOL, 0, 1,1, 0, 1000, 0, 250,0, 920,880, 0,25*60-1, 60, 5, 920,880 }; // 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, 920,880 }; config.light = {CONTROL_DAY_NIGHT, 0, 1,1, 0, 1000, 0, 250,0, 920,880, 0,25*60-1, 60, 5, 920,880 }; // Block 6 - Environment and Operations bSendStatusSerial = false; bConfigSaved = false; bStatusSaved = false; // // Reserved // // TBD for (int i = 0; i < 17; i = i + 8) { m_nChipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i; } m_nPublicPort = (uint16_t)(m_nChipId & 0xFFFF); m_nDeviceType = THIS_DEVICE_TYPE; // // WiFi Client Only // m_nDisplayTime = 1800; m_nDisplayTempHigh = 40; m_nDisplayTempLow = 20; m_nTempHigh = 20; m_fShowRealTime = 0x000F; // Names strcpy(m_sDeviceName, "Beta X"); strcpy(m_sMake, "VisionSoft"); strcpy(m_sModel, "HermitCrab"); strncpy(m_sVersion, HC__VERSION, 11); // WiFi - SSID and Password strcpy(ssid, "RECALL"); strcpy(pw, "BBBB9999"); 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; ESP_LOGI(TAG,"Config Saved"); }