Azure SDK for Embedded C
Azure IoT Client Coding Patterns

Introduction

This page introduces you to coding patterns that are MQTT stack agnostic. These examples will give you a general overview of the API calls and structure needed to use the Azure IoT Embedded C SDK features.

To view scenario-focused examples using the API calls, please view the introductory examples on the Azure IoT Client README file.

For a more extensive demonstration of the API, please view and run the sample code, which uses Paho MQTT.

Examples

IoT Hub Client with MQTT Stack

Below is an implementation for using the IoT Hub Client SDK. This is meant to guide users in incorporating their MQTT stack with the IoT Hub Client SDK. Note for simplicity reasons, this code will not compile. Ideally, guiding principles can be inferred from reading through this snippet to create an IoT solution.

#include <az/az_core.h>
#include <az/az_iot.h>
static az_span my_iothub_hostname = AZ_SPAN_LITERAL_FROM_STR("<your hub fqdn here>");
static az_span my_device_id = AZ_SPAN_LITERAL_FROM_STR("<your device id here>");
//Make sure the buffer is large enough to fit the user name (100 is an example)
static char my_mqtt_user_name[100];
//Make sure the buffer is large enough to fit the client id (16 is an example)
static char my_mqtt_client_id[16];
//This assumes an X509 Cert. SAS keys may also be used.
static const char my_device_cert[]= "-----BEGIN CERTIFICATE-----abcdefg-----END CERTIFICATE-----";
static char telemetry_topic[128];
static char telemetry_payload[] = "Hello World";
void handle_iot_message(mqtt_client_message* msg);
int main(void)
{
//Get the default IoT Hub options
//Initialize the client with hostname, device id, and options
az_iot_hub_client_init(&my_client, my_iothub_hostname, my_device_id, &options);
//Get the MQTT user name to connect
az_iot_hub_client_get_user_name(&my_client, my_mqtt_user_name,
sizeof(my_mqtt_user_name), NULL);
//Get the MQTT client id to connect
az_iot_hub_client_get_client_id(&my_client, my_mqtt_client_id,
sizeof(my_mqtt_client_id), NULL);
//Initialize MQTT client with necessary parameters (example params shown)
mqtt_client my_mqtt_client;
mqtt_client_init(&my_mqtt_client, my_iothub_hostname, my_mqtt_client_id);
//Subscribe to c2d messages
mqtt_client_subscribe(&my_mqtt_client, AZ_IOT_HUB_CLIENT_C2D_SUBSCRIBE_TOPIC);
//Subscribe to device methods
mqtt_client_subscribe(&my_mqtt_client, AZ_IOT_HUB_CLIENT_METHODS_SUBSCRIBE_TOPIC);
//Subscribe to twin patch topic
mqtt_client_subscribe(&my_mqtt_client, AZ_IOT_HUB_CLIENT_TWIN_PATCH_SUBSCRIBE_TOPIC);
//Subscribe to twin response topic
mqtt_client_subscribe(&my_mqtt_client, AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_SUBSCRIBE_TOPIC);
//Connect to the IoT Hub with your chosen mqtt stack
mqtt_client_connect(&my_mqtt_client, my_mqtt_user_name, my_device_cert);
//This example would run to receive any incoming message and send a telemetry message five times
int iterations = 0;
mqtt_client_message msg;
while(iterations++ < 5)
{
if(mqtt_client_receive(&msg))
{
handle_iot_message(&msg);
}
send_telemetry_message();
}
//Disconnect from the IoT Hub
mqtt_client_disconnect(&my_mqtt_client);
//Destroy the mqtt client
mqtt_client_destroy(&my_mqtt_client);
}
void send_telemetry_message(void)
{
//Get the topic to send a telemetry message
az_iot_hub_client_telemetry_get_publish_topic(&client, NULL, telemetry_topic, sizeof(telemetry_topic), NULL);
//Send the telemetry message with the MQTT client
mqtt_client_publish(telemetry_topic, telemetry_payload, AZ_HUB_CLIENT_DEFAULT_MQTT_TELEMETRY_QOS);
}
void handle_iot_message(mqtt_client_message* msg)
{
//Initialize the incoming topic to a span
az_span incoming_topic = az_span_create(msg->topic, msg->topic_len);
//The message could be for three features so parse the topic to see which it is for
if (az_result_succeeded(az_iot_hub_client_methods_parse_received_topic(&client, incoming_topic, &method_request)))
{
//Handle the method request
}
else if (az_result_succeeded(az_iot_hub_client_c2d_parse_received_topic(&client, incoming_topic, &c2d_request)))
{
//Handle the c2d message
}
else if (az_result_succeeded(az_iot_hub_client_twin_parse_received_topic(&client, incoming_topic, &twin_response)))
{
//Handle the twin message
}
}
az_iot_hub_client_init
AZ_NODISCARD az_result az_iot_hub_client_init(az_iot_hub_client *client, az_span iot_hub_hostname, az_span device_id, az_iot_hub_client_options const *options)
Initializes an Azure IoT Hub Client.
az_iot_hub_client_options_default
AZ_NODISCARD az_iot_hub_client_options az_iot_hub_client_options_default()
Gets the default Azure IoT Hub Client options.
az_iot_hub_client_twin_parse_received_topic
AZ_NODISCARD az_result az_iot_hub_client_twin_parse_received_topic(az_iot_hub_client const *client, az_span received_topic, az_iot_hub_client_twin_response *out_response)
Attempts to parse a received message's topic for twin features.
az_iot_hub_client_get_user_name
AZ_NODISCARD az_result az_iot_hub_client_get_user_name(az_iot_hub_client const *client, char *mqtt_user_name, size_t mqtt_user_name_size, size_t *out_mqtt_user_name_length)
Gets the MQTT user name.
az_iot_hub_client_get_client_id
AZ_NODISCARD az_result az_iot_hub_client_get_client_id(az_iot_hub_client const *client, char *mqtt_client_id, size_t mqtt_client_id_size, size_t *out_mqtt_client_id_length)
Gets the MQTT client ID.
az_span_create
AZ_NODISCARD az_span az_span_create(uint8_t *ptr, int32_t size)
Returns an az_span over a byte buffer.
AZ_IOT_HUB_CLIENT_METHODS_SUBSCRIBE_TOPIC
#define AZ_IOT_HUB_CLIENT_METHODS_SUBSCRIBE_TOPIC
The MQTT topic filter to subscribe to method requests.
Definition: az_iot_hub_client.h:385
AZ_IOT_HUB_CLIENT_TWIN_PATCH_SUBSCRIBE_TOPIC
#define AZ_IOT_HUB_CLIENT_TWIN_PATCH_SUBSCRIBE_TOPIC
Gets the MQTT topic filter to subscribe to twin desired property changes.
Definition: az_iot_hub_client.h:564
az_result_succeeded
AZ_NODISCARD AZ_INLINE bool az_result_succeeded(az_result result)
Checks whether the result provided indicates a success.
Definition: az_result.h:162
az_iot_hub_client_telemetry_get_publish_topic
AZ_NODISCARD az_result az_iot_hub_client_telemetry_get_publish_topic(az_iot_hub_client const *client, az_iot_message_properties const *properties, char *mqtt_topic, size_t mqtt_topic_size, size_t *out_mqtt_topic_length)
Gets the MQTT topic that must be used for device to cloud telemetry messages.
az_iot_hub_client_methods_parse_received_topic
AZ_NODISCARD az_result az_iot_hub_client_methods_parse_received_topic(az_iot_hub_client const *client, az_span received_topic, az_iot_hub_client_method_request *out_request)
Attempts to parse a received message's topic for method features.
az_iot_hub_client_c2d_parse_received_topic
AZ_NODISCARD az_result az_iot_hub_client_c2d_parse_received_topic(az_iot_hub_client const *client, az_span received_topic, az_iot_hub_client_c2d_request *out_request)
Attempts to parse a received message's topic for C2D features.
AZ_IOT_HUB_CLIENT_C2D_SUBSCRIBE_TOPIC
#define AZ_IOT_HUB_CLIENT_C2D_SUBSCRIBE_TOPIC
The MQTT topic filter to subscribe to Cloud-to-Device requests.
Definition: az_iot_hub_client.h:336
az_iot_hub_client_c2d_request
The Cloud-To-Device Request.
Definition: az_iot_hub_client.h:343
az_iot_hub_client_options
Azure IoT Hub Client options.
Definition: az_iot_hub_client.h:43
az_iot_hub_client_method_request
A method request received from IoT Hub.
Definition: az_iot_hub_client.h:392
az_iot_hub_client_twin_response
Twin response.
Definition: az_iot_hub_client.h:583
az_iot_hub_client
Azure IoT Hub Client.
Definition: az_iot_hub_client.h:78
AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_SUBSCRIBE_TOPIC
#define AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_SUBSCRIBE_TOPIC
The MQTT topic filter to subscribe to twin operation responses.
Definition: az_iot_hub_client.h:558
AZ_SPAN_LITERAL_FROM_STR
#define AZ_SPAN_LITERAL_FROM_STR(STRING_LITERAL)
Returns a literal az_span over a literal string. The size of the az_span is equal to the length of th...
Definition: az_span.h:112
az_span
Represents a "view" over a byte buffer that represents a contiguous region of memory....
Definition: az_span.h:33