This article came up from the need to have a guidance step-by-step for Azure beginners on the IoT integration and from the guidance from Ranga Vadlamudi

Goals

  • Stream data to time series insights
  • Stream data to cosmos db using azure stream analytics
  • Code for cosmos db change feeds and invoke azure functions

alt text

Overview

alt text

Steps

Connect to wifi

  • Put the device into AP mode by doing the following:
  • Press and hold the B button
  • With the B button held down, press and release the Reset button
  • Release the B button
  • Verify that an SSID and an IP address appear on the device screen. The IP address is the one that we will use to connect to the device from our laptop.

alt text 

alt text 

alt text

alt text

Create IoT Hub:

Go to the Azure Portal, click Create New Resource and type Iot Hub,

alt text

Select IoT Hub and click create

alt text

Select Create IoT hub

alt text

alt text

Set up Time Series Insight

Now we will create a Time Series Insight service.

Go to the Azure Portal, click on Create New Resource and type Time Series Insights as shown below:

alt text

Then select Time Series Insights: 

alt text

Click create: 

alt text

Now we will provide the environment parameters as follows:

  • Environment name: gab2018-cdmx
  • Subscription - your Azure Pass / Subscription
  • Resource group: gab2018-cdmx
  • Location: West Europe
  • SKU: S1

alt text

**Note: In case we are using an Azure Pass, please review the cost $150 per month!

alt text

Then Check the pin to the dashboard and click create.

It is important to have a consumer group for TSI (Time Series Insights), go to the IoT Hub, click Endpoints, select Events 

alt text

Now we will create a New Consumer Group and then click save:

alt text

Now go back to Time Series Insights, click on Event Sources and Click on Add:

alt text

We will provide the following parameters to create the Event Source:

  • Event source name: gab2018cdmx
  • source: IoT Hub
  • Import Option: user IoT Hub from available subscription
  • Subscription ID. VS Enterprise / Azure Pass
  • IoT Hub name: gab2018-cdmx
  • IoT Hub policy name: iothubowner
  • IoT consumer group: gabevents
  • Serialization format: json

Click create

alt text 

alt text

Now go to overview and click go to the environment:

alt text

**Note: We will add data access policies later. We will not be able to access the data in the environment with no data access policies defined.

We will see a screen like below:

alt text

Create a Stream analytics Job

Now go back to the Azure Portal, we will create a Stream analytics Job. Go to Create New Resource, type Stream analytics:

alt text

Select stream analytics job: 

  1. Provide the following parameters:

  • Job Name: gab2018-cdmx
  • Subscription: VS enterprise / Azure Pass
  • Resource Group: gab2018-cdmx
  • Location: West Europe
  • Hosting environment: cloud

Note: if already connected the device to Wifi we could try Cloud, otherwise try using Edge and connect the device to Wifi later on

alt text

Create a cosmos db account.

Now we will create a cosmos db account.

Select create new resource, type cosmos db, select Azure Cosmos DB:

alt text

alt text

alt text

Click create:

Provide the following parameters:

  •  • ID: gab2018cdmx
  •  • API: SQL
  •  • Subscription: VS Enterprise / Azure Pass
  •  • Resource group: gab2018cdmx
  •  • Location: West Europe

Click Create

It is important to highlight that we should try to deploy all the resources in the same location to avoid future issues within the service availability

alt text

Create a Logic App.

Now we will create a Logic App.

Go to Create new Resource, type logic app

alt text

Select Logic App:

alt text

Click on Logic App, Create:

alt text

Now provide the following parameters:

  • Name. gab2018cdmx
  • Subscription: VS Enterprise
  • Resource Group. Gab2018 cdmx
  • Location: West Europe
  • Log Analytics: Off (for this case we are going to enable them later on)

Click Create

alt text

Now go back to the azure stream analytics job, from the blade select Input:

alt text

The input for this will be the IoTHub we created in the resource group

Select Add stream input, then select IoT Hub:

alt text

Provide the following parameters:

  • Input alias
  • Select IoT Hub from the subscription
  • Subscription
  • IoT Hub (Azure will automatically provide us the available IoT Hub)
  • Endpoint
  • Shared access policy name
  • Shared access policy key
  • Consumer Group
  • Event serialization format (we could select None, CSV, Json, etc.) Then click save

alt text

Now we will generate the Output.

Generate output

This output will be the Cosmos DB we created in the resource group

Go to the Stream Job blade and select Output:

alt text

Now click Add and select Cosmos DB.

alt text

Provide the following parameters:

  • Output alias
  • Select the Option: "Select Cosmos DB from your subscription"
  • Select the subscription
  • Account ID
  • Account key
  • Database
  • Collection name pattern
  • Document ID

alt text

Please Note:

If the chosen resource and the stream analytics job are located in different regions, we will be billed to move data between regions.

 Now we will define a Stream Job Function.

Go to the stream job blade and click on Overview, then click on Edit query:

alt text

 Copy and paste the following query:

SELECT [deviceId] AS [deviceId], avg(temperature) AS avgtemp INTO CosmosDB FROM mvp-iothub GROUP BY deviceId, TumblingWindow(second, 15);

Now click save:

alt text

Create an Event Hub which will receive data from Azure Function.

Now we will Create an Event Hub which will receive data from Azure Function.

TIP: Take a note of keys and name.

Go to create Resources and type Event hub:

alt text

Now select the event hubs and click create:

alt text

alt text

Provide the following parameters and click create:

  • Name
  • Pricing tier
  • Subscription
  • Resource Group
  • Location
  • Throughput units
  • Enable auto-inflate
  • Specify upper limit to 20

alt text

Once provisioned we will create a new event hub:

alt text

Also, create a SAS policy for this event hub:

alt text

Create an Azure Function.

Now Create an Azure Function.

Go to create New Resource, type functions:

alt text

Select Functions App: 

alt text

Click create: 

alt text

Now Provide the following parameters:

  • App name
  • Subscription
  • Resource Group
  • OS: Windows
  • Hosting Plan: Consumption Plan
  • Location: West US
  • Storage: Create New
  • Application Insights: ON

alt text

Use Azure Functions on the Azure Portal.

Go to the Azure portal, we will select the function and click on Add then we will select Azure Cosmos DB Trigger:

alt text

alt text

Now click on New and confirm this new account connection is related to our previously created Cosmos DB

alt text

alt text

Replace with the following code:

#r "D:\home\site\wwwroot\CosmosTriggerCSharp1\bin\Microsoft.Azure.Documents.Client.dll"
#r "D:\home\site\wwwroot\CosmosTriggerCSharp1\bin\Microsoft.Azure.EventHubs.dll"
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using Microsoft.Azure.Documents;
using Microsoft.Azure.EventHubs;
public static void Run(IReadOnlyList<Document> documents, TraceWriter log){
if (documents != null && documents.Count > 0) {
string connectionString = ConfigurationManager.ConnectionStrings["EventHubConnection"].ConnectionString;
log.Verbose(connectionString);
var connectionStringBuilder = new EventHubsConnectionStringBuilder(connectionString) {
EntityPath = "eventhub"
};
var client = Microsoft.Azure.EventHubs.EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
foreach (var doc in documents) {
string json = string.Format("{{\"iotid\":\"{0}\",\"temp\":{1}}}", doc.GetPropertyValue<string>("iotid"),
doc.GetPropertyValue<string>("temp"));
EventData data = new Microsoft.Azure.EventHubs.EventData(Encoding.UTF8.GetBytes(json));
client.SendAsync(data);
}
}
}

Now click on the function, then select platform features, then click on Kudu Console:

alt text

We will be redirected to another tab in our browser to manage our Azure Function:

https://gab2018-cdmx.scm.azurewebsites.net/

Click on debug console, cmd:

alt text

Navigate to function directory by double clicking on wwwroot and then create a bin directory using the “+” sign.

alt text

alt text

Go to Bin directory by double clicking on it and then add the following DLL. Get these DLL from nuget

alt text

alt text

Repeat this for each of the following:

*Note: We can user 7zip and extract the nupkg to quickly get the DLLs.

Once we downloaded these packages go back to AppSettings Click on AppSettings

On another tab open the Event Hub and get the Connection String: 

alt text

Copy the connection-primary-key:

alt text

Now go back to our Azure Function, select Application Settings and click on add connection string:

alt text

Then provide a Connection Name, then paste the connection string and select Connection Type as Custom

alt text

Now go up and click Save:

alt text

Now go back to our Logic App. Select "When events are available in EventHub"

alt text

Then provide a name and select the Event Hub. 

alt text

Now click Create. 

alt text

Now provide the interval and frequency to check for events coming in:

alt text

Now Click new step then click add action 

alt text

Look for "Send an email"

alt text

We will need to authorize our account, Office 365 for this case,then setup the details for the email:

alt text

Other Languages: