Compare commits
18 Commits
feature/wi
...
bugfix/sta
| Author | SHA256 | Date | |
|---|---|---|---|
| f3f6f1bc5f | |||
| b718073907 | |||
| d36b91a0fd | |||
| 40f757b7d1 | |||
| a9ec101bc6 | |||
| 0236ebcdd1 | |||
| 05757a5038 | |||
| 020eb63e05 | |||
| 67929580d5 | |||
| 10f9645580 | |||
| df3825df3a | |||
| 8c3dbc2886 | |||
| 267197ec20 | |||
| 781f9a1445 | |||
| 09a3c3a22d | |||
| 0775fda0ca | |||
| cd73985740 | |||
| af307fd403 |
207
main/control.c
207
main/control.c
@ -25,9 +25,9 @@
|
|||||||
(60U * 4U) // Burner fault detection after 4 minutes
|
(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 sControlState = CONTROL_STARTING;
|
static eControlState gControlState = CONTROL_STARTING;
|
||||||
// Control table for daily schedules
|
// Control table for daily schedules
|
||||||
static const sControlDay aControlTable[] = {
|
static const sControlDay gControlTable[] = {
|
||||||
{MONDAY,
|
{MONDAY,
|
||||||
2U,
|
2U,
|
||||||
{{{4, 45},
|
{{{4, 45},
|
||||||
@ -85,15 +85,25 @@ static const sControlDay aControlTable[] = {
|
|||||||
RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT,
|
RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT,
|
||||||
CHAMBER_TEMPERATURE_TARGET}}},
|
CHAMBER_TEMPERATURE_TARGET}}},
|
||||||
};
|
};
|
||||||
static sControlTemperatureEntry currentControlEntry =
|
static sControlTemperatureEntry gCurrentControlEntry =
|
||||||
aControlTable[0].aTemperatureEntries[0];
|
gControlTable[0].aTemperatureEntries[0];
|
||||||
|
static SemaphoreHandle_t xMutexAccessControl = NULL;
|
||||||
|
|
||||||
// Function prototypes
|
// Function prototypes
|
||||||
void taskControl(void *pvParameters);
|
void taskControl(void *pvParameters);
|
||||||
void findControlCurrentTemperatureEntry(void);
|
void findControlCurrentTemperatureEntry(void);
|
||||||
|
void setControlState(eControlState state);
|
||||||
|
|
||||||
void initControl(void)
|
void initControl(void)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
xMutexAccessControl = xSemaphoreCreateRecursiveMutex();
|
||||||
|
if (xMutexAccessControl == NULL)
|
||||||
|
{
|
||||||
|
ESP_LOGE(TAG, "Unable to create mutex");
|
||||||
|
}
|
||||||
|
xSemaphoreGiveRecursive(xMutexAccessControl);
|
||||||
|
|
||||||
BaseType_t taskCreated =
|
BaseType_t taskCreated =
|
||||||
xTaskCreate(taskControl, // Function to implement the task
|
xTaskCreate(taskControl, // Function to implement the task
|
||||||
"taskControl", // Task name
|
"taskControl", // Task name
|
||||||
@ -117,7 +127,7 @@ void taskControl(void *pvParameters)
|
|||||||
{
|
{
|
||||||
bool bHeatingInAction = false;
|
bool bHeatingInAction = false;
|
||||||
bool bSummerMode = false;
|
bool bSummerMode = false;
|
||||||
eBurnerState eBurnerState = BURNER_UNKNOWN;
|
eBurnerState burnerState = BURNER_UNKNOWN;
|
||||||
int64_t i64BurnerEnableTimestamp = esp_timer_get_time();
|
int64_t i64BurnerEnableTimestamp = esp_timer_get_time();
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
@ -128,7 +138,7 @@ void taskControl(void *pvParameters)
|
|||||||
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!");
|
||||||
sControlState = CONTROL_FAULT_SAFETY;
|
setControlState(CONTROL_FAULT_SAFETY);
|
||||||
if (bHeatingInAction)
|
if (bHeatingInAction)
|
||||||
{
|
{
|
||||||
ESP_LOGW(TAG, "Disabling burner due to safety fault");
|
ESP_LOGW(TAG, "Disabling burner due to safety fault");
|
||||||
@ -143,7 +153,7 @@ void taskControl(void *pvParameters)
|
|||||||
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!");
|
||||||
sControlState = CONTROL_FAULT_SNTP;
|
setControlState(CONTROL_FAULT_SNTP);
|
||||||
if (bHeatingInAction)
|
if (bHeatingInAction)
|
||||||
{
|
{
|
||||||
ESP_LOGW(TAG, "Disabling burner due to SNTP fault");
|
ESP_LOGW(TAG, "Disabling burner due to SNTP fault");
|
||||||
@ -155,8 +165,6 @@ void taskControl(void *pvParameters)
|
|||||||
}
|
}
|
||||||
|
|
||||||
findControlCurrentTemperatureEntry();
|
findControlCurrentTemperatureEntry();
|
||||||
sControlTemperatureEntry currentControlEntry =
|
|
||||||
getControlCurrentTemperatureEntry();
|
|
||||||
|
|
||||||
if (getOutdoorTemperature().fDampedValue >=
|
if (getOutdoorTemperature().fDampedValue >=
|
||||||
SUMMER_MODE_TEMPERATURE_THRESHOLD_HIGH)
|
SUMMER_MODE_TEMPERATURE_THRESHOLD_HIGH)
|
||||||
@ -171,33 +179,33 @@ void taskControl(void *pvParameters)
|
|||||||
|
|
||||||
// Enable burner if outdoor temperature is low and return flow temperature
|
// Enable burner if outdoor temperature is low and return flow temperature
|
||||||
// is cooled down
|
// is cooled down
|
||||||
if (!bHeatingInAction && (eBurnerState != BURNER_FAULT))
|
if (!bHeatingInAction && (burnerState != BURNER_FAULT))
|
||||||
{
|
{
|
||||||
if (bSummerMode)
|
if (bSummerMode)
|
||||||
{
|
{
|
||||||
// ESP_LOGI(TAG, "Outdoor temperature too warm: Disabling heating");
|
// ESP_LOGI(TAG, "Outdoor temperature too warm: Disabling heating");
|
||||||
setBurnerState(DISABLED);
|
setBurnerState(DISABLED);
|
||||||
setSafetyControlState(DISABLED);
|
setSafetyControlState(DISABLED);
|
||||||
sControlState = CONTROL_OUTDOOR_TOO_WARM;
|
setControlState(CONTROL_OUTDOOR_TOO_WARM);
|
||||||
}
|
}
|
||||||
else if ((getReturnFlowTemperature().average60s.fValue <=
|
else if ((getReturnFlowTemperature().average60s.fValue <=
|
||||||
currentControlEntry.fReturnFlowTemperature) &&
|
getControlCurrentTemperatureEntry().fReturnFlowTemperature) &&
|
||||||
(getChamberTemperature().fCurrentValue <=
|
(getChamberTemperature().fCurrentValue <=
|
||||||
CHAMBER_TEMPERATURE_THRESHOLD))
|
CHAMBER_TEMPERATURE_THRESHOLD))
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG,
|
ESP_LOGI(TAG,
|
||||||
"Enabling burner: Return flow temperature target reached");
|
"Enabling burner: Return flow temperature target reached");
|
||||||
eBurnerState = BURNER_UNKNOWN;
|
burnerState = BURNER_UNKNOWN;
|
||||||
bHeatingInAction = true;
|
bHeatingInAction = true;
|
||||||
setBurnerState(ENABLED);
|
setBurnerState(ENABLED);
|
||||||
setSafetyControlState(ENABLED);
|
setSafetyControlState(ENABLED);
|
||||||
i64BurnerEnableTimestamp = esp_timer_get_time();
|
i64BurnerEnableTimestamp = esp_timer_get_time();
|
||||||
sControlState = CONTROL_HEATING;
|
setControlState(CONTROL_HEATING);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// ESP_LOGI(TAG, "Return flow temperature too warm: Disabling heating");
|
// ESP_LOGI(TAG, "Return flow temperature too warm: Disabling heating");
|
||||||
sControlState = CONTROL_RETURN_FLOW_TOO_WARM;
|
setControlState(CONTROL_RETURN_FLOW_TOO_WARM);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,9 +213,9 @@ void taskControl(void *pvParameters)
|
|||||||
if (bHeatingInAction)
|
if (bHeatingInAction)
|
||||||
{
|
{
|
||||||
if ((getChamberTemperature().fCurrentValue >=
|
if ((getChamberTemperature().fCurrentValue >=
|
||||||
currentControlEntry.fChamberTemperature) ||
|
getControlCurrentTemperatureEntry().fChamberTemperature) ||
|
||||||
(getChamberTemperature().predict60s.fValue >=
|
(getChamberTemperature().predict60s.fValue >=
|
||||||
currentControlEntry.fChamberTemperature))
|
getControlCurrentTemperatureEntry().fChamberTemperature))
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "Chamber target temperature reached: Disabling burner");
|
ESP_LOGI(TAG, "Chamber target temperature reached: Disabling burner");
|
||||||
bHeatingInAction = false;
|
bHeatingInAction = false;
|
||||||
@ -217,14 +225,14 @@ void taskControl(void *pvParameters)
|
|||||||
else if (esp_timer_get_time() - i64BurnerEnableTimestamp >=
|
else if (esp_timer_get_time() - i64BurnerEnableTimestamp >=
|
||||||
BURNER_FAULT_DETECTION_THRESHOLD * 1000000U)
|
BURNER_FAULT_DETECTION_THRESHOLD * 1000000U)
|
||||||
{
|
{
|
||||||
if (eBurnerState == BURNER_UNKNOWN)
|
if (burnerState == BURNER_UNKNOWN)
|
||||||
{
|
{
|
||||||
if (getBurnerError() == FAULT)
|
if (getBurnerError() == FAULT)
|
||||||
{
|
{
|
||||||
// ESP_LOGW(TAG, "Burner fault detected: Disabling burner");
|
// ESP_LOGW(TAG, "Burner fault detected: Disabling burner");
|
||||||
bHeatingInAction = false;
|
bHeatingInAction = false;
|
||||||
eBurnerState = BURNER_FAULT;
|
burnerState = BURNER_FAULT;
|
||||||
sControlState = CONTROL_FAULT_BURNER;
|
setControlState(CONTROL_FAULT_BURNER);
|
||||||
setBurnerState(DISABLED);
|
setBurnerState(DISABLED);
|
||||||
setSafetyControlState(ENABLED);
|
setSafetyControlState(ENABLED);
|
||||||
}
|
}
|
||||||
@ -232,7 +240,7 @@ void taskControl(void *pvParameters)
|
|||||||
{
|
{
|
||||||
// ESP_LOGI(TAG, "No burner fault detected: Marking burner as
|
// ESP_LOGI(TAG, "No burner fault detected: Marking burner as
|
||||||
// fired");
|
// fired");
|
||||||
eBurnerState = BURNER_FIRED;
|
burnerState = BURNER_FIRED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -253,17 +261,47 @@ void taskControl(void *pvParameters)
|
|||||||
} // End of while(1)
|
} // End of while(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
eControlState getControlState(void) { return sControlState; }
|
void setControlState(eControlState state)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (xSemaphoreTakeRecursive(xMutexAccessControl, pdMS_TO_TICKS(5000)) == pdTRUE)
|
||||||
|
{
|
||||||
|
gControlState = state;
|
||||||
|
xSemaphoreGiveRecursive(xMutexAccessControl);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ESP_LOGE(TAG, "Unable to take mutex: setControlState()");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
eControlState getControlState(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
eControlState ret = CONTROL_FAULT_SAFETY;
|
||||||
|
|
||||||
|
if (xSemaphoreTakeRecursive(xMutexAccessControl, pdMS_TO_TICKS(5000)) == pdTRUE)
|
||||||
|
{
|
||||||
|
ret = gControlState;
|
||||||
|
xSemaphoreGiveRecursive(xMutexAccessControl);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ESP_LOGE(TAG, "Unable to take mutex: getControlState()");
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
eControlWeekday getControlCurrentWeekday(void)
|
eControlWeekday getControlCurrentWeekday(void)
|
||||||
{
|
{
|
||||||
|
// Get current time
|
||||||
time_t now;
|
time_t now;
|
||||||
struct tm *timeinfo;
|
struct tm timeinfo;
|
||||||
|
|
||||||
time(&now);
|
time(&now);
|
||||||
timeinfo = localtime(&now);
|
localtime_r(&now, &timeinfo);
|
||||||
|
|
||||||
int day = timeinfo->tm_wday;
|
int day = timeinfo.tm_wday;
|
||||||
return (eControlWeekday)((day == 0) ? 6 : day - 1);
|
return (eControlWeekday)((day == 0) ? 6 : day - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -294,66 +332,87 @@ void findControlCurrentTemperatureEntry(void)
|
|||||||
int currentHour = timeinfo.tm_hour;
|
int currentHour = timeinfo.tm_hour;
|
||||||
int currentMinute = timeinfo.tm_min;
|
int currentMinute = timeinfo.tm_min;
|
||||||
|
|
||||||
// ESP_LOGI(TAG, "Searching for control entry - Day: %d, Time: %02d:%02d", currentDay, currentHour, currentMinute);
|
if (xSemaphoreTakeRecursive(xMutexAccessControl, pdMS_TO_TICKS(5000)) == pdTRUE)
|
||||||
|
|
||||||
// Search through all days and entries
|
|
||||||
for (int dayIndex = 0; dayIndex < 7; dayIndex++)
|
|
||||||
{
|
{
|
||||||
const sControlDay *day = &aControlTable[dayIndex];
|
|
||||||
|
|
||||||
for (int entryIndex = 0; entryIndex < day->entryCount; entryIndex++)
|
// 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 sControlTemperatureEntry *entry = &day->aTemperatureEntries[entryIndex];
|
const sControlDay *day = &gControlTable[dayIndex];
|
||||||
|
|
||||||
// Check if this entry is in the future (next active entry)
|
for (int entryIndex = 0; entryIndex < day->entryCount; entryIndex++)
|
||||||
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
|
const sControlTemperatureEntry *entry = &day->aTemperatureEntries[entryIndex];
|
||||||
if (entryIndex > 0)
|
|
||||||
|
// 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)
|
||||||
{
|
{
|
||||||
// Use previous entry from same day
|
|
||||||
currentControlEntry = day->aTemperatureEntries[entryIndex - 1];
|
// 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;
|
||||||
}
|
}
|
||||||
else if (dayIndex > 0)
|
|
||||||
{
|
|
||||||
// Use last entry from previous day
|
|
||||||
const sControlDay *previousDay = &aControlTable[dayIndex - 1];
|
|
||||||
currentControlEntry = previousDay->aTemperatureEntries[previousDay->entryCount - 1];
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// First entry of the week - wrap to last entry of Sunday
|
|
||||||
const sControlDay *sunday = &aControlTable[6];
|
|
||||||
currentControlEntry = sunday->aTemperatureEntries[sunday->entryCount - 1];
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
ESP_LOGI(TAG, "Active entry found - Time: %02d:%02d, "
|
|
||||||
"Return Temp: %lf, Chamber Temp: %lf",
|
|
||||||
currentControlEntry.timestamp.hour,
|
|
||||||
currentControlEntry.timestamp.minute,
|
|
||||||
currentControlEntry.fReturnFlowTemperature,
|
|
||||||
currentControlEntry.fChamberTemperature);
|
|
||||||
*/
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If we reached here, current time is after all entries this week
|
||||||
|
// 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()");
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we reached here, current time is after all entries this week
|
|
||||||
// Use the last entry (Sunday evening)
|
|
||||||
const sControlDay *sunday = &aControlTable[6];
|
|
||||||
currentControlEntry = sunday->aTemperatureEntries[sunday->entryCount - 1];
|
|
||||||
|
|
||||||
// ESP_LOGI(TAG, "Using last entry of week - Time: %02d:%02d", currentControlEntry.timestamp.hour, currentControlEntry.timestamp.minute);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sControlTemperatureEntry getControlCurrentTemperatureEntry(void)
|
sControlTemperatureEntry getControlCurrentTemperatureEntry(void)
|
||||||
{
|
{
|
||||||
return currentControlEntry;
|
sControlTemperatureEntry ret = gControlTable[0].aTemperatureEntries[0];
|
||||||
|
if (xSemaphoreTakeRecursive(xMutexAccessControl, pdMS_TO_TICKS(5000)) == pdTRUE)
|
||||||
|
{
|
||||||
|
ret = gCurrentControlEntry;
|
||||||
|
xSemaphoreGiveRecursive(xMutexAccessControl);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ESP_LOGE(TAG, "Unable to take mutex: getControlCurrentTemperatureEntry()");
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -49,7 +49,12 @@ void initInputs(void)
|
|||||||
.intr_type = GPIO_INTR_DISABLE // Disable interrupts
|
.intr_type = GPIO_INTR_DISABLE // Disable interrupts
|
||||||
};
|
};
|
||||||
|
|
||||||
gpio_config(&ioConfBurnerFault);
|
esp_err_t ret = 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)
|
||||||
@ -94,17 +99,17 @@ void initMeasurement(sMeasurement *pMeasurement)
|
|||||||
pMeasurement->average10s.fValue = INITIALISATION_VALUE;
|
pMeasurement->average10s.fValue = INITIALISATION_VALUE;
|
||||||
pMeasurement->average10s.bufferCount = 0U;
|
pMeasurement->average10s.bufferCount = 0U;
|
||||||
pMeasurement->average10s.bufferIndex = 0U;
|
pMeasurement->average10s.bufferIndex = 0U;
|
||||||
memset(pMeasurement->average10s.samples, 0U, AVG10S_SAMPLE_SIZE);
|
memset(pMeasurement->average10s.samples, 0U, sizeof(float) * AVG10S_SAMPLE_SIZE);
|
||||||
|
|
||||||
pMeasurement->average60s.fValue = INITIALISATION_VALUE;
|
pMeasurement->average60s.fValue = INITIALISATION_VALUE;
|
||||||
pMeasurement->average60s.bufferCount = 0U;
|
pMeasurement->average60s.bufferCount = 0U;
|
||||||
pMeasurement->average60s.bufferIndex = 0U;
|
pMeasurement->average60s.bufferIndex = 0U;
|
||||||
memset(pMeasurement->average60s.samples, 0U, AVG60S_SAMPLE_SIZE);
|
memset(pMeasurement->average60s.samples, 0U, sizeof(float) * AVG60S_SAMPLE_SIZE);
|
||||||
|
|
||||||
pMeasurement->predict60s.fValue = INITIALISATION_VALUE;
|
pMeasurement->predict60s.fValue = INITIALISATION_VALUE;
|
||||||
pMeasurement->predict60s.bufferCount = 0U;
|
pMeasurement->predict60s.bufferCount = 0U;
|
||||||
pMeasurement->predict60s.bufferIndex = 0U;
|
pMeasurement->predict60s.bufferIndex = 0U;
|
||||||
memset(pMeasurement->predict60s.samples, 0U, PRED60S_SAMPLE_SIZE);
|
memset(pMeasurement->predict60s.samples, 0U, sizeof(float) * PRED60S_SAMPLE_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateAverage(sMeasurement *pMeasurement)
|
void updateAverage(sMeasurement *pMeasurement)
|
||||||
@ -122,12 +127,19 @@ void updateAverage(sMeasurement *pMeasurement)
|
|||||||
}
|
}
|
||||||
|
|
||||||
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];
|
||||||
}
|
}
|
||||||
|
|
||||||
pMeasurement->average10s.fValue = sum / pMeasurement->average10s.bufferCount;
|
if (pMeasurement->average10s.bufferCount == 0U)
|
||||||
|
{
|
||||||
|
pMeasurement->average10s.fValue = 0.0f;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
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;
|
||||||
@ -144,7 +156,14 @@ void updateAverage(sMeasurement *pMeasurement)
|
|||||||
sum += pMeasurement->average60s.samples[i];
|
sum += pMeasurement->average60s.samples[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
pMeasurement->average60s.fValue = sum / pMeasurement->average60s.bufferCount;
|
if (pMeasurement->average60s.bufferCount == 0U)
|
||||||
|
{
|
||||||
|
pMeasurement->average60s.fValue = 0.0f;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
pMeasurement->average60s.fValue = sum / pMeasurement->average60s.bufferCount;
|
||||||
|
}
|
||||||
|
|
||||||
// Damped current value
|
// Damped current value
|
||||||
if (pMeasurement->fDampedValue == INITIALISATION_VALUE)
|
if (pMeasurement->fDampedValue == INITIALISATION_VALUE)
|
||||||
|
|||||||
@ -301,23 +301,23 @@ void taskMetrics(void *pvParameters)
|
|||||||
|
|
||||||
// Wifi RSSI
|
// Wifi RSSI
|
||||||
wifi_ap_record_t ap;
|
wifi_ap_record_t ap;
|
||||||
esp_wifi_sta_get_ap_info(&ap);
|
ap.rssi = 0U;
|
||||||
|
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++;
|
||||||
|
|
||||||
ESP_ERROR_CHECK(u16MetricCounter > METRIC_MAX_COUNT);
|
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, strlen(caHtmlResponse));
|
memset(caHtmlResponse, 0U, HTML_RESPONSE_SIZE);
|
||||||
for (uint16_t u16Index = 0U; u16Index < u16Size; u16Index++)
|
for (uint16_t u16Index = 0U; u16Index < u16Size; u16Index++)
|
||||||
{
|
{
|
||||||
char caValueBuffer[64];
|
char caValueBuffer[64];
|
||||||
|
|||||||
@ -41,9 +41,26 @@ void initOutputs(void)
|
|||||||
.intr_type = GPIO_INTR_DISABLE // Disable interrupts
|
.intr_type = GPIO_INTR_DISABLE // Disable interrupts
|
||||||
};
|
};
|
||||||
|
|
||||||
gpio_config(&ioConfCirculationPump);
|
esp_err_t ret = gpio_config(&ioConfCirculationPump);
|
||||||
gpio_config(&ioConfBurner);
|
if (ret != ESP_OK)
|
||||||
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)
|
||||||
@ -55,7 +72,17 @@ void initOutputs(void)
|
|||||||
|
|
||||||
eOutput getCirculationPumpState(void)
|
eOutput getCirculationPumpState(void)
|
||||||
{
|
{
|
||||||
return sCirculationPumpState;
|
eOutput ret = ENABLED;
|
||||||
|
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)
|
||||||
@ -70,6 +97,7 @@ 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;
|
||||||
}
|
}
|
||||||
@ -108,6 +136,7 @@ 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;
|
||||||
}
|
}
|
||||||
@ -146,6 +175,7 @@ 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;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,10 +2,12 @@
|
|||||||
#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;
|
||||||
@ -91,7 +93,7 @@ void checkSensorSanity(void)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (sCurrentMeasurement.fCurrentValue == sanityChecks[i].fSensorTemperatureLast)
|
if (fabsf(sCurrentMeasurement.fCurrentValue - sanityChecks[i].fSensorTemperatureLast) < FLOAT_EPSILON)
|
||||||
{
|
{
|
||||||
sanityChecks[i].uUnchangedCounter++;
|
sanityChecks[i].uUnchangedCounter++;
|
||||||
if (sanityChecks[i].uUnchangedCounter >= (SENSOR_GRACE_PERIOD / PERIODIC_INTERVAL))
|
if (sanityChecks[i].uUnchangedCounter >= (SENSOR_GRACE_PERIOD / PERIODIC_INTERVAL))
|
||||||
@ -103,6 +105,7 @@ 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)
|
||||||
@ -119,12 +122,10 @@ 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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,7 +144,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;
|
||||||
strcpy(pSensorSanityChecks[i].name, sanityChecks[i].name);
|
strncpy(pSensorSanityChecks[i].name, sanityChecks[i].name, MAX_ERROR_STRING_SIZE);
|
||||||
}
|
}
|
||||||
xSemaphoreGiveRecursive(xMutexAccessSafety);
|
xSemaphoreGiveRecursive(xMutexAccessSafety);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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 eSntpState sntpState = SYNC_NOT_STARTED;
|
static volatile 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)
|
||||||
|
|||||||
58
main/wifi.c
58
main/wifi.c
@ -13,26 +13,37 @@
|
|||||||
|
|
||||||
#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_netif_dhcpc_stop(my_sta);
|
ESP_ERROR_CHECK(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_netif_set_ip_info(my_sta, &ip_info);
|
ESP_ERROR_CHECK(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));
|
||||||
@ -84,7 +95,9 @@ 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,
|
||||||
@ -96,13 +109,46 @@ 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)
|
||||||
{
|
{
|
||||||
esp_wifi_connect();
|
wifi_event_sta_disconnected_t *event = (wifi_event_sta_disconnected_t *)event_data;
|
||||||
ESP_LOGI(TAG, "Retry to connect to the AP");
|
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();
|
||||||
|
s_retry_num++;
|
||||||
|
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));
|
||||||
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
s_retry_num = 0;
|
||||||
|
|
||||||
|
if (s_initial_connect)
|
||||||
|
{
|
||||||
|
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ESP_LOGI(TAG, "Successfully reconnected to AP");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user