espressif / esp-mqtt

ESP32 mqtt component
Apache License 2.0
603 stars 255 forks source link

How to Link ali-mqtt (IDFGH-4094) #171

Closed hzhh110 closed 3 years ago

hzhh110 commented 3 years ago

How can I directly use this framework to connect to ali-mqtt? Can you give me a sample? All online are connected with linkiot framework and arduino

david-cermak commented 3 years ago

Hi @hzhh110

Unfortunately, we have no example of connecting to ali-mqtt. You can follow the steps described here https://partners-intl.aliyun.com/help/doc-detail/73742.htm to initiate a connection

For calculating hmac-sha1, you can use mbedtls API, for example:

    #include "mbedtls/cipher.h"
    #include "mbedtls/md.h"

....
    unsigned char digest[20];
    mbedtls_md_context_t md_ctx;
    const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );

    mbedtls_md_setup( &md_ctx, md_info, 1 );
    mbedtls_md_hmac_starts( &md_ctx, key, sizeof(key) );
    mbedtls_md_hmac_update( &md_ctx, buffer, sizeof(buffer) -1);
    mbedtls_md_hmac_finish( &md_ctx, digest );

And init the mqtt client (using the client data from the example in https://partners-intl.aliyun.com/help/doc-detail/73742.htm)

    esp_mqtt_client_config_t mqtt_config = {
             ...

            .client_id = "12345|securemode=3,signmethod=hmacsha1,timestamp=789|",
            .username = "device&pk",
            .password = ... // ==digest calculated as above, where 
//    const unsigned char buffer[] = "clientId12345deviceNamedeviceproductKeypktimestamp789";
//    const unsigned char key[] = "secret";
           ...
    };
david-cermak commented 3 years ago

@hzhh110 Did the suggested calculation work? Closing now, as it's not an issue of the esp-mqtt.