Azure IoT Development Cookbook
eBook - ePub

Azure IoT Development Cookbook

  1. 254 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Azure IoT Development Cookbook

About this book

Over 50 recipes to drive IoT innovation with Microsoft AzureAbout This Book• Build secure and scalable IoT solutions with Azure IoT platform• Learn techniques to build end to end IoT solutions leveraging the Azure IoT platform• Filled with practical recipes to help you increase connectivity and automation across IoT devicesWho This Book Is ForIf you are an application developer and want to build robust and secure IoT solution for your organization using Azure IoT, then this book is for you.What You Will Learn• Build IoT Solutions using Azure IoT & Services• Learn device configuration and communication protocols• Understand IoT Suite and Pre-configured solutions• Manage Secure Device communications• Understand Device management, alerts• Introduction with IoT Analytics, reference IoT Architectures• Reference Architectures from Industry• Pre-Configured IoT Suite solutionsIn DetailMicrosoft's end-to-end IoT platform is the most complete IoT offering, empowering enterprises to build and realize value from IoT solutions efficiently. It is important to develop robust and reliable solutions for your organization to leverage IoT services.This book focuses on how to start building custom solutions using the IoT hub or the preconfigured solution of Azure IoT suite. As a developer, you will be taught how to connect multiple devices to the Azure IoT hub, develop, manage the IoT hub service and integrate the hub with cloud. We will be covering REST APIs along with HTTP, MQTT and AMQP protocols. It also helps you learn Pre-Configured IoT Suite solution.Moving ahead we will be covering topics like: -Process device-to-cloud messages and cloud-to-device messages using.Net-Direct methods and device management-Query Language, Azure IoT SDK for.Net-Creating and managing, Securing IoT hub, IoT Suite and many more. We will be using windows 10 IoT core, Visual Studio, universal Windows platform. At the end, we will take you through IoT analytics and provide a demo of connecting real device with Azure IoT.Style and approachA set of exciting recipes of using Microsoft Azure IoT more effectively.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Information

Azure IoT Communication Protocols

In this chapter, you will learn the following recipes:
  • Hyper Text Transfer Protocol Secure (HTTPS)
  • Advanced Message Queuing Protocol (AMQP)
  • Using AMQP library to communicate with IoT Hub
  • Message Queuing Telemetry Transport Protocol (MQTT)
  • IoT protocol gateway
  • Using MQTT .NET library to communicate with IoT Hub
  • Connecting IoT Hub using MQTT client tools
  • How to choose between protocols

Introduction

Accessing IoT Hubs is easy now that SDKs have been made available by Microsoft. These are open source and can be easily downloaded from GitHub. There are multiple language supports for using these SDKs. We will be using C# samples in this chapter to create a simulator and communicate with IoT Hub, followed by receiving these in another console's IoT solutions.
We can use HTTP, AMQP, or MQTT protocols. To connect with other platforms, we can use the protocol gateway that implements the custom communication; it is nothing but a framework. One of the options for implementing it is a cloud gateway, where we accept the data as a cloud service and then ingest it into the IoT Hub.

Hyper Text Transfer Protocol Secure (HTTPS)

HTTPS is one of the application protocols used for communicating between device-to-cloud and cloud-to-device. When you are using HTTP, devices poll IoT Hub for messages.
The HTTP protocol works for the command/response IoT system between the IoT device and the cloud application.

How to do it...

We will create a simulator and ingest data to IoT Hub:
  1. Create a device simulator to send a message to the IoT:
 deviceClient = DeviceClient.CreateFromConnectionString("HostName=IoTHubCookBook.azure-devices.net;DeviceId=myFirstDevice;SharedAccessKey=XXXXXXXXXXXXX", TransportType.Http1);

new DeviceSimulator().SendDeviceToCloudMessagesAsync(deviceClient, "myFirstDevice");
  1. Send IoT Hub messages using HTTP:
 public async void SendDeviceToCloudMessagesAsync(DeviceClient deviceClient1, string deviceId) public async void SendDeviceToCloudMessagesAsync(DeviceClient deviceClient1, string deviceId) {
double avgWindSpeed = 10; // m/s
Random rand = new Random();
double currentWindSpeed = 0;
int i = 0;
while (i<10)
{
currentWindSpeed = avgWindSpeed + rand.NextDouble() * 4 - 2;
var telemetryDataPoint = new
{
deviceId = deviceId,
windSpeed = currentWindSpeed,
highTemp = 72.3,
lowtemp = 11.2,
latitude = "17.5122560",
longitude = "70.7760470"
};
var messageString = JsonConvert.SerializeObject(telemetryDataPoint);
var message = new Message(Encoding.ASCII.GetBytes(messageString));
await deviceClient1.SendEventAsync(message);
i += 1;
await Task.Delay(1000);
}
}
  1. Lets use the following code block to process the message ingested into IoT Hub:
 readonly string iotHubD2cEndpoint = "messages/events";readonly string iotHubD2cEndpoint = "messages/events";
eventHubClient = EventHubClient.CreateFromConnectionString(AzureIoTHub.GetConnectionString(), iotHubD2cEndpoint);
var d2cPartitions = eventHubClient.GetRuntimeInformation().PartitionIds;
String data = "";
foreach (string partition in d2cPartitions)
{
var result = ReceiveMessagesFromDeviceAsync(partition);
data = result.Result.ToString();
if (data != "")
return data;
}
private async static Task<String> ReceiveMessagesFromDeviceAsync(string partition)
{
var eventHubReceiver = eventHubClient.GetDefaultConsumerGroup().CreateReceiver(partition, DateTime.Now);
while (true)
{
EventData eventData = await eventHubReceiver.ReceiveAsync();
if (eventData == null) continue;
var data = Encoding.UTF8.GetString(eventData.GetBytes());
return data;
}
}

How it works...

IoT Hub SDK provides easy options to select between the communication protocols that we would like to make the device communicate with the IoT Hub cloud-based solutions. In this case, the simulator code is making use of IoT device SDKs to send the telemetry data using AMQP protocol. We need to define the transport type while making the connection. These SDKs will be required on the device side to be implemented to use the Azure IoT device SDK.
Once the IoT device gets connected with the IoT Hub and starts ingesting the data, it is available for IoT solution to pull these messages and process some data that could need to transformed before it proceeds further to the next step. Sometimes, it can be used through the stream analytics to provide real-time analytics on top of the data ingested.
Microsoft Azure has launched a new service Azure Time Series Analytics, which is yet another powerful PaaS offering to work with time series data analytics.

Advanced Message Queuing Protocol (AMQP)

AMQP is considered to be a secure and reliable IoT protocol; it is also an advanced protocol. AMQP is mostly used in business messaging. AMQP works well for enterprise applications and server-to-server communication.

How to do it...

We will create a simulator application to ingest messages using AMQP protocol:
  1. Create a device simulator to send a message to IoT Hub:
 deviceClient = DeviceClient.CreateFromConnectionString("HostName=IoTHubCookBook.azure-devices.net;DeviceId=myFirstDevice;SharedAccessKey=XXXXXXXXXXXXX", TransportType.Amqp);

new DeviceSimulator().SendDeviceToCloudMessagesAsync(deviceClient, "myFirstDevice");
  1. Send IoT Hub messages using AMQP:
 public async void SendDeviceToCloudMessagesAsync(DeviceClient deviceClient1, string deviceId) public async void SendDeviceToCloudMessagesAsync(DeviceClient deviceClient1, string deviceId) {
double avgWindSpeed = 10; // m/s
Random rand = new Random();
double currentWindSpeed = 0;
int i = 0;
while (i<10)
{
currentWindSpeed = avgWindSpeed + rand.NextDouble() * 4 - 2;
var telemetryDataPoint = new
{
deviceId = deviceId,
windSpeed = currentWindSpeed,
highTemp = 72.3,
lowtemp = 11.2,
latitude = "17.5122560",
longitude = "70.7760470"
};
var messageString = JsonConvert.SerializeObject(telemetryDataPoint);
var message = new Message(Encoding.ASCII.GetBytes(messageString));
await deviceClient1.SendEventAsync(message);
i += 1;
await Task.Delay(1000);
}
}
  1. Process the message ingested into IoT Hub:
 readonly string iotHubD2cEndp...

Table of contents

  1. Title Page
  2. Copyright
  3. Credits
  4. About the Author
  5. About the Reviewers
  6. www.PacktPub.com
  7. Customer Feedback
  8. Preface
  9. Getting Started with the Azure IoT Platform
  10. Introducing Device Management
  11. IoT Hub Messaging and Commands
  12. Azure IoT Communication Protocols
  13. Azure IoT Hub Security and Best Practices
  14. IoT Suite and Pre-Configured Solutions
  15. Azure IoT Analytics
  16. Using Real Devices to Connect and Implement Azure IoT Hub
  17. Managing the Azure IoT Hub

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Azure IoT Development Cookbook by Yatish Patil in PDF and/or ePUB format, as well as other popular books in Computer Science & Computer Networking. We have over one million books available in our catalogue for you to explore.