\CAStore.pem'
```
#### IoT Hub X.509 Certificate Samples
Set the following environment variables if running any of these samples: `paho_iot_hub_c2d_sample`, `paho_iot_hub_methods_sample`, `paho_iot_hub_telemetry_sample`, `paho_iot_hub_twin_sample`, `paho_iot_pnp_sample`, `paho_iot_pnp_component_sample`
Instructions to set environment variables for IoT Hub X.509 Certificate samples:
1. Retrieve variable information:
Access your Azure IoT Hub from either your Azure Portal or Azure IoT Explorer.
- `AZ_IOT_HUB_DEVICE_ID`: From the IoT devices tab, select your device. Copy its Device Id.
- `AZ_IOT_HUB_HOSTNAME`: From the Overview tab, copy your Azure IoT hub Hostname.
2. Set the variables:
Linux:
```bash
export AZ_IOT_HUB_DEVICE_ID=
export AZ_IOT_HUB_HOSTNAME=
```
Windows (PowerShell):
```powershell
$env:AZ_IOT_HUB_DEVICE_ID=''
$env:AZ_IOT_HUB_HOSTNAME=''
```
#### IoT Provisioning X.509 Certificate Sample
Set the following environment variables if running the sample: `paho_iot_pnp_with_provisioning_sample`, `paho_iot_provisioning_sample`
Instructions to set environment variables for DPS X.509 Certificate sample:
1. Retrieve variable information:
Access your Azure IoT Hub Device Provisioning Service from your Azure Portal.
- `AZ_IOT_PROVISIONING_REGISTRATION_ID`: Set this to `paho-sample-device1`.
- `AZ_IOT_PROVISIONING_ID_SCOPE`: From the Overview tab, copy the Id Scope.
2. Set the variables:
Linux:
```bash
export AZ_IOT_PROVISIONING_REGISTRATION_ID=
export AZ_IOT_PROVISIONING_ID_SCOPE=
```
Windows (PowerShell):
```powershell
$env:AZ_IOT_PROVISIONING_REGISTRATION_ID=''
$env:AZ_IOT_PROVISIONING_ID_SCOPE=''
```
#### IoT Hub Symetric Key (SAS) Sample
Set the following environment variables if running the sample: `paho_iot_hub_sas_telemetry_sample`
Instructions to set environment variables for IoT Hub Symmetric Key sample:
1. Retrieve variable information:
Access your Azure IoT Hub from either your Azure Portal or Azure IoT Explorer.
- `AZ_IOT_HUB_SAS_DEVICE_ID`: From the IoT devices tab, select your device. Copy its Device Id.
- `AZ_IOT_HUB_SAS_KEY`: From the same page, copy its Primary Key.
- `AZ_IOT_HUB_HOSTNAME`: From the Overview tab, copy your Azure IoT hub Hostname.
2. Set the variables:
Linux:
```bash
export AZ_IOT_HUB_SAS_DEVICE_ID=
export AZ_IOT_HUB_SAS_KEY=
export AZ_IOT_HUB_HOSTNAME=
```
Windows (PowerShell):
```powershell
$env:AZ_IOT_HUB_SAS_DEVICE_ID=''
$env:AZ_IOT_HUB_SAS_KEY=''
$env:AZ_IOT_HUB_HOSTNAME=''
```
#### IoT Provisioning Symmetric Key (SAS) Sample
Set the following environment variables if running the sample: `paho_iot_provisioning_sas_sample`
Instructions to set environment variables for DPS Symmetric Key sample:
1. Retrieve variable information:
Access your Azure IoT Hub Device Provisioning Service from your Azure Portal.
- `AZ_IOT_PROVISIONING_SAS_REGISTRATION_ID`: From the Manage enrollments tab, under Individual Enrollments, copy the Registration Id of your SAS device.
- `AZ_IOT_PROVISIONING_SAS_KEY`: Under Individual Enrollments, select your SAS device. Copy its Primary Key.
- `AZ_IOT_PROVISIONING_ID_SCOPE`: From the Overview tab, copy the Id Scope.
2. Set the variables:
Linux:
```bash
export AZ_IOT_PROVISIONING_SAS_REGISTRATION_ID=
export AZ_IOT_PROVISIONING_SAS_KEY=
export AZ_IOT_PROVISIONING_ID_SCOPE=
```
Windows (PowerShell):
```powershell
$env:AZ_IOT_PROVISIONING_SAS_REGISTRATION_ID=''
$env:AZ_IOT_PROVISIONING_SAS_KEY=''
$env:AZ_IOT_PROVISIONING_ID_SCOPE=''
```
## Build and Run the Sample
1. Build the Azure SDK for Embedded C directory structure.
From the root of the SDK directory `azure-sdk-for-c`:
```bash
mkdir build
cd build
cmake -DTRANSPORT_PAHO=ON ..
```
2. Compile and run the sample.
Linux:
```bash
cmake --build .
./sdk/samples/iot/
```
Windows (PowerShell):
```powershell
.\az.sln
```
- Navigate to the "Solution Explorer" panel and find the sample project you would like to run.
- Right-click on the sample project, then click on "Set as Startup Project". (This makes it the default startup project.)
- Build and run the project (`F5` on most installations).
## Sample Descriptions
This section provides an overview of the different samples available to run and what to expect from each.
### IoT Hub C2D Sample
- *Executable:* `paho_iot_hub_c2d_sample`
This [sample](https://github.com/Azure/azure-sdk-for-c/blob/main/sdk/samples/iot/paho_iot_hub_c2d_sample.c) receives incoming cloud-to-device (C2D) messages sent from the Azure IoT Hub to the device. It will successfully receive up to 5 messages sent from the service. If a timeout occurs while waiting for a message, the sample will exit. X509 authentication is used.
How to interact with the C2D sample:
To send a C2D message: Select your device's "Message to Device" tab in the Azure Portal for your IoT Hub. Enter a message in the "Message Body" and select "Send Message".
### IoT Hub Methods Sample
- *Executable:* `paho_iot_hub_methods_sample`
This [sample](https://github.com/Azure/azure-sdk-for-c/blob/main/sdk/samples/iot/paho_iot_hub_methods_sample.c) receives incoming method commands invoked from the the Azure IoT Hub to the device. It will successfully receive up to 5 method commands sent from the service. If a timeout occurs while waiting for a message, the sample will exit. X509 authentication is used.
How to interact with the Methods sample:
A method named `ping` is supported for this sample.
To invoke a method: Select your device's "Direct Method" tab in the Azure Portal for your IoT Hub. Enter a method name and select "Invoke Method". If successful, the sample will return a JSON payload of the following:
```json
{"response": "pong"}
```
No other method commands are supported. If any other methods are attempted to be invoked, the log will report the method is not found.
### IoT Hub Telemetry Sample
- *Executable:* `paho_iot_hub_telemetry_sample`
This [sample](https://github.com/Azure/azure-sdk-for-c/blob/main/sdk/samples/iot/paho_iot_hub_telemetry_sample.c) sends five telemetry messages to the Azure IoT Hub. X509 authentication is used.
### IoT Hub SAS Telemetry Sample
- *Executable:* `paho_iot_hub_sas_telemetry_sample`
This [sample](https://github.com/Azure/azure-sdk-for-c/blob/main/sdk/samples/iot/paho_iot_hub_sas_telemetry_sample.c) sends five telemetry messages to the Azure IoT Hub. SAS authentication is used.
### IoT Hub Twin Sample
- *Executable:* `paho_iot_hub_twin_sample`
This [sample](https://github.com/Azure/azure-sdk-for-c/blob/main/sdk/samples/iot/paho_iot_hub_twin_sample.c) utilizes the Azure IoT Hub to get the device twin document, send a reported property message, and receive up to 5 desired property messages. If a timeout occurs while waiting for a message from the Azure IoT Hub, the sample will exit. Upon receiving a desired property message, the sample will update the twin property locally and send a reported property message back to the service. X509 authentication is used.
How to interact with the Twin sample:
A desired property named `device_count` is supported for this sample.
To send a device twin desired property message: Select your device's "Device Twin" tab in the Azure Portal of your IoT Hub. Add the property `device_count` along with a corresponding value to the `desired` section of the JSON. Select "Save" to update the twin document and send the twin message to the device.
```json
"properties": {
"desired": {
"device_count": 42,
}
}
```
No other property names sent in a desired property message are supported. If any are sent, the log will report there is nothing to update.
### IoT Plug and Play Sample
This [sample](https://github.com/Azure/azure-sdk-for-c/blob/main/sdk/samples/iot/paho_iot_pnp_sample.c) connects an IoT Plug and Play enabled device (a thermostat) with the Digital Twin Model ID (DTMI) detailed [here](https://github.com/Azure/opendigitaltwins-dtdl/blob/master/DTDL/v2/samples/Thermostat.json). If a timeout occurs while waiting for a message from the Azure IoT Explorer, the sample will continue. If 3 timeouts occur consecutively, the sample will disconnect. X509 authentication is used.
How to interact with the IoT Plug and Play sample:
The easiest way to interact with this sample from the service side is to use Azure IoT Explorer. To use the sample:
- Follow the initial setup instructions described above.
- Install [Azure IoT Explorer](https://github.com/Azure/azure-iot-explorer/#plug-and-play).
- Download [the Thermostat model](https://github.com/Azure/opendigitaltwins-dtdl/blob/master/DTDL/v2/samples/Thermostat.json) to a local directory.
- Build and run `paho_iot_pnp_sample`.
- Start Azure IoT Explorer and then:
- [Configure your hub](https://github.com/Azure/azure-iot-explorer/#configure-an-iot-hub-connection). Once you've created your thermostat device, you should see it listed in the UX.
- Go to `IoT Plug and Play Settings` on the home screen, select `Local Folder` for the location of the model definitions, and point to the folder you downloaded the thermostat model.
- Go to the devices list and select your thermostat device. Now select `IoT Plug and Play components` and then `Default Component`.
- You will now be able to interact with the IoT Plug and Play device.
Additional instructions for Azure IoT Explorer, including screenshots, are available [here](https://github.com/Azure/azure-iot-explorer/#plug-and-play).
### IoT Plug and Play with Provisioning Sample
- *Executable:* `paho_iot_pnp_with_provisioning_sample`
This [sample](https://github.com/Azure/azure-sdk-for-c/blob/main/sdk/samples/iot/paho_iot_pnp_with_provisioning_sample.c) has the same functionality as the `paho_iot_pnp_sample` but uses the Azure Device Provisioning Service for authentication. The same steps above should be followed for interacting with the sample in Azure IoT Explorer.
### IoT Plug and Play Multiple Component Sample
This [sample](https://github.com/Azure/azure-sdk-for-c/blob/main/sdk/samples/iot/paho_iot_pnp_component_sample.c) connects an IoT Plug and Play enabled device simulating a temperature controller directly to Azure IoT Hub. This device is described via the Digital Twin Model ID (DTMI) detailed [here](https://github.com/Azure/opendigitaltwins-dtdl/blob/master/DTDL/v2/samples/TemperatureController.json). X509 authentication is used.
This Temperature Controller is made up of multiple components. These are implemented in the [./pnp](https://github.com/Azure/azure-sdk-for-c/blob/main/sdk/samples/iot/pnp) subdirectory.
- [Device Info](https://devicemodels.azure.com/dtmi/azure/devicemanagement/deviceinformation-1.json)
- [Temperature Sensor 1](https://github.com/Azure/opendigitaltwins-dtdl/blob/master/DTDL/v2/samples/Thermostat.json)
- [Temperature Sensor 2](https://github.com/Azure/opendigitaltwins-dtdl/blob/master/DTDL/v2/samples/Thermostat.json)
- The properties, commands, and telemetry of the Temperature Controller itself.
How to interact with the IoT Plug and Play Multiple Component sample:
The easiest way to interact with this sample from the service side is to use Azure IoT Explorer. To use the sample:
- Follow the initial setup instructions described above.
- Install [Azure IoT Explorer](https://github.com/Azure/azure-iot-explorer/#plug-and-play).
- Download [the Temperature Controller model](https://github.com/Azure/opendigitaltwins-dtdl/blob/master/DTDL/v2/samples/TemperatureController.json) to a local directory.
- Build and run `paho_iot_pnp_component_sample`.
- Start Azure IoT Explorer and then:
- [Configure your hub](https://github.com/Azure/azure-iot-explorer/#configure-an-iot-hub-connection). Once you've created your thermostat device, you should see it listed in the UX.
- Go to `IoT Plug and Play Settings` on the home screen, select `Local Folder` for the location of the model definitions, and point to the folder you downloaded the thermostat model.
- Go to the devices list and select your thermostat device. Now select `IoT Plug and Play components` and then `Default Component`.
- You will now be able to interact with the IoT Plug and Play device.
Additional instructions for Azure IoT Explorer, including screenshots, are available [here](https://github.com/Azure/azure-iot-explorer/#plug-and-play).
### IoT Provisioning Certificate Sample
- *Executable:* `paho_iot_provisioning_sample`
This [sample](https://github.com/Azure/azure-sdk-for-c/blob/main/sdk/samples/iot/paho_iot_provisioning_sample.c) registers a device with the Azure IoT Device Provisioning Service. It will wait to receive the registration status before disconnecting. X509 authentication is used.
### IoT Provisioning SAS Sample
- *Executable:* `paho_iot_provisioning_sas_sample`
This [sample](https://github.com/Azure/azure-sdk-for-c/blob/main/sdk/samples/iot/paho_iot_provisioning_sas_sample.c) registers a device with the Azure IoT Device Provisioning Service. It will wait to receive the registration status before disconnecting. SAS authentication is used.
## Using IoT Hub with an ECC Server Certificate Chain
To work with the new Azure Cloud ECC server certificate chain, the TLS stack must be configured to prevent RSA cipher-suites from being advertised, as described [here](https://docs.microsoft.com/azure/iot-hub/iot-hub-tls-support#elliptic-curve-cryptography-ecc-server-tls-certificate-preview).
When using Paho MQTT for C, modify the samples by adding the following TLS option:
```C
mqtt_ssl_options.enabledCipherSuites = "ECDH+ECDSA+HIGH";
```
## Next Steps and Additional Documentation
Start using the Azure Embedded C SDK IoT Clients in your solutions!
- A general overview of the Embedded C SDK and additional background on running samples can be found in the [Azure SDK for Embedded C README](https://github.com/Azure/azure-sdk-for-c#azure-sdk-for-embedded-c).
- More SDK details pertaining to the Azure IoT Client library can be found in the [Azure IoT Client README](https://github.com/Azure/azure-sdk-for-c/tree/main/sdk/docs/iot#azure-iot-clients).
- The [Azure IoT Client MQTT State Machine](https://github.com/Azure/azure-sdk-for-c/blob/main/sdk/docs/iot/mqtt_state_machine.md) provides a high-level architecture and API information.
- For extensive documentation on Azure IoT Hub, see the [Microsoft API reference documentation](https://docs.microsoft.com/azure/iot-hub/about-iot-hub).
- For extensive documentation on Azure IoT Hub Device Provisioning Service, see the [Microsoft API reference documentation](https://docs.microsoft.com/azure/iot-dps/).
## Troubleshooting
- The error policy for the Embedded C SDK client library is documented [here](https://github.com/Azure/azure-sdk-for-c/blob/main/sdk/docs/iot/mqtt_state_machine.md#error-policy).
- File an issue via [Github Issues](https://github.com/Azure/azure-sdk-for-c/issues/new/choose).
- Check [previous questions](https://stackoverflow.com/questions/tagged/azure+c) or ask new ones on StackOverflow using the `azure` and `c` tags.
## Contributing
This project welcomes contributions and suggestions. Find more contributing details [here](https://github.com/Azure/azure-sdk-for-c/blob/main/CONTRIBUTING.md).
### License
Azure SDK for Embedded C is licensed under the [MIT](https://github.com/Azure/azure-sdk-for-c/blob/main/LICENSE) license.