Merge branch 'dev-esp32-wifi' into dev-esp32-mqtt

This commit is contained in:
2025-12-09 00:47:34 +01:00
2 changed files with 18 additions and 12 deletions
@@ -1,10 +1,12 @@
#ifndef WIFI_CREDENTIALS_H #ifndef CONFIG_H
#define WIFI_CREDENTIALS_H #define CONFIG_H
// TODO: make this a config.h // TODO: make this a config.h
#define SSID "your_wifi_ssid" #define SSID "your_wifi_ssid"
#define PASSWORD "your_wifi_password" #define PASSWORD "your_wifi_password"
#define AUTH_MODE WIFI_AUTH_WPA2_PSK #define AUTH_MODE WIFI_AUTH_WPA2_PSK
#define MQTT_ADDR "mqtt://yourmqttserver"
#define MQTT_PORT 1883
#endif #endif
+14 -10
View File
@@ -6,17 +6,19 @@
#include "esp_log.h" #include "esp_log.h"
#include "nvs_flash.h" #include "nvs_flash.h"
#include "main.hpp" #include "main.hpp"
#include "wifi-credentials.h" #include "config.h"
#define WIFI_TIMEOUT_MS 1500 #define WIFI_TIMEOUT_TICKS pdMS_TO_TICKS(8000)
static const char* TAG = "WIFI"; static const char* TAG = "WIFI";
EventGroupHandle_t wifi_event_group;
static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
if (event_base == WIFI_EVENT) { if (event_base == WIFI_EVENT) {
switch (event_id) { switch (event_id) {
case WIFI_EVENT_STA_START: case WIFI_EVENT_STA_START:
ESP_LOGV(TAG, "Process started, connecting..."); ESP_LOGI(TAG, "Wi-Fi process started, connecting...");
esp_wifi_connect(); esp_wifi_connect();
break; break;
@@ -24,7 +26,7 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t e
wifi_event_sta_disconnected_t* disconn = (wifi_event_sta_disconnected_t*) event_data; wifi_event_sta_disconnected_t* disconn = (wifi_event_sta_disconnected_t*) event_data;
ESP_LOGW(TAG, "Disconnected, reason: %d", disconn->reason); ESP_LOGW(TAG, "Disconnected, reason: %d", disconn->reason);
xEventGroupClearBits(connectivity_event_group, WIFI_CONNECTED_BIT); xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT);
esp_wifi_connect(); esp_wifi_connect();
break; break;
} }
@@ -34,8 +36,8 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t e
} }
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_LOGD(TAG, "Got IP: " IPSTR, IP2STR(&event->ip_info.ip)); ESP_LOGI(TAG, "Got IP: " IPSTR, IP2STR(&event->ip_info.ip));
xEventGroupSetBits(connectivity_event_group, WIFI_CONNECTED_BIT); xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT);
} }
} }
@@ -47,6 +49,8 @@ static void wifi_init_sta(void) {
ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(nvs_flash_init());
} }
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default()); ESP_ERROR_CHECK(esp_event_loop_create_default());
@@ -71,7 +75,7 @@ static void wifi_init_sta(void) {
} }
void wifiTask(void *pvParameters) { void wifiTask(void *pvParameters) {
ESP_LOGI(TAG, "Task started"); ESP_LOGI(TAG, "WiFi task started");
wifi_init_sta(); wifi_init_sta();
ESP_ERROR_CHECK(esp_task_wdt_add(NULL)); ESP_ERROR_CHECK(esp_task_wdt_add(NULL));
@@ -83,10 +87,10 @@ void wifiTask(void *pvParameters) {
} }
//TODO: manage data send rate if with slow network //TODO: manage data send rate if with slow network
// or just let mqtt know (useful for making a notice on home manager)
ESP_ERROR_CHECK(esp_task_wdt_reset()); ESP_ERROR_CHECK(esp_task_wdt_reset());
ESP_LOGV(TAG, "Task reset"); ESP_LOGI(TAG, "Wi-Fi task reset");
vTaskDelay(pdMS_TO_TICKS(WIFI_TIMEOUT_MS)); vTaskDelay(pdMS_TO_TICKS(WIFI_TIMEOUT_TICKS));
} }
ESP_LOGI(TAG, "Task stopped");
} }