This folder contains Azure IoT Device Management samples written in C#
To get started exploring the C# language samples for Device Management:
- Open the Visual Studio solution
- Run a Nuget Package Restore
- Copy
config/config.default.yaml
toconfig/config.yaml
* ... please see Configuration below for details* - Open
config/config.yaml
and enter: * The IoT Hub HostName * The IoT Hub Connection string * A unique DeviceId & Nickname - Set the CreateDeviceIdentity.csproj as the start-up project.
- Run the solution.
- Confirm that the
Key
setting for the configured device is now populated in theconfig/config.yaml
file.
This C# solution includes a number of samples that demonstrate the basic Device Management functionality of the Azure IoT Hub. Each of the solution/simulations listed below will walk you though a happy path usage of IoT Hub and its associated SDKs.
Begin by opening the C# Visual Studio solution and kickoff a Nuget Restore.
Each project in the solution leverages a shared configuration file config/config.yaml
, which you will notice is missing from
the config
folder. We've excluded the configuration file used at runtime to help prevent accidental pushes of secure keys
to source control, so you'll need to copy config.default.yaml
to config.yaml
before entering configuration values.
Keep in mind that the config.yaml file is serialized and deserialized from an object
model that is included in the Azure.IoTHub.Examples.CSharp.Core
project and may be read and
written to from any number of projects in this solution. \
At the bare minimum, you will need to add configuration information for the IoT Hub, including host name and connection strings and create a unique DeviceId to tests with. Please follow these handy guides for setting up an Azure Resource Group and IoT Hub:
With the resource group and IoT Hub created:
- copy the primary connection string into the
config.yaml->ConnectionString
field.
- copy the hub host name into the
config.yaml->Hostname
field.
Lastly, set a unique DeviceId
in the config.yaml
file and proceed to the Create a Device Identity
project.
- Create a Device Identity
- Delete a Device Identity
- Push Data to Azure IoT Hub
- Read Data Pushed to Azure IoT Hub
- Send Notification from Azure Iot Hub to Device
- Send Command to Device to Upload File
- Pushing Different Data Types
This example utilizes the connection string and device id of the config.yaml
file to configure
a device in your Azure IoT Hub. After running the project with the debugger, the device's PrimaryKey
field in the config.yaml
file will be updated for use in other simulations. A list of properties
that can be configured for your test device can be found on the Iot Hub Guide for Developers
This example performs the following steps:
- Create a device identity in your Azure IoT Hub if it does not already exist based on the
config.yaml
file. - Display a list of configured devices (should be at least 1 device after step 1).
- Remove the device configured in the
config.yaml
file. - Display a list of configured devices (if there are any).
- Add the device configured in teh
config.yaml
file back into your Azure IoT Hub.
See the public static async Task RemoveDevice(Device device)
method in Program.cs
for the RegistryManager
call to remove configured devices.
This example is a simulated IoT device that posts semi-randomized wind measurements to the configured Azure IoT Hub. This project is used in most of the following simulations as the data pump and has several features to facilite the testing of different data schema.
In this example we use two projects,DeviceSimulator
and ReadDeviceToCoudMessages
to initiate traffic
to the Azure IoT Hub and then read the pushed data back. This simulation might represent a streaming aggregation
or monitoring applciation for connected devices. To run this simulation you will need to make sure that the device
configured in the config.yaml
file, has been added to the Azure IoT Hub using the Create a Device Identity
project and the solution has been rebuilt if the config.yaml
was updated.
Once the simulator device has been added to your Azure IoT Hub, you will want to set both the DeviceSimulator
and the ReadDeviceToCoudMessages
projects as Startup Projects, instructions here.
Running the debugger will start both projects and you should see two console windows popup displaying data
flowing to and being read from your Azure IoT Hub.
In this example we use two projects, the DeviceSimulator
and the SendCloudToDeviceMessages
to initiate traffic
from the Azure IoT Hub to the device, having the device then ACK the received commands. This simulation represents a naive SCADA
implementation for remote control of devices. To run this simulation you will need to make sure that the device
configured in the config.yaml
file, has been added to the Azure IoT Hub using the Create a Device Identity
project and the solution has been rebuilt if the config.yaml
was updated.
Once the simulator device has been added to your Azure IoT Hub, you will want to set both the DeviceSimulator
and the SendCloudToDeviceMessages
projects as Startup Projects, instructions here.
Running the debugger will start both projects and you should see two console windows displaying data
flowing to the Azure Iot Hub from the device simulator, and a prompt to send control messages to the device
from the SendCloudToDeviceMessages
console.
Suggestion: play with sending multiple messages quickly and see how messages are pulled from the hub byt the controler.
In this example we will use two projects, the DeviceSimulator
and the SendCloudToDeviceMessages
to explore
uploading of bulk observation data from our simulated device. The simulation might represent the transfer of data collected
during a network outage or perhaps aggregate statistics collected by the device. To run this simulation you will need to make sure that:
- the device configured in the
config.yaml
file, has been added to the Azure IoT Hub using the Create a Device Identity project - A configured storage account has been attached to your Azure IoT Hub for use with your Azure IoT Hub and, create or select a default storage container.
- Turn ON 'Receive Notification for uploaded files' under the Azure IoT Hub blade.
- The IoT Hub Storage Conatiner name has been added to the
config.yaml
- the solution has been rebuilt, if the
config.yaml
was updated.
With the simulator device registered and blob storage attached to the hub, set both the DeviceSimulator
and the SendCloudToDeviceMessages
projects as Startup Projects, instructions here.
Running the debugger will start both projects and you should see two console windows displaying data
flowing to the Azure Iot Hub from the device simulator, and a prompt to send control messages to the device
from the SendCloudToDeviceMessages
console. In the SendCloudToDeviceMessages
terminal type the phrase
upload bulk data
and press Enter
to request that the device upload a file to blob storage. Once the file
has uploaded the SendCloudToDeviceMessages
will display a receipt notification.
The DeviceSimulator
project includes several demo data generation functions for use in exporing other Azure products
such as Azure Stream Analytics. To utilize these functions
open the project's Program.cs
file, find the Data Generator Functions
section and uncomment the examples to send
JSON or CSV data form the simulated device to the IoT Hub.
This project is licensed under the MIT License