Hi @Diego,
I am facing a new kind of error while connecting to hivemq. I am connecting to it via TCP and it’s a secure domain.
Controller: ESP-32 wrover
This the Log for the same
I (32615) MQTT_EXAMPLE: Other event id:7
E (32615) esp-tls: couldn't get hostname for :b9ff4eeb544e4ac18142a019bc399b43.s2.eu.hivemq.cloud: getaddrinfo() returns 202, addrinfo=0x0
E (32615) esp-tls: Failed to open new connection
E (32625) transport_base: Failed to open a new connection
E (32625) mqtt_client: Error transport connect
I (32635) MQTT_EXAMPLE: MQTT_EVENT_ERROR
E (32635) MQTT_EXAMPLE: Last error reported from esp-tls: 0x8001
I (32645) MQTT_EXAMPLE: Last errno string (Success)
I (32655) MQTT_EXAMPLE: MQTT_EVENT_DISCONNECTED
E (39035) mqtt_client: Client has not connected
This the code snippet Iam using
/**
* MQTT Application:
* Date Created: 05-05-2024
* Author : Tarun
*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/queue.h"
#include "lwip/sockets.h"
#include "lwip/dns.h"
#include "lwip/netdb.h"
#include "esp_log.h"
#include "wifi_app.h"
#include "mqtt_client.h"
#include "mqtt_app.h"
#include "gpio_esp.h"
#include "ultrasonic.h"
#include "nvs_app.h"
static const char *TAG = "MQTT_EXAMPLE";
esp_mqtt_client_handle_t client;
char buffer[50]; // buffer for subscribed data
uint8_t mac_id[20];
//string response_mqtt;
bool mqtt_connect_status = false; // Flag to check connection to MQTT-broker
void stringcopy(char * st1, char * st2) //st1 -> source || st2 -> destination
{
int i = 0;
for (i = 0; st1[i]!='\0'; i++)
{
st2[i] = st1[i];
}
st2[i] = '\0';
}
static void log_error_if_nonzero(const char *message, int error_code)
{
if (error_code != 0) {
ESP_LOGE(TAG, "Last error %s: 0x%x", message, error_code);
}
}
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
{
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
esp_mqtt_event_handle_t event = event_data;
esp_mqtt_client_handle_t client = event->client;
int msg_id;
switch ((esp_mqtt_event_id_t)event_id) {
case MQTT_EVENT_CONNECTED:
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
mqtt_connect_status = true;
msg_id = esp_mqtt_client_subscribe(client, "my_topic1", 0);
//ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
msg_id = esp_mqtt_client_publish(client, "my_topic1", "vivid", 5, 0, 0);
//ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
break;
case MQTT_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
mqtt_connect_status = false;
break;
case MQTT_EVENT_SUBSCRIBED:
//ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
// msg_id = esp_mqtt_client_publish(client, "my_topic1", i+1, 2, 0, 0);
// ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
break;
case MQTT_EVENT_UNSUBSCRIBED:
ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_PUBLISHED:
ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_DATA:
ESP_LOGI(TAG, "MQTT_EVENT_DATA");
printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
printf("DATA=%.*s\r\n", event->data_len, event->data);
strncpy(buffer,event->data,event->data_len);
//stringcopy(event->data, buffer);
if( strlen(buffer) == 0)
{
buffer[0] = '\0'; //
//memset(buffer, 0, sizeof(buffer)); //setting whole string at "0"
}
//strncpy(buffer,event->data,event->data_len); // Copying data into data buffer/ Need to find alternate the
printf("\nbuffer : %s\n", buffer);
// val[] = event->data;
break;
case MQTT_EVENT_ERROR:
ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
log_error_if_nonzero("reported from esp-tls", event->error_handle->esp_tls_last_esp_err);
log_error_if_nonzero("reported from tls stack", event->error_handle->esp_tls_stack_err);
log_error_if_nonzero("captured as transport's socket errno", event->error_handle->esp_transport_sock_errno);
ESP_LOGI(TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno));
}
break;
default:
ESP_LOGI(TAG, "Other event id:%d", event->event_id);
break;
}
}
void mqtt_app_start(void)
{
esp_mqtt_client_config_t mqtt_cfg = { // MQTT broker configurations
.uri = CONFIG_BROKER_URL,
.username = "my_username",
.password = "my_password",
.client_id = "Tarun123",
};
client = esp_mqtt_client_init(&mqtt_cfg);
/* The last argument may be used to pass data to the event handler, I am not passing any arguement as of now. */
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
esp_mqtt_client_start(client);
}
Kindly, help me in this this quiet urgent and important for me.