Azure IoT Hub, Microsoft’s core IoT module, is a relative newcomer to the IoT Platform landscape (only publicly released in February of this year). This module mainly adds device management capabilities to Azure, and so it would become the main point of entry for any IoT deployment. The IoT Hub acts as an extension of Azure’s Event Hub, basically a messaging bus triggered by device activity. Read on for a tutorial on how you might tie into the IoT Event Hub on any Azure deployment and get data flowing to Scriptr, where you’d be able to take advantage of our web-based Javascript development environment, widgets, and connectors.
Getting Started
If you don’t already have an application on Azure, spinning up an Azure account and an IoT Hub is straightforward, which is all you’ll need to see how data moves through the system. A walkthrough is here which covers creating an IoT Hub and getting your connection string which you’ll need next.
Azure doesn’t provide a UI for device management, so you have to use API’s or the CLI to add even a single device for evaluation/testing. The iothub-explorer package is available via NPM and will get you through this step relatively simply.
npm install -g iothub-explorer
iothub-explorer login "yourConnectionStringHere" //available from Shared Access Policies
iothub-explorer create test-device
Azure will then return the credentials/connection string for this newly registered test device. To start pushing data, you’ll need to use a client library (Microsoft todo: support pushing test messages from the CLI).
Microsoft provides a simple http client via node. My compadre Mika Vatanen created a node service that uses this and pushes data to an Azure device channel when invoked. You can check it out here and use it to generate test data. As a bonus, Mika got an Emotiv unit running and shows how to invoke this service from the Emotiv SDK with this example.
#emotiv demo: sending brain waves data to #Azure during a 2 days hackathon #builtinflux #iothub pic.twitter.com/uQPsav8h6N
— Microsoft Flux (@MSFTFlux) September 3, 2016
Hint: To get a feel for the depth and breadth of services that could be implemented as part of an Azure application, Microsoft has set up a dedicated onboarding system that allows for a one-click deployment of entire sample projects, complete with test data runners. These can be quite overwhelming at first if you aren’t familiar with Azure to begin with, but still worth looking at. You do this from a separate site after you have your Azure account: http://azureiotsuite.com
Meanwhile, on the other side of IoTHub…
Once device messages are going into IoTHub, we want to get them flowing to Scriptr.io. We will do this by triggering the execution of a “Function App” inside Azure which will send the data via HTTP.
Once you’ve created a Function App service, you’ll be able to create a new Function with a template called “EventHubTrigger – Node”. This will allow you to trigger the execution of the Node container with every event of a specific Event Hub. In this case, we want to use the IoT Hub that we previously created, which comes with an “Event Hub Compatible Endpoint”.
You’ll need to use this information to build a Connection String to input into the Function App’s setup. You build it like so (Microsoft todo: provide this for the user automatically):
Endpoint=eventHubCompatibleEndpoint;SharedAccessKeyName=iotHubPolicyName;SharedAccessKey=iotHubPolicyKey
Endpoint=sb://iothub-ns-biggie-63524-d99asdfg7650.servicebus.windows.net/;SharedAccessKeyName=iothubowner;SharedAccessKey=kMpcfeIPDJHeRJu5R2WEyOP2xmkeXkA= //example
Again, Function Apps are just node containers. So, Azure allows us to manipulate the file structure of those containers, which we can use to create a package.json to bring in dependencies. In this case, we want to bring in the “request” module, like so:
Azure also provides us the ability to bring up a console into the container environment. We can use this to run NPM and bring in the dependencies.
We can now build out our Function App to POST to a Scriptr.io script. Use the following index.js for your Function App:
var request = require('request'); module.exports = function (context, myEventHubTrigger) { context.log('Node.js eventhub trigger function processed work item', myEventHubTrigger); var endpoint = "http://mySubDomain.scriptrapps.io"; var path = "/message"; var params = { method: "POST", url: endpoint + path, body: JSON.stringify(myEventHubTrigger) }; request(params); context.done(); };
Data is flowing! From Scriptr you’ll see activity from Azure instantly and can take start taking advantage of our web-based Javascript development environment to quickly build a system for stream analytics, embeddable widgets, or mashups with external services.