33 Commits

Author SHA1 Message Date
eeeck 626bbe9dbb Remove task handle: managed in dev-twi main.cpp 2025-12-10 00:27:30 +01:00
eeeck 1a893547b6 Finalize event groups to improve performance 2025-12-09 23:50:26 +01:00
eeeck 06a672d1f7 Remove old code about I2C 2025-12-09 23:40:53 +01:00
eeeck b1a6dfddd3 Wait for Wi-Fi connection before initializing MQTT 2025-12-09 23:23:10 +01:00
eeeck 2e6e829829 Only clear Wi-Fi bit 2025-12-09 23:22:35 +01:00
eeeck 6a2a933002 Better wait implementation with no spinlock from MQTT task 2025-12-09 23:02:21 +01:00
eeeck aa395c381b Remove redundant log 2025-12-09 10:49:32 +01:00
eeeck eaad4469bc Normalize spacing in ESP32 firmware 2025-12-09 10:04:06 +01:00
eeeck 4e78a5f2ed Go in critical error if MQTT is not being initialized 2025-12-09 10:03:06 +01:00
eeeck c53783072e Set MQTT username and password 2025-12-09 10:02:47 +01:00
eeeck 9bad1790a7 Log MQTT errors 2025-12-09 10:02:29 +01:00
eeeck 4cd9a3210c Comment out I2C task handles 2025-12-09 10:01:54 +01:00
eeeck 2c7951c2bb Fix missing code from merge for connectivity_event_group 2025-12-09 01:37:18 +01:00
eeeck 88c7635ed9 Merge branch 'dev-esp32-wifi' into dev-esp32-mqtt 2025-12-09 01:00:26 +01:00
eeeck b0059f6e9f Move user configuration to config.h 2025-12-09 00:24:36 +01:00
eeeck 985642e0fe Fix compilation issues with espressif mqtt lib 2025-12-04 23:00:46 +01:00
eeeck fb98ce800a Add network speed check for future use 2025-12-04 21:51:16 +01:00
eeeck a332c37e72 Replace polling it with a blocking wait on connectivity flag and
notification
2025-12-04 21:51:02 +01:00
eeeck 567fca2916 Tweak logs 2025-12-01 00:55:59 +01:00
eeeck 04b5fb0a7c Fix task event notification 2025-12-01 00:54:00 +01:00
eeeck 6fd2d2523d Add global event group handle for connectivity 2025-12-01 00:53:12 +01:00
eeeck c6c4011a47 Fix vTaskDelay syntax error 2025-11-30 23:48:08 +01:00
eeeck 213f6b399f Changed some logs type from informative to debug 2025-11-30 23:40:59 +01:00
eeeck 0a418805a4 Changed some logs type from informative to verbose 2025-11-30 23:40:24 +01:00
eeeck 3670b58f8e Implement task handle for managing I2C 2025-11-30 23:36:54 +01:00
eeeck 5dd2c00ac0 Parametrize task delay values 2025-11-30 23:36:27 +01:00
eeeck 2fcc3172aa Fix message logs for consistency with codebase 2025-11-30 22:24:33 +01:00
eeeck 665125e07f Implement MQTT event groups 2025-11-30 22:24:09 +01:00
eeeck 401c610a58 Create shared event group 2025-11-30 22:22:26 +01:00
eeeck 1ef2476f78 MQTT initialization 2025-11-30 17:40:54 +01:00
eeeck 28ac3f44b3 Fix task delay duration 2025-11-29 14:16:11 +01:00
eeeck 29ad9350a3 Fix task name 2025-11-29 14:15:17 +01:00
eeeck cd4d2658b9 Initialize MQTT task 2025-11-06 19:19:52 +01:00
9 changed files with 173 additions and 44 deletions
+17
View File
@@ -0,0 +1,17 @@
#ifndef CONFIG_H
#define CONFIG_H
// TODO: make this a config.h
// WI-FI
#define SSID "your_wifi_ssid"
#define PASSWORD "your_wifi_password"
#define AUTH_MODE WIFI_AUTH_WPA2_PSK
// MQTT
#define MQTT_ADDR "mqtt://yourmqttserver"
#define MQTT_PORT 1883
#define MQTT_USER "your_username"
#define MQTT_PASS "your_password"
#endif
+9 -5
View File
@@ -1,9 +1,13 @@
#ifdef __cplusplus
extern "C" {
#endif
#ifndef MAIN_HPP
#define MAIN_HPP
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
extern volatile bool criticalErrorFlag;
extern EventGroupHandle_t connectivity_event_group;
#define WIFI_CONNECTED_BIT BIT0
#define MQTT_CONNECTED_BIT BIT1
#ifdef __cplusplus
}
#endif
+6
View File
@@ -0,0 +1,6 @@
#ifndef MQTT_HPP
#define MQTT_HPP
void mqttTask(void *pvParameters);
#endif
@@ -1,8 +0,0 @@
#ifndef WIFI_CREDENTIALS_H
#define WIFI_CREDENTIALS_H
#define SSID "your_wifi_ssid"
#define PASSWORD "your_wifi_password"
#define AUTH_MODE WIFI_AUTH_WPA2_PSK
#endif
+3 -1
View File
@@ -1,5 +1,6 @@
idf_component_register(SRCS "./main.cpp"
"./wifi.cpp"
"./mqtt.cpp"
INCLUDE_DIRS "../include"
REQUIRES esp_wifi
nvs_flash
@@ -7,4 +8,5 @@ idf_component_register(SRCS "./main.cpp"
esp_hw_support
esp_system
freertos
log)
log
mqtt)
+2
View File
@@ -0,0 +1,2 @@
dependencies:
espressif/mqtt: "*"
+12 -5
View File
@@ -6,6 +6,11 @@
#include "esp_pm.h"
#include "esp_task_wdt.h"
#include "wifi.hpp"
#include "mqtt.hpp"
#define WATCHDOG_KEEPALIVE_MS 8000
EventGroupHandle_t connectivity_event_group;
static const char* TAG = "MAIN";
@@ -22,18 +27,16 @@ static void watchdogTask(void *pvParameters) {
ESP_ERROR_CHECK(esp_task_wdt_add(NULL));
const TickType_t keepAlivePeriod = pdMS_TO_TICKS(8000);
for(;;) {
if (criticalErrorFlag) {
if(criticalErrorFlag) {
ESP_LOGE(TAG, "Critical error flag raised. Rebooting.");
esp_restart();
}
ESP_ERROR_CHECK(esp_task_wdt_reset());
ESP_LOGI(TAG, "WDT reset");
ESP_LOGV(TAG, "WDT reset");
vTaskDelay(keepAlivePeriod);
vTaskDelay(pdMS_TO_TICKS(WATCHDOG_KEEPALIVE_MS));
}
}
@@ -46,6 +49,10 @@ extern "C" void app_main(void)
};
esp_pm_configure(&pm_cfg);
connectivity_event_group = xEventGroupCreate();
xTaskCreate(watchdogTask, "watchdogs", 2048, NULL, configMAX_PRIORITIES-1, NULL);
xTaskCreate(wifiTask, "wifi", 4096, NULL, configMAX_PRIORITIES-4, NULL);
xTaskCreate(mqttTask, "mqtt", 2048, NULL, configMAX_PRIORITIES-8, NULL);
}
+106
View File
@@ -0,0 +1,106 @@
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "esp_task_wdt.h"
#include "esp_event.h"
#include "esp_log.h"
#include "mqtt_client.h"
#include "main.hpp"
//#include "i2c.hpp"
#include "config-erick.h"
#define MQTT_TASK_TIMEOUT_MS 3000
static const char* TAG = "MQTT";
static esp_mqtt_client_handle_t mqtt_client = NULL;
static char global_rx_buffer[50];
static void mqtt_subscribe(void) {
esp_mqtt_client_subscribe(mqtt_client, "/topic/qos0", 0);
}
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 =(esp_mqtt_event_handle_t)event_data;
switch(event->event_id) {
case MQTT_EVENT_CONNECTED:
xEventGroupSetBits(connectivity_event_group, MQTT_CONNECTED_BIT);
mqtt_subscribe();
ESP_LOGV(TAG, "Connected to broker");
break;
case MQTT_EVENT_DISCONNECTED:
xEventGroupClearBits(connectivity_event_group, MQTT_CONNECTED_BIT);
ESP_LOGW(TAG, "Disconnected from broker");
break;
case MQTT_EVENT_DATA:
{
ESP_LOGD(TAG, "Data received");
int len = event->data_len;
if(len > sizeof(global_rx_buffer) - 1) len = sizeof(global_rx_buffer) - 1;
memcpy(global_rx_buffer, event->data, len);
global_rx_buffer[len] = '\0';
break;
}
case MQTT_EVENT_ERROR:
ESP_LOGE(TAG, "Error event");
if(event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) ESP_LOGE(TAG, "Network Error: %s", strerror(event->error_handle->esp_transport_sock_errno));
else if(event->error_handle->error_type == MQTT_ERROR_TYPE_CONNECTION_REFUSED) ESP_LOGE(TAG, "Connection Refused! Reason code: 0x%x", event->error_handle->connect_return_code);
break;
default:
break;
}
}
static void mqtt_init(void) {
esp_mqtt_client_config_t mqtt_cfg = {};
mqtt_cfg.broker.address.uri = MQTT_ADDR;
mqtt_cfg.broker.address.port = MQTT_PORT;
mqtt_cfg.credentials.username = MQTT_USER;
mqtt_cfg.credentials.authentication.password = MQTT_PASS;
mqtt_client = esp_mqtt_client_init(&mqtt_cfg);
if(mqtt_client == NULL) {
ESP_LOGE(TAG, "esp_mqtt_client_init returned NULL");
criticalErrorFlag = true;
}
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);
}
void mqtt_publish(void) {}
void mqttTask(void *pvParameters) {
ESP_LOGV(TAG, "Task started");
ESP_LOGV(TAG, "Waiting for Wi-Fi before initializing server");
xEventGroupWaitBits(connectivity_event_group, WIFI_CONNECTED_BIT, pdFALSE, pdTRUE, portMAX_DELAY);
ESP_LOGV(TAG, "Server initialized");
mqtt_init();
ESP_ERROR_CHECK(esp_task_wdt_add(NULL));
for(;;) {
ESP_LOGV(TAG, "Checking Wi-Fi connection...");
EventBits_t bits = xEventGroupWaitBits(connectivity_event_group, WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT, pdFALSE, pdTRUE, pdMS_TO_TICKS(MQTT_TASK_TIMEOUT_MS));
if ((bits & (WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT)) == (WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT)) {
ESP_LOGV(TAG, "Wi-Fi and MQTT up");
} else ESP_LOGV(TAG, "Wi-Fi or MQTT down");
ESP_ERROR_CHECK(esp_task_wdt_reset());
ESP_LOGV(TAG, "Task reset");
}
}
+18 -25
View File
@@ -6,28 +6,26 @@
#include "esp_log.h"
#include "nvs_flash.h"
#include "main.hpp"
#include "wifi-credentials.h"
#include "config-erick.h"
#define WIFI_TIMEOUT_TICKS pdMS_TO_TICKS(8000)
#define WIFI_TIMEOUT_MS 4500
static const char* TAG = "WIFI";
const int WIFI_CONNECTED_BIT = BIT0;
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) {
if (event_base == WIFI_EVENT) {
switch (event_id) {
if(event_base == WIFI_EVENT) {
switch(event_id) {
case WIFI_EVENT_STA_START:
ESP_LOGI(TAG, "Wi-Fi process started, connecting...");
ESP_LOGV(TAG, "Process started, connecting...");
esp_wifi_connect();
break;
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);
xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT);
xEventGroupClearBits(connectivity_event_group, WIFI_CONNECTED_BIT);
esp_wifi_connect();
break;
}
@@ -35,26 +33,23 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t e
break;
}
}
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;
ESP_LOGI(TAG, "Got IP: " IPSTR, IP2STR(&event->ip_info.ip));
xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT);
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;
ESP_LOGD(TAG, "Got IP: " IPSTR, IP2STR(&event->ip_info.ip));
xEventGroupSetBits(connectivity_event_group, WIFI_CONNECTED_BIT);
}
}
static void wifi_init_sta(void) {
esp_err_t nvs_err = nvs_flash_init();
if (nvs_err == ESP_ERR_NVS_NO_FREE_PAGES || nvs_err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
if(nvs_err == ESP_ERR_NVS_NO_FREE_PAGES || nvs_err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ESP_ERROR_CHECK(nvs_flash_init());
}
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_sta();
@@ -77,16 +72,14 @@ static void wifi_init_sta(void) {
}
void wifiTask(void *pvParameters) {
ESP_LOGI(TAG, "WiFi task started");
ESP_LOGV(TAG, "Task started");
wifi_init_sta();
ESP_ERROR_CHECK(esp_task_wdt_add(NULL));
for(;;) {
EventBits_t bits = xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_BIT, pdFALSE, pdTRUE, WIFI_TIMEOUT_TICKS);
ESP_ERROR_CHECK(esp_task_wdt_reset());
ESP_LOGI(TAG, "Wi-Fi task reset");
vTaskDelay(pdMS_TO_TICKS(4000));
ESP_LOGV(TAG, "Task reset");
vTaskDelay(pdMS_TO_TICKS(WIFI_TIMEOUT_MS));
}
}