#AZURE – #AzureIoT Hub Sample using Windows Universal Apps #Windows10

Hi !

So, after a couple of week learning on how to use Garmin ConnectIQ SDK, now I got my Garmin device ready to interact with the cloud. In order to prepare my Azure IoT environment, I’ll write a couple of posts on the basic steps to perform this. This post is also based on an article in the Azure portal which have plenty of additional details on Azure IoT Hub also.

The final output of the post will be a couple of Windows Universal Apps sending and receiving information as you can see in the next video.

2016 02 28 Azure Iot Wua Samples

Create the Azure IoT Hub

Let’s start from the beginning. First of all, we need an Azure Account (or a trial one). From the Azure Portal, we need to create an AzureIot Hub.

image

I’ll name this one IoTGarminLabs

Important: The default value for the price and scale is S1, if you are going to use this for a test, change it to F1 Free.

image

After a couple of minutes, the IoT Hub will be created. If there is any problem with the deploy, the alert section will display a nice message with all the details about it.

image

Once you get the correct configurations, the IoT hub  will be created. The important information to be used later is the Host Name: <YourIoTHubName>.azure-devices.net and also the credentials and connection string. To access this information

  • Click on the key on the toolbar
  • Click on the iothubowner policy
  • Copy the connection string primary key value

image

The

Create sample Visual Studio Console App projects

So now is time to go to Visual Studio and create a Console App to connect and test the HUB. I’ll create the main console app named “ConsoleAzureIoT01

image

Now we need to add a NuGet package named Microsoft.Azure.Devices

image

So, as the tutorial explains the first step is to create a device identity for our device in the IoT Hub. With the following lines we can perform this device registration

Note: Replace the IoT Connection String (line 11) and choose the Device Name (line 12) as your own choice.

image

We run the program and we have the device registered. And we also have the DEvice Key, we need to store this key.

image

Now we can see one device registered in the Azure portal in our IoT Hub “IoTGarminLabs

image

Ok, next step in the Azure IoT Hub tutorial is to create another Console App to read device-to-cloud messages from the IoT Hub. I created a Console app named “ConsoleAzureIoT02”, and later I added the “WindowsAzure.ServiceBus” NuGet package.

image

A couple of lines of code will allows us to display the received messages.

Note: Again, is important to type the correct connection string.

image

Finally we need to create a 3rd Console app to publish the messages. My data for this app is

Name: ConsoleAzuireIot03

NuGet packages: Microsoft Azure Devices Client

image

With the following code, where is important to use the Information generated with the ConsoleAzureIot01 app.

image

If we setup the app to run both the 02 and 03 app we will the send and receive flow of messages every one second. And if we go back to the Azure portal, we will see the messages we send during the execution time.

image

Sample using Visual Studio Windows Universal App projects

So, as a sample this works great however I want to make a test with some other technologies so I added 2 new Windows Universal App projects to the solution.

The first app to receive the messages has the following configuration

Name: WuaAzureIotSendMessage

Objective: Send messages with a button

image

The fist time I run the app I get the following error

System.NotImplementedException occurred
HResult=-2147467263
IsTransient=false
Message=Amqp protocol is not supported
Source=Microsoft.Azure.Devices.Client
StackTrace:
at Microsoft.Azure.Devices.Client.DeviceClient.CreateFromConnectionString(String connectionString, TransportType transportType)

There seems to be a problem with WUA and AMQP (I still don’t have my Raspberry PI 2 here in Toronto to test this with Windows IoT Core). So after a quick review I realize that I need to overwrite the default connection message in the Windows Universal App to sent messages, because AMQP was not supported. I changed the device creation line, and I explicit used the HTTP transport protocol for this type of apps. Line 22

image

I’ve also created another app to receive the messages with the following configuration

Name: WuaAzureIotReceiveMessage

Objective: Receive messages and display Toast notification into the Windows 10 Action Center

image

For this app I realize that I can’t use the ,WindowsAzure.ServiceBus, package because is not supported in Universal Apps (glup!). So I used Azure SB Lite pack from the great Paolo Patierno to manage the receive event from Azure Service Bus.

And later when the app receives a message, a new Toast notification is displayed using the Notification Extension Win 10 NuGet package

And now is the moment to go back to the beginning and review the video with the 3 apps sending and receiving messages.

Greetings @ Toronto

-El Bruno

References

13 comments

  1. Hi,
    I’m new in this and I’m trying to make UWP app for receiving messages from Azure IoT hub, and I use your example code from git hub, but problem is in these lines of code

    var factory = MessagingFactory.CreateFromConnectionString(Config.ConnectionStringAzureSbLite);
    var client = factory.CreateEventHubClient(Config.EventHubCompatibleName);

    Where can I find out what is my ConnectionStringAzureSbLite and what is my EventHubCompatibleName?

    Like

    1. Hi Emin

      thanks for writing, the connection string and additional information are in the Azure Portal, you must go to Settings, and later to Messaging to access this information.

      Regards
      -El Bruno

      Like

  2. Hi
    you almost saved my life 😀

    please I want the code sample for the UWP app that receives the message data from the partition you have sent the message to.

    Thanks in advance

    Like

  3. On Xamarin Andriod i am Event Hub for receive data from IOT HUB.How can we receive data from particular device.

    For Eg if we have devcie001 and device002.I have Receive telmetry from IoT hub for only Devcie001 and telmetry from IoT hub for only Devcie002 using Event Hub or is there any other way.

    Thanks and Regard Ramesh kumar

    Like

    1. Hi Ram

      I don’t think I got your question. However, if you are asking to use a single Event Hub to receive information from different devices, you can do this 😀
      I hope this helps.

      Regards
      -Bruno

      Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.