Compare commits
26 Commits
dddf2c9bf0
...
bugfix/sta
| Author | SHA256 | Date | |
|---|---|---|---|
| b6c27f049d | |||
| 0c3779bf72 | |||
| 7f2461679e | |||
| 5a223ddbe5 | |||
| c935eacbdd | |||
| f3f6f1bc5f | |||
| b718073907 | |||
| d36b91a0fd | |||
| 40f757b7d1 | |||
| a9ec101bc6 | |||
| 0236ebcdd1 | |||
| 05757a5038 | |||
| 020eb63e05 | |||
| 67929580d5 | |||
| 10f9645580 | |||
| df3825df3a | |||
| 8c3dbc2886 | |||
| 267197ec20 | |||
| 781f9a1445 | |||
| 09a3c3a22d | |||
| 0775fda0ca | |||
| cd73985740 | |||
| b77cba87ed | |||
| af307fd403 | |||
| 618a6974bf | |||
| cb69bea618 |
2
.gitignore
vendored
2
.gitignore
vendored
@ -291,3 +291,5 @@ dkms.conf
|
||||
*.out
|
||||
*.app
|
||||
|
||||
managed_components/
|
||||
dependencies.lock
|
||||
|
||||
@ -1,6 +1,3 @@
|
||||
|
||||
set(EXTRA_COMPONENT_DIRS $ENV{ESP_IDF_LIB_PATH}/components)
|
||||
|
||||
# The following lines of boilerplate have to be in your project's CMakeLists
|
||||
# in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
56
README.md
56
README.md
@ -19,7 +19,11 @@ Sntp <|-- Metrics
|
||||
|
||||
class Inputs{
|
||||
+initInputs()
|
||||
-initMeasurement()
|
||||
-updateAverage()
|
||||
-updatePrediction()
|
||||
-taskInput()
|
||||
-linearRegressionPredict()
|
||||
+getChamberTemperature()
|
||||
+getOutdoorTemperature()
|
||||
+getInletFlowTemperature()
|
||||
@ -38,7 +42,11 @@ Sntp <|-- Metrics
|
||||
}
|
||||
|
||||
class Control{
|
||||
initControl()
|
||||
+taskControl()
|
||||
+getControlCurrentWeekday()
|
||||
-findControlCurrentTemperatureEntry()
|
||||
+getControlCurrentTemperatureEntry()
|
||||
-controlTable
|
||||
+getControlState()
|
||||
}
|
||||
@ -79,33 +87,41 @@ Sntp <|-- Metrics
|
||||
burner_fault_pending 1
|
||||
circulation_pump_enabled 1
|
||||
burner_enabled 0
|
||||
safety_contact_enabled 0
|
||||
chamber_temperature 58.750000
|
||||
chamber_temperature_avg10 58.931252
|
||||
chamber_temperature_avg60 59.190475
|
||||
chamber_temperature_pred60 55.870998
|
||||
inlet_flow_temperature 53.875000
|
||||
inlet_flow_temperature_avg10 53.900002
|
||||
inlet_flow_temperature_avg60 53.994320
|
||||
inlet_flow_temperature_pred60 52.848743
|
||||
outdoor_temperature 18.000000
|
||||
outdoor_temperature_avg10 18.006250
|
||||
outdoor_temperature_avg60 18.002840
|
||||
outdoor_temperature_pred60 18.050785
|
||||
return_flow_temperature 48.625000
|
||||
return_flow_temperature_avg10 48.718750
|
||||
return_flow_temperature_avg60 48.846592
|
||||
return_flow_temperature_pred60 47.383083
|
||||
safety_contact_enabled 1
|
||||
chamber_temperature 37.250000
|
||||
chamber_temperature_avg10 37.237499
|
||||
chamber_temperature_avg60 37.438541
|
||||
chamber_temperature_damped 42.185040
|
||||
chamber_temperature_pred60 36.638443
|
||||
inlet_flow_temperature 35.625000
|
||||
inlet_flow_temperature_avg10 35.618752
|
||||
inlet_flow_temperature_avg60 35.415627
|
||||
inlet_flow_temperature_damped 39.431259
|
||||
inlet_flow_temperature_pred60 36.078678
|
||||
outdoor_temperature 14.687500
|
||||
outdoor_temperature_avg10 14.662500
|
||||
outdoor_temperature_avg60 14.646875
|
||||
outdoor_temperature_damped 9.169084
|
||||
outdoor_temperature_pred60 14.660233
|
||||
return_flow_temperature 39.937500
|
||||
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
|
||||
outdoor_temperature_state 0
|
||||
inlet_flow_temperature_state 0
|
||||
return_flow_temperature_state 0
|
||||
safety_state 0
|
||||
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
|
||||
system_unixtime 1735242392
|
||||
uptime_seconds 40
|
||||
wifi_rssi -74
|
||||
system_unixtime 1762012743
|
||||
uptime_seconds 465229
|
||||
wifi_rssi -72
|
||||
```
|
||||
|
||||
#### Status Encoding
|
||||
|
||||
@ -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
|
||||
|
||||
320
main/control.c
320
main/control.c
@ -1,10 +1,10 @@
|
||||
#include "control.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_timer.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_timer.h"
|
||||
#include "esp_log.h"
|
||||
#include "control.h"
|
||||
#include "outputs.h"
|
||||
#include "inputs.h"
|
||||
#include "outputs.h"
|
||||
#include "safety.h"
|
||||
#include "sntp.h"
|
||||
|
||||
@ -15,33 +15,97 @@
|
||||
#define RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT 25.0f
|
||||
#define CHAMBER_TEMPERATURE_TARGET 80.0f // Max cutoff temperature
|
||||
#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
|
||||
#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 eControlState sControlState = CONTROL_STARTING;
|
||||
static eControlState gControlState = CONTROL_STARTING;
|
||||
// Control table for daily schedules
|
||||
static const sControlDay aControlTable[] = {
|
||||
{MONDAY, 2U, {{{4, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMBER_TEMPERATURE_TARGET}, {{22, 0}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, 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 const sControlDay gControlTable[] = {
|
||||
{MONDAY,
|
||||
2U,
|
||||
{{{4, 45},
|
||||
RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY,
|
||||
CHAMBER_TEMPERATURE_TARGET},
|
||||
{{22, 0},
|
||||
RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT,
|
||||
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 currentControlEntry = aControlTable[0].aTemperatureEntries[0];
|
||||
static sControlTemperatureEntry gCurrentControlEntry =
|
||||
gControlTable[0].aTemperatureEntries[0];
|
||||
static SemaphoreHandle_t xMutexAccessControl = NULL;
|
||||
|
||||
// Function prototypes
|
||||
void taskControl(void *pvParameters);
|
||||
void findControlCurrentTemperatureEntry(void);
|
||||
void setControlState(eControlState state);
|
||||
|
||||
void initControl(void)
|
||||
{
|
||||
BaseType_t taskCreated = xTaskCreate(
|
||||
taskControl, // Function to implement the task
|
||||
|
||||
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
|
||||
8192, // Stack size (in words, not bytes)
|
||||
NULL, // Parameters to the task function (none in this case)
|
||||
@ -63,7 +127,7 @@ void taskControl(void *pvParameters)
|
||||
{
|
||||
bool bHeatingInAction = false;
|
||||
bool bSummerMode = false;
|
||||
eBurnerState eBurnerState = BURNER_UNKNOWN;
|
||||
eBurnerState burnerState = BURNER_UNKNOWN;
|
||||
int64_t i64BurnerEnableTimestamp = esp_timer_get_time();
|
||||
|
||||
while (1)
|
||||
@ -74,7 +138,7 @@ void taskControl(void *pvParameters)
|
||||
if (getSafetyState() != SAFETY_NO_ERROR)
|
||||
{
|
||||
ESP_LOGW(TAG, "Control not possible due to safety fault!");
|
||||
sControlState = CONTROL_FAULT_SAFETY;
|
||||
setControlState(CONTROL_FAULT_SAFETY);
|
||||
if (bHeatingInAction)
|
||||
{
|
||||
ESP_LOGW(TAG, "Disabling burner due to safety fault");
|
||||
@ -89,7 +153,7 @@ void taskControl(void *pvParameters)
|
||||
if (getSntpState() != SYNC_SUCCESSFUL)
|
||||
{
|
||||
ESP_LOGW(TAG, "Control not possible due to SNTP fault!");
|
||||
sControlState = CONTROL_FAULT_SNTP;
|
||||
setControlState(CONTROL_FAULT_SNTP);
|
||||
if (bHeatingInAction)
|
||||
{
|
||||
ESP_LOGW(TAG, "Disabling burner due to SNTP fault");
|
||||
@ -100,80 +164,91 @@ void taskControl(void *pvParameters)
|
||||
continue;
|
||||
}
|
||||
|
||||
sControlTemperatureEntry currentControlEntry = getControlCurrentTemperatureEntry();
|
||||
findControlCurrentTemperatureEntry();
|
||||
|
||||
if (getOutdoorTemperature().fDampedValue >= SUMMER_MODE_TEMPERATURE_THRESHOLD_HIGH)
|
||||
if (getOutdoorTemperature().fDampedValue >=
|
||||
SUMMER_MODE_TEMPERATURE_THRESHOLD_HIGH)
|
||||
{
|
||||
bSummerMode = true;
|
||||
}
|
||||
else if (getOutdoorTemperature().fDampedValue <= SUMMER_MODE_TEMPERATURE_THRESHOLD_LOW)
|
||||
else if (getOutdoorTemperature().fDampedValue <=
|
||||
SUMMER_MODE_TEMPERATURE_THRESHOLD_LOW)
|
||||
{
|
||||
bSummerMode = false;
|
||||
}
|
||||
|
||||
// Enable burner if outdoor temperature is low and return flow temperature is cooled down
|
||||
if (!bHeatingInAction && (eBurnerState != BURNER_FAULT))
|
||||
// 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);
|
||||
setSafetyControlState(DISABLED);
|
||||
sControlState = CONTROL_OUTDOOR_TOO_WARM;
|
||||
setControlState(CONTROL_OUTDOOR_TOO_WARM);
|
||||
}
|
||||
else if ((getReturnFlowTemperature().average60s.fValue <= currentControlEntry.fReturnFlowTemperature) &&
|
||||
(getChamberTemperature().fCurrentValue <= CHAMBER_TEMPERATURE_THRESHOLD))
|
||||
else if ((getReturnFlowTemperature().average60s.fValue <=
|
||||
getControlCurrentTemperatureEntry().fReturnFlowTemperature) &&
|
||||
(getChamberTemperature().fCurrentValue <=
|
||||
CHAMBER_TEMPERATURE_THRESHOLD))
|
||||
{
|
||||
ESP_LOGI(TAG, "Enabling burner: Return flow temperature target reached");
|
||||
eBurnerState = BURNER_UNKNOWN;
|
||||
ESP_LOGI(TAG,
|
||||
"Enabling burner: Return flow temperature target reached");
|
||||
burnerState = BURNER_UNKNOWN;
|
||||
bHeatingInAction = true;
|
||||
setBurnerState(ENABLED);
|
||||
setSafetyControlState(ENABLED);
|
||||
i64BurnerEnableTimestamp = esp_timer_get_time();
|
||||
sControlState = CONTROL_HEATING;
|
||||
setControlState(CONTROL_HEATING);
|
||||
}
|
||||
else
|
||||
{
|
||||
// ESP_LOGI(TAG, "Return flow temperature too warm: Disabling heating");
|
||||
sControlState = CONTROL_RETURN_FLOW_TOO_WARM;
|
||||
setControlState(CONTROL_RETURN_FLOW_TOO_WARM);
|
||||
}
|
||||
}
|
||||
|
||||
// Disable burner if target temperature is reached or a fault occurred
|
||||
if (bHeatingInAction)
|
||||
{
|
||||
if ((getChamberTemperature().fCurrentValue >= currentControlEntry.fChamberTemperature) ||
|
||||
(getChamberTemperature().predict60s.fValue >= currentControlEntry.fChamberTemperature))
|
||||
if ((getChamberTemperature().fCurrentValue >=
|
||||
getControlCurrentTemperatureEntry().fChamberTemperature) ||
|
||||
(getChamberTemperature().predict60s.fValue >=
|
||||
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)
|
||||
else if (esp_timer_get_time() - i64BurnerEnableTimestamp >=
|
||||
BURNER_FAULT_DETECTION_THRESHOLD * 1000000U)
|
||||
{
|
||||
if (eBurnerState == BURNER_UNKNOWN)
|
||||
if (burnerState == BURNER_UNKNOWN)
|
||||
{
|
||||
if (getBurnerError() == FAULT)
|
||||
{
|
||||
// ESP_LOGW(TAG, "Burner fault detected: Disabling burner");
|
||||
bHeatingInAction = false;
|
||||
eBurnerState = BURNER_FAULT;
|
||||
sControlState = CONTROL_FAULT_BURNER;
|
||||
burnerState = BURNER_FAULT;
|
||||
setControlState(CONTROL_FAULT_BURNER);
|
||||
setBurnerState(DISABLED);
|
||||
setSafetyControlState(ENABLED);
|
||||
}
|
||||
else
|
||||
{
|
||||
// ESP_LOGI(TAG, "No burner fault detected: Marking burner as fired");
|
||||
eBurnerState = BURNER_FIRED;
|
||||
// ESP_LOGI(TAG, "No burner fault detected: Marking burner as
|
||||
// fired");
|
||||
burnerState = BURNER_FIRED;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Manage circulation pump
|
||||
if (getChamberTemperature().fCurrentValue <= CIRCULATION_PUMP_TEMPERATURE_THRESHOLD)
|
||||
if (getChamberTemperature().fCurrentValue <=
|
||||
CIRCULATION_PUMP_TEMPERATURE_THRESHOLD)
|
||||
{
|
||||
// ESP_LOGI(TAG, "Burner cooled down: Disabling circulation pump");
|
||||
setCirculationPumpState(DISABLED);
|
||||
@ -186,51 +261,158 @@ 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)
|
||||
{
|
||||
return sControlState;
|
||||
|
||||
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)
|
||||
{
|
||||
time_t now;
|
||||
struct tm *timeinfo;
|
||||
|
||||
time(&now);
|
||||
timeinfo = localtime(&now);
|
||||
|
||||
int day = timeinfo->tm_wday;
|
||||
return (eControlWeekday)((day == 0) ? 6 : day - 1);
|
||||
}
|
||||
|
||||
void findControlCurrentTemperatureEntry(void)
|
||||
{
|
||||
eControlWeekday currentDay = getControlCurrentWeekday();
|
||||
|
||||
// Get current time
|
||||
time_t now;
|
||||
struct tm timeinfo;
|
||||
time(&now);
|
||||
localtime_r(&now, &timeinfo);
|
||||
|
||||
int hour = timeinfo.tm_hour;
|
||||
int minute = timeinfo.tm_min;
|
||||
int day = timeinfo.tm_wday;
|
||||
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();
|
||||
|
||||
// Get current time
|
||||
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)
|
||||
{
|
||||
for (int j = 0; j < aControlTable[i].entryCount; j++)
|
||||
|
||||
// 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++)
|
||||
{
|
||||
if ((aControlTable[i].day > currentDay) ||
|
||||
(aControlTable[i].day == currentDay && aControlTable[i].aTemperatureEntries[j].timestamp.hour > hour) ||
|
||||
(aControlTable[i].day == currentDay && aControlTable[i].aTemperatureEntries[j].timestamp.hour == hour && aControlTable[i].aTemperatureEntries[j].timestamp.minute >= minute))
|
||||
const sControlDay *day = &gControlTable[dayIndex];
|
||||
|
||||
for (int entryIndex = 0; entryIndex < day->entryCount; entryIndex++)
|
||||
{
|
||||
currentControlEntry = aControlTable[i].aTemperatureEntries[j];
|
||||
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];
|
||||
}
|
||||
currentControlEntry = aControlTable[i].aTemperatureEntries[j];
|
||||
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
|
||||
// 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()");
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
17
main/idf_component.yml
Normal file
17
main/idf_component.yml
Normal file
@ -0,0 +1,17 @@
|
||||
## IDF Component Manager Manifest File
|
||||
dependencies:
|
||||
## Required IDF version
|
||||
idf:
|
||||
version: '>=4.1.0'
|
||||
# # Put list of dependencies here
|
||||
# # For components maintained by Espressif:
|
||||
# component: "~1.0.0"
|
||||
# # For 3rd party components:
|
||||
# username/component: ">=1.0.0,<2.0.0"
|
||||
# username2/component2:
|
||||
# version: "~1.0.0"
|
||||
# # For transient dependencies `public` flag can be set.
|
||||
# # `public` flag doesn't have an effect dependencies of the `main` component.
|
||||
# # All dependencies of `main` are public by default.
|
||||
# public: true
|
||||
esp-idf-lib/ds18x20: '*'
|
||||
@ -49,7 +49,12 @@ void initInputs(void)
|
||||
.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();
|
||||
if (xMutexAccessInputs == NULL)
|
||||
@ -94,17 +99,17 @@ void initMeasurement(sMeasurement *pMeasurement)
|
||||
pMeasurement->average10s.fValue = INITIALISATION_VALUE;
|
||||
pMeasurement->average10s.bufferCount = 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.bufferCount = 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.bufferCount = 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)
|
||||
@ -122,12 +127,19 @@ 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;
|
||||
@ -144,7 +156,14 @@ 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)
|
||||
|
||||
@ -301,23 +301,23 @@ void taskMetrics(void *pvParameters)
|
||||
|
||||
// Wifi RSSI
|
||||
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");
|
||||
aMetrics[u16MetricCounter].type = INTEGER_64;
|
||||
aMetrics[u16MetricCounter].i64MetricValue = ap.rssi;
|
||||
u16MetricCounter++;
|
||||
|
||||
ESP_ERROR_CHECK(u16MetricCounter > METRIC_MAX_COUNT);
|
||||
configASSERT(!(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, strlen(caHtmlResponse));
|
||||
memset(caHtmlResponse, 0U, HTML_RESPONSE_SIZE);
|
||||
for (uint16_t u16Index = 0U; u16Index < u16Size; u16Index++)
|
||||
{
|
||||
char caValueBuffer[64];
|
||||
|
||||
@ -41,9 +41,26 @@ void initOutputs(void)
|
||||
.intr_type = GPIO_INTR_DISABLE // Disable interrupts
|
||||
};
|
||||
|
||||
gpio_config(&ioConfCirculationPump);
|
||||
gpio_config(&ioConfBurner);
|
||||
gpio_config(&ioConfSafetyContact);
|
||||
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;
|
||||
}
|
||||
|
||||
xMutexAccessOutputs = xSemaphoreCreateRecursiveMutex();
|
||||
if (xMutexAccessOutputs == NULL)
|
||||
@ -55,7 +72,17 @@ void initOutputs(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)
|
||||
@ -70,6 +97,7 @@ void setCirculationPumpState(eOutput in)
|
||||
break;
|
||||
case DISABLED:
|
||||
gpio_set_level(uCirculationPumpGpioPin, 1U); // Switch off Circulation Pump
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@ -108,6 +136,7 @@ void setBurnerState(eOutput in)
|
||||
break;
|
||||
case DISABLED:
|
||||
gpio_set_level(uBurnerGpioPin, 1U); // Switch off Burner
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@ -146,6 +175,7 @@ void setSafetyControlState(eOutput in)
|
||||
break;
|
||||
case DISABLED:
|
||||
gpio_set_level(uSafetyContactGpioPin, 1U); // Switch off power for Burner
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
@ -2,10 +2,12 @@
|
||||
#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;
|
||||
@ -91,7 +93,7 @@ void checkSensorSanity(void)
|
||||
}
|
||||
else
|
||||
{
|
||||
if (sCurrentMeasurement.fCurrentValue == sanityChecks[i].fSensorTemperatureLast)
|
||||
if (fabsf(sCurrentMeasurement.fCurrentValue - sanityChecks[i].fSensorTemperatureLast) < FLOAT_EPSILON)
|
||||
{
|
||||
sanityChecks[i].uUnchangedCounter++;
|
||||
if (sanityChecks[i].uUnchangedCounter >= (SENSOR_GRACE_PERIOD / PERIODIC_INTERVAL))
|
||||
@ -103,6 +105,7 @@ void checkSensorSanity(void)
|
||||
}
|
||||
else
|
||||
{
|
||||
sanityChecks[i].uUnchangedCounter = 0U;
|
||||
sanityChecks[i].fSensorTemperatureLast = sCurrentMeasurement.fCurrentValue;
|
||||
|
||||
if (sCurrentMeasurement.fCurrentValue > sanityChecks[i].sSensorLimit.max)
|
||||
@ -119,12 +122,10 @@ void checkSensorSanity(void)
|
||||
}
|
||||
else
|
||||
{
|
||||
sanityChecks[i].uUnchangedCounter = 0U;
|
||||
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
|
||||
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);
|
||||
}
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
#include "sntp.h"
|
||||
|
||||
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 initSntp(void)
|
||||
|
||||
94
main/wifi.c
94
main/wifi.c
@ -13,26 +13,38 @@
|
||||
|
||||
#define WIFI_CONNECTED_BIT BIT0
|
||||
#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 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,
|
||||
int32_t event_id, void *event_data);
|
||||
static bool parse_bssid(const char *bssid_str, uint8_t *bssid);
|
||||
|
||||
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_netif_dhcpc_stop(my_sta);
|
||||
ESP_ERROR_CHECK(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_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();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
@ -56,6 +68,21 @@ void initWifi(void)
|
||||
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
|
||||
},
|
||||
};
|
||||
|
||||
#if CONFIG_ENV_WIFI_BSSID_LOCK
|
||||
/* Lock to specific AP by BSSID */
|
||||
if (parse_bssid(CONFIG_ENV_WIFI_BSSID, wifi_config.sta.bssid))
|
||||
{
|
||||
wifi_config.sta.bssid_set = true;
|
||||
ESP_LOGI(TAG, "BSSID lock enabled: %s", CONFIG_ENV_WIFI_BSSID);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "Invalid BSSID format: %s", CONFIG_ENV_WIFI_BSSID);
|
||||
wifi_config.sta.bssid_set = false;
|
||||
}
|
||||
#endif
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
||||
|
||||
@ -84,7 +111,9 @@ void initWifi(void)
|
||||
{
|
||||
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,
|
||||
@ -96,13 +125,70 @@ static void event_handler(void *arg, esp_event_base_t event_base,
|
||||
}
|
||||
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_LOGI(TAG, "Retry to connect to the AP");
|
||||
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)
|
||||
{
|
||||
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
|
||||
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);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(TAG, "Successfully reconnected to AP");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Parse BSSID string to byte array
|
||||
*
|
||||
* @param bssid_str BSSID string in format "XX:XX:XX:XX:XX:XX"
|
||||
* @param bssid Output byte array (6 bytes)
|
||||
* @return true on success, false on parse error
|
||||
*/
|
||||
static bool parse_bssid(const char *bssid_str, uint8_t *bssid)
|
||||
{
|
||||
unsigned int tmp[6];
|
||||
int parsed = sscanf(bssid_str, "%x:%x:%x:%x:%x:%x",
|
||||
&tmp[0], &tmp[1], &tmp[2],
|
||||
&tmp[3], &tmp[4], &tmp[5]);
|
||||
if (parsed != 6)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
bssid[i] = (uint8_t)tmp[i];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user