Fix WiFi configuration initialization and add proper logging
This commit is contained in:
@@ -1,48 +1,72 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
#include "freertos/semphr.h"
|
#include "freertos/semphr.h"
|
||||||
#include "esp_system.h"
|
#include "esp_system.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
//#include "esp_pm.h"
|
|
||||||
#include "esp_task_wdt.h"
|
#include "esp_task_wdt.h"
|
||||||
#include "esp_wifi.h"
|
#include "esp_wifi.h"
|
||||||
|
#include "esp_event.h"
|
||||||
|
#include "esp_netif.h"
|
||||||
|
#include "nvs_flash.h"
|
||||||
#include "main.hpp"
|
#include "main.hpp"
|
||||||
#include "wifi-credentials.h"
|
#include "wifi-credentials.h"
|
||||||
|
|
||||||
//check this
|
static const char* TAG = "WIFI";
|
||||||
wifi_mode_t wifi_mode = WIFI_MODE_STA;
|
|
||||||
wifi_init_config_ wifi_cfg = {
|
static wifi_config_t wifi_cfg;
|
||||||
.sta = {
|
|
||||||
.ssid = WIFI_SSID,
|
|
||||||
.password = WIFI_PASSWORD,
|
|
||||||
.threshold.authmode = WIFI_AUTH,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
static void wifiInit() {
|
static void wifiInit() {
|
||||||
ESP_ERROR_CHECK(esp_wifi_init(*wifi_cfg));
|
ESP_LOGI(TAG, "Initializing WiFi...");
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_mode(wifi_mode));
|
// Initialize WiFi configuration
|
||||||
|
memset(&wifi_cfg, 0, sizeof(wifi_cfg));
|
||||||
|
strcpy((char*)wifi_cfg.sta.ssid, WIFI_SSID);
|
||||||
|
strcpy((char*)wifi_cfg.sta.password, WIFI_PASSWORD);
|
||||||
|
wifi_cfg.sta.threshold.authmode = WIFI_AUTH;
|
||||||
|
|
||||||
|
ESP_ERROR_CHECK(nvs_flash_init());
|
||||||
|
ESP_ERROR_CHECK(esp_netif_init());
|
||||||
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||||
|
|
||||||
|
esp_netif_create_default_wifi_sta();
|
||||||
|
|
||||||
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg));
|
||||||
|
|
||||||
esp_wifi_start();
|
ESP_ERROR_CHECK(esp_wifi_start());
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void wifiTryConnect() {
|
static inline void wifiTryConnect() {
|
||||||
wifiInit();
|
wifiInit();
|
||||||
|
|
||||||
if(esp_wifi_get_mode(wifi_mode)) ESP_ERROR_CHECK(esp_wifi_connect());
|
wifi_mode_t mode;
|
||||||
else criticalErrorFlag = true;
|
if(esp_wifi_get_mode(&mode) == ESP_OK && mode == WIFI_MODE_STA) {
|
||||||
|
ESP_LOGI(TAG, "Attempting to connect to WiFi...");
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_connect());
|
||||||
|
} else {
|
||||||
|
ESP_LOGE(TAG, "WiFi mode check failed, setting critical error flag");
|
||||||
|
criticalErrorFlag = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void wifiManager() {
|
static void wifiManager() {
|
||||||
if(!esp_wifi_get_mode(wifi_mode)) wifiTryConnect();
|
wifi_mode_t mode;
|
||||||
|
if(esp_wifi_get_mode(&mode) != ESP_OK || mode != WIFI_MODE_STA) {
|
||||||
|
wifiTryConnect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifiTask(void *pvParameters) {
|
void wifiTask(void *pvParameters) {
|
||||||
|
ESP_LOGI(TAG, "WiFi task started");
|
||||||
ESP_ERROR_CHECK(esp_task_wdt_add(NULL));
|
ESP_ERROR_CHECK(esp_task_wdt_add(NULL));
|
||||||
|
|
||||||
wifiManager();
|
wifiManager();
|
||||||
|
|
||||||
esp_task_wdt_reset();
|
for(;;) {
|
||||||
vTaskDelay(pdMS_TO_TICKS(2500));
|
esp_task_wdt_reset();
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(2500));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user