1 Commits

Author SHA256 Message Date
d14ae528c0 silence error due to missing hardware 2025-02-17 21:18:48 +01:00
11 changed files with 207 additions and 583 deletions

View File

@ -19,11 +19,7 @@ Sntp <|-- Metrics
class Inputs{ class Inputs{
+initInputs() +initInputs()
-initMeasurement()
-updateAverage()
-updatePrediction()
-taskInput() -taskInput()
-linearRegressionPredict()
+getChamberTemperature() +getChamberTemperature()
+getOutdoorTemperature() +getOutdoorTemperature()
+getInletFlowTemperature() +getInletFlowTemperature()
@ -42,11 +38,7 @@ Sntp <|-- Metrics
} }
class Control{ class Control{
initControl()
+taskControl() +taskControl()
+getControlCurrentWeekday()
-findControlCurrentTemperatureEntry()
+getControlCurrentTemperatureEntry()
-controlTable -controlTable
+getControlState() +getControlState()
} }
@ -87,41 +79,33 @@ Sntp <|-- Metrics
burner_fault_pending 1 burner_fault_pending 1
circulation_pump_enabled 1 circulation_pump_enabled 1
burner_enabled 0 burner_enabled 0
safety_contact_enabled 1 safety_contact_enabled 0
chamber_temperature 37.250000 chamber_temperature 58.750000
chamber_temperature_avg10 37.237499 chamber_temperature_avg10 58.931252
chamber_temperature_avg60 37.438541 chamber_temperature_avg60 59.190475
chamber_temperature_damped 42.185040 chamber_temperature_pred60 55.870998
chamber_temperature_pred60 36.638443 inlet_flow_temperature 53.875000
inlet_flow_temperature 35.625000 inlet_flow_temperature_avg10 53.900002
inlet_flow_temperature_avg10 35.618752 inlet_flow_temperature_avg60 53.994320
inlet_flow_temperature_avg60 35.415627 inlet_flow_temperature_pred60 52.848743
inlet_flow_temperature_damped 39.431259 outdoor_temperature 18.000000
inlet_flow_temperature_pred60 36.078678 outdoor_temperature_avg10 18.006250
outdoor_temperature 14.687500 outdoor_temperature_avg60 18.002840
outdoor_temperature_avg10 14.662500 outdoor_temperature_pred60 18.050785
outdoor_temperature_avg60 14.646875 return_flow_temperature 48.625000
outdoor_temperature_damped 9.169084 return_flow_temperature_avg10 48.718750
outdoor_temperature_pred60 14.660233 return_flow_temperature_avg60 48.846592
return_flow_temperature 39.937500 return_flow_temperature_pred60 47.383083
return_flow_temperature_avg10 40.087502
return_flow_temperature_avg60 41.146873
return_flow_temperature_damped 32.385151
return_flow_temperature_pred60 37.311958
chamber_temperature_state 0 chamber_temperature_state 0
outdoor_temperature_state 0 outdoor_temperature_state 0
inlet_flow_temperature_state 0 inlet_flow_temperature_state 0
return_flow_temperature_state 0 return_flow_temperature_state 0
safety_state 0 safety_state 0
control_state 3 control_state 3
control_current_weekday 5
control_current_entry_time 17100
control_current_entry_chamber_temperature 80.000000
control_current_entry_return_flow_temperature 30.000000
sntp_state 0 sntp_state 0
system_unixtime 1762012743 system_unixtime 1735242392
uptime_seconds 465229 uptime_seconds 40
wifi_rssi -72 wifi_rssi -74
``` ```
#### Status Encoding #### Status Encoding

View File

@ -1,111 +1,41 @@
#include "control.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "inputs.h" #include "esp_timer.h"
#include "esp_log.h"
#include "control.h"
#include "outputs.h" #include "outputs.h"
#include "inputs.h"
#include "safety.h" #include "safety.h"
#include "sntp.h" #include "sntp.h"
#define PERIODIC_INTERVAL 1U // Run control loop every 1 second #define PERIODIC_INTERVAL 1U // run control loop every 1sec
// Temperature thresholds #define RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY 30.0
#define RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY 30.0f #define RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT 25.0
#define RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT 25.0f #define CHAMPER_TEMPERATURE_TARGET 80.0
#define CHAMBER_TEMPERATURE_TARGET 80.0f // Max cutoff temperature #define BURNER_FAULT_DETECTION_THRESHOLD (60U * 3U) // Detect burner fault if after 3 minutes no burner start detected
#define CHAMBER_TEMPERATURE_THRESHOLD 45.0f // Min threshold for burner enable
#define SUMMER_MODE_TEMPERATURE_THRESHOLD_HIGH \
20.0f // Summer mode will be activated
#define SUMMER_MODE_TEMPERATURE_THRESHOLD_LOW \
15.0f // Summer mode will be deactivated --> Heating starts
#define CIRCULATION_PUMP_TEMPERATURE_THRESHOLD \
30.0f // Min threshold of chamber for circulation pump enable
#define BURNER_FAULT_DETECTION_THRESHOLD \
(60U * 4U) // Burner fault detection after 4 minutes
static const char *TAG = "smart-oil-heater-control-system-control"; static const char *TAG = "smart-oil-heater-control-system-control";
static eControlState gControlState = CONTROL_STARTING; static eControlState sControlState = CONTROL_STARTING;
// Control table for daily schedules
static const sControlDay gControlTable[] = { static sControlDay aControlTable[] = {
{MONDAY, {MONDAY, 2U, {{{4, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMPER_TEMPERATURE_TARGET}, {{22, 0}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, CHAMPER_TEMPERATURE_TARGET}}},
2U, {TUESDAY, 2U, {{{4, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMPER_TEMPERATURE_TARGET}, {{22, 0}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, CHAMPER_TEMPERATURE_TARGET}}},
{{{4, 45}, {WEDNESDAY, 2U, {{{4, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMPER_TEMPERATURE_TARGET}, {{22, 0}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, CHAMPER_TEMPERATURE_TARGET}}},
RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, {THURSDAY, 2U, {{{4, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMPER_TEMPERATURE_TARGET}, {{22, 0}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, CHAMPER_TEMPERATURE_TARGET}}},
CHAMBER_TEMPERATURE_TARGET}, {FRIDAY, 2U, {{{4, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMPER_TEMPERATURE_TARGET}, {{23, 0}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, CHAMPER_TEMPERATURE_TARGET}}},
{{22, 0}, {SATURDAY, 2U, {{{6, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMPER_TEMPERATURE_TARGET}, {{23, 30}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, CHAMPER_TEMPERATURE_TARGET}}},
RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, {SUNDAY, 2U, {{{6, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMPER_TEMPERATURE_TARGET}, {{22, 30}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, CHAMPER_TEMPERATURE_TARGET}}},
CHAMBER_TEMPERATURE_TARGET}}}, };
{TUESDAY,
2U,
{{{4, 45},
RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY,
CHAMBER_TEMPERATURE_TARGET},
{{22, 0},
RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT,
CHAMBER_TEMPERATURE_TARGET}}},
{WEDNESDAY,
2U,
{{{4, 45},
RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY,
CHAMBER_TEMPERATURE_TARGET},
{{22, 0},
RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT,
CHAMBER_TEMPERATURE_TARGET}}},
{THURSDAY,
2U,
{{{4, 45},
RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY,
CHAMBER_TEMPERATURE_TARGET},
{{22, 0},
RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT,
CHAMBER_TEMPERATURE_TARGET}}},
{FRIDAY,
2U,
{{{4, 45},
RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY,
CHAMBER_TEMPERATURE_TARGET},
{{23, 0},
RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT,
CHAMBER_TEMPERATURE_TARGET}}},
{SATURDAY,
2U,
{{{6, 45},
RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY,
CHAMBER_TEMPERATURE_TARGET},
{{23, 30},
RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT,
CHAMBER_TEMPERATURE_TARGET}}},
{SUNDAY,
2U,
{{{6, 45},
RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY,
CHAMBER_TEMPERATURE_TARGET},
{{22, 30},
RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT,
CHAMBER_TEMPERATURE_TARGET}}},
};
static sControlTemperatureEntry gCurrentControlEntry =
gControlTable[0].aTemperatureEntries[0];
static SemaphoreHandle_t xMutexAccessControl = NULL;
// Function prototypes
void taskControl(void *pvParameters); void taskControl(void *pvParameters);
void findControlCurrentTemperatureEntry(void); eControlWeekday getCurrentWeekday(void);
void setControlState(eControlState state); sControlTemperatureEntry getCurrentTemperatureEntry(void);
void initControl(void) void initControl(void)
{ {
BaseType_t taskCreated = xTaskCreate(
xMutexAccessControl = xSemaphoreCreateRecursiveMutex(); taskControl, // Function to implement the task
if (xMutexAccessControl == NULL)
{
ESP_LOGE(TAG, "Unable to create mutex");
}
xSemaphoreGiveRecursive(xMutexAccessControl);
BaseType_t taskCreated =
xTaskCreate(taskControl, // Function to implement the task
"taskControl", // Task name "taskControl", // Task name
8192, // Stack size (in words, not bytes) 8192, // Stack size (in words, not bytes)
NULL, // Parameters to the task function (none in this case) NULL, // Parameters to the task function (none in this case)
@ -126,293 +56,182 @@ void initControl(void)
void taskControl(void *pvParameters) void taskControl(void *pvParameters)
{ {
bool bHeatingInAction = false; bool bHeatingInAction = false;
bool bSummerMode = false; bool bBurnerFaultDetected = false;
eBurnerState burnerState = BURNER_UNKNOWN;
int64_t i64BurnerEnableTimestamp = esp_timer_get_time(); int64_t i64BurnerEnableTimestamp = esp_timer_get_time();
time_t now;
while (1) while (1)
{ {
// Get the current time
time(&now);
ESP_LOGW(TAG, "Control loop time: %lli", now);
vTaskDelay(PERIODIC_INTERVAL * 1000U / portTICK_PERIOD_MS); vTaskDelay(PERIODIC_INTERVAL * 1000U / portTICK_PERIOD_MS);
// Check for safety faults
if (getSafetyState() != SAFETY_NO_ERROR) if (getSafetyState() != SAFETY_NO_ERROR)
{ {
ESP_LOGW(TAG, "Control not possible due to safety fault!"); //ESP_LOGW(TAG, "Control not possible due to safety fault!");
setControlState(CONTROL_FAULT_SAFETY); sControlState = CONTROL_FAULT_SAFETY;
if (bHeatingInAction) if (bHeatingInAction == true)
{ {
ESP_LOGW(TAG, "Disabling burner due to safety fault"); ESP_LOGW(TAG, "Control not possible due to safety fault: Disable burner");
bHeatingInAction = false; bHeatingInAction = false;
setCirculationPumpState(ENABLED);
setBurnerState(DISABLED); setBurnerState(DISABLED);
setSafetyControlState(ENABLED); setSafetyControlState(ENABLED);
} }
continue; continue;
} }
// Check for SNTP faults
if (getSntpState() != SYNC_SUCCESSFUL) if (getSntpState() != SYNC_SUCCESSFUL)
{ {
ESP_LOGW(TAG, "Control not possible due to SNTP fault!"); ESP_LOGW(TAG, "Control not possible due to sntp fault!");
setControlState(CONTROL_FAULT_SNTP); sControlState = CONTROL_FAULT_SNTP;
if (bHeatingInAction) if (bHeatingInAction == true)
{ {
ESP_LOGW(TAG, "Disabling burner due to SNTP fault"); ESP_LOGW(TAG, "Control not possible due to sntp fault: Disable burner");
bHeatingInAction = false; bHeatingInAction = false;
setCirculationPumpState(ENABLED);
setBurnerState(DISABLED); setBurnerState(DISABLED);
setSafetyControlState(ENABLED); setSafetyControlState(ENABLED);
} }
continue; continue;
} }
findControlCurrentTemperatureEntry(); sControlTemperatureEntry currentControlEntry = getCurrentTemperatureEntry();
// ESP_LOGI(TAG, "Control Entry Hour: %i Minute: %i ChamberTemp: %lf ReturnFlowTemp: %lf", currentControlEntry.timestamp.hour, currentControlEntry.timestamp.minute, currentControlEntry.fChamberTemperature, currentControlEntry.fReturnFlowTemperature);
if (getOutdoorTemperature().fDampedValue >= if (bHeatingInAction == true)
SUMMER_MODE_TEMPERATURE_THRESHOLD_HIGH)
{ {
bSummerMode = true; if ((getChamberTemperature().fCurrentValue >= currentControlEntry.fChamberTemperature) || (getChamberTemperature().predict60s.fValue >= currentControlEntry.fChamberTemperature))
}
else if (getOutdoorTemperature().fDampedValue <=
SUMMER_MODE_TEMPERATURE_THRESHOLD_LOW)
{ {
bSummerMode = false; ESP_LOGI(TAG, "Chamber Target Temperature reached: Disable burner");
} bHeatingInAction = false;
setCirculationPumpState(ENABLED);
// Enable burner if outdoor temperature is low and return flow temperature
// is cooled down
if (!bHeatingInAction && (burnerState != BURNER_FAULT))
{
if (bSummerMode)
{
// ESP_LOGI(TAG, "Outdoor temperature too warm: Disabling heating");
setBurnerState(DISABLED); setBurnerState(DISABLED);
setSafetyControlState(DISABLED);
setControlState(CONTROL_OUTDOOR_TOO_WARM);
}
else if ((getReturnFlowTemperature().average60s.fValue <=
getControlCurrentTemperatureEntry().fReturnFlowTemperature) &&
(getChamberTemperature().fCurrentValue <=
CHAMBER_TEMPERATURE_THRESHOLD))
{
ESP_LOGI(TAG,
"Enabling burner: Return flow temperature target reached");
burnerState = BURNER_UNKNOWN;
bHeatingInAction = true;
setBurnerState(ENABLED);
setSafetyControlState(ENABLED); setSafetyControlState(ENABLED);
i64BurnerEnableTimestamp = esp_timer_get_time();
setControlState(CONTROL_HEATING);
} }
else else
{ {
// ESP_LOGI(TAG, "Return flow temperature too warm: Disabling heating");
setControlState(CONTROL_RETURN_FLOW_TOO_WARM);
}
}
// Disable burner if target temperature is reached or a fault occurred
if (bHeatingInAction) if (bHeatingInAction)
{ {
if ((getChamberTemperature().fCurrentValue >= int64_t i64Delta = esp_timer_get_time() - i64BurnerEnableTimestamp;
getControlCurrentTemperatureEntry().fChamberTemperature) ||
(getChamberTemperature().predict60s.fValue >= if ((i64Delta / 1000000U) >= BURNER_FAULT_DETECTION_THRESHOLD)
getControlCurrentTemperatureEntry().fChamberTemperature))
{
ESP_LOGI(TAG, "Chamber target temperature reached: Disabling burner");
bHeatingInAction = false;
setBurnerState(DISABLED);
setSafetyControlState(ENABLED);
}
else if (esp_timer_get_time() - i64BurnerEnableTimestamp >=
BURNER_FAULT_DETECTION_THRESHOLD * 1000000U)
{
if (burnerState == BURNER_UNKNOWN)
{ {
if (getBurnerError() == FAULT) if (getBurnerError() == FAULT)
{ {
// ESP_LOGW(TAG, "Burner fault detected: Disabling burner"); ESP_LOGW(TAG, "Detected burner fault after %lli seconds!", (i64Delta / 1000000U));
ESP_LOGW(TAG, "Control not possible due to burner fault: Disable burner");
sControlState = CONTROL_FAULT_BURNER;
bHeatingInAction = false; bHeatingInAction = false;
burnerState = BURNER_FAULT; bBurnerFaultDetected = true;
setControlState(CONTROL_FAULT_BURNER); setCirculationPumpState(ENABLED);
setBurnerState(DISABLED); setBurnerState(DISABLED);
setSafetyControlState(ENABLED); setSafetyControlState(ENABLED);
} }
else
{
// ESP_LOGI(TAG, "No burner fault detected: Marking burner as
// fired");
burnerState = BURNER_FIRED;
} }
} }
} }
} }
// Manage circulation pump if ((bHeatingInAction == false) && (bBurnerFaultDetected == false))
if (getChamberTemperature().fCurrentValue <=
CIRCULATION_PUMP_TEMPERATURE_THRESHOLD)
{ {
// ESP_LOGI(TAG, "Burner cooled down: Disabling circulation pump"); if ((getReturnFlowTemperature().average60s.fValue <= currentControlEntry.fReturnFlowTemperature) && (getChamberTemperature().fCurrentValue <= 45.0))
setCirculationPumpState(DISABLED);
}
else
{ {
// ESP_LOGI(TAG, "Burner heated: Enabling circulation pump"); ESP_LOGI(TAG, "Return Flow Target Temperature reached: Enable Burner");
bHeatingInAction = true;
setCirculationPumpState(ENABLED); setCirculationPumpState(ENABLED);
} setBurnerState(ENABLED);
} // End of while(1) setSafetyControlState(ENABLED);
} i64BurnerEnableTimestamp = esp_timer_get_time();
sControlState = CONTROL_HEATING;
void setControlState(eControlState state)
{
if (xSemaphoreTakeRecursive(xMutexAccessControl, pdMS_TO_TICKS(5000)) == pdTRUE)
{
gControlState = state;
xSemaphoreGiveRecursive(xMutexAccessControl);
} }
else else
{ {
ESP_LOGE(TAG, "Unable to take mutex: setControlState()"); sControlState = CONTROL_RETURN_FLOW_TOO_WARM;
}
}
} }
} }
eControlState getControlState(void) eControlState getControlState(void)
{ {
return sControlState;
}
eControlState ret = CONTROL_FAULT_SAFETY; eControlWeekday getCurrentWeekday(void)
if (xSemaphoreTakeRecursive(xMutexAccessControl, pdMS_TO_TICKS(5000)) == pdTRUE)
{ {
ret = gControlState; time_t now;
xSemaphoreGiveRecursive(xMutexAccessControl); struct tm *timeinfo;
// Get the current time
time(&now);
timeinfo = localtime(&now); // Convert to local time
// Get the day of the week (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
int day = timeinfo->tm_wday;
// Adjust so that Monday = 0, Sunday = 6
if (day == 0)
{
day = 6; // Sunday becomes 6
} }
else else
{ {
ESP_LOGE(TAG, "Unable to take mutex: getControlState()"); day -= 1; // Shift other days to make Monday = 0
} }
return ret; return (eControlWeekday)day;
} }
eControlWeekday getControlCurrentWeekday(void) sControlTemperatureEntry getCurrentTemperatureEntry(void)
{ {
// Get current time sControlTemperatureEntry result = aControlTable[0].aTemperatureEntries[0];
eControlWeekday currentDay = getCurrentWeekday();
time_t now; time_t now;
struct tm timeinfo; struct tm timeinfo;
// Get the current time
time(&now); time(&now);
// Convert to local time structure
localtime_r(&now, &timeinfo); localtime_r(&now, &timeinfo);
// Extract hour and minute
int hour = timeinfo.tm_hour; // Hour (0-23)
int minute = timeinfo.tm_min; // Minute (0-59)u
int day = timeinfo.tm_wday; // ESP_LOGI(TAG, "Current Day: %i Hour: %i Minute: %i", currentDay, hour, minute);
return (eControlWeekday)((day == 0) ? 6 : day - 1);
}
/** for (int i = 0; i < sizeof(aControlTable) / sizeof(aControlTable[0]); i++)
* @brief Finds the active temperature control entry for the current time.
*
* Searches through the weekly schedule to find the most recent entry
* that should be active at the current date/time. Falls back to the
* last entry in the week if no suitable entry is found.
*/
/**
* @brief Finds the active temperature control entry for the current time.
*
* Searches through the weekly schedule to find the most recent entry
* that should be active at the current date/time. Falls back to the
* last entry in the week if no suitable entry is found.
*/
void findControlCurrentTemperatureEntry(void)
{ {
eControlWeekday currentDay = getControlCurrentWeekday(); /// loops through days
// ESP_LOGI(TAG, "Day %d: %d", i + 1, aControlTable[i].day);
// int numberOfEntries = aControlTable[i].entryCount;
// ESP_LOGI(TAG, "Number of entries: %i", numberOfEntries);
// Get current time for (int j = 0; j < aControlTable[i].entryCount; j++)
time_t now;
struct tm timeinfo;
time(&now);
localtime_r(&now, &timeinfo);
int currentHour = timeinfo.tm_hour;
int currentMinute = timeinfo.tm_min;
if (xSemaphoreTakeRecursive(xMutexAccessControl, pdMS_TO_TICKS(5000)) == pdTRUE)
{ {
if ((aControlTable[i].day) > currentDay)
// ESP_LOGI(TAG, "Searching for control entry - Day: %d, Time: %02d:%02d", currentDay, currentHour, currentMinute);
// Search through all days and entries
for (int dayIndex = 0; dayIndex < 7; dayIndex++)
{ {
const sControlDay *day = &gControlTable[dayIndex]; // ESP_LOGI(TAG, "DAY Return Control Entry Day: %i Hour: %i Minute: %i ChamberTemp: %lf ReturnFlowTemp: %lf", aControlTable[i].day, aControlTable[i].aTemperatureEntries[j].timestamp.hour, aControlTable[i].aTemperatureEntries[j].timestamp.minute, aControlTable[i].aTemperatureEntries[j].fChamberTemperature, aControlTable[i].aTemperatureEntries[j].fReturnFlowTemperature);
return result;
for (int entryIndex = 0; entryIndex < day->entryCount; entryIndex++)
{
const sControlTemperatureEntry *entry = &day->aTemperatureEntries[entryIndex];
// Check if this entry is in the future (next active entry)
bool isFutureDay = (day->day > currentDay);
bool isTodayFutureTime = (day->day == currentDay) &&
((entry->timestamp.hour > currentHour) ||
(entry->timestamp.hour == currentHour &&
entry->timestamp.minute > currentMinute));
if (isFutureDay || isTodayFutureTime)
{
// Found next scheduled entry, so determine the previous (active) one
if (entryIndex > 0)
{
// Use previous entry from same day
gCurrentControlEntry = day->aTemperatureEntries[entryIndex - 1];
}
else if (dayIndex > 0)
{
// Use last entry from previous day
const sControlDay *previousDay = &gControlTable[dayIndex - 1];
gCurrentControlEntry = previousDay->aTemperatureEntries[previousDay->entryCount - 1];
}
else
{
// First entry of the week - wrap to last entry of Sunday
const sControlDay *sunday = &gControlTable[6];
gCurrentControlEntry = sunday->aTemperatureEntries[sunday->entryCount - 1];
}
/*
ESP_LOGI(TAG, "Active entry found - Time: %02d:%02d, "
"Return Temp: %lf, Chamber Temp: %lf",
gCurrentControlEntry.timestamp.hour,
gCurrentControlEntry.timestamp.minute,
gCurrentControlEntry.fReturnFlowTemperature,
gCurrentControlEntry.fChamberTemperature);
*/
return;
}
}
} }
// If we reached here, current time is after all entries this week if ((aControlTable[i].day == currentDay) && (aControlTable[i].aTemperatureEntries[j].timestamp.hour > hour))
// Use the last entry (Sunday evening)
const sControlDay *sunday = &gControlTable[6];
gCurrentControlEntry = sunday->aTemperatureEntries[sunday->entryCount - 1];
// ESP_LOGI(TAG, "Using last entry of week - Time: %02d:%02d", gCurrentControlEntry.timestamp.hour, gCurrentControlEntry.timestamp.minute);
xSemaphoreGiveRecursive(xMutexAccessControl);
}
else
{ {
ESP_LOGE(TAG, "Unable to take mutex: findControlCurrentTemperatureEntry()"); // ESP_LOGI(TAG, "HOUR Return Control Entry Day: %i Hour: %i Minute: %i ChamberTemp: %lf ReturnFlowTemp: %lf", aControlTable[i].day, aControlTable[i].aTemperatureEntries[j].timestamp.hour, aControlTable[i].aTemperatureEntries[j].timestamp.minute, aControlTable[i].aTemperatureEntries[j].fChamberTemperature, aControlTable[i].aTemperatureEntries[j].fReturnFlowTemperature);
} return result;
} }
sControlTemperatureEntry getControlCurrentTemperatureEntry(void) if ((aControlTable[i].day == currentDay) && (aControlTable[i].aTemperatureEntries[j].timestamp.hour == hour) && (aControlTable[i].aTemperatureEntries[j].timestamp.minute == minute))
{ {
sControlTemperatureEntry ret = gControlTable[0].aTemperatureEntries[0]; // ESP_LOGI(TAG, "MINUTE Return Control Entry Day: %i Hour: %i Minute: %i ChamberTemp: %lf ReturnFlowTemp: %lf", aControlTable[i].day, aControlTable[i].aTemperatureEntries[j].timestamp.hour, aControlTable[i].aTemperatureEntries[j].timestamp.minute, aControlTable[i].aTemperatureEntries[j].fChamberTemperature, aControlTable[i].aTemperatureEntries[j].fReturnFlowTemperature);
if (xSemaphoreTakeRecursive(xMutexAccessControl, pdMS_TO_TICKS(5000)) == pdTRUE) return result;
{
ret = gCurrentControlEntry;
xSemaphoreGiveRecursive(xMutexAccessControl);
}
else
{
ESP_LOGE(TAG, "Unable to take mutex: getControlCurrentTemperatureEntry()");
} }
return ret; // ESP_LOGI(TAG, "SET Return Control Entry Day: %i Hour: %i Minute: %i ChamberTemp: %lf ReturnFlowTemp: %lf", aControlTable[i].day, aControlTable[i].aTemperatureEntries[j].timestamp.hour, aControlTable[i].aTemperatureEntries[j].timestamp.minute, aControlTable[i].aTemperatureEntries[j].fChamberTemperature, aControlTable[i].aTemperatureEntries[j].fReturnFlowTemperature);
result = aControlTable[i].aTemperatureEntries[j];
}
}
return result;
} }

View File

@ -14,13 +14,6 @@ typedef enum _ControlState
CONTROL_FAULT_SNTP, CONTROL_FAULT_SNTP,
} eControlState; } eControlState;
typedef enum _BurnerState
{
BURNER_UNKNOWN, // Burner is disabled or state after enabling is still unkown
BURNER_FIRED, // Burner fired successfully
BURNER_FAULT // Burner was unable to fire successfully
} eBurnerState;
typedef enum _ControlWeekday typedef enum _ControlWeekday
{ {
MONDAY, MONDAY,
@ -54,5 +47,3 @@ typedef struct _ControlDay
void initControl(void); void initControl(void);
eControlState getControlState(void); eControlState getControlState(void);
eControlWeekday getControlCurrentWeekday(void);
sControlTemperatureEntry getControlCurrentTemperatureEntry(void);

View File

@ -17,7 +17,7 @@ const uint8_t uBurnerFaultPin = 19U;
const uint8_t uDS18B20Pin = 4U; const uint8_t uDS18B20Pin = 4U;
const onewire_addr_t uChamperTempSensorAddr = 0xd00000108cd01d28; const onewire_addr_t uChamperTempSensorAddr = 0xd00000108cd01d28;
const onewire_addr_t uOutdoorTempSensorAddr = 0xd70000108a9b9128; const onewire_addr_t uOutdoorTempSensorAddr = 0x78000000c6c2f728;
const onewire_addr_t uInletFlowTempSensorAddr = 0x410000108b8c0628; const onewire_addr_t uInletFlowTempSensorAddr = 0x410000108b8c0628;
const onewire_addr_t uReturnFlowTempSensorAddr = 0x90000108cc77c28; const onewire_addr_t uReturnFlowTempSensorAddr = 0x90000108cc77c28;
@ -49,12 +49,7 @@ void initInputs(void)
.intr_type = GPIO_INTR_DISABLE // Disable interrupts .intr_type = GPIO_INTR_DISABLE // Disable interrupts
}; };
esp_err_t ret = gpio_config(&ioConfBurnerFault); gpio_config(&ioConfBurnerFault);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "GPIO config failed: %s", esp_err_to_name(ret));
return;
}
xMutexAccessInputs = xSemaphoreCreateRecursiveMutex(); xMutexAccessInputs = xSemaphoreCreateRecursiveMutex();
if (xMutexAccessInputs == NULL) if (xMutexAccessInputs == NULL)
@ -93,23 +88,22 @@ void initMeasurement(sMeasurement *pMeasurement)
return; return;
pMeasurement->state = MEASUREMENT_FAULT; pMeasurement->state = MEASUREMENT_FAULT;
pMeasurement->fCurrentValue = INITIALISATION_VALUE; pMeasurement->fCurrentValue = 0.0f;
pMeasurement->fDampedValue = INITIALISATION_VALUE;
pMeasurement->average10s.fValue = INITIALISATION_VALUE; pMeasurement->average10s.fValue = 0.0f;
pMeasurement->average10s.bufferCount = 0U; pMeasurement->average10s.bufferCount = 0U;
pMeasurement->average10s.bufferIndex = 0U; pMeasurement->average10s.bufferIndex = 0U;
memset(pMeasurement->average10s.samples, 0U, sizeof(float) * AVG10S_SAMPLE_SIZE); memset(pMeasurement->average10s.samples, 0U, AVG10_SAMPLE_SIZE);
pMeasurement->average60s.fValue = INITIALISATION_VALUE; pMeasurement->average60s.fValue = 0.0f;
pMeasurement->average60s.bufferCount = 0U; pMeasurement->average60s.bufferCount = 0U;
pMeasurement->average60s.bufferIndex = 0U; pMeasurement->average60s.bufferIndex = 0U;
memset(pMeasurement->average60s.samples, 0U, sizeof(float) * AVG60S_SAMPLE_SIZE); memset(pMeasurement->average60s.samples, 0U, AVG60_SAMPLE_SIZE);
pMeasurement->predict60s.fValue = INITIALISATION_VALUE; pMeasurement->predict60s.fValue = 0.0f;
pMeasurement->predict60s.bufferCount = 0U; pMeasurement->predict60s.bufferCount = 0U;
pMeasurement->predict60s.bufferIndex = 0U; pMeasurement->predict60s.bufferIndex = 0U;
memset(pMeasurement->predict60s.samples, 0U, sizeof(float) * PRED60S_SAMPLE_SIZE); memset(pMeasurement->predict60s.samples, 0U, PRED60_SAMPLE_SIZE);
} }
void updateAverage(sMeasurement *pMeasurement) void updateAverage(sMeasurement *pMeasurement)
@ -119,33 +113,26 @@ void updateAverage(sMeasurement *pMeasurement)
// Average form the last 10sec // Average form the last 10sec
pMeasurement->average10s.samples[pMeasurement->average10s.bufferIndex] = pMeasurement->fCurrentValue; pMeasurement->average10s.samples[pMeasurement->average10s.bufferIndex] = pMeasurement->fCurrentValue;
pMeasurement->average10s.bufferIndex = (pMeasurement->average10s.bufferIndex + 1) % AVG10S_SAMPLE_SIZE; pMeasurement->average10s.bufferIndex = (pMeasurement->average10s.bufferIndex + 1) % AVG10_SAMPLE_SIZE;
if (pMeasurement->average10s.bufferCount < AVG10S_SAMPLE_SIZE) if (pMeasurement->average10s.bufferCount < AVG10_SAMPLE_SIZE)
{ {
pMeasurement->average10s.bufferCount++; pMeasurement->average10s.bufferCount++;
} }
float sum = 0.0; float sum = 0.0;
for (int i = 0; i < pMeasurement->average10s.bufferCount; i++) for (int i = 0; i <= pMeasurement->average10s.bufferCount; i++)
{ {
sum += pMeasurement->average10s.samples[i]; sum += pMeasurement->average10s.samples[i];
} }
if (pMeasurement->average10s.bufferCount == 0U)
{
pMeasurement->average10s.fValue = 0.0f;
}
else
{
pMeasurement->average10s.fValue = sum / pMeasurement->average10s.bufferCount; pMeasurement->average10s.fValue = sum / pMeasurement->average10s.bufferCount;
}
// Average form the last 60sec // Average form the last 60sec
pMeasurement->average60s.samples[pMeasurement->average60s.bufferIndex] = pMeasurement->fCurrentValue; pMeasurement->average60s.samples[pMeasurement->average60s.bufferIndex] = pMeasurement->fCurrentValue;
pMeasurement->average60s.bufferIndex = (pMeasurement->average60s.bufferIndex + 1) % AVG60S_SAMPLE_SIZE; pMeasurement->average60s.bufferIndex = (pMeasurement->average60s.bufferIndex + 1) % AVG60_SAMPLE_SIZE;
if (pMeasurement->average60s.bufferCount < AVG60S_SAMPLE_SIZE) if (pMeasurement->average60s.bufferCount < AVG60_SAMPLE_SIZE)
{ {
pMeasurement->average60s.bufferCount++; pMeasurement->average60s.bufferCount++;
} }
@ -156,34 +143,9 @@ void updateAverage(sMeasurement *pMeasurement)
sum += pMeasurement->average60s.samples[i]; sum += pMeasurement->average60s.samples[i];
} }
if (pMeasurement->average60s.bufferCount == 0U)
{
pMeasurement->average60s.fValue = 0.0f;
}
else
{
pMeasurement->average60s.fValue = sum / pMeasurement->average60s.bufferCount; pMeasurement->average60s.fValue = sum / pMeasurement->average60s.bufferCount;
} }
// Damped current value
if (pMeasurement->fDampedValue == INITIALISATION_VALUE)
{
pMeasurement->fDampedValue = pMeasurement->fCurrentValue;
}
else
{
if (pMeasurement->fCurrentValue > pMeasurement->fDampedValue)
{
pMeasurement->fDampedValue = pMeasurement->fDampedValue + (DAMPING_FACTOR_WARMER * (pMeasurement->fCurrentValue - pMeasurement->fDampedValue));
}
if (pMeasurement->fCurrentValue < pMeasurement->fDampedValue)
{
pMeasurement->fDampedValue = pMeasurement->fDampedValue - (DAMPING_FACTOR_COLDER * (pMeasurement->fDampedValue - pMeasurement->fCurrentValue));
}
}
}
void updatePrediction(sMeasurement *pMeasurement) void updatePrediction(sMeasurement *pMeasurement)
{ {
if (!pMeasurement) if (!pMeasurement)
@ -192,8 +154,8 @@ void updatePrediction(sMeasurement *pMeasurement)
// Update predict60s buffer // Update predict60s buffer
sPredict *predict60s = &pMeasurement->predict60s; sPredict *predict60s = &pMeasurement->predict60s;
predict60s->samples[predict60s->bufferIndex] = pMeasurement->fCurrentValue; predict60s->samples[predict60s->bufferIndex] = pMeasurement->fCurrentValue;
predict60s->bufferIndex = (predict60s->bufferIndex + 1) % PRED60S_SAMPLE_SIZE; predict60s->bufferIndex = (predict60s->bufferIndex + 1) % PRED60_SAMPLE_SIZE;
if (predict60s->bufferCount < PRED60S_SAMPLE_SIZE) if (predict60s->bufferCount < PRED60_SAMPLE_SIZE)
predict60s->bufferCount++; predict60s->bufferCount++;
// Predict 60s future value using linear regression // Predict 60s future value using linear regression
@ -227,12 +189,12 @@ void taskInput(void *pvParameters)
if (ds18x20_scan_devices(uDS18B20Pin, uOneWireAddresses, MAX_DN18B20_SENSORS, &sSensorCount) != ESP_OK) if (ds18x20_scan_devices(uDS18B20Pin, uOneWireAddresses, MAX_DN18B20_SENSORS, &sSensorCount) != ESP_OK)
{ {
ESP_LOGE(TAG, "1-Wire device scan error!"); // ESP_LOGE(TAG, "1-Wire device scan error!");
} }
if (!sSensorCount) if (!sSensorCount)
{ {
ESP_LOGW(TAG, "No 1-Wire devices detected!"); // ESP_LOGW(TAG, "No 1-Wire devices detected!");
} }
else else
{ {
@ -241,14 +203,14 @@ void taskInput(void *pvParameters)
if (sSensorCount > MAX_DN18B20_SENSORS) if (sSensorCount > MAX_DN18B20_SENSORS)
{ {
sSensorCount = MAX_DN18B20_SENSORS; sSensorCount = MAX_DN18B20_SENSORS;
ESP_LOGW(TAG, "More 1-Wire devices found than expected!"); // ESP_LOGW(TAG, "More 1-Wire devices found than expected!");
} }
for (size_t iReadLoop = 0; iReadLoop < ONE_WIRE_LOOPS; iReadLoop++) for (size_t iReadLoop = 0; iReadLoop < ONE_WIRE_LOOPS; iReadLoop++)
{ {
if (ds18x20_measure_and_read_multi(uDS18B20Pin, uOneWireAddresses, sSensorCount, fDS18B20Temps) != ESP_OK) if (ds18x20_measure_and_read_multi(uDS18B20Pin, uOneWireAddresses, sSensorCount, fDS18B20Temps) != ESP_OK)
{ {
ESP_LOGE(TAG, "1-Wire devices read error"); // ESP_LOGE(TAG, "1-Wire devices read error");
vTaskDelay(PERIODIC_INTERVAL * 100U / portTICK_PERIOD_MS); // Wait 100ms if bus error occurred vTaskDelay(PERIODIC_INTERVAL * 100U / portTICK_PERIOD_MS); // Wait 100ms if bus error occurred
} }
else else
@ -309,9 +271,9 @@ void taskInput(void *pvParameters)
float linearRegressionPredict(const float *samples, size_t count, size_t bufferIndex, float futureIndex) float linearRegressionPredict(const float *samples, size_t count, size_t bufferIndex, float futureIndex)
{ {
if (count == 0) if (count == 0)
return INITIALISATION_VALUE; // No prediction possible with no data return 0.0f; // No prediction possible with no data
float sumX = INITIALISATION_VALUE, sumY = INITIALISATION_VALUE, sumXY = INITIALISATION_VALUE, sumX2 = INITIALISATION_VALUE; float sumX = 0.0f, sumY = 0.0f, sumXY = 0.0f, sumX2 = 0.0f;
for (size_t i = 0; i < count; i++) for (size_t i = 0; i < count; i++)
{ {

View File

@ -1,13 +1,9 @@
#pragma once #pragma once
#define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b))
#define INITIALISATION_VALUE 0.0f #define AVG10_SAMPLE_SIZE 10U
#define AVG10S_SAMPLE_SIZE 10U #define AVG60_SAMPLE_SIZE 60U
#define AVG60S_SAMPLE_SIZE 60U #define PRED60_SAMPLE_SIZE 60U
#define AVG24H_SAMPLE_SIZE 24U
#define PRED60S_SAMPLE_SIZE 60U
#define DAMPING_FACTOR_WARMER 0.00001f // 0.001%
#define DAMPING_FACTOR_COLDER 0.00005f // 0.005%
typedef enum _BurnerErrorState typedef enum _BurnerErrorState
{ {
@ -24,7 +20,7 @@ typedef enum _MeasurementErrorState
typedef struct _Average typedef struct _Average
{ {
float fValue; float fValue;
float samples[MAX(AVG10S_SAMPLE_SIZE, MAX(AVG60S_SAMPLE_SIZE, AVG24H_SAMPLE_SIZE))]; float samples[MAX(AVG10_SAMPLE_SIZE, AVG60_SAMPLE_SIZE)];
size_t bufferIndex; size_t bufferIndex;
size_t bufferCount; size_t bufferCount;
} sAverage; } sAverage;
@ -32,7 +28,7 @@ typedef struct _Average
typedef struct _Predict typedef struct _Predict
{ {
float fValue; float fValue;
float samples[PRED60S_SAMPLE_SIZE]; float samples[PRED60_SAMPLE_SIZE];
size_t bufferIndex; size_t bufferIndex;
size_t bufferCount; size_t bufferCount;
} sPredict; } sPredict;
@ -40,7 +36,6 @@ typedef struct _Predict
typedef struct _Measurement typedef struct _Measurement
{ {
float fCurrentValue; float fCurrentValue;
float fDampedValue;
sAverage average10s; sAverage average10s;
sAverage average60s; sAverage average60s;
sPredict predict60s; sPredict predict60s;

View File

@ -128,12 +128,6 @@ void taskMetrics(void *pvParameters)
aMetrics[u16MetricCounter].fMetricValue = getChamberTemperature().average60s.fValue; aMetrics[u16MetricCounter].fMetricValue = getChamberTemperature().average60s.fValue;
u16MetricCounter++; u16MetricCounter++;
// Chamber Temperature Damped
strcpy(aMetrics[u16MetricCounter].caMetricName, "chamber_temperature_damped");
aMetrics[u16MetricCounter].type = FLOAT;
aMetrics[u16MetricCounter].fMetricValue = getChamberTemperature().fDampedValue;
u16MetricCounter++;
// Chamber Temperature Predict 60s // Chamber Temperature Predict 60s
strcpy(aMetrics[u16MetricCounter].caMetricName, "chamber_temperature_pred60"); strcpy(aMetrics[u16MetricCounter].caMetricName, "chamber_temperature_pred60");
aMetrics[u16MetricCounter].type = FLOAT; aMetrics[u16MetricCounter].type = FLOAT;
@ -158,12 +152,6 @@ void taskMetrics(void *pvParameters)
aMetrics[u16MetricCounter].fMetricValue = getInletFlowTemperature().average60s.fValue; aMetrics[u16MetricCounter].fMetricValue = getInletFlowTemperature().average60s.fValue;
u16MetricCounter++; u16MetricCounter++;
// Inlet Flow Temperature Damped
strcpy(aMetrics[u16MetricCounter].caMetricName, "inlet_flow_temperature_damped");
aMetrics[u16MetricCounter].type = FLOAT;
aMetrics[u16MetricCounter].fMetricValue = getInletFlowTemperature().fDampedValue;
u16MetricCounter++;
// Inlet Flow Temperature Predict 60s // Inlet Flow Temperature Predict 60s
strcpy(aMetrics[u16MetricCounter].caMetricName, "inlet_flow_temperature_pred60"); strcpy(aMetrics[u16MetricCounter].caMetricName, "inlet_flow_temperature_pred60");
aMetrics[u16MetricCounter].type = FLOAT; aMetrics[u16MetricCounter].type = FLOAT;
@ -188,12 +176,6 @@ void taskMetrics(void *pvParameters)
aMetrics[u16MetricCounter].fMetricValue = getOutdoorTemperature().average60s.fValue; aMetrics[u16MetricCounter].fMetricValue = getOutdoorTemperature().average60s.fValue;
u16MetricCounter++; u16MetricCounter++;
// Outdoor Temperature Average Damped
strcpy(aMetrics[u16MetricCounter].caMetricName, "outdoor_temperature_damped");
aMetrics[u16MetricCounter].type = FLOAT;
aMetrics[u16MetricCounter].fMetricValue = getOutdoorTemperature().fDampedValue;
u16MetricCounter++;
// Outdoor Temperature Predict 60s // Outdoor Temperature Predict 60s
strcpy(aMetrics[u16MetricCounter].caMetricName, "outdoor_temperature_pred60"); strcpy(aMetrics[u16MetricCounter].caMetricName, "outdoor_temperature_pred60");
aMetrics[u16MetricCounter].type = FLOAT; aMetrics[u16MetricCounter].type = FLOAT;
@ -218,12 +200,6 @@ void taskMetrics(void *pvParameters)
aMetrics[u16MetricCounter].fMetricValue = getReturnFlowTemperature().average60s.fValue; aMetrics[u16MetricCounter].fMetricValue = getReturnFlowTemperature().average60s.fValue;
u16MetricCounter++; u16MetricCounter++;
// Return Flow Temperature Damped
strcpy(aMetrics[u16MetricCounter].caMetricName, "return_flow_temperature_damped");
aMetrics[u16MetricCounter].type = FLOAT;
aMetrics[u16MetricCounter].fMetricValue = getReturnFlowTemperature().fDampedValue;
u16MetricCounter++;
// Return Flow Temperature Predict 60s // Return Flow Temperature Predict 60s
strcpy(aMetrics[u16MetricCounter].caMetricName, "return_flow_temperature_pred60"); strcpy(aMetrics[u16MetricCounter].caMetricName, "return_flow_temperature_pred60");
aMetrics[u16MetricCounter].type = FLOAT; aMetrics[u16MetricCounter].type = FLOAT;
@ -254,31 +230,6 @@ void taskMetrics(void *pvParameters)
aMetrics[u16MetricCounter].u8MetricValue = getControlState(); aMetrics[u16MetricCounter].u8MetricValue = getControlState();
u16MetricCounter++; u16MetricCounter++;
// Control Current Weekday
strcpy(aMetrics[u16MetricCounter].caMetricName, "control_current_weekday");
aMetrics[u16MetricCounter].type = INTEGER_U8;
aMetrics[u16MetricCounter].u8MetricValue = getControlCurrentWeekday();
u16MetricCounter++;
// Control Current Entry Time
strcpy(aMetrics[u16MetricCounter].caMetricName, "control_current_entry_time");
aMetrics[u16MetricCounter].type = INTEGER_64;
int64_t i64SecondsSinceMidnight = (getControlCurrentTemperatureEntry().timestamp.hour * 60U * 60U) + (getControlCurrentTemperatureEntry().timestamp.minute * 60U);
aMetrics[u16MetricCounter].i64MetricValue = i64SecondsSinceMidnight;
u16MetricCounter++;
// Control Current Entry Chamber Temperature
strcpy(aMetrics[u16MetricCounter].caMetricName, "control_current_entry_chamber_temperature");
aMetrics[u16MetricCounter].type = FLOAT;
aMetrics[u16MetricCounter].fMetricValue = getControlCurrentTemperatureEntry().fChamberTemperature;
u16MetricCounter++;
// Control Current Entry Return Flow Temperature
strcpy(aMetrics[u16MetricCounter].caMetricName, "control_current_entry_return_flow_temperature");
aMetrics[u16MetricCounter].type = FLOAT;
aMetrics[u16MetricCounter].fMetricValue = getControlCurrentTemperatureEntry().fReturnFlowTemperature;
u16MetricCounter++;
// SNTP State // SNTP State
strcpy(aMetrics[u16MetricCounter].caMetricName, "sntp_state"); strcpy(aMetrics[u16MetricCounter].caMetricName, "sntp_state");
aMetrics[u16MetricCounter].type = INTEGER_U8; aMetrics[u16MetricCounter].type = INTEGER_U8;
@ -301,23 +252,22 @@ void taskMetrics(void *pvParameters)
// Wifi RSSI // Wifi RSSI
wifi_ap_record_t ap; wifi_ap_record_t ap;
ap.rssi = 0U; esp_wifi_sta_get_ap_info(&ap);
ESP_ERROR_CHECK(esp_wifi_sta_get_ap_info(&ap));
strcpy(aMetrics[u16MetricCounter].caMetricName, "wifi_rssi"); strcpy(aMetrics[u16MetricCounter].caMetricName, "wifi_rssi");
aMetrics[u16MetricCounter].type = INTEGER_64; aMetrics[u16MetricCounter].type = INTEGER_64;
aMetrics[u16MetricCounter].i64MetricValue = ap.rssi; aMetrics[u16MetricCounter].i64MetricValue = ap.rssi;
u16MetricCounter++; u16MetricCounter++;
configASSERT(!(u16MetricCounter > METRIC_MAX_COUNT));
vSetMetrics(aMetrics, u16MetricCounter); vSetMetrics(aMetrics, u16MetricCounter);
} }
} }
void vSetMetrics(sMetric *paMetrics, uint16_t u16Size) void vSetMetrics(sMetric *paMetrics, uint16_t u16Size)
{ {
if (xSemaphoreTakeRecursive(xMutexAccessMetricResponse, pdMS_TO_TICKS(5000)) == pdTRUE) if (xSemaphoreTakeRecursive(xMutexAccessMetricResponse, pdMS_TO_TICKS(5000)) == pdTRUE)
{ {
memset(caHtmlResponse, 0U, HTML_RESPONSE_SIZE); memset(caHtmlResponse, 0U, strlen(caHtmlResponse));
for (uint16_t u16Index = 0U; u16Index < u16Size; u16Index++) for (uint16_t u16Index = 0U; u16Index < u16Size; u16Index++)
{ {
char caValueBuffer[64]; char caValueBuffer[64];

View File

@ -4,7 +4,7 @@
#define HTML_RESPONSE_SIZE 4096U #define HTML_RESPONSE_SIZE 4096U
#define METRIC_NAME_MAX_SIZE 64U #define METRIC_NAME_MAX_SIZE 64U
#define METRIC_MAX_COUNT 38U #define METRIC_MAX_COUNT 32U
typedef enum _MetricValueType typedef enum _MetricValueType
{ {

View File

@ -41,26 +41,9 @@ void initOutputs(void)
.intr_type = GPIO_INTR_DISABLE // Disable interrupts .intr_type = GPIO_INTR_DISABLE // Disable interrupts
}; };
esp_err_t ret = gpio_config(&ioConfCirculationPump); gpio_config(&ioConfCirculationPump);
if (ret != ESP_OK) gpio_config(&ioConfBurner);
{ gpio_config(&ioConfSafetyContact);
ESP_LOGE(TAG, "GPIO config failed: %s", esp_err_to_name(ret));
return;
}
ret = gpio_config(&ioConfBurner);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "GPIO config failed: %s", esp_err_to_name(ret));
return;
}
ret = gpio_config(&ioConfSafetyContact);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "GPIO config failed: %s", esp_err_to_name(ret));
return;
}
xMutexAccessOutputs = xSemaphoreCreateRecursiveMutex(); xMutexAccessOutputs = xSemaphoreCreateRecursiveMutex();
if (xMutexAccessOutputs == NULL) if (xMutexAccessOutputs == NULL)
@ -72,17 +55,7 @@ void initOutputs(void)
eOutput getCirculationPumpState(void) eOutput getCirculationPumpState(void)
{ {
eOutput ret = ENABLED; return sCirculationPumpState;
if (xSemaphoreTakeRecursive(xMutexAccessOutputs, pdMS_TO_TICKS(5000)) == pdTRUE)
{
ret = sCirculationPumpState;
xSemaphoreGiveRecursive(xMutexAccessOutputs);
}
else
{
ESP_LOGE(TAG, "Unable to take mutex: getCirculationPumpState()");
}
return ret;
} }
void setCirculationPumpState(eOutput in) void setCirculationPumpState(eOutput in)
@ -97,7 +70,6 @@ void setCirculationPumpState(eOutput in)
break; break;
case DISABLED: case DISABLED:
gpio_set_level(uCirculationPumpGpioPin, 1U); // Switch off Circulation Pump gpio_set_level(uCirculationPumpGpioPin, 1U); // Switch off Circulation Pump
break;
default: default:
break; break;
} }
@ -136,7 +108,6 @@ void setBurnerState(eOutput in)
break; break;
case DISABLED: case DISABLED:
gpio_set_level(uBurnerGpioPin, 1U); // Switch off Burner gpio_set_level(uBurnerGpioPin, 1U); // Switch off Burner
break;
default: default:
break; break;
} }
@ -175,7 +146,6 @@ void setSafetyControlState(eOutput in)
break; break;
case DISABLED: case DISABLED:
gpio_set_level(uSafetyContactGpioPin, 1U); // Switch off power for Burner gpio_set_level(uSafetyContactGpioPin, 1U); // Switch off power for Burner
break;
default: default:
break; break;
} }

View File

@ -2,12 +2,10 @@
#include "freertos/task.h" #include "freertos/task.h"
#include "esp_log.h" #include "esp_log.h"
#include <string.h> #include <string.h>
#include <math.h>
#include "safety.h" #include "safety.h"
#define PERIODIC_INTERVAL 1U // run safety checks every 1sec #define PERIODIC_INTERVAL 1U // run safety checks every 1sec
#define SENSOR_GRACE_PERIOD (60U * 30U) // period that a sensor can report the same reading in seconds #define SENSOR_GRACE_PERIOD (60U * 30U) // period that a sensor can report the same reading in seconds
#define FLOAT_EPSILON 0.0001f
static const char *TAG = "smart-oil-heater-control-system-safety"; static const char *TAG = "smart-oil-heater-control-system-safety";
static SemaphoreHandle_t xMutexAccessSafety = NULL; static SemaphoreHandle_t xMutexAccessSafety = NULL;
@ -87,13 +85,13 @@ void checkSensorSanity(void)
if (sCurrentMeasurement.state == MEASUREMENT_FAULT) if (sCurrentMeasurement.state == MEASUREMENT_FAULT)
{ {
ESP_LOGE(TAG, "%s Sensor not found!", sanityChecks[i].name); //ESP_LOGE(TAG, "%s Sensor not found!", sanityChecks[i].name);
sanityChecks[i].state = SENSOR_NOT_FOUND; sanityChecks[i].state = SENSOR_NOT_FOUND;
sSafetyState = SAFETY_SENSOR_ERROR; sSafetyState = SAFETY_SENSOR_ERROR;
} }
else else
{ {
if (fabsf(sCurrentMeasurement.fCurrentValue - sanityChecks[i].fSensorTemperatureLast) < FLOAT_EPSILON) if (sCurrentMeasurement.fCurrentValue == sanityChecks[i].fSensorTemperatureLast)
{ {
sanityChecks[i].uUnchangedCounter++; sanityChecks[i].uUnchangedCounter++;
if (sanityChecks[i].uUnchangedCounter >= (SENSOR_GRACE_PERIOD / PERIODIC_INTERVAL)) if (sanityChecks[i].uUnchangedCounter >= (SENSOR_GRACE_PERIOD / PERIODIC_INTERVAL))
@ -105,7 +103,6 @@ void checkSensorSanity(void)
} }
else else
{ {
sanityChecks[i].uUnchangedCounter = 0U;
sanityChecks[i].fSensorTemperatureLast = sCurrentMeasurement.fCurrentValue; sanityChecks[i].fSensorTemperatureLast = sCurrentMeasurement.fCurrentValue;
if (sCurrentMeasurement.fCurrentValue > sanityChecks[i].sSensorLimit.max) if (sCurrentMeasurement.fCurrentValue > sanityChecks[i].sSensorLimit.max)
@ -122,10 +119,12 @@ void checkSensorSanity(void)
} }
else else
{ {
sanityChecks[i].uUnchangedCounter = 0U;
sanityChecks[i].state = SENSOR_NO_ERROR; sanityChecks[i].state = SENSOR_NO_ERROR;
} }
} }
} }
// printf(" state: %u\n", sanityChecks[i].state);
} }
} }
@ -144,7 +143,7 @@ void getSensorSanityStates(sSensorSanityCheck *pSensorSanityChecks)
{ {
// Copy only the needed attributes // Copy only the needed attributes
pSensorSanityChecks[i].state = sanityChecks[i].state; pSensorSanityChecks[i].state = sanityChecks[i].state;
strncpy(pSensorSanityChecks[i].name, sanityChecks[i].name, MAX_ERROR_STRING_SIZE); strcpy(pSensorSanityChecks[i].name, sanityChecks[i].name);
} }
xSemaphoreGiveRecursive(xMutexAccessSafety); xSemaphoreGiveRecursive(xMutexAccessSafety);
} }

View File

@ -6,7 +6,7 @@
#include "sntp.h" #include "sntp.h"
static const char *TAG = "smart-oil-heater-control-system-sntp"; static const char *TAG = "smart-oil-heater-control-system-sntp";
static volatile eSntpState sntpState = SYNC_NOT_STARTED; static eSntpState sntpState = SYNC_NOT_STARTED;
void time_sync_notification_cb(struct timeval *tv); void time_sync_notification_cb(struct timeval *tv);
void initSntp(void) void initSntp(void)

View File

@ -13,37 +13,26 @@
#define WIFI_CONNECTED_BIT BIT0 #define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1 #define WIFI_FAIL_BIT BIT1
#define MAX_RETRY_COUNT 10
#define RETRY_DELAY_MS 1000
static const char *TAG = "smart-oil-heater-control-system-wifi"; static const char *TAG = "smart-oil-heater-control-system-wifi";
static EventGroupHandle_t s_wifi_event_group; static EventGroupHandle_t s_wifi_event_group;
static int s_retry_num = 0;
static bool s_initial_connect = true;
static void event_handler(void *arg, esp_event_base_t event_base, static void event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data); int32_t event_id, void *event_data);
void initWifi(void) void initWifi(void)
{ {
s_wifi_event_group = xEventGroupCreate(); s_wifi_event_group = xEventGroupCreate();
if (s_wifi_event_group == NULL)
{
ESP_LOGE(TAG, "xEventGroupCreate() failed!");
return;
}
ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default()); ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_t *my_sta = esp_netif_create_default_wifi_sta(); esp_netif_t *my_sta = esp_netif_create_default_wifi_sta();
ESP_ERROR_CHECK(esp_netif_dhcpc_stop(my_sta)); esp_netif_dhcpc_stop(my_sta);
esp_netif_ip_info_t ip_info; esp_netif_ip_info_t ip_info;
ip_info.ip.addr = ipaddr_addr(CONFIG_STATIC_IP_ADDR); ip_info.ip.addr = ipaddr_addr(CONFIG_STATIC_IP_ADDR);
ip_info.gw.addr = ipaddr_addr(CONFIG_STATIC_GATEWAY_IP_ADDR); ip_info.gw.addr = ipaddr_addr(CONFIG_STATIC_GATEWAY_IP_ADDR);
ip_info.netmask.addr = ipaddr_addr(CONFIG_STATIC_IP_NETMASK); ip_info.netmask.addr = ipaddr_addr(CONFIG_STATIC_IP_NETMASK);
ESP_ERROR_CHECK(esp_netif_set_ip_info(my_sta, &ip_info)); esp_netif_set_ip_info(my_sta, &ip_info);
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_ERROR_CHECK(esp_wifi_init(&cfg));
@ -95,9 +84,7 @@ void initWifi(void)
{ {
ESP_LOGE(TAG, "Unexpected event"); ESP_LOGE(TAG, "Unexpected event");
} }
vEventGroupDelete(s_wifi_event_group);
// Mark initial connection phase complete - do NOT delete the event group
s_initial_connect = false;
} }
static void event_handler(void *arg, esp_event_base_t event_base, static void event_handler(void *arg, esp_event_base_t event_base,
@ -109,46 +96,13 @@ static void event_handler(void *arg, esp_event_base_t event_base,
} }
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
{ {
wifi_event_sta_disconnected_t *event = (wifi_event_sta_disconnected_t *)event_data;
ESP_LOGW(TAG, "Disconnected from AP (reason: %d)", event->reason);
if (s_initial_connect)
{
// During initial connection phase, use retry limit
if (s_retry_num < MAX_RETRY_COUNT)
{
vTaskDelay(pdMS_TO_TICKS(RETRY_DELAY_MS));
esp_wifi_connect(); esp_wifi_connect();
s_retry_num++; ESP_LOGI(TAG, "Retry to connect to the AP");
ESP_LOGI(TAG, "Retry to connect to the AP (%d/%d)", s_retry_num, MAX_RETRY_COUNT);
}
else
{
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
ESP_LOGE(TAG, "Failed to connect after %d attempts", MAX_RETRY_COUNT);
}
}
else
{
// After initial connection, always try to reconnect with delay
vTaskDelay(pdMS_TO_TICKS(RETRY_DELAY_MS));
esp_wifi_connect();
ESP_LOGI(TAG, "Attempting to reconnect to the AP...");
}
} }
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
{ {
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
ESP_LOGI(TAG, "Got ip:" IPSTR, IP2STR(&event->ip_info.ip)); ESP_LOGI(TAG, "Got ip:" IPSTR, IP2STR(&event->ip_info.ip));
s_retry_num = 0;
if (s_initial_connect)
{
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
} }
else
{
ESP_LOGI(TAG, "Successfully reconnected to AP");
}
}
} }