diff --git a/common_defines.h b/common_defines.h index ba28e2d..292b0f9 100644 --- a/common_defines.h +++ b/common_defines.h @@ -13,11 +13,6 @@ #define PUMP_CONTROLLER 2 //pump controller #define OTA_CONTROLLER 3 // OTA factory app -//#define ACTIVE_CONTROLLER AGATE_CONTROLLER -//#define ACTIVE_CONTROLLER PUMP_CONTROLLER -//#define ACTIVE_CONTROLLER OTA_CONTROLLER -//#define CTRL_DEV_ID 1 - #include "project_specific.h" #define USER_TASK_PRIORITY 5 @@ -50,37 +45,16 @@ #define DEVICE_TOPIC_Q "gnetdev/query" #define DEVICE_TOPIC_R "gnetdev/response" -/* - * topics base definition and USR_MQTT - * prefix + 2 digit ctrl_id - * pump + 01 for pump with CTRL_DEV_ID = 1 - */ #if ACTIVE_CONTROLLER == OTA_CONTROLLER - #define TOPIC_STATE "ota/state" - #define TOPIC_MONITOR "NA" - #define TOPIC_CMD "ota/cmd" - #define TOPIC_CTRL "ota/ctrl" - #define TOPIC_SYSTEM "ota/system" - #define USER_MQTT "OTA" #define DEV_NAME "OTA" #endif #if ACTIVE_CONTROLLER == AGATE_CONTROLLER - #define TOPIC_STATE "gate01/state" - #define TOPIC_MONITOR "gate01/monitor" - #define TOPIC_CMD "gate01/cmd" - #define TOPIC_CTRL "gate01/ctrl" - #define USER_MQTT "gate01" #define DEV_NAME "Poarta Auto" #endif #if ACTIVE_CONTROLLER == PUMP_CONTROLLER - #define TOPIC_STATE "pump01/state" - #define TOPIC_MONITOR "pump01/monitor" - #define TOPIC_CMD "pump01/cmd" - #define TOPIC_CTRL "pump01/ctrl" - #define USER_MQTT "pump01" #define DEV_NAME "Pompa Foraj" #endif diff --git a/external_defs.h b/external_defs.h index 012bec6..2e566d1 100644 --- a/external_defs.h +++ b/external_defs.h @@ -8,4 +8,5 @@ extern esp_vfs_spiffs_conf_t conf_spiffs; extern esp_mqtt_client_handle_t client; extern int client_connected; extern int restart_in_progress; +extern char USER_MQTT[32]; #endif //EXTERNAL_DEFS_H_ diff --git a/tcp/mqtt_ctrl.c b/tcp/mqtt_ctrl.c index 766f1c1..a404eb1 100644 --- a/tcp/mqtt_ctrl.c +++ b/tcp/mqtt_ctrl.c @@ -43,6 +43,9 @@ esp_mqtt_client_handle_t client; int client_connected = 0; static const char *TAG = "MQTTClient"; +static char TOPIC_STATE[32], TOPIC_MONITOR[32], TOPIC_CTRL[32], TOPIC_CMD[32]; +char USER_MQTT[32]; + extern const uint8_t client_cert_pem_start[] asm("_binary_client_crt_start"); extern const uint8_t client_cert_pem_end[] asm("_binary_client_crt_end"); extern const uint8_t client_key_pem_start[] asm("_binary_client_key_start"); @@ -50,6 +53,8 @@ extern const uint8_t client_key_pem_end[] asm("_binary_client_key_end"); extern const uint8_t server_cert_pem_start[] asm("_binary_ca_crt_start"); extern const uint8_t server_cert_pem_end[] asm("_binary_ca_crt_end"); +static void create_topics(void); + static struct { struct arg_str *topic; @@ -300,6 +305,7 @@ int mqtt_start(void) .task_stack = 8192, //.disable_clean_session = false, }; + create_topics(); client = esp_mqtt_client_init(&mqtt_cfg); if(client) { @@ -310,7 +316,27 @@ int mqtt_start(void) return ret; } -void publish(char *topic, char *msg, int qos, int retain) +void publish_state(char *msg, int qos, int retain) + { + time_t now; + struct tm timeinfo; + char strtime[1024]; + if(!client) + ESP_LOGE(TAG, "Client not connected"); + else + { + time(&now); + localtime_r(&now, &timeinfo); + strftime(strtime, sizeof(strtime), "%Y-%m-%d/%H:%M:%S\1", &timeinfo); + strcat(strtime, USER_MQTT); + strcat(strtime, "\1"); + strcat(strtime, msg); + + esp_mqtt_client_publish(client, TOPIC_STATE, strtime, strlen(strtime), qos, retain); + } + } + +void publish_monitor(char *msg, int qos, int retain) { time_t now; struct tm timeinfo; @@ -326,7 +352,7 @@ void publish(char *topic, char *msg, int qos, int retain) strcat(strtime, "\1"); strcat(strtime, msg); - esp_mqtt_client_publish(client, topic, strtime, strlen(strtime), qos, retain); + esp_mqtt_client_publish(client, TOPIC_MONITOR, strtime, strlen(strtime), qos, retain); } } @@ -336,7 +362,26 @@ void publish_MQTT_client_status() char msg[150]; uint64_t tmsec = esp_timer_get_time() /1000000; time(&now); - sprintf(msg, "%s\1" IPSTR "\1%d\1%lu\1%llu", - DEV_NAME, IP2STR(&dev_ipinfo.ip), CTRL_DEV_ID, now, tmsec); - publish(DEVICE_TOPIC_R, msg, 0, 1); + sprintf(msg, "%s\1%s\1" IPSTR "\1%d\1%lu\1%llu", + USER_MQTT, DEV_NAME, IP2STR(&dev_ipinfo.ip), CTRL_DEV_ID, now, tmsec); + esp_mqtt_client_publish(client, DEVICE_TOPIC_R, msg, strlen(msg), 0, 1); + } + +void create_topics() + { +#if ACTIVE_CONTROLLER == PUMP_CONTROLLER + sprintf(USER_MQTT, "pump%02d", CTRL_DEV_ID); +#elif ACTIVE_CONTROLLER == AGATE_CONTROLLER + sprintf(USER_MQTT, "gate%02d", CTRL_DEV_ID); +#elif ACTIVE_CONTROLLER == OTA_CONTROLLER + sprintf(USER_MQTT, "ota%02d", CTRL_DEV_ID); +#endif + strcpy(TOPIC_STATE, USER_MQTT); + strcat(TOPIC_STATE, "/state"); + strcpy(TOPIC_MONITOR, USER_MQTT); + strcat(TOPIC_MONITOR, "/monitor"); + strcpy(TOPIC_CMD, USER_MQTT); + strcat(TOPIC_CMD, "/cmd"); + strcpy(TOPIC_CTRL, USER_MQTT); + strcat(TOPIC_CTRL, "/ctrl"); } diff --git a/tcp/mqtt_ctrl.h b/tcp/mqtt_ctrl.h index 66b3fec..52e5b7e 100644 --- a/tcp/mqtt_ctrl.h +++ b/tcp/mqtt_ctrl.h @@ -10,6 +10,7 @@ int mqtt_start(void); void register_mqtt(void); -void publish(char *topic, char *msg, int qos, int retain); +void publish_state(char *msg, int qos, int retain); +void publish_monitor(char *msg, int qos, int retain); void publish_MQTT_client_status(); #endif /* TCP_MQTT_CTRL_H_ */ diff --git a/tcp/tcp_log.c b/tcp/tcp_log.c index ddf086b..1351e72 100644 --- a/tcp/tcp_log.c +++ b/tcp/tcp_log.c @@ -20,7 +20,12 @@ #include "freertos/freertos.h" #include "freertos/task.h" #include "freertos/queue.h" +#include "esp_spiffs.h" +#include "esp_vfs_dev.h" +#include "esp_vfs_fat.h" +#include "mqtt_client.h" #include "common_defines.h" +#include "external_defs.h" #include "tcp_log.h" static int tcp_log_enable;