Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[paho-dev] Cannot connect to Azure IoTHub in SSL with C paho library

Hello everyone and thanks in advance for your support.


I cannot connect to Azure IoTHub via the C paho library (on linux). I’m using the latest release, v1.2.1.

I’m following the official Microsoft documentation here (

I have managed to successfully connect with both the python library (un Ubuntu) and the Java GUI client (on Windows) and to exchange messages, so I guess the credentials I’m using are correct.


I'm compiling the C paho library, altough my application is in C++, I don't know if this can be a problem.

What happens after I try to Connect is connectFailure with a null response pointer.

This is a snippet of my MQTT_connect() function, linked against the mqtt3as library:

#define MQTT_BROKER_ADDRESS "ssl://xxx:8883"                              


void onConnectFailure(void* context, MQTTAsync_failureData* response)                                                                                                                                               


  printf("Connect failed, rc %d %s\n", response ? response->code : 0, response->message);                                                                                                                              }  

 int main() {}

  char clientID[] = "xxxx";                                                                                                                                                                                  

  MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;                                                                                                                                       

  MQTTAsync_SSLOptions ssl_opts = MQTTAsync_SSLOptions_initializer;                                                                                                                                                


  //MQTTAsync_willOptions will_opts = MQTTAsync_willOptions_initializer;                                                                                                                                            


  MQTTAsync_create(&client, MQTT_BROKER_ADDRESS, clientID,                                                                                                                                                          

                   MQTTCLIENT_PERSISTENCE_NONE, NULL);                                                                                                                                                             


  MQTTAsync_setCallbacks(client, client, NULL, OnMessageArrived, NULL);                                                                                                                                            


  string username = "xxxx";                                                                                                                                                   

  string password = "xxxx ";                                           


  //ssl_opts.struct_version = 2;                                                                                                                                                                                    

  ssl_opts.enableServerCertAuth = 1;                                                                                                                                                                               

  ssl_opts.sslVersion = MQTT_SSL_VERSION_TLS_1_2;                                                                                                                                                                  

  ssl_opts.trustStore = "/etc/ssl/certs/Baltimore_CyberTrust_Root.pem";                                                                                                                                            

  //ssl_opts.keyStore ="/etc/ssl/certs/ca-certificates.crt";                                                                                                                                                        

  //ssl_opts.CApath = "/etc/ssl/certs";                                                                                                                                                                             



  //conn_opts.keepAliveInterval = MQTT_KEEPALIVE;                                                                                                                                                                  

  //conn_opts.cleansession = MQTT_CLEAN_SESSION;                                                                                                                                                                    

  //conn_opts.will = &will_opts;                                                                                                                                                                                    


  conn_opts.username = username.c_str();                                                                                                                                                                            

  conn_opts.password = password.c_str();                                                                                                                                                                           


  //conn_opts.connectTimeout = MQTT_TIMEOUT;                                                                                                                                                                        

  conn_opts._onSuccess_ = onConnect;                                                                                                                                                                                  

  conn_opts._onFailure_ = onConnectFailure;                                                                                                                                                                           

  conn_opts.context = client;                                                                                                                                                                                      

  //conn_opts.automaticReconnect = 0;                                                                                                                                                                               

  conn_opts.ssl = &ssl_opts;                                                                                                                                                                                        

  conn_opts.MQTTVersion = 4;                                                                                                                                                                                        


//  gAppLog.PrintLog(LOG_DEBUG_WS, "MQTT connecting to: %s", MQTT_BROKER_ADDRESS);                                                                                                                                 

  printf("host: %s \nclientID: %s \nusername: %s \npassword: %s \n", MQTT_BROKER_ADDRESS,clientID,username.c_str(), password.c_str());                                                                             


  int rc;                                                                                                                                                                                                           

  rc = MQTTAsync_connect(client, &conn_opts);



Back to the top