Add global event group handle for connectivity
This commit is contained in:
@@ -1,9 +1,13 @@
|
|||||||
#ifdef __cplusplus
|
#ifndef MAIN_HPP
|
||||||
extern "C" {
|
#define MAIN_HPP
|
||||||
#endif
|
|
||||||
|
#include "freertos/FreeRTOS.h"
|
||||||
|
#include "freertos/event_groups.h"
|
||||||
|
|
||||||
extern volatile bool criticalErrorFlag;
|
extern volatile bool criticalErrorFlag;
|
||||||
|
extern EventGroupHandle_t connectivity_event_group;
|
||||||
|
|
||||||
|
#define WIFI_CONNECTED_BIT BIT0
|
||||||
|
#define MQTT_CONNECTED_BIT BIT1
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -9,8 +9,7 @@
|
|||||||
|
|
||||||
#define WATCHDOG_KEEPALIVE_MS 8000
|
#define WATCHDOG_KEEPALIVE_MS 8000
|
||||||
|
|
||||||
extern EventGroupHandle_t connectivity_event_group;
|
EventGroupHandle_t connectivity_event_group;
|
||||||
extern EventBits_t bits = xEventGroupGetBits(connectivity_event_group);
|
|
||||||
|
|
||||||
static const char* TAG = "MAIN";
|
static const char* TAG = "MAIN";
|
||||||
|
|
||||||
@@ -49,6 +48,8 @@ extern "C" void app_main(void)
|
|||||||
};
|
};
|
||||||
esp_pm_configure(&pm_cfg);
|
esp_pm_configure(&pm_cfg);
|
||||||
|
|
||||||
|
connectivity_event_group = xEventGroupCreate();
|
||||||
|
|
||||||
xTaskCreate(watchdogTask, "watchdogs", 2048, NULL, configMAX_PRIORITIES-1, NULL);
|
xTaskCreate(watchdogTask, "watchdogs", 2048, NULL, configMAX_PRIORITIES-1, NULL);
|
||||||
xTaskCreate(wifiTask, "wifi", 4096, NULL, configMAX_PRIORITIES-4, NULL);
|
xTaskCreate(wifiTask, "wifi", 4096, NULL, configMAX_PRIORITIES-4, NULL);
|
||||||
|
|
||||||
|
|||||||
@@ -14,14 +14,14 @@ static const char* TAG = "MQTT";
|
|||||||
|
|
||||||
static TaskHandle_t i2c_task_handle = NULL;
|
static TaskHandle_t i2c_task_handle = NULL;
|
||||||
static esp_mqtt_client_handle_t mqtt_client = NULL;
|
static esp_mqtt_client_handle_t mqtt_client = NULL;
|
||||||
const int MQTT_CONNECTED_BIT = BIT0;
|
|
||||||
|
|
||||||
static void mqtt_subscribe(void) {
|
static void mqtt_subscribe(void) {
|
||||||
msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0);
|
msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) {
|
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) {
|
||||||
esp_mqtt_event_handle_t event = 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);
|
||||||
@@ -65,8 +65,6 @@ static void mqtt_init(void) {
|
|||||||
esp_mqtt_client_start(client);
|
esp_mqtt_client_start(client);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mqtt_publish(void) {}
|
|
||||||
|
|
||||||
void mqttTask(void *pvParameters) {
|
void mqttTask(void *pvParameters) {
|
||||||
ESP_LOGV(TAG, "Task started");
|
ESP_LOGV(TAG, "Task started");
|
||||||
|
|
||||||
@@ -79,8 +77,10 @@ void mqttTask(void *pvParameters) {
|
|||||||
for(;;) {
|
for(;;) {
|
||||||
ESP_LOGV(TAG, "Checking broker connection...");
|
ESP_LOGV(TAG, "Checking broker connection...");
|
||||||
|
|
||||||
if(bits && MQTT_CONNECTED_BIT) {
|
EventBits_t bits = xEventGroupGetBits(connectivity_event_group);
|
||||||
ESP_LOGV(TAG, "Connection estabilished");
|
|
||||||
|
if ((bits & (WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT)) == (WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT)) {
|
||||||
|
ESP_LOGV(TAG, "Connection established");
|
||||||
|
|
||||||
uint32_t task_notification = ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(MQTT_TASK_TIMEOUT_MS));
|
uint32_t task_notification = ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(MQTT_TASK_TIMEOUT_MS));
|
||||||
if (task_notification) twi_do(global_rx_buffer); //FIXME: develop i2c libs! (this func should then call mqtt_publish() if needed)
|
if (task_notification) twi_do(global_rx_buffer); //FIXME: develop i2c libs! (this func should then call mqtt_publish() if needed)
|
||||||
|
|||||||
@@ -12,8 +12,6 @@
|
|||||||
|
|
||||||
static const char* TAG = "WIFI";
|
static const char* TAG = "WIFI";
|
||||||
|
|
||||||
const int WIFI_CONNECTED_BIT = BIT0;
|
|
||||||
|
|
||||||
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) {
|
||||||
@@ -49,8 +47,6 @@ static void wifi_init_sta(void) {
|
|||||||
ESP_ERROR_CHECK(nvs_flash_init());
|
ESP_ERROR_CHECK(nvs_flash_init());
|
||||||
}
|
}
|
||||||
|
|
||||||
connectivity_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());
|
||||||
|
|||||||
Reference in New Issue
Block a user