GoogleCloudPlatform / iot-device-sdk-embedded-c

Cloud IoT Device SDK for Connectivity to IoT Core.
Other
247 stars 83 forks source link

How to properly "consume" iot-device-sdk-embedded-c #129

Open bojankoce opened 2 years ago

bojankoce commented 2 years ago

I'm running Ubuntu 20.04 machine where I'm trying to develop a ROS2-based application in C/C++. One of the requirements I have is to create ROS2 node that will establish MQTT connection with the Google Cloud, so my natural choice was to benefit from the iot-device-sdk-embedded-c. I started reading the SDK documentation and was able to clone the repo, and build it by calling make command from the root directory. Moreover, trying examples/iot_core_mqtt_client example also went well - I was able to connect to the Cloud and periodically publish data. Sweet!

Now, the trouble comes when I want to use/"consume" iot-device-sdk-embedded-c SDK in my ROS2 development environment. What I did is the following:

target_include_directories(mqtt_client_node PRIVATE ${IOTC_PATH}/include) ...

libiotc

target_link_libraries(mqtt_client_node ${IOTC_PATH}/bin/linux/libiotc.a)

mbedTLS

set(IOTC_MBEDTLS_LIB_PATH ${IOTC_PATH}/third_party/tls/mbedtls/library) target_link_libraries(mqtt_client_node ${IOTC_MBEDTLS_LIB_PATH}/libmbedtls.a) target_link_libraries(mqtt_client_node ${IOTC_MBEDTLS_LIB_PATH}/libmbedx509.a) target_link_libraries(mqtt_client_node ${IOTC_MBEDTLS_LIB_PATH}/libmbedcrypto.a)

The approach is taken from your `examples/zephyr_native_posix` example and I was hoping it will be enough to properly build my ROS2-based executable that will, as a first try, behave the same way as your `examples/iot_core_mqtt_client` example. 

The application was compiled successfully (Yay!) but when I started the app and tried to connect to the MQTT server with `iotc_connect()` I get the following error from `on_connection_state_changed()` callback function:

ERROR! Connection has failed reason 20 :



According to your error dictionary from `iotc_error.h` file, 20 is the error code for `IOTC_TLS_FAILED_LOADING_CERTIFICATE`. 

Do you have any idea what I am doing wrong or missing? Public/Private keys and generated JWT are completely the same as in my first successful attempt when I just compiled your example. 

Thank you very much for your time and efforts. It is really appreaciated. 
Looking forward to reading from you. 
atigyi commented 2 years ago

Probably you'll need to put the roots.pem file next to the binary. This file contains the two trusted root CA certs for mqtt.2030.ltsapis.goog.