feat: add MAQ device ID and include it in MQTT status

This commit is contained in:
master 2026-01-05 12:58:43 +00:00
parent 87afe944bc
commit 522071ff44
4 changed files with 47 additions and 10 deletions

6
main/include/version.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#define FW_NAME "LED_shit"
#define FW_VERSION "1.0.0-dev"
#define FW_BUILD_DATE __DATE__
#define FW_BUILD_TIME __TIME__

View File

@ -28,6 +28,7 @@ static const char *TAG = "MQTT_CMD";
extern esp_mqtt_client_handle_t mqtt_client;
extern char topic_resp[64];
// Variável global declarada noutro ficheiro
extern bool modo_bloqueado;

View File

@ -22,6 +22,8 @@ static const char *TAG = "MQTT";
#define MQTT_USER "xupa"
#define MQTT_PASS "xupa"
extern char MAQ_ID[];
esp_mqtt_client_handle_t mqtt_client = NULL;
static esp_timer_handle_t mqtt_watchdog = NULL;
static bool mqtt_connected = false;
@ -39,10 +41,12 @@ static void send_status(void) {
char buf[160];
snprintf(buf, sizeof(buf),
"{\"uptime\":%lu,\"heap\":%lu}",
"{\"id\":\"%s\",\"uptime\":%lu,\"heap\":%lu}",
MAQ_ID,
(unsigned long)(esp_log_timestamp() / 1000),
(unsigned long)esp_get_free_heap_size());
esp_mqtt_client_publish(mqtt_client, topic_status, buf, 0, 1, false);
ESP_LOGI(TAG, "📤 STATUS -> %s", buf);
}
@ -157,15 +161,17 @@ void mqtt_handler_start(void) {
snprintf(device_id, sizeof(device_id), "esp_%02X%02X%02X", mac[3], mac[4], mac[5]);
esp_netif_t *netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
if (netif) esp_netif_set_hostname(netif, device_id);
ESP_LOGI(TAG, "🆔 ID do dispositivo: %s", device_id);
if (netif) esp_netif_set_hostname(netif, MAQ_ID);
ESP_LOGI(TAG, "🆔 ID do dispositivo: %s", MAQ_ID);
snprintf(topic_status, sizeof(topic_status), "maq/%s/status", MAQ_ID);
snprintf(topic_cmd, sizeof(topic_cmd), "maq/%s/cmd", MAQ_ID);
snprintf(topic_resp, sizeof(topic_resp), "maq/%s/resp", MAQ_ID);
snprintf(topic_lwt, sizeof(topic_lwt), "maq/%s/lwt", MAQ_ID);
mqtt_cfg.credentials.client_id = MAQ_ID;
snprintf(topic_status, sizeof(topic_status), "esp/%s/status", device_id);
snprintf(topic_cmd, sizeof(topic_cmd), "esp/%s/cmd", device_id);
snprintf(topic_resp, sizeof(topic_resp), "esp/%s/resp", device_id);
snprintf(topic_lwt, sizeof(topic_lwt), "esp/%s/lwt", device_id);
mqtt_cfg.credentials.client_id = device_id;
mqtt_cfg.credentials.username = MQTT_USER;
mqtt_cfg.credentials.authentication.password = MQTT_PASS;
// ======================================================

View File

@ -23,6 +23,7 @@
#include "dns_server.h"
#ifndef MIN
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif
@ -42,6 +43,28 @@ static bool got_ip = false;
// ✅ Flag: quando true estamos em portal (APSTA só para scan), NÃO conectar
static bool g_portal_mode = false;
char MAQ_ID[20] = {0};
static void init_maq_id(void)
{
uint8_t mac[6];
unsigned int id;
// MAC STA existe em TODOS os ESP-IDF
esp_wifi_get_mac(WIFI_IF_STA, mac);
id = ((unsigned int)mac[3] << 16) |
((unsigned int)mac[4] << 8) |
((unsigned int)mac[5]);
snprintf(MAQ_ID, sizeof(MAQ_ID),
"MAQ.%06X", id);
ESP_LOGI(TAG, "🆔 ID da máquina: %s", MAQ_ID);
}
/* ========================= NVS init seguro ========================= */
static void ensure_nvs_ready(void)
{
@ -426,6 +449,7 @@ void wifi_config_portal_init(wifi_connected_cb_t cb, bool have_creds)
ensure_nvs_ready();
init_maq_id(); // 👈 AQUI. UMA VEZ. SEMPRE.
g_on_connected = cb;
ESP_ERROR_CHECK(esp_netif_init());