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
In this chapter, you will learn the following recipes:
IoT Hub provides messaging, which is reliable and durable, by providing the following two types of communication between devices and IoT backend solutions:
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...
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...
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.
We...
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:
In this section we will send a cloud-to-device message. Upon receiving it, the IoT device will perform an action:
ServiceClient serviceClient;
serviceClient = ServiceClient.CreateFromConnectionString...
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:
Devices can initiate file uploads by sending a notification through...
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.
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:
client = ServiceClient.CreateFromConnectionString...