Connect IoT to Azure IoT with Visual Studio Connected Service Extension
- Posted: Mar 18, 2016 at 6:00AM
- 1 comment
Loading user information from Channel 9
Something went wrong getting user information from Channel 9
Loading user information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
In our last Visual Studio Extension Theme Week post, how do we include not only VS Extensibility but also device/hardware/IoT?
HA! We've got not one, but two posts to highlight!
On the Azure IoT team, a key part of our mission is to make it as easy as possible for developers to connect devices to IoT Hub. We’ve been continually improving the developer experience since we first launched the public preview in September 2015, but there’s still room to do more.
Say hello to the VS Connected Service for IoT Hub
Today, we’re releasing a Visual Studio extension that takes away the hassle of dealing with the connection strings and other details when connecting your application to IoT Hub. With the VS Connected Service for Azure IoT Hub, you can connect your application to IoT Hub in seconds (literally!), without ever leaving Visual Studio.
You can install the extension from the Visual Studio Gallery. Alternatively, you can find it from the Visual Studio by navigating to Tools, Extensions and Updates, Online.
Type “Azure IoT” in the search box on the right. Download the extension and restart the Visual Studio if prompted to do so. To use the extension, just follow these instructions.
The extension generates some “boiler plate” code you can invoke from your application. That code would be different for each application type. For C#, you will see a new file AzureIoTHub.cs added to your project.
The class AzureIoTHub defined in this file has two methods: SendDeviceToCloudMessageAsync and ReceiveCloudToDeviceMessageAsync. These methods contain everything necessary to send messages to and receive from Azure, and you can call them directly from elsewhere in your application. Of course, you should treat the code in these methods merely as a jumping-off place for you to write your own logic.
A note on the connection details
For your convenience, the Connected Service for Azure IoT Hub inserted the deviceConnectionString variable containing the shared access key required to connect to your instance of IoT Hub on behalf of the chosen device. Keep in mind, anyone who comes into the possession of this information will be able to send and receive messages on behalf of that device. Therefore, it is recommended you remove this string from the source code before committing your code into a source control. Consider storing it in a configuration file, an environment variable, or other solutions.
Feedback? We’re listening!
We believe this extension will reduce the hassle of connecting your applications to Azure IoT. That said, we can do so much more! For example, it would great if the extension allowed you to create instances of Azure IoT Hubs, avoiding the need to do this in the Azure portal. Have other ideas? Head over to GitHub and submit a proposed enhancement, or report a bug.
A key part of being productive is having great tools. For developers, this can make the difference between a successful app that delights our users and a project that never got off the ground.
In our last post, we talked about writing a UWP app that connects to the Azure IoT Hub. We said that we’re far from done in our mission to make cloud connectivity easy and accessible. So we went back to work and built a tool that we think you’re going to love.
Say hi to Connected Service for Azure IoT Hub
Previously, connecting to Azure IoT Hub involved getting the connection string from the Azure Portal, feeding that to another tool such as the iothub-explorer to create a device identity, then using the device-specific connection string in your code. You also needed to figure out what API to pass this connection string to, and so on.
To make this process simpler, we built a new tool – the Connected Service for Azure IoT Hub, a Visual Studio extension that takes away the hassle of dealing with connection strings and other error-prone details.
You can download the extension from the Visual Studio Gallery here: http://aka.ms/azure-iot-hub-vs-cs-vs-gallery. Simply click the Download button and follow the prompts to install the extension.
The extension supports C# and C++ languages, but we’re looking at supporting other project types in the future.
add it to your project.
take a closer look at the generated code in AzureIoTHub.cs. The file contains a class, AzureIoTHub with two static functions, SendDeviceToCloudMessageAsync and ReceiveCloudToDeviceMessageAsync.
The functions create a device client and send a simple message to the device specified in the connection string, which is also generated in the file:
Did it work?
There are several ways to verify that your message has arrived at its destination. You could build a simple app as described in our previous post (scroll to the end of the post). Another option is to use the iothub-explorer tool (also introduced in that post):
iothub-explorer <yourConnectionString> monitor-events MyDevice
We hope you find the Connected Service for Azure IoT Hub useful. As always, your feedback is welcome – leave your comments here or head over to our GitHub page and submit an issue.