Skip to content

Commit

Permalink
some rework regarding mqtt topic naming
Browse files Browse the repository at this point in the history
  • Loading branch information
ves011 committed Mar 26, 2023
1 parent 150a81f commit 36a4dd1
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 32 deletions.
26 changes: 0 additions & 26 deletions common_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions external_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
55 changes: 50 additions & 5 deletions tcp/mqtt_ctrl.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,18 @@ 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");
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;
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
Expand All @@ -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);
}
}

Expand All @@ -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");
}
3 changes: 2 additions & 1 deletion tcp/mqtt_ctrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */
5 changes: 5 additions & 0 deletions tcp/tcp_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 36a4dd1

Please sign in to comment.