Thursday, January 14, 2016

Pushing Telemetry data to Azure IoT Suite from Windows 10 IoT Core

Hi Folks,

Microsoft is providing an Open Source SDK to address the Azure IoT Hub and Suite, that is compatible Linux, Windows and OSLess systems for various languages such as C, C#,...
The repository is accessible on GitHub : https://github.com/Azure/azure-iot-sdks/

Using this SDK to push data to the Azure IoT Suite default Solution from a Windows 10 IoT Core Universal Application, is not straight forward, as there is no C# version of the remote-monitoring application.



Pre-requisites

1- Create an AzureIoTSuite solution
2- Create a Device
To add a new device to your remote monitoring solution, complete the following steps in the solution dashboard:
  1. In the lower left-hand corner of the dashboard, click Add a device.
  2. In the Custom Device panel, click on Add new.
  3. Choose Let me define my own Device ID, enter a Device ID such as mydevice, click Check ID to verify that name isn't in use, and then click Create to provision the device.
  4. Make a note the device credentials (Device ID, IoT Hub Hostname, and Device Key), your client application will need them to connect your device to the remote monitoring solution. Then click Done.
  5. Make sure your device displays correctly in the devices section. The status is Pending until the device establishes a connection to the remote monitoring solution.
  6. Check the device details and write down the following properties
Device ID = ----IoT Hub Hostname = ----.azure-devices.netDevice Key = ------




Develop an application

Create a new Universal App project
1- File -> New -> Project
2- Select Templates -> Visual C# -> Windows -> Universal -> Blank App (Universal Windows)
3- Choose a name : IoTCoreDemo

Nuget Package Installation

1- Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution
2- Select Browse
3- Check the Include prerelease
4- Search for Microsoft.Azure.Devices.client
5- Hit Install and Acknowledge

Modify the application

1- Expand MainPage.xaml in the Solution Explorer window
2- Edit file MainPage.xaml.cs
3- Add missing dependencies to Azure SDK classes and other required namespaces
<
 // Azure SDK classes  
 using System.Text;  
 using System.Threading.Tasks;  
 using Microsoft.Azure.Devices.Client;  

3- Add ReportValueToAzure function in the MainPage class
 async void ReportValueToAzureAsync()
{

 }  
4- Add Azure IoT Suite device configuration
   // Device Parameters  
   // ==> Need to be adjusted  
   string iotHubUri = "-----.azure-devices.net";  
   string deviceID = "------";  
   string deviceKey = "u5YnmWlmP+L/WPAGTublYg==";  
5- Define a device client that can connect the Azure Iot Suite using the device configuration
   // Azure Client creation  
   var deviceClient = DeviceClient.Create(  
     iotHubUri,  
     AuthenticationMethodFactory.CreateAuthenticationWithRegistrySymmetricKey(deviceID, deviceKey),  
     TransportType.Http1);  

6- Create the message to send to the service for temperature reporting
   // Message string  
   var str = "{\"DeviceId\":\"" + deviceID + "\", \"Temperature\":" + (double)50.0) + ", \"Humidity\":" + (double)(45.0) + ", \"ExternalTemperature\":" + (double)(55.0) + "}";  

7- Convert string message into a Azure IoT Hub Message
   // Azure Message creation  
   var message = new Message(Encoding.ASCII.GetBytes(str));  

8- Send the message
   // Transmission of the Message  
   await deviceClient.SendEventAsync(message);  
9- Then the function should look like this :

 async void ReportValueToAzureAsync()  
 {  
   // Device Parameters  
   // ==> Need to be adjusted  
   string iotHubUri = "----.azure-devices.net";  
   string deviceID = "----";  
   string deviceKey = "u5YnmWlmP+L/WPAGTublYg==";  
   // Azure Client creation  
   var deviceClient = DeviceClient.Create(  
     iotHubUri,  
     AuthenticationMethodFactory.CreateAuthenticationWithRegistrySymmetricKey(deviceID, deviceKey),  
     TransportType.Http1);  
   // Message string  
   var str = "{\"DeviceId\":\"" + deviceID + "\", \"Temperature\":" + (double)50.0) + ", \"Humidity\":" + (double)(45.0) + ", \"ExternalTemperature\":" + (double)(55.0) + "}";  
   // Azure Message creation  
   var message = new Message(Encoding.ASCII.GetBytes(str));  
   // Transmission of the Message  
   await deviceClient.SendEventAsync(message);  
 }  

10- Create a Task that will process the sending of the data
 async void LaunchTask()  
 {  
   while (true)  
   {  
     ReportValueToAzureAsync();  
     await Task.Delay(TimeSpan.FromSeconds(1));  
   }  
 }  

11- Launch the task from the constructor
 public MainPage()  
 {  
   this.InitializeComponent();  
   this.LaunchTask();  
 }  
And that's it, you have an application ready to publish data to the Azure IoT Suite.

Enjoy!

-Nicolas

No comments:

Post a Comment