Reader small image

You're reading from  Azure IoT Development Cookbook

Product typeBook
Published inAug 2017
Reading LevelIntermediate
PublisherPackt
ISBN-139781787283008
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Yatish Patil
Yatish Patil
author image
Yatish Patil

Yatish Patil is currently working with Saviant Consulting as a technical project manager. He has delivered enterprise IoT and analytics applications using Microsoft Azure, ASP.NET, MVC, C#, SQL Server, and NoSQL. He has diverse industrial experience in IT and has worked in a variety of domains, such as utilities, manufacturing, and engineering. He has completed his certification in Developing Azure Solutions in the Microsoft Azure Certification. Yatish was also the technical reviewer for a Microsoft Azure-based technology book Microsoft Azure IaaS Essentials, which teaches guides Microsoft Azure subscribers how to design, configure, and build cloud-based infrastructure using Microsoft Azure. Yatish was among the industry speakers at India IoT Symposium, 2016. He delivered the industry session on remote asset monitoring with Microsoft Azure IoT Suite.
Read more about Yatish Patil

Right arrow

IoT Hub Messaging and Commands

In this chapter, you will learn the following recipes:

  • Messaging - device-to-cloud
  • Processing device-to-cloud messaging
  • Messaging - commands and control
  • File uploads with IoT Hub
  • Device firmware updates

Introduction

IoT Hub provides messaging, which is reliable and durable, by providing the following two types of communication between devices and IoT backend solutions:

  • Send device-to-cloud messages
  • Send cloud-to-device messages

IoT Hub implements at least once delivery guarantees for the messages. That means, when using device-to-cloud and cloud-to-device messaging, it will deliver the messages at least once. IoT Hub currently supports different devices: facing protocols mostly known as MQTT, AMQP, and HTTPS.

IoT Hub has defined a common message format, so that all device-facing protocols the communication work seamlessly. To make sure our devices are running as we expect them to, we will also want to be notified of our IoT device's state, which can be either be running or stopped. IoT Hub is built on technologies like event hubs and service bus to facilitate this device...

Messaging - device-to-cloud

The IoT Hub device-to-cloud messaging, which is referred to as telemetry data, is implemented as a streaming messaging pattern. The telemetry can be sensor data collected by the IoT device, along with the timestamp and metadata for the device. IoT Hub exposes the endpoints that will enable the device-to-cloud messages. The default route is /devices//messages/events, which is compatible with event hubs. Once the IoT Hub is configured for message routing, the routing rules engine then routes your messages to one of the service-facing endpoints on your IoT Hub which are sent from the IoT devices, example a temperature sensor is sending data, suppose the temperature falls below 25 C it will be routed to custom endpoints.

IoT Hub also provides the message routing mechanism while sending the device-to-cloud message. When we have many IoT devices out in the...

Processing device-to-cloud messaging

Cloud-to-device messages are important for any IoT device, which are mainly focused on some feedback or maintenance of these IoT devices. We send cloud-to-device messages through a service-facing endpoint (/messages/devicebound). A device receives them through a device-specific endpoint (/devices/{deviceId}/messages/devicebound) and takes the appropriate action on the device side based on the logic developed for the device.

These communications make the device bidirectional; some examples of bi-directional commands could be to change the configuration for a device, these fields can be time interval the device sends the data, updating some local model, size information.

How to do it...

We...

Messaging - commands and control

Any IoT device which is capable of receiving messages from the cloud makes a bidirectional command execution easy. The device can receive the message and process upon the instruction on the hardware side. Once the action is taken, an acknowledgement is sent to the cloud about the action completion. With the IoT Hub SDK for the device and the backend application, we can achieve this. We must follow the following steps:

How to do it...

In this section we will send a cloud-to-device message. Upon receiving it, the IoT device will perform an action:

  1. Send a cloud-to-device message from a cloud solution to an IoT device:
ServiceClient serviceClient;
serviceClient = ServiceClient.CreateFromConnectionString...

File uploads with IoT Hub

In most communication from IoT devices, it will send a relatively small message to IoT Hub, (up to 4 KB max allowed message size), such as telemetry data, readings, and so on. However, sometimes, we might have the need to send larger files, such as images, log files, or batch telemetry data in some scenarios.

IoT Hub provides a file ingestion method, which helps to solve this problem. Instead of sending messages through IoT Hub itself, IoT Hub instead acts as a dispatcher to an associated Azure storage configured account in IoT Hub. It uploads the files to the storage account, and then sends a notification message informing that the file ingestion has been completed. The following image is from the Microsoft Azure IoT documentation that shows how file ingestion works:

File ingestion

Devices can initiate file uploads by sending a notification through...

Device firmware updates

The backend app is responsible for initiating a firmware update for the device through a direct method that it can run on the IoT device. The device then acts and downloads the firmware file, runs the firmware update, and finally reconnects to the IoT Hub service. Throughout the process, the IoT device uses the reported properties to update the progress and status of the device.

How to do it...

In this topic, we are going to create a C# simulator, which will use the device twin and direct methods to send the firmware update to device:

  1. Create C# console IoT application.
  2. Add a Azure.Devices package for NuGet.
  3. Trigger the remote update to update the device firmware:
client = ServiceClient.CreateFromConnectionString...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Azure IoT Development Cookbook
Published in: Aug 2017Publisher: PacktISBN-13: 9781787283008
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Yatish Patil

Yatish Patil is currently working with Saviant Consulting as a technical project manager. He has delivered enterprise IoT and analytics applications using Microsoft Azure, ASP.NET, MVC, C#, SQL Server, and NoSQL. He has diverse industrial experience in IT and has worked in a variety of domains, such as utilities, manufacturing, and engineering. He has completed his certification in Developing Azure Solutions in the Microsoft Azure Certification. Yatish was also the technical reviewer for a Microsoft Azure-based technology book Microsoft Azure IaaS Essentials, which teaches guides Microsoft Azure subscribers how to design, configure, and build cloud-based infrastructure using Microsoft Azure. Yatish was among the industry speakers at India IoT Symposium, 2016. He delivered the industry session on remote asset monitoring with Microsoft Azure IoT Suite.
Read more about Yatish Patil