Compare commits

..

No commits in common. "59c31f11596abc0b5b8b05ff6fde54213916a435" and "c0e0e624db1ba3a06d0f28464a7709c2f345f75b" have entirely different histories.

4 changed files with 10 additions and 15 deletions

View file

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

View file

@ -9,12 +9,11 @@
#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, topic, "Test message");
mqtt_publish(mqtt_client, "Test message");
vTaskDelay(pdMS_TO_TICKS(1000));
}

View file

@ -1,7 +1,6 @@
#include "esp_event_base.h"
#include "esp_log.h"
#include "mqtt_client.h"
#include "secrets.h"
#include <stdint.h>
#include <string.h>
@ -11,6 +10,8 @@ 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();
@ -45,9 +46,8 @@ 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", SECRETS_MQTT_BROKER);
esp_mqtt_client_config_t mqtt_config = {.broker.address.uri =
SECRETS_MQTT_BROKER};
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_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,15 +55,14 @@ esp_mqtt_client_handle_t mqtt_connect()
return client;
}
void mqtt_publish(esp_mqtt_client_handle_t client, char *topic,
const char *payload)
void mqtt_publish(esp_mqtt_client_handle_t client, const char *payload)
{
if (MQTT_CONNECTED) {
ESP_LOGI(TAG, "Publishing payload %s to topic %s", payload, topic);
esp_mqtt_client_publish(client, topic, payload, strlen(payload), 0, 0);
ESP_LOGI(TAG, "Publishing payload %s to topic %s", payload, MQTT_TOPIC);
esp_mqtt_client_publish(client, MQTT_TOPIC, payload, strlen(payload), 0, 0);
} else {
ESP_LOGI(TAG, "Could not publish to MQTT topic %s. Not connected to broker",
topic);
MQTT_TOPIC);
}
}

View file

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