You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have set up an IoT Hub, DPS and symmetric key enrollment group.
I'm working with the SDK on Debian. Using the provided prov_dev_client_sample, with a generated device key from the symmetric key, I can successfully provision / enroll my device on the hub.
Now I wish to send data from the device to the hub. I am using the iothub_ll_telemetry sample as a reference.
The only way I can do this is by manually getting device connection string from the hub and using IoTHubDeviceClient_LL_CreateFromConnectionString(connectionString, protocol)
However, I want my device to be able to connect without manual intervention. For simplicity, my hub name is "test-hub" and my device name is "test-device".
When I try to create a device handle as follows : IoTHubDeviceClient_LL_CreateFromDeviceAuth("test-hub.azure-devices.net", "test-device", protocol)
I get the following failure which I don't understand:
Alternatively, When I try to create a device handle using IoTHubDeviceClient_LL_CreateWithTransport(&config) ,
by firstly attempting to create a transport handle as follows : TRANSPORT_HANDLE transport_handle = IoTHubTransport_Create(protocol, "test-hub", "azure-devices.net");
I get the following failure, which I don't understand either :
Creating IoTHub Device handle
Error: Time:Wed Apr 17 23:31:52 2024 File:/home/azure-iot-sdk-c/iothub_client/src/iothubtransport_mqtt_common.c Func:IoTHubTransport_MQTT_Common_Create Line:3212 Invalid Argument: auth_module_handle is NULL)
Error: Time:Wed Apr 17 23:31:52 2024 File:/home/azure-iot-sdk-c/iothub_client/src/iothubtransport.c Func:IoTHubTransport_Create Line:78 Lower Layer transport not created.
Failure creating transport handle
I am struggling to understand and solve this. Below is my complete source code that I am using to attempt to connect to an already existing provisioned device. As mentioned, It only works by using the device connection string directly. I would appreciate any help with this. Thanks
#include <stdio.h>
#include <stdlib.h>
#include "iothub.h"
#include "azure_prov_client/prov_device_ll_client.h"
#include "azure_prov_client/prov_transport_mqtt_client.h"
#include "iothub_device_client_ll.h"
#include "iothub_device_client.h"
#include "iothub_client_options.h"
#include "iothub_message.h"
#include "azure_c_shared_utility/threadapi.h"
#include "azure_c_shared_utility/crt_abstractions.h"
#include "azure_c_shared_utility/shared_util_options.h"
#ifdef SET_TRUSTED_CERT_IN_SAMPLES
#include "certs.h"
#endif // SET_TRUSTED_CERT_IN_SAMPLES
/* This sample uses the _LL APIs of iothub_client for example purposes.
Simply changing the using the convenience layer (functions not having _LL)
and removing calls to _DoWork will yield the same results. */
// The protocol you wish to use should be uncommented
//
#define SAMPLE_MQTT
//#define SAMPLE_MQTT_OVER_WEBSOCKETS
//#define SAMPLE_AMQP
//#define SAMPLE_AMQP_OVER_WEBSOCKETS
//#define SAMPLE_HTTP
#ifdef SAMPLE_MQTT
#include "iothubtransportmqtt.h"
#include "azure_prov_client/prov_transport_mqtt_client.h"
#endif // SAMPLE_MQTT
#ifdef SAMPLE_MQTT_OVER_WEBSOCKETS
#include "iothubtransportmqtt_websockets.h"
#include "azure_prov_client/prov_transport_mqtt_ws_client.h"
#endif // SAMPLE_MQTT_OVER_WEBSOCKETS
#ifdef SAMPLE_AMQP
#include "iothubtransportamqp.h"
#include "azure_prov_client/prov_transport_amqp_client.h"
#endif // SAMPLE_AMQP
#ifdef SAMPLE_AMQP_OVER_WEBSOCKETS
#include "iothubtransportamqp_websockets.h"
#include "azure_prov_client/prov_transport_amqp_ws_client.h"
#endif // SAMPLE_AMQP_OVER_WEBSOCKETS
#ifdef SAMPLE_HTTP
#include "iothubtransporthttp.h"
#include "azure_prov_client/prov_transport_http_client.h"
#endif // SAMPLE_HTTP
/* Paste in the your iothub connection string */
//static const char* connectionString = "[device connection string]";
/* Paste in the your iothub device connection string */
static const char* connectionString = "HostName=test-hub.azure-devices.net;DeviceId=test-device;SharedAccessKey=XXXXXXX";
#define MESSAGE_COUNT 5
static bool g_continueRunning = true;
static size_t g_message_count_send_confirmations = 0;
static void send_confirm_callback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
{
(void)userContextCallback;
// When a message is sent this callback will get invoked
g_message_count_send_confirmations++;
(void)printf("Confirmation callback received for message %lu with result %s\r\n", (unsigned long)g_message_count_send_confirmations, MU_ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result));
}
static void connection_status_callback(IOTHUB_CLIENT_CONNECTION_STATUS result, IOTHUB_CLIENT_CONNECTION_STATUS_REASON reason, void* user_context)
{
(void)reason;
(void)user_context;
// This sample DOES NOT take into consideration network outages.
if (result == IOTHUB_CLIENT_CONNECTION_AUTHENTICATED)
{
(void)printf("The device client is connected to iothub\r\n");
}
else
{
(void)printf("The device client has been disconnected\r\n");
}
}
int main(void)
{
IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol;
IOTHUB_MESSAGE_HANDLE message_handle;
size_t messages_sent = 0;
const char* telemetry_msg = "test_message";
// Select the Protocol to use with the connection
#ifdef SAMPLE_MQTT
protocol = MQTT_Protocol;
#endif // SAMPLE_MQTT
#ifdef SAMPLE_MQTT_OVER_WEBSOCKETS
protocol = MQTT_WebSocket_Protocol;
#endif // SAMPLE_MQTT_OVER_WEBSOCKETS
#ifdef SAMPLE_AMQP
protocol = AMQP_Protocol;
#endif // SAMPLE_AMQP
#ifdef SAMPLE_AMQP_OVER_WEBSOCKETS
protocol = AMQP_Protocol_over_WebSocketsTls;
#endif // SAMPLE_AMQP_OVER_WEBSOCKETS
#ifdef SAMPLE_HTTP
protocol = HTTP_Protocol;
#endif // SAMPLE_HTTP
// Used to initialize IoTHub SDK subsystem
(void)IoTHub_Init();
IOTHUB_DEVICE_CLIENT_LL_HANDLE device_ll_handle;
//IOTHUB_DEVICE_CLIENT_HANDLE _device_handle;
(void)printf("Creating IoTHub Device handle\r\n");
// Create the iothub handle here
// ONLY THIS WORKS !!
//device_ll_handle = IoTHubDeviceClient_LL_CreateFromConnectionString(connectionString, protocol);
// THIS FAILS !!
//device_ll_handle = IoTHubDeviceClient_LL_CreateFromDeviceAuth("test-hub.azure-devices.net", "test-device", protocol);
// THIS FAILS !!
TRANSPORT_HANDLE transport_handle = IoTHubTransport_Create(protocol, "test-hub", "azure-devices.net");
if (transport_handle == NULL)
{
(void)printf("Failure creating transport handle\r\n");
return -1;
}
IOTHUB_CLIENT_DEVICE_CONFIG config = {0};
config.protocol = protocol;
config.transportHandle = IoTHubTransport_GetLLTransport(transport_handle);
config.deviceId = "test-device";
config.deviceKey = NULL;
config.deviceSasToken = "XXXXXXX";
device_ll_handle = IoTHubDeviceClient_LL_CreateWithTransport(&config);
if (device_ll_handle == NULL)
{
(void)printf("Failure creating IotHub device. Hint: Check your connection string.\r\n");
}
else
{
(void)printf("Device handle created.\r\n");
// Set any option that are necessary.
// For available options please see the iothub_sdk_options.md documentation
#ifndef SAMPLE_HTTP
// Can not set this options in HTTP
bool traceOn = true;
IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_LOG_TRACE, &traceOn);
#endif
#ifdef SET_TRUSTED_CERT_IN_SAMPLES
// Setting the Trusted Certificate. This is only necessary on systems without
// built in certificate stores.
IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_TRUSTED_CERT, certificates);
#endif // SET_TRUSTED_CERT_IN_SAMPLES
#if defined SAMPLE_MQTT || defined SAMPLE_MQTT_OVER_WEBSOCKETS
//Setting the auto URL Encoder (recommended for MQTT). Please use this option unless
//you are URL Encoding inputs yourself.
//ONLY valid for use with MQTT
bool urlEncodeOn = true;
(void)IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_AUTO_URL_ENCODE_DECODE, &urlEncodeOn);
#endif
// Setting connection status callback to get indication of connection to iothub
(void)IoTHubDeviceClient_LL_SetConnectionStatusCallback(device_ll_handle, connection_status_callback, NULL);
do
{
if (messages_sent < MESSAGE_COUNT)
{
// Construct the iothub message from a string or a byte array
message_handle = IoTHubMessage_CreateFromString(telemetry_msg);
//message_handle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText)));
// Set Message property
/*
(void)IoTHubMessage_SetMessageId(message_handle, "MSG_ID");
(void)IoTHubMessage_SetCorrelationId(message_handle, "CORE_ID");
(void)IoTHubMessage_SetContentTypeSystemProperty(message_handle, "application%2fjson");
(void)IoTHubMessage_SetContentEncodingSystemProperty(message_handle, "utf-8");
(void)IoTHubMessage_SetMessageCreationTimeUtcSystemProperty(message_handle, "2020-07-01T01:00:00.346Z");
*/
// Add custom properties to message
(void)IoTHubMessage_SetProperty(message_handle, "property_key", "property_value");
(void)printf("Sending message %d to IoTHub\r\n", (int)(messages_sent + 1));
IoTHubDeviceClient_LL_SendEventAsync(device_ll_handle, message_handle, send_confirm_callback, NULL);
// The message is copied to the sdk so the we can destroy it
IoTHubMessage_Destroy(message_handle);
messages_sent++;
}
else if (g_message_count_send_confirmations >= MESSAGE_COUNT)
{
// After all messages are all received stop running
g_continueRunning = false;
}
IoTHubDeviceClient_LL_DoWork(device_ll_handle);
ThreadAPI_Sleep(1);
} while (g_continueRunning);
// Clean up the iothub sdk handle
IoTHubDeviceClient_LL_Destroy(device_ll_handle);
}
// Free all the sdk subsystem
IoTHub_Deinit();
printf("Press any key to continue");
(void)getchar();
return 0;
}
The text was updated successfully, but these errors were encountered:
pavel808
changed the title
Why isn't it possible to create a client device handle using Transport or Auth
It's not possible to create a client device handle using Transport or Auth
Apr 17, 2024
Hi @mulligan252 , we will close this issue based on the reply from @ericwol-msft , which provides a good example of how to get the result from the provisioning and using the provisioned hub and credential to connect to the Azure IoT Hub.
If you have further questions or would like to follow up, please feel free to reopen this issue.
Thank you,
Azure IoT SDKs Team
I have set up an IoT Hub, DPS and symmetric key enrollment group.
I'm working with the SDK on Debian. Using the provided prov_dev_client_sample, with a generated device key from the symmetric key, I can successfully provision / enroll my device on the hub.
Now I wish to send data from the device to the hub. I am using the iothub_ll_telemetry sample as a reference.
The only way I can do this is by manually getting device connection string from the hub and using
IoTHubDeviceClient_LL_CreateFromConnectionString(connectionString, protocol)
However, I want my device to be able to connect without manual intervention. For simplicity, my hub name is "test-hub" and my device name is "test-device".
When I try to create a device handle as follows :
IoTHubDeviceClient_LL_CreateFromDeviceAuth("test-hub.azure-devices.net", "test-device", protocol)
I get the following failure which I don't understand:
Alternatively, When I try to create a device handle using
IoTHubDeviceClient_LL_CreateWithTransport(&config)
,by firstly attempting to create a transport handle as follows :
TRANSPORT_HANDLE transport_handle = IoTHubTransport_Create(protocol, "test-hub", "azure-devices.net");
I get the following failure, which I don't understand either :
I am struggling to understand and solve this. Below is my complete source code that I am using to attempt to connect to an already existing provisioned device. As mentioned, It only works by using the device connection string directly. I would appreciate any help with this. Thanks
The text was updated successfully, but these errors were encountered: