Compare commits

..

2 commits

Author SHA1 Message Date
59c31f1159 add clang formatter rules 2026-02-09 18:55:33 +00:00
9a0ec170e5 refine mqtt mod to receive params 2026-02-09 18:55:17 +00:00
4 changed files with 15 additions and 10 deletions

2
.clang-format Normal file
View file

@ -0,0 +1,2 @@
BasedOnStyle: LLVM
BreakBeforeBraces: Linux

View file

@ -9,11 +9,12 @@
#include "wifi.h"
static const char *TAG = "main";
static char *topic = "test_topic";
void loop(esp_mqtt_client_handle_t mqtt_client)
{
ESP_LOGI(TAG, "Loop running...");
mqtt_publish(mqtt_client, "Test message");
mqtt_publish(mqtt_client, topic, "Test message");
vTaskDelay(pdMS_TO_TICKS(1000));
}

View file

@ -1,6 +1,7 @@
#include "esp_event_base.h"
#include "esp_log.h"
#include "mqtt_client.h"
#include "secrets.h"
#include <stdint.h>
#include <string.h>
@ -10,8 +11,6 @@ static const char *TAG = "mqtt"; // Logs are coming from this mqtt module
static uint32_t MQTT_CONNECTED = 0;
static int s_retry_num = 0;
#define MQTT_TOPIC "/topic/test"
#define MQTT_BROKER "mqtt://192.168.178.53:1883"
#define MAX_RETRY 5
esp_mqtt_client_handle_t mqtt_connect();
@ -46,8 +45,9 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base,
esp_mqtt_client_handle_t mqtt_connect()
{
ESP_LOGI(TAG, "Attempting to connect to MQTT broker %s", MQTT_BROKER);
esp_mqtt_client_config_t mqtt_config = {.broker.address.uri = MQTT_BROKER};
ESP_LOGI(TAG, "Attempting to connect to MQTT broker %s", SECRETS_MQTT_BROKER);
esp_mqtt_client_config_t mqtt_config = {.broker.address.uri =
SECRETS_MQTT_BROKER};
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_config);
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler,
client);
@ -55,14 +55,15 @@ esp_mqtt_client_handle_t mqtt_connect()
return client;
}
void mqtt_publish(esp_mqtt_client_handle_t client, const char *payload)
void mqtt_publish(esp_mqtt_client_handle_t client, char *topic,
const char *payload)
{
if (MQTT_CONNECTED) {
ESP_LOGI(TAG, "Publishing payload %s to topic %s", payload, MQTT_TOPIC);
esp_mqtt_client_publish(client, MQTT_TOPIC, payload, strlen(payload), 0, 0);
ESP_LOGI(TAG, "Publishing payload %s to topic %s", payload, topic);
esp_mqtt_client_publish(client, topic, payload, strlen(payload), 0, 0);
} else {
ESP_LOGI(TAG, "Could not publish to MQTT topic %s. Not connected to broker",
MQTT_TOPIC);
topic);
}
}

View file

@ -3,6 +3,7 @@
#include "mqtt_client.h"
esp_mqtt_client_handle_t mqtt_connect();
void mqtt_publish(esp_mqtt_client_handle_t client, const char *payload);
void mqtt_publish(esp_mqtt_client_handle_t client, char *topic,
const char *payload);
#endif // MQTT_H