Developer's introduction to Azure IoT
For the vast quantity of articles, guides and opinions written about the Internet of Things (IoT), it seems much attention goes to shiny, new “Things,” while the humble, foundational “Internet,” waits in the wing for its moment in the spotlight. The truth of the matter is that IoT as we know it today takes an ensemble cast and crew to make the show sing. Behind the scenes, developers are hard at work making any number of decisions on how to connect devices to the cloud, how to determine which protocols can be used, how to secure connections and the best methods of authentication.
This post is for the cast and crew of developers that make any IoT solution work for its intended audience. It’s a guide not just to the “Things,” but to their secure connection to the “Internet” that powers every IoT solution.
What is Azure IoT?
Microsoft’s end-to-end IoT platform is the most complete IoT offering, enabling enterprises to build and realize value from IoT solutions quickly and efficiently. Our IoT solutions, including Azure IoT Suite and Azure IoT Hub, harness the power of our full cloud, data and developer offerings for the enterprise to provide hyperscale IoT services, rich data and analytics, and deep integration. Developers can start building custom solutions using IoT Hub or get started quickly with the comprehensive preconfigured solutions of Azure IoT Suite (which includes Azure IoT Hub). This post will focus mostly on Azure IoT Hub.
To help developers leverage the power of IoT in their endeavors and get started quickly, Azure IoT offers a set of open source SDKs, samples, preconfigured solutions and tools. Microsoft engineers work on these SDKs, tools and samples on GitHub, collaborating with the community and partners in the open to add features; support new languages, protocols, and devices and platforms; fix bugs; and improve performance.
The Azure IoT developer center is a great place to start your journey with Azure IoT, and this article will give you a big picture of what to expect when you jump in.
Develop for Azure IoT Hub
Azure IoT Hub provides an easy and secure way to connect, provision and manage millions of IoT devices sending and receiving billions of messages per month. IoT Hub is the bridge between your devices and their solutions in the cloud, allowing them to store, analyze and act on that data in real time. IoT Hub enables secure, reliable, two-way communication — from device to cloud and cloud to device — over open protocols such as MQTT, HTTPS and AMQPS that are already widely used in IoT.
As a developer you will be looking at three main objectives: connecting devices to IoT Hub, managing the IoT Hub service itself and integrating IoT Hub into your overall IoT solution in the cloud.
To achieve all three, the Azure IoT Hub service exposes REST APIs along with AMQP and MQTT communication support, but implementing communication protocols is not trivial by nature. We’ve got you covered on both the service and the device client sides with open source SDKs and samples that abstract the complexity of these protocols and expose simple and straightforward APIs. All our client SDKs are open sourced on GitHub and offer the following:
- Device client:
- Support for AMQP, AMQP over WebSockets, MQTT and HTTP/REST for the device–to-cloud communication
- Support for SSL (using third-party dependencies such as openSSL or WolfSSL)
- Simple APIs to:
- Establish a secure connection to IoT Hub
- Send messages to IoT Hub
- Receive messages from IoT Hub
- Service client:
- Simple APIs to:
- Manage the device registry (Create, Remove, Update, Delete)
- Read data from IoT Hub
- Send messages to specific devices
For connecting devices that cannot talk to the protocols supported by IoT Hub, there is an open source sample for a cloud protocol gateway that serves as a bridge between the protocol of your choice and IoT Hub.
The diagram below shows where the open source SDKs and samples can be used.
Connecting securely to Azure IoT Hub
Azure IoT Hub offers a secure mechanism for connecting devices.
First is the notion of access control. The Azure IoT Hub service uses a set of permissions to grant access to endpoints. Developers can set up permissions using shared access policies for services, apps and devices and can create per-device security credentials leveraging the device identity registry feature of the service.
Then Azure IoT Hub will authenticate endpoints by verifying a token against the shared access policies and device identity registry security credentials. Security credentials, such as symmetric keys, are never sent over the wire. The security token generation is implemented in the device client SDKs.
Last but not least, all endpoints connect to Azure IoT Hub over TLS, ensuring no endpoint is ever exposed on unencrypted or unsecured channels.
For devices, the device client SDKs implement all of this under the hoods for you, so as a developer you can focus on the development of the actual solution trusting that the plumbing is robust and secured.
Connecting a device is as simple as the below line of C code:
iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, AMQP_Protocol);
For managing the service from an application, the service client SDKs also implement all that’s needed to connect securely to Azure IoT Hub.
To learn more on communication security for Azure IoT Hub, read the security section of the service developer’s guide.
Sending and receiving messages
Once a secure connection is established from devices to Azure IoT Hub, all that’s left to do is to send and receive messages. Azure IoT Hub offers a raw messaging infrastructure, meaning you can put whatever you want and need in the body of your messages.
Sending messages is as simple as the C code snippet below:
msgHandle = IoTHubMessage_CreateFromByteArray(msgText, strlen(msgText));
IoTHubClient_SendEventAsync(iotHubClientHandle, msgHandle, SendConfirmationCallback, NULL);
And if you want to receive messages from Azure IoT Hub on the device, you need to register a callback like in the below C example:
IoTHubClient_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, NULL);
The SDKs also allow to know and notify when a message has been received on the other side.
You will find more details in the various samples in the SDKs repository for each of the languages.
Packages, tools, documentation and samples to make your life easier
In addition to simple APIs, developers will benefit from binary packages, tools, documentation and samples, all part of the SDKs open source repository, to get started faster and implement and debug their solutions efficiently.
In the process of creating and debugging your IoT solution with Azure IoT Hub, you will need to configure access credentials in the IoT Hub service, create device identities, monitor messages coming from devices and send messages to devices to test your code. To achieve these tasks easily, a couple tools are provided:
- iothub-explorer is a cross-platform CLI tool based on node.js that allows CRUD operations on an Azure IoT Hub device registry and sending and receiving messages to and from a specific device.
- Device Explorer is a Windows application that comes with a Windows user interface and allows pretty much the same operations as the iothub-explorer tool.
When it comes to documentation, you can find three types:
- Documentation that is close to the code in the GitHub repository, with articles such as:
- API Reference documentation
- Articles covering more in-depth descriptions of the SDKs, the APIs and the implementations
Last but not least, tons of open source samples are available in the SDKs repository for you to get started faster. Simple samples show, for each language, how each of the supported protocols work to send and receive messages, while more advanced ones show how to connect to Azure IoT Suite or how to implement a simple temperature anomaly detection system. You can find a list of all these samples in the readme file of the project.
End-to-end samples are also published on the Azure samples portal by the broader community that you can get inspired by as well.
Hardware and software requirements
You can connect a wide range of devices directly to Azure IoT Hub. Your device will have to have enough resources to have an IP address and establish a TCP over SSL connection. The device client SDKs support various platforms depending on the languages: microcontrollers, real-time OS, Linux, Windows, etc. You can find an exhaustive list of all the supported OS platforms, HW platforms tested, protocols here.
If you prefer shopping for hardware that already supports Azure IoT connectivity, check out the extensive list of supported platforms along with a list of the boards certified for Azure IoT Hub.
When it comes to the OS you will be developing on, you can work on Windows, Linux or OSX, depending on the devices you are targeting and whether or not you intend to compile the SDKs yourself or use one of the binary packages. You can find details on how to set up your development environment in the various sample instructions.
Developing for Azure IoT Suite
IoT Hub is an amazing, foundational service for a customized IoT solution, but it is just the start. Microsoft goes beyond building-block services by providing preconfigured IoT solutions, so that what used to take weeks for a customer to build can now be automatically provisioned in minutes with Azure IoT Suite. Azure IoT Suite enables customers to quickly provision a working, end-to-end IoT solution that includes IoT Hub as well as other Azure services — and even includes simulated devices to help you get started rapidly prototyping your end-to-end solution with a one-click deploy in minutes.
Developing for Azure IoT Suite is divided in two simple steps:
- Connecting devices to a solution, which consists of connecting devices to Azure IoT Hub and ensure the devices are sending the data in the right format for the IoT Suite solution instance. For each of the Azure IoT Suite preconfigured solutions, you will find samples and instructions for connecting devices.
- Customizing the services deployed as part of the Azure IoT Suite preconfigured solution. All the preconfigured solutions are provided as source code, which allows you to modify, adapt and redeploy every single part of the solution. Extensive documentation can be found here.
To learn more on the development for Azure IoT Suite, check out its developers’ documentation.
And more …
Azure IoT Hub and Azure IoT Suite are obviously not the only things you will want to work with and develop against when creating an end-to-end solution. There are plenty other services you can utilize, benefit from and spin up on Azure to create your IoT solution. A good starting point for a first implementation of an end-to-end IoT solution is Azure IoT Suite, as you will very easily deploy a set of services that are pre-configured and up and running in a few minutes.
This was only the introduction to what Azure IoT has to offer to developers, from securely connecting “Thing” to the “Internet” to sending and receiving messages and debugging your IoT solutions, SDKs, tools, samples and documentation. This will allow you to focus on what counts: your idea for tomorrow’s next big IoT solution.
Visit our Azure IoT developer center, check out some of the introduction videos on IoT Hub or IoT Suite, scan the list of certified hardware, try out some of our getting started instructions and samples, and as always, if you have questions, suggestions or issues, come meet the Azure IoT dev team on GitHub.
Source: Microsoft Azure News