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.
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.
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.
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.
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
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”
Now we need to add a NuGet package named Microsoft.Azure.Devices
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.
We run the program and we have the device registered. And we also have the DEvice Key, we need to store this key.
Now we can see one device registered in the Azure portal in our IoT Hub “IoTGarminLabs”
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.
A couple of lines of code will allows us to display the received messages.
Note: Again, is important to type the correct connection string.
Finally we need to create a 3rd Console app to publish the messages. My data for this app is
NuGet packages: Microsoft Azure Devices Client
With the following code, where is important to use the Information generated with the ConsoleAzureIot01 app.
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.
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
Objective: Send messages with a button
The fist time I run the app I get the following error
Message=Amqp protocol is not supported
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
I’ve also created another app to receive the messages with the following configuration
Objective: Receive messages and display Toast notification into the Windows 10 Action Center
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
- AZURE Getting Started with Azure IOT Hub for .Net
- NuGet Microsoft Azure IoT Service SDK
- NuGet Microsoft Azure Service Bus
- NuGet Microsoft Azure Devices Client
- NuGet Azure SB Lite from Paolo Patierno
- Paolo Patierno Azure IoT Hub Get Telemetry data using AMPQ stack and Azure SB Lite
- MSDN Quickstart: Sending a local toast notification and handling activations from it (Windows 10)
- NuGet Notification Extension Win 10
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?
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.
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
all the code I’ve write for the samples is on GitHub on https://github.com/elbruno/Blog/tree/master/Azure
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
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.