- 254 pages
- English
- ePUB (mobile friendly)
- Available on iOS & Android
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.
Frequently asked questions
Information
Azure IoT Communication Protocols
- 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
Hyper Text Transfer Protocol Secure (HTTPS)
How to do it...
- 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");
- 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);
}
}
- 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...
Advanced Message Queuing Protocol (AMQP)
How to do it...
- 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");
- 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);
}
}
- Process the message ingested into IoT Hub:
readonly string iotHubD2cEndp...
Table of contents
- Title Page
- Copyright
- Credits
- About the Author
- About the Reviewers
- www.PacktPub.com
- Customer Feedback
- Preface
- Getting Started with the Azure IoT Platform
- Introducing Device Management
- IoT Hub Messaging and Commands
- Azure IoT Communication Protocols
- Azure IoT Hub Security and Best Practices
- IoT Suite and Pre-Configured Solutions
- Azure IoT Analytics
- Using Real Devices to Connect and Implement Azure IoT Hub
- Managing the Azure IoT Hub