refine mqtt mod to receive params
This commit is contained in:
parent
c0e0e624db
commit
9a0ec170e5
3 changed files with 13 additions and 10 deletions
|
|
@ -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));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue