Directory restructure
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
idf_component_register(SRCS "../source/main.cpp"
|
||||
"../source/wifi.cpp"
|
||||
idf_component_register(SRCS "./main.cpp"
|
||||
"./wifi.cpp"
|
||||
INCLUDE_DIRS "../include"
|
||||
REQUIRES esp_wifi
|
||||
nvs_flash
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_pm.h"
|
||||
#include "esp_task_wdt.h"
|
||||
#include "wifi.hpp"
|
||||
|
||||
static const char* TAG = "MAIN";
|
||||
|
||||
volatile bool criticalErrorFlag = false;
|
||||
|
||||
static void watchdogTask(void *pvParameters) {
|
||||
esp_task_wdt_config_t twdt_config = {
|
||||
.timeout_ms = 12000,
|
||||
.idle_core_mask = 0,
|
||||
.trigger_panic = true,
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_task_wdt_init(&twdt_config));
|
||||
ESP_ERROR_CHECK(esp_task_wdt_add(NULL));
|
||||
|
||||
const TickType_t keepAlivePeriod = pdMS_TO_TICKS(8000);
|
||||
|
||||
for(;;) {
|
||||
if (criticalErrorFlag) {
|
||||
ESP_LOGE(TAG, "Critical error flag raised. Rebooting.");
|
||||
esp_restart();
|
||||
}
|
||||
|
||||
esp_task_wdt_reset();
|
||||
|
||||
vTaskDelay(keepAlivePeriod);
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" void app_main(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Hello from user land – bootloader worked yay!");
|
||||
|
||||
static const esp_pm_config_t pm_cfg = {
|
||||
.max_freq_mhz = 80,
|
||||
.min_freq_mhz = 10,
|
||||
.light_sleep_enable = true
|
||||
};
|
||||
esp_pm_configure(&pm_cfg);
|
||||
|
||||
xTaskCreate(watchdogTask, "watchdogs", 2048, NULL, configMAX_PRIORITIES-1, NULL);
|
||||
xTaskCreate(wifiTask, "wifi", 4096, NULL, configMAX_PRIORITIES-4, NULL);
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_task_wdt.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_netif.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "main.hpp"
|
||||
#include "wifi-credentials.h"
|
||||
|
||||
static const char* TAG = "WIFI";
|
||||
|
||||
static wifi_config_t wifi_cfg;
|
||||
|
||||
static void wifiInit() {
|
||||
ESP_LOGI(TAG, "Initializing WiFi...");
|
||||
|
||||
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_ERROR_CHECK(esp_wifi_start());
|
||||
}
|
||||
|
||||
static inline void wifiTryConnect() {
|
||||
wifiInit();
|
||||
|
||||
wifi_mode_t mode;
|
||||
|
||||
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() {
|
||||
wifi_mode_t mode;
|
||||
if(esp_wifi_get_mode(&mode) != ESP_OK || mode != WIFI_MODE_STA) {
|
||||
wifiTryConnect();
|
||||
}
|
||||
}
|
||||
|
||||
void wifiTask(void *pvParameters) {
|
||||
ESP_LOGI(TAG, "WiFi task started");
|
||||
ESP_ERROR_CHECK(esp_task_wdt_add(NULL));
|
||||
|
||||
wifiManager();
|
||||
|
||||
for(;;) {
|
||||
esp_task_wdt_reset();
|
||||
vTaskDelay(pdMS_TO_TICKS(2500));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user