fran_martinv
fran_martinv

Reputation: 21

Problem connecting ESP-MQTT with TCP (ESP-IDF)

I have a problema that I have a correctly connection to my mobile hotspot wifi, but when I try to connect MQTT to upload some sensor data to my PC ip (I did it with mosquitto broker), I always have the same problem.

esp_mqtt_client_config_t mqtt_cfg = {
    .uri            =   "mqtt://192.168.166.66:1883",
    //.host             =   "192.168.166.66",
    //.port             =   1883,
    .buffer_size    =   1024,
    .client_id      =   "ESP-TFG-FRAN",
    .lwt_qos        =   1,
    //.transport        =   MQTT_TRANSPORT_OVER_TCP,
    .protocol_ver   =   MQTT_PROTOCOL_V_3_1_1,
};

client = esp_mqtt_client_init(&mqtt_cfg);
vTaskDelay(100/portTICK_RATE_MS);
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client);
ret = esp_mqtt_client_start(client);
    if(ret != ESP_OK) {
        printf("ERROR in first connecting of MQTT: %x\n", ret);
        esp_mqtt_client_reconnect(client);
    }

Could someone help me to solve it?

This is my error code that I always have:

error codes on esp-idf monitor

I will think it is probably a problem with the sdk and the idf.py menuconfig or the TCP/IP protocol... but I can`t fine the solution of the problem...

Upvotes: 1

Views: 1676

Answers (1)

hardillb
hardillb

Reputation: 59771

The error means that the ESP could not connect to the machine with the broker. This could be for a number of reasons

  1. The broker host is not on the IP address you've given
  2. The WiFi Access point you are using is set up with client separation enabled, which means that it won't allow 2 connected client to talk to each other.
  3. If the Broker is not connected to the Mobile Access Point then you probably need to be using the public (static or have DynDNS enabled) address of your network and have Port forwarding enabled

Upvotes: 2

Related Questions