espressif / esp-mqtt

ESP32 mqtt component
Apache License 2.0
591 stars 254 forks source link

mqtt idf 4.4 azure iot hub (IDFGH-8336) #235

Closed sl2121 closed 1 year ago

sl2121 commented 1 year ago

hi, i would like help on how connect to the azure iot hub withe "mqtt_client" library.

my configuration is the following :

static const unsigned char DigiCertGlobalRootG2[] = ( "-----BEGIN CERTIFICATE-----\r\n" "MIIDjjCCAnagAwIBAgIQAzrx5qcRqaC7KGSxHQn65TANBgkqhkiG9w0BAQsFADBh\r\n" "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\r\n" "d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBH\r\n" "MjAeFw0xMzA4MDExMjAwMDBaFw0zODAxMTUxMjAwMDBaMGExCzAJBgNVBAYTAlVT\r\n" "MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j\r\n" "b20xIDAeBgNVBAMTF0RpZ2lDZXJ0IEdsb2JhbCBSb290IEcyMIIBIjANBgkqhkiG\r\n" "9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuzfNNNx7a8myaJCtSnX/RrohCgiN9RlUyfuI\r\n" "2/Ou8jqJkTx65qsGGmvPrC3oXgkkRLpimn7Wo6h+4FR1IAWsULecYxpsMNzaHxmx\r\n" "1x7e/dfgy5SDN67sH0NO3Xss0r0upS/kqbitOtSZpLYl6ZtrAGCSYP9PIUkY92eQ\r\n" "q2EGnI/yuum06ZIya7XzV+hdG82MHauVBJVJ8zUtluNJbd134/tJS7SsVQepj5Wz\r\n" "tCO7TG1F8PapspUwtP1MVYwnSlcUfIKdzXOS0xZKBgyMUNGPHgm+F6HmIcr9g+UQ\r\n" "vIOlCsRnKPZzFBQ9RnbDhxSJITRNrw9FDKZJobq7nMWxM4MphQIDAQABo0IwQDAP\r\n" "BgNVHRMBAf8EBTADAQH/MA4GA1UdDwEB/wQEAwIBhjAdBgNVHQ4EFgQUTiJUIBiV\r\n" "5uNu5g/6+rkS7QYXjzkwDQYJKoZIhvcNAQELBQADggEBAGBnKJRvDkhj6zHd6mcY\r\n" "1Yl9PMWLSn/pvtsrF9+wX3N3KjITOYFnQoQj8kVnNeyIv/iPsGEMNKSuIEyExtv4\r\n" "NeF22d+mQrvHRAiGfzZ0JFrabA0UWTW98kndth/Jsw1HKj2ZL7tcu7XUIOGZX1NG\r\n" "Fdtom/DzMNU+MeKNhJ7jitralj41E6Vf8PlwUHBHQRFXGU7Aj64GxJUTFy8bJZ91\r\n" "8rGOmaFvE7FBcf6IKshPECBV1/MUReXgRPTqh5Uykw7+U0b6LJ3/iyK5S9kJRaTe\r\n" "pLiaWN0bfVKfjllDiIGknibVb63dDcY3fe0Dkhvld1927jyNxF1WW6LZZm6zNTfl\r\n" "MrY=\r\n" "-----END CERTIFICATE-----\r\n" );

const esp_mqtt_client_config_t mqtt_cfg = {

        .uri = "mqtts://myhubname.azure-devices.net:8883",
        .client_id = "device",
                .username = "myhubname.azure-devices.net/device/?api-version=2020-09-30",
        .password = MQTT_PASSWD,//(SAS Token)
        .cert_pem =(const char *)DigiCertGlobalRootG2,
        //.cert_len = sizeof(certificates),
        //.port = 8883,
        //.disable_auto_reconnect = true,
        .lwt_qos = 1,
        //.transport = MQTT_TRANSPORT_OVER_SSL,
        .protocol_ver = MQTT_PROTOCOL_V_3_1_1,
        //.use_secure_element = true,
        .lwt_topic = "/lwt",
        .lwt_msg = "offline",
        .lwt_retain = 0

};

esp_err_t err; esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); err = esp_mqtt_client_start(client); ESP_LOGI (TAG, "Client connect. Error = %d %s", err, esp_err_to_name (err));

With these settings i can't connect. Thanks

euripedesrocha commented 1 year ago

Hi @sl2121, could you share the logs for the failed connection?

sl2121 commented 1 year ago

I have started from the sntp example and i continued with part of the the mqtt example. What is the meaning of : esp-tls-mbedtls: Certificate verified. Is that the esp has verified that the certificate is correct ? Or is the answer of the server?

Operating System: windows 10 Java Runtime Version: 17.0.4.1+1 Eclipse Version: 4.24.0.v20220607-0700 Eclipse CDT Version: 10.7.0.202204131742 IDF Eclipse Plugin Version: 2.6.0.202208110735 ESP-IDF v4.4.1-dirty Python set for IDF_PYTHON_ENV: Python 3.10.4

Thanks.

I (17122) MQTTS_EXAMPLE: Other event id:7 E (23882) esp-tls-mbedtls: mbedtls_ssl_handshake returned -0x7080 I (23892) esp-tls-mbedtls: Certificate verified. E (23892) esp-tls: Failed to open new connection E (23892) TRANSPORT_BASE: Failed to open a new connection E (23902) MQTT_CLIENT: Error transport connect I (23902) MQTTS_EXAMPLE: MQTT_EVENT_ERROR I (23912) MQTTS_EXAMPLE: Last error code reported from esp-tls: 0x801a I (23922) MQTTS_EXAMPLE: Last tls stack error number: 0x7080 I (23922) MQTTS_EXAMPLE: Last captured errno : 0 (Success) I (23932) MQTTS_EXAMPLE: MQTT_EVENT_DISCONNECTED

sl2121 commented 1 year ago

hi i have loaded the ESP-MQTT SSL Sample application.

static void mqtt_app_start(void) { const esp_mqtt_client_config_t mqtt_cfg = { .uri = CONFIG_BROKER_URI, .cert_pem = (const char *)mqtt_eclipseprojects_io_pem_start, }; mqtt_eclipseprojects_io_pem_start inside here i have put the DigiCertGlobalRootG2[] "MIIDjjCCAn...MrY" and also i have tried with the AWS Amazon Root CA1.

and i see :

I (6592) example_connect: Got IPv4 event: Interface "example_connect: sta" address: * I (6602) example_connect: Connected to example_connect: sta I (6612) example_connect: - IPv4 address: *** I (6612) example_connect: - IPv6 address: f****, type: ESP_IP6_ADDR_IS_LINK_LOCAL I (6622) MQTTS_EXAMPLE: [APP] Free memory: 234100 bytes I (6632) MQTTS_EXAMPLE: Other event id:7 I (15212) MQTTS_EXAMPLE: MQTT_EVENT_CONNECTED I (15222) MQTTS_EXAMPLE: sent subscribe successful, msg_id=53250 I (15222) MQTTS_EXAMPLE: sent subscribe successful, msg_id=60112 I (15222) MQTTS_EXAMPLE: sent unsubscribe successful, msg_id=18944 I (15422) MQTTS_EXAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=53250 I (15432) MQTTS_EXAMPLE: sent publish successful, msg_id=0 I (15472) MQTTS_EXAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=60112 I (15482) MQTTS_EXAMPLE: sent publish successful, msg_id=0 I (15632) MQTTS_EXAMPLE: MQTT_EVENT_DATA TOPIC=/topic/qos1 DATA=data_3

sl2121 commented 1 year ago

After in the static void mqtt_app_start(void) { const esp_mqtt_client_config_t mqtt_cfg = { .uri = "mqtts://miothub.azure-devices.net:8883", .cert_pem = (const char *)mqtt_eclipseprojects_io_pem_start, };

i see

I (6612) example_connect: - IPv4 address: * I (6612) example_connect: - IPv6 address: *****, type: ESP_IP6_ADDR_IS_LINK_LOCAL I (6622) MQTTS_EXAMPLE: [APP] Free memory: 234100 bytes I (6632) MQTTS_EXAMPLE: Other event id:7 W (23892) esp-tls: Failed to open new connection in specified timeout E (23892) TRANSPORT_BASE: Failed to open a new connection E (23892) MQTT_CLIENT: Error transport connect I (23892) MQTTS_EXAMPLE: MQTT_EVENT_ERROR I (23902) MQTTS_EXAMPLE: Last error code reported from esp-tls: 0x8006 I (23912) MQTTS_EXAMPLE: Last tls stack error number: 0x0 I (23912) MQTTS_EXAMPLE: Last captured errno : 0 (Success) I (23922) MQTTS_EXAMPLE: MQTT_EVENT_DISCONNECTED I (38922) MQTTS_EXAMPLE: Other event id:7 W (55842) esp-tls: Failed to open new connection in specified timeout E (55842) TRANSPORT_BASE: Failed to open a new connection E (55842) MQTT_CLIENT: Error transport connect I (55842) MQTTS_EXAMPLE: MQTT_EVENT_ERROR I (55852) MQTTS_EXAMPLE: Last error code reported from esp-tls: 0x8006 I (55852) MQTTS_EXAMPLE: Last tls stack error number: 0x0 I (55862) MQTTS_EXAMPLE: Last captured errno : 0 (Success) I (55872) MQTTS_EXAMPLE: MQTT_EVENT_DISCONNECTED I (70872) MQTTS_EXAMPLE: Other event id:7

Thanks.

euripedesrocha commented 1 year ago

Hi @sl2121, check the error codes reported from esp-tls to identify the issue reported in the connection.

You may also want to check Azure Iot Hub side because this seems to be an authentication issue. They had a console application that you may use to monitor and debug this type of issues, unfortunately I don't remember the name of it. I recommend that you double-check your device credentials configuration. From the perspective of esp-mqtt the configuration you show have the needed data, so I would say, from the information you gave, that the issue is having the correct configuration in the IoT hub side.

Alvin1Zhang commented 1 year ago

Thanks for reporting, will close due to short of feedback, feel free to reopen with more updates. Thanks.