2 Commits

Author SHA256 Message Date
b77cba87ed fix wifi event handler 2026-01-09 23:44:53 +01:00
618a6974bf enable ap lock 2026-01-09 23:26:46 +01:00
8 changed files with 111 additions and 212 deletions

View File

@ -18,5 +18,19 @@ menu "Smart Oil Heating Control System"
config SNTP_SERVER_IP_ADDR
string "SNTP IPv4 server address"
default "192.168.0.1"
config ENV_WIFI_BSSID_LOCK
bool "Lock to specific Access Point (BSSID)"
default n
help
When enabled, the device will only connect to the access point
with the specified MAC address (BSSID). Useful when multiple APs
share the same SSID.
config ENV_WIFI_BSSID
string "Access Point MAC Address (BSSID)"
default "00:00:00:00:00:00"
depends on ENV_WIFI_BSSID_LOCK
help
MAC address of the access point to connect to.
Format: XX:XX:XX:XX:XX:XX (uppercase or lowercase)
endmenu

View File

@ -25,9 +25,9 @@
(60U * 4U) // Burner fault detection after 4 minutes
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 const sControlDay aControlTable[] = {
{MONDAY,
2U,
{{{4, 45},
@ -85,25 +85,15 @@ static const sControlDay gControlTable[] = {
RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT,
CHAMBER_TEMPERATURE_TARGET}}},
};
static sControlTemperatureEntry gCurrentControlEntry =
gControlTable[0].aTemperatureEntries[0];
static SemaphoreHandle_t xMutexAccessControl = NULL;
static sControlTemperatureEntry currentControlEntry =
aControlTable[0].aTemperatureEntries[0];
// Function prototypes
void taskControl(void *pvParameters);
void findControlCurrentTemperatureEntry(void);
void setControlState(eControlState state);
void initControl(void)
{
xMutexAccessControl = xSemaphoreCreateRecursiveMutex();
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
@ -127,7 +117,7 @@ void taskControl(void *pvParameters)
{
bool bHeatingInAction = false;
bool bSummerMode = false;
eBurnerState burnerState = BURNER_UNKNOWN;
eBurnerState eBurnerState = BURNER_UNKNOWN;
int64_t i64BurnerEnableTimestamp = esp_timer_get_time();
while (1)
@ -138,7 +128,7 @@ void taskControl(void *pvParameters)
if (getSafetyState() != SAFETY_NO_ERROR)
{
ESP_LOGW(TAG, "Control not possible due to safety fault!");
setControlState(CONTROL_FAULT_SAFETY);
sControlState = CONTROL_FAULT_SAFETY;
if (bHeatingInAction)
{
ESP_LOGW(TAG, "Disabling burner due to safety fault");
@ -153,7 +143,7 @@ void taskControl(void *pvParameters)
if (getSntpState() != SYNC_SUCCESSFUL)
{
ESP_LOGW(TAG, "Control not possible due to SNTP fault!");
setControlState(CONTROL_FAULT_SNTP);
sControlState = CONTROL_FAULT_SNTP;
if (bHeatingInAction)
{
ESP_LOGW(TAG, "Disabling burner due to SNTP fault");
@ -165,6 +155,8 @@ void taskControl(void *pvParameters)
}
findControlCurrentTemperatureEntry();
sControlTemperatureEntry currentControlEntry =
getControlCurrentTemperatureEntry();
if (getOutdoorTemperature().fDampedValue >=
SUMMER_MODE_TEMPERATURE_THRESHOLD_HIGH)
@ -179,33 +171,33 @@ void taskControl(void *pvParameters)
// Enable burner if outdoor temperature is low and return flow temperature
// is cooled down
if (!bHeatingInAction && (burnerState != BURNER_FAULT))
if (!bHeatingInAction && (eBurnerState != BURNER_FAULT))
{
if (bSummerMode)
{
// ESP_LOGI(TAG, "Outdoor temperature too warm: Disabling heating");
setBurnerState(DISABLED);
setSafetyControlState(DISABLED);
setControlState(CONTROL_OUTDOOR_TOO_WARM);
sControlState = CONTROL_OUTDOOR_TOO_WARM;
}
else if ((getReturnFlowTemperature().average60s.fValue <=
getControlCurrentTemperatureEntry().fReturnFlowTemperature) &&
currentControlEntry.fReturnFlowTemperature) &&
(getChamberTemperature().fCurrentValue <=
CHAMBER_TEMPERATURE_THRESHOLD))
{
ESP_LOGI(TAG,
"Enabling burner: Return flow temperature target reached");
burnerState = BURNER_UNKNOWN;
eBurnerState = BURNER_UNKNOWN;
bHeatingInAction = true;
setBurnerState(ENABLED);
setSafetyControlState(ENABLED);
i64BurnerEnableTimestamp = esp_timer_get_time();
setControlState(CONTROL_HEATING);
sControlState = CONTROL_HEATING;
}
else
{
// ESP_LOGI(TAG, "Return flow temperature too warm: Disabling heating");
setControlState(CONTROL_RETURN_FLOW_TOO_WARM);
sControlState = CONTROL_RETURN_FLOW_TOO_WARM;
}
}
@ -213,9 +205,9 @@ void taskControl(void *pvParameters)
if (bHeatingInAction)
{
if ((getChamberTemperature().fCurrentValue >=
getControlCurrentTemperatureEntry().fChamberTemperature) ||
currentControlEntry.fChamberTemperature) ||
(getChamberTemperature().predict60s.fValue >=
getControlCurrentTemperatureEntry().fChamberTemperature))
currentControlEntry.fChamberTemperature))
{
ESP_LOGI(TAG, "Chamber target temperature reached: Disabling burner");
bHeatingInAction = false;
@ -225,14 +217,14 @@ void taskControl(void *pvParameters)
else if (esp_timer_get_time() - i64BurnerEnableTimestamp >=
BURNER_FAULT_DETECTION_THRESHOLD * 1000000U)
{
if (burnerState == BURNER_UNKNOWN)
if (eBurnerState == BURNER_UNKNOWN)
{
if (getBurnerError() == FAULT)
{
// ESP_LOGW(TAG, "Burner fault detected: Disabling burner");
bHeatingInAction = false;
burnerState = BURNER_FAULT;
setControlState(CONTROL_FAULT_BURNER);
eBurnerState = BURNER_FAULT;
sControlState = CONTROL_FAULT_BURNER;
setBurnerState(DISABLED);
setSafetyControlState(ENABLED);
}
@ -240,7 +232,7 @@ void taskControl(void *pvParameters)
{
// ESP_LOGI(TAG, "No burner fault detected: Marking burner as
// fired");
burnerState = BURNER_FIRED;
eBurnerState = BURNER_FIRED;
}
}
}
@ -261,47 +253,17 @@ void taskControl(void *pvParameters)
} // End of while(1)
}
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;
}
eControlState getControlState(void) { return sControlState; }
eControlWeekday getControlCurrentWeekday(void)
{
// Get current time
time_t now;
struct tm timeinfo;
time(&now);
localtime_r(&now, &timeinfo);
struct tm *timeinfo;
int day = timeinfo.tm_wday;
time(&now);
timeinfo = localtime(&now);
int day = timeinfo->tm_wday;
return (eControlWeekday)((day == 0) ? 6 : day - 1);
}
@ -332,15 +294,12 @@ void findControlCurrentTemperatureEntry(void)
int currentHour = timeinfo.tm_hour;
int currentMinute = timeinfo.tm_min;
if (xSemaphoreTakeRecursive(xMutexAccessControl, pdMS_TO_TICKS(5000)) == pdTRUE)
{
// 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];
const sControlDay *day = &aControlTable[dayIndex];
for (int entryIndex = 0; entryIndex < day->entryCount; entryIndex++)
{
@ -355,32 +314,31 @@ void findControlCurrentTemperatureEntry(void)
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];
currentControlEntry = 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];
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 = &gControlTable[6];
gCurrentControlEntry = sunday->aTemperatureEntries[sunday->entryCount - 1];
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",
gCurrentControlEntry.timestamp.hour,
gCurrentControlEntry.timestamp.minute,
gCurrentControlEntry.fReturnFlowTemperature,
gCurrentControlEntry.fChamberTemperature);
currentControlEntry.timestamp.hour,
currentControlEntry.timestamp.minute,
currentControlEntry.fReturnFlowTemperature,
currentControlEntry.fChamberTemperature);
*/
return;
}
@ -389,30 +347,13 @@ void findControlCurrentTemperatureEntry(void)
// 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];
const sControlDay *sunday = &aControlTable[6];
currentControlEntry = 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, "Using last entry of week - Time: %02d:%02d", currentControlEntry.timestamp.hour, currentControlEntry.timestamp.minute);
}
sControlTemperatureEntry getControlCurrentTemperatureEntry(void)
{
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;
return currentControlEntry;
}

View File

@ -49,12 +49,7 @@ void initInputs(void)
.intr_type = GPIO_INTR_DISABLE // Disable interrupts
};
esp_err_t ret = gpio_config(&ioConfBurnerFault);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "GPIO config failed: %s", esp_err_to_name(ret));
return;
}
gpio_config(&ioConfBurnerFault);
xMutexAccessInputs = xSemaphoreCreateRecursiveMutex();
if (xMutexAccessInputs == NULL)
@ -99,17 +94,17 @@ void initMeasurement(sMeasurement *pMeasurement)
pMeasurement->average10s.fValue = INITIALISATION_VALUE;
pMeasurement->average10s.bufferCount = 0U;
pMeasurement->average10s.bufferIndex = 0U;
memset(pMeasurement->average10s.samples, 0U, sizeof(float) * AVG10S_SAMPLE_SIZE);
memset(pMeasurement->average10s.samples, 0U, AVG10S_SAMPLE_SIZE);
pMeasurement->average60s.fValue = INITIALISATION_VALUE;
pMeasurement->average60s.bufferCount = 0U;
pMeasurement->average60s.bufferIndex = 0U;
memset(pMeasurement->average60s.samples, 0U, sizeof(float) * AVG60S_SAMPLE_SIZE);
memset(pMeasurement->average60s.samples, 0U, AVG60S_SAMPLE_SIZE);
pMeasurement->predict60s.fValue = INITIALISATION_VALUE;
pMeasurement->predict60s.bufferCount = 0U;
pMeasurement->predict60s.bufferIndex = 0U;
memset(pMeasurement->predict60s.samples, 0U, sizeof(float) * PRED60S_SAMPLE_SIZE);
memset(pMeasurement->predict60s.samples, 0U, PRED60S_SAMPLE_SIZE);
}
void updateAverage(sMeasurement *pMeasurement)
@ -127,19 +122,12 @@ void updateAverage(sMeasurement *pMeasurement)
}
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];
}
if (pMeasurement->average10s.bufferCount == 0U)
{
pMeasurement->average10s.fValue = 0.0f;
}
else
{
pMeasurement->average10s.fValue = sum / pMeasurement->average10s.bufferCount;
}
// Average form the last 60sec
pMeasurement->average60s.samples[pMeasurement->average60s.bufferIndex] = pMeasurement->fCurrentValue;
@ -156,14 +144,7 @@ void updateAverage(sMeasurement *pMeasurement)
sum += pMeasurement->average60s.samples[i];
}
if (pMeasurement->average60s.bufferCount == 0U)
{
pMeasurement->average60s.fValue = 0.0f;
}
else
{
pMeasurement->average60s.fValue = sum / pMeasurement->average60s.bufferCount;
}
// Damped current value
if (pMeasurement->fDampedValue == INITIALISATION_VALUE)

View File

@ -301,23 +301,23 @@ void taskMetrics(void *pvParameters)
// Wifi RSSI
wifi_ap_record_t ap;
ap.rssi = 0U;
ESP_ERROR_CHECK(esp_wifi_sta_get_ap_info(&ap));
esp_wifi_sta_get_ap_info(&ap);
strcpy(aMetrics[u16MetricCounter].caMetricName, "wifi_rssi");
aMetrics[u16MetricCounter].type = INTEGER_64;
aMetrics[u16MetricCounter].i64MetricValue = ap.rssi;
u16MetricCounter++;
configASSERT(!(u16MetricCounter > METRIC_MAX_COUNT));
ESP_ERROR_CHECK(u16MetricCounter > METRIC_MAX_COUNT);
vSetMetrics(aMetrics, u16MetricCounter);
}
}
void vSetMetrics(sMetric *paMetrics, uint16_t u16Size)
{
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++)
{
char caValueBuffer[64];

View File

@ -41,26 +41,9 @@ void initOutputs(void)
.intr_type = GPIO_INTR_DISABLE // Disable interrupts
};
esp_err_t ret = gpio_config(&ioConfCirculationPump);
if (ret != ESP_OK)
{
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;
}
gpio_config(&ioConfCirculationPump);
gpio_config(&ioConfBurner);
gpio_config(&ioConfSafetyContact);
xMutexAccessOutputs = xSemaphoreCreateRecursiveMutex();
if (xMutexAccessOutputs == NULL)
@ -72,17 +55,7 @@ void initOutputs(void)
eOutput getCirculationPumpState(void)
{
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;
return sCirculationPumpState;
}
void setCirculationPumpState(eOutput in)
@ -97,7 +70,6 @@ void setCirculationPumpState(eOutput in)
break;
case DISABLED:
gpio_set_level(uCirculationPumpGpioPin, 1U); // Switch off Circulation Pump
break;
default:
break;
}
@ -136,7 +108,6 @@ void setBurnerState(eOutput in)
break;
case DISABLED:
gpio_set_level(uBurnerGpioPin, 1U); // Switch off Burner
break;
default:
break;
}
@ -175,7 +146,6 @@ void setSafetyControlState(eOutput in)
break;
case DISABLED:
gpio_set_level(uSafetyContactGpioPin, 1U); // Switch off power for Burner
break;
default:
break;
}

View File

@ -2,12 +2,10 @@
#include "freertos/task.h"
#include "esp_log.h"
#include <string.h>
#include <math.h>
#include "safety.h"
#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 FLOAT_EPSILON 0.0001f
static const char *TAG = "smart-oil-heater-control-system-safety";
static SemaphoreHandle_t xMutexAccessSafety = NULL;
@ -93,7 +91,7 @@ void checkSensorSanity(void)
}
else
{
if (fabsf(sCurrentMeasurement.fCurrentValue - sanityChecks[i].fSensorTemperatureLast) < FLOAT_EPSILON)
if (sCurrentMeasurement.fCurrentValue == sanityChecks[i].fSensorTemperatureLast)
{
sanityChecks[i].uUnchangedCounter++;
if (sanityChecks[i].uUnchangedCounter >= (SENSOR_GRACE_PERIOD / PERIODIC_INTERVAL))
@ -105,7 +103,6 @@ void checkSensorSanity(void)
}
else
{
sanityChecks[i].uUnchangedCounter = 0U;
sanityChecks[i].fSensorTemperatureLast = sCurrentMeasurement.fCurrentValue;
if (sCurrentMeasurement.fCurrentValue > sanityChecks[i].sSensorLimit.max)
@ -122,10 +119,12 @@ void checkSensorSanity(void)
}
else
{
sanityChecks[i].uUnchangedCounter = 0U;
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
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);
}

View File

@ -6,7 +6,7 @@
#include "sntp.h"
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 initSntp(void)

View File

@ -28,22 +28,16 @@ static void event_handler(void *arg, esp_event_base_t event_base,
void initWifi(void)
{
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_event_loop_create_default());
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;
ip_info.ip.addr = ipaddr_addr(CONFIG_STATIC_IP_ADDR);
ip_info.gw.addr = ipaddr_addr(CONFIG_STATIC_GATEWAY_IP_ADDR);
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();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));