Fix missing code from merge for connectivity_event_group

This commit is contained in:
2025-12-09 01:37:18 +01:00
parent 88c7635ed9
commit 2c7951c2bb
2 changed files with 45 additions and 45 deletions
+33 -22
View File
@@ -6,6 +6,7 @@
#include "mqtt_client.h" #include "mqtt_client.h"
#include "main.hpp" #include "main.hpp"
//#include "i2c.hpp" //#include "i2c.hpp"
#include "config-erick.h"
#define MQTT_TASK_TIMEOUT_MS 3000 #define MQTT_TASK_TIMEOUT_MS 3000
@@ -16,7 +17,7 @@ static esp_mqtt_client_handle_t mqtt_client = NULL;
static char global_rx_buffer[50]; static char global_rx_buffer[50];
static inline void mqtt_subscribe(void) { static void mqtt_subscribe(void) {
esp_mqtt_client_subscribe(mqtt_client, "/topic/qos0", 0); esp_mqtt_client_subscribe(mqtt_client, "/topic/qos0", 0);
} }
@@ -24,52 +25,56 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
esp_mqtt_event_handle_t event = (esp_mqtt_event_handle_t)event_data; esp_mqtt_event_handle_t event = (esp_mqtt_event_handle_t)event_data;
switch (event->event_id) { switch (event->event_id) {
case MQTT_EVENT_CONNECTED: { case MQTT_EVENT_CONNECTED:
xEventGroupSetBits(connectivity_event_group, MQTT_CONNECTED_BIT); xEventGroupSetBits(connectivity_event_group, MQTT_CONNECTED_BIT);
mqtt_subscribe(); mqtt_subscribe();
ESP_LOGV(TAG, "Connected to broker"); ESP_LOGV(TAG, "Connected to broker");
break; break;
}
case MQTT_EVENT_DISCONNECTED: { case MQTT_EVENT_DISCONNECTED:
xEventGroupClearBits(connectivity_event_group, MQTT_CONNECTED_BIT); xEventGroupClearBits(connectivity_event_group, MQTT_CONNECTED_BIT);
ESP_LOGW(TAG, "Disconnected from broker"); ESP_LOGW(TAG, "Disconnected from broker");
break; break;
}
case MQTT_EVENT_DATA: { case MQTT_EVENT_DATA:
ESP_LOGD(TAG, "Listening to broker"); {
ESP_LOGD(TAG, "Topic: %.*s\n", event->topic_len, event->topic); ESP_LOGD(TAG, "Data received");
ESP_LOGD(TAG, "Data: %.*s\n", event->data_len, event->data);
int len = event->data_len; int len = event->data_len;
if (len > sizeof(global_rx_buffer) - 1) len = sizeof(global_rx_buffer) - 1; if (len > sizeof(global_rx_buffer) - 1) len = sizeof(global_rx_buffer) - 1;
memcpy(global_rx_buffer, event->data, len); memcpy(global_rx_buffer, event->data, len);
global_rx_buffer[len] = '\0'; global_rx_buffer[len] = '\0';
if (i2c_task_handle != NULL) xTaskNotifyGive(i2c_task_handle); if (i2c_task_handle != NULL) xTaskNotifyGive(i2c_task_handle);
break; break;
} }
default: {
default:
break; break;
}
} }
} }
static void mqtt_init(void) { static void mqtt_init(void) {
// TODO; implement Trust only or Mutual TLS in the future (and secure boot + flash encryption)
// TODO: add to config.h
esp_mqtt_client_config_t mqtt_cfg = {}; esp_mqtt_client_config_t mqtt_cfg = {};
mqtt_cfg.broker.address.uri = "mqtt://yourmqttserver";
mqtt_cfg.broker.address.port = 1883; mqtt_cfg.broker.address.uri = MQTT_ADDR;
mqtt_cfg.broker.address.port = MQTT_PORT;
mqtt_client = esp_mqtt_client_init(&mqtt_cfg); mqtt_client = esp_mqtt_client_init(&mqtt_cfg);
esp_mqtt_client_register_event(mqtt_client, (esp_mqtt_event_id_t)ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); esp_mqtt_client_register_event(mqtt_client, (esp_mqtt_event_id_t)ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
esp_mqtt_client_start(mqtt_client); esp_mqtt_client_start(mqtt_client);
} }
void mqtt_publish(void) {}
void mqttTask(void *pvParameters) { void mqttTask(void *pvParameters) {
ESP_LOGI(TAG, "Task started"); ESP_LOGV(TAG, "Task started");
i2c_task_handle = xTaskGetCurrentTaskHandle(); i2c_task_handle = xTaskGetCurrentTaskHandle();
mqtt_init(); mqtt_init();
ESP_ERROR_CHECK(esp_task_wdt_add(NULL)); ESP_ERROR_CHECK(esp_task_wdt_add(NULL));
@@ -77,16 +82,22 @@ void mqttTask(void *pvParameters) {
for(;;) { for(;;) {
ESP_LOGV(TAG, "Checking broker connection..."); ESP_LOGV(TAG, "Checking broker connection...");
xEventGroupWaitBits(connectivity_event_group, WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT, pdFALSE, pdTRUE, portMAX_DELAY); EventBits_t bits = xEventGroupGetBits(connectivity_event_group);
ESP_LOGV(TAG, "Connection established"); if ((bits & (WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT)) == (WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT)) {
if (ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(MQTT_TASK_TIMEOUT_MS))) { ESP_LOGV(TAG, "Connection established");
//i2c_do(global_rx_buffer); PLACEHOLDER FUNC!
uint32_t task_notification = ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(MQTT_TASK_TIMEOUT_MS));
if (task_notification) {} //{twi_do(global_rx_buffer)}
} else {
ESP_LOGW(TAG, "Waiting for connection...");
vTaskDelay(pdMS_TO_TICKS(MQTT_TASK_TIMEOUT_MS/2));
} }
ESP_ERROR_CHECK(esp_task_wdt_reset()); ESP_ERROR_CHECK(esp_task_wdt_reset());
ESP_LOGV(TAG, "Task reset"); ESP_LOGV(TAG, "Task reset");
} }
ESP_LOGI(TAG, "Task stopped");
} }
+12 -23
View File
@@ -6,27 +6,26 @@
#include "esp_log.h" #include "esp_log.h"
#include "nvs_flash.h" #include "nvs_flash.h"
#include "main.hpp" #include "main.hpp"
#include "config.h" #include "config-erick.h"
#define WIFI_TIMEOUT_TICKS pdMS_TO_TICKS(8000) #define WIFI_TIMEOUT_MS 4500
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_LOGI(TAG, "Wi-Fi process started, connecting..."); ESP_LOGV(TAG, "Process started, connecting...");
esp_wifi_connect(); esp_wifi_connect();
break; break;
case WIFI_EVENT_STA_DISCONNECTED: { case WIFI_EVENT_STA_DISCONNECTED: {
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(wifi_event_group, WIFI_CONNECTED_BIT); xEventGroupClearBits(connectivity_event_group, WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT);
esp_wifi_connect(); esp_wifi_connect();
break; break;
} }
@@ -36,8 +35,9 @@ 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_LOGI(TAG, "Got IP: " IPSTR, IP2STR(&event->ip_info.ip)); ESP_LOGD(TAG, "Got IP: " IPSTR, IP2STR(&event->ip_info.ip));
xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT);
xEventGroupSetBits(connectivity_event_group, WIFI_CONNECTED_BIT);
} }
} }
@@ -49,10 +49,7 @@ 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());
esp_netif_create_default_wifi_sta(); esp_netif_create_default_wifi_sta();
@@ -75,22 +72,14 @@ static void wifi_init_sta(void) {
} }
void wifiTask(void *pvParameters) { void wifiTask(void *pvParameters) {
ESP_LOGI(TAG, "WiFi task started"); ESP_LOGV(TAG, "Task started");
wifi_init_sta(); wifi_init_sta();
ESP_ERROR_CHECK(esp_task_wdt_add(NULL)); ESP_ERROR_CHECK(esp_task_wdt_add(NULL));
for(;;) { for(;;) {
wifi_ap_record_t ap;
if (esp_wifi_sta_get_ap_info(&ap) == ESP_OK) {
ESP_LOGV(TAG, "RSSI: %d", ap.rssi);
}
//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_LOGI(TAG, "Wi-Fi task reset"); ESP_LOGV(TAG, "Task reset");
vTaskDelay(pdMS_TO_TICKS(WIFI_TIMEOUT_TICKS)); vTaskDelay(pdMS_TO_TICKS(WIFI_TIMEOUT_MS));
} }
} }