8 Commits

Author SHA256 Message Date
0c3779bf72 Merge pull request 'Fix: WiFi Reconnect' (#27) from bugfix/wifi-event-handler into main
Reviewed-on: #27
2026-01-25 14:58:36 +01:00
7f2461679e Merge branch 'main' into bugfix/wifi-event-handler 2026-01-25 14:49:55 +01:00
5a223ddbe5 Lock to specific Access Point (#26)
Reviewed-on: #26
Co-authored-by: localhorst <localhorst@mosad.xyz>
Co-committed-by: localhorst <localhorst@mosad.xyz>
2026-01-25 14:46:47 +01:00
c935eacbdd update to new IDF component lib (#31)
https://components.espressif.com/components/esp-idf-lib/ds18x20/versions/1.2.8/readme
Reviewed-on: #31
Co-authored-by: localhorst <localhorst@mosad.xyz>
Co-committed-by: localhorst <localhorst@mosad.xyz>
2026-01-25 14:28:03 +01:00
b77cba87ed fix wifi event handler 2026-01-09 23:44:53 +01:00
af307fd403 handle reconnect 2026-01-09 23:35:44 +01:00
618a6974bf enable ap lock 2026-01-09 23:26:46 +01:00
cb69bea618 Update control entry (#25)
Reviewed-on: #25
Co-authored-by: localhorst <localhorst@mosad.xyz>
Co-committed-by: localhorst <localhorst@mosad.xyz>
2026-01-09 23:17:26 +01:00
6 changed files with 291 additions and 59 deletions

2
.gitignore vendored
View File

@ -291,3 +291,5 @@ dkms.conf
*.out *.out
*.app *.app
managed_components/
dependencies.lock

View File

@ -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 # The following lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly # in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16) cmake_minimum_required(VERSION 3.16)

View File

@ -18,5 +18,19 @@ menu "Smart Oil Heating Control System"
config SNTP_SERVER_IP_ADDR config SNTP_SERVER_IP_ADDR
string "SNTP IPv4 server address" string "SNTP IPv4 server address"
default "192.168.0.1" 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 endmenu

View File

@ -1,10 +1,10 @@
#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 "esp_timer.h"
#include "esp_log.h"
#include "control.h"
#include "outputs.h"
#include "inputs.h" #include "inputs.h"
#include "outputs.h"
#include "safety.h" #include "safety.h"
#include "sntp.h" #include "sntp.h"
@ -15,24 +15,78 @@
#define RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT 25.0f #define RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT 25.0f
#define CHAMBER_TEMPERATURE_TARGET 80.0f // Max cutoff temperature #define CHAMBER_TEMPERATURE_TARGET 80.0f // Max cutoff temperature
#define CHAMBER_TEMPERATURE_THRESHOLD 45.0f // Min threshold for burner enable #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_HIGH \
#define SUMMER_MODE_TEMPERATURE_THRESHOLD_LOW 15.0f // Summer mode will be deactivated --> Heating starts 20.0f // Summer mode will be activated
#define CIRCULATION_PUMP_TEMPERATURE_THRESHOLD 30.0f // Min threshold of chamber for circulation pump enable #define SUMMER_MODE_TEMPERATURE_THRESHOLD_LOW \
#define BURNER_FAULT_DETECTION_THRESHOLD (60U * 4U) // Burner fault detection after 4 minutes 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 sControlState = CONTROL_STARTING; static eControlState sControlState = CONTROL_STARTING;
// Control table for daily schedules // Control table for daily schedules
static const sControlDay aControlTable[] = { 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}}}, {MONDAY,
{TUESDAY, 2U, {{{4, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMBER_TEMPERATURE_TARGET}, {{22, 0}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, CHAMBER_TEMPERATURE_TARGET}}}, 2U,
{WEDNESDAY, 2U, {{{4, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMBER_TEMPERATURE_TARGET}, {{22, 0}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, CHAMBER_TEMPERATURE_TARGET}}}, {{{4, 45},
{THURSDAY, 2U, {{{4, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMBER_TEMPERATURE_TARGET}, {{22, 0}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, CHAMBER_TEMPERATURE_TARGET}}}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY,
{FRIDAY, 2U, {{{4, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMBER_TEMPERATURE_TARGET}, {{23, 0}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, CHAMBER_TEMPERATURE_TARGET}}}, 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}}}, {{22, 0},
{SUNDAY, 2U, {{{6, 45}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_DAY, CHAMBER_TEMPERATURE_TARGET}, {{22, 30}, RETURN_FLOW_TEMPERATURE_LOWER_LIMIT_NIGHT, CHAMBER_TEMPERATURE_TARGET}}}, 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 currentControlEntry =
aControlTable[0].aTemperatureEntries[0];
// Function prototypes // Function prototypes
void taskControl(void *pvParameters); void taskControl(void *pvParameters);
@ -40,8 +94,8 @@ void findControlCurrentTemperatureEntry(void);
void initControl(void) void initControl(void)
{ {
BaseType_t taskCreated = xTaskCreate( BaseType_t taskCreated =
taskControl, // Function to implement the task 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)
@ -101,18 +155,22 @@ void taskControl(void *pvParameters)
} }
findControlCurrentTemperatureEntry(); findControlCurrentTemperatureEntry();
sControlTemperatureEntry currentControlEntry = getControlCurrentTemperatureEntry(); sControlTemperatureEntry currentControlEntry =
getControlCurrentTemperatureEntry();
if (getOutdoorTemperature().fDampedValue >= SUMMER_MODE_TEMPERATURE_THRESHOLD_HIGH) if (getOutdoorTemperature().fDampedValue >=
SUMMER_MODE_TEMPERATURE_THRESHOLD_HIGH)
{ {
bSummerMode = true; bSummerMode = true;
} }
else if (getOutdoorTemperature().fDampedValue <= SUMMER_MODE_TEMPERATURE_THRESHOLD_LOW) else if (getOutdoorTemperature().fDampedValue <=
SUMMER_MODE_TEMPERATURE_THRESHOLD_LOW)
{ {
bSummerMode = false; bSummerMode = false;
} }
// Enable burner if outdoor temperature is low and return flow temperature is cooled down // Enable burner if outdoor temperature is low and return flow temperature
// is cooled down
if (!bHeatingInAction && (eBurnerState != BURNER_FAULT)) if (!bHeatingInAction && (eBurnerState != BURNER_FAULT))
{ {
if (bSummerMode) if (bSummerMode)
@ -122,10 +180,13 @@ void taskControl(void *pvParameters)
setSafetyControlState(DISABLED); setSafetyControlState(DISABLED);
sControlState = CONTROL_OUTDOOR_TOO_WARM; sControlState = CONTROL_OUTDOOR_TOO_WARM;
} }
else if ((getReturnFlowTemperature().average60s.fValue <= currentControlEntry.fReturnFlowTemperature) && else if ((getReturnFlowTemperature().average60s.fValue <=
(getChamberTemperature().fCurrentValue <= CHAMBER_TEMPERATURE_THRESHOLD)) currentControlEntry.fReturnFlowTemperature) &&
(getChamberTemperature().fCurrentValue <=
CHAMBER_TEMPERATURE_THRESHOLD))
{ {
ESP_LOGI(TAG, "Enabling burner: Return flow temperature target reached"); ESP_LOGI(TAG,
"Enabling burner: Return flow temperature target reached");
eBurnerState = BURNER_UNKNOWN; eBurnerState = BURNER_UNKNOWN;
bHeatingInAction = true; bHeatingInAction = true;
setBurnerState(ENABLED); setBurnerState(ENABLED);
@ -143,15 +204,18 @@ void taskControl(void *pvParameters)
// Disable burner if target temperature is reached or a fault occurred // Disable burner if target temperature is reached or a fault occurred
if (bHeatingInAction) if (bHeatingInAction)
{ {
if ((getChamberTemperature().fCurrentValue >= currentControlEntry.fChamberTemperature) || if ((getChamberTemperature().fCurrentValue >=
(getChamberTemperature().predict60s.fValue >= currentControlEntry.fChamberTemperature)) currentControlEntry.fChamberTemperature) ||
(getChamberTemperature().predict60s.fValue >=
currentControlEntry.fChamberTemperature))
{ {
ESP_LOGI(TAG, "Chamber target temperature reached: Disabling burner"); ESP_LOGI(TAG, "Chamber target temperature reached: Disabling burner");
bHeatingInAction = false; bHeatingInAction = false;
setBurnerState(DISABLED); setBurnerState(DISABLED);
setSafetyControlState(ENABLED); 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 (eBurnerState == BURNER_UNKNOWN)
{ {
@ -166,7 +230,8 @@ void taskControl(void *pvParameters)
} }
else else
{ {
// ESP_LOGI(TAG, "No burner fault detected: Marking burner as fired"); // ESP_LOGI(TAG, "No burner fault detected: Marking burner as
// fired");
eBurnerState = BURNER_FIRED; eBurnerState = BURNER_FIRED;
} }
} }
@ -174,7 +239,8 @@ void taskControl(void *pvParameters)
} }
// Manage circulation pump // 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"); // ESP_LOGI(TAG, "Burner cooled down: Disabling circulation pump");
setCirculationPumpState(DISABLED); setCirculationPumpState(DISABLED);
@ -187,10 +253,7 @@ void taskControl(void *pvParameters)
} // End of while(1) } // End of while(1)
} }
eControlState getControlState(void) eControlState getControlState(void) { return sControlState; }
{
return sControlState;
}
eControlWeekday getControlCurrentWeekday(void) eControlWeekday getControlCurrentWeekday(void)
{ {
@ -204,33 +267,92 @@ eControlWeekday getControlCurrentWeekday(void)
return (eControlWeekday)((day == 0) ? 6 : day - 1); return (eControlWeekday)((day == 0) ? 6 : day - 1);
} }
/**
* @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) void findControlCurrentTemperatureEntry(void)
{ {
eControlWeekday currentDay = getControlCurrentWeekday(); eControlWeekday currentDay = getControlCurrentWeekday();
// Get current time
time_t now; time_t now;
struct tm timeinfo; struct tm timeinfo;
time(&now); time(&now);
localtime_r(&now, &timeinfo); localtime_r(&now, &timeinfo);
int hour = timeinfo.tm_hour; int currentHour = timeinfo.tm_hour;
int minute = timeinfo.tm_min; int currentMinute = timeinfo.tm_min;
for (int i = 0; i < sizeof(aControlTable) / sizeof(aControlTable[0]); i++) // 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++)
{ {
for (int j = 0; j < aControlTable[i].entryCount; j++) const sControlDay *day = &aControlTable[dayIndex];
for (int entryIndex = 0; entryIndex < day->entryCount; entryIndex++)
{ {
if ((aControlTable[i].day > currentDay) || const sControlTemperatureEntry *entry = &day->aTemperatureEntries[entryIndex];
(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)) // 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)
{ {
currentControlEntry = aControlTable[i].aTemperatureEntries[j]; // Found next scheduled entry, so determine the previous (active) one
if (entryIndex > 0)
{
// Use previous entry from same day
currentControlEntry = day->aTemperatureEntries[entryIndex - 1];
} }
currentControlEntry = aControlTable[i].aTemperatureEntries[j]; 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 = &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; return currentControlEntry;

17
main/idf_component.yml Normal file
View 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: '*'

View File

@ -13,12 +13,18 @@
#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);
static bool parse_bssid(const char *bssid_str, uint8_t *bssid);
void initWifi(void) void initWifi(void)
{ {
@ -56,6 +62,21 @@ void initWifi(void)
.threshold.authmode = WIFI_AUTH_WPA2_PSK, .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_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
@ -84,7 +105,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 +119,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) 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();
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) 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");
}
}
}
/**
* @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;
} }