All Collections
Developer Guides
Connecting Azure IoT with Ubidots (Part I)[DEPRECATED]
Connecting Azure IoT with Ubidots (Part I)[DEPRECATED]

How to configure Azure IoT to send data to Ubidots

S
Written by Sergio M
Updated over a week ago


IMPORTANT NOTE

THIS ARTICLE WAS DEPRECATED ON JANUARY 5 2023.


Ubidots has many prebuilt features designed to get you faster to market, including an user interface that's ready to be put in front of your end-customers. If your project's requirements demand an additional backend layer, then Microsoft Azure IoT offers additional tools to boost your project, including:

  • Device management (Reboot, Reset, Config, Update, Status)

  • Device connectivity incl. MQTT and AMQP protocols

  • Data processing and predictive analytics

  • Workflow automations and integrations

Before beginning with Azure we recommend you check out the "Developer's Introduction to Azure IoT." Check out this reference for Azure's documentation.

In this article, we will show how one can build an IoT solution working with the Azure's resources integration and Ubidots. We will simulate a device using node.js to serve as an example. We recommend following this entire tutorial using this simulation and then applying the acquired knowledge to connect your own device to Azure. 

Table of Contents

Microsoft Azure setup...

  1. Resource Group

  2. Create an IoT Hub

  3. Register a device

  4. Setup your device

  5. Create a Service Bus namespace and add queue

  6. Add an endpoint and routing rule

  7. Create and configure a logic app

  8. Create an HTTP request

Sending values from Azure to Ubidots...

  1. Node.js simulation device code

  2. Logic app

  3. Visualization

Microsoft Azure setup

Azure IoT

The Azure platform is a cloud computing service created by Microsoft for building, testing, deploying, and managing applications and services through a global network of Microsoft-managed data centers. Similar to many of the development environments that already exist, Azure begins with a dashboard to custom-fit to your work and style, thus staying on top of the things that matter most by pinning them to the dashboard.

Azure Integration

To begin the integration we have to create a "Resource group"; the resource group provides a way to monitor, control access, provision, and manage billing for collections of assets that are required to run an application.

  • Resource group

1.- Select the icon shown on the image below to create the resource group:

2.- Press "add" to open the resource group builder. Next, you have to assign a resource group name, select the subscription, and location. Please assign "ubidots" as the name; if you wish to visualize your resource groups to your dashboard select "Pin to dashboard":

3.- Once you've created the resource group, you need to complete the process on the flowing page by clicking "Create resources". For a more in-depth look into resources, check out this link to Microsoft's resources.

  • Create an IoT Hub

The first resource that we have to add is the IoT Hub; the Azure IoT Hub is a fully managed service that helps enable reliable and secure bi-directional communications with IoT devices. 

1.-  Search "IoT Hub" within the search bar, then select the IoT Hub option to open the create options.

In the following window select "Create" to create the IoT Hub:

2.- Once the IoT Hub builder is created and opened, assign a name and set the prices, units, resource group, and location of your IoT Hub. Make sure that the resource group selected is the one just created.

Pro Tip: check out the following link for specific terminology of Azure's IoT Hub setup. 

4.- Once the IoT Hub is created, copy the hostname and save it in a separate notepad for later. The hostname makes a connection string that communicates with your IoT Hub. See below for where to identify the hostname in your Hub.

5.- Next, in the left-hand pane of your hub, you will find your application's categories and properties. 

  • Select the "Shared access policies" option, and click the "iothubowner" policy.

  • Copy and save your "Connection string" of your IoT Hub into your notepad with hostname. The connection strings define the permissions for devices and services to connect to your IoT Hub.

  • Register a device in the IoT Hub

1.- Open your IoT Hub, and click "Device Explorer" from the left-hand pane. 

Within the Device Explorer window click "Add" to add a device to your IoT Hub; the Device Explorer runs on your local machine and connects to your IoT hub in Azure. It communicates with these IoT Hub endpoints.

2.- After selecting "+Add" (displayed above) assign the parameters below:

  • Device ID: my-first-device 

  • Authentication type: select "Symmetric key"

  • Auto Generate Keys: select this check box

  • Connect device to IoT Hub: click "Enable"

Pro Tip: We recommend using the same nomenclature as this tutorial until you feel more comfortable assigning custom identifiers to your IoT Hub. Once more comfortable, feel free to adjust your application as needed. 

Once you've added all the parameters press "Save."

3.- After the device is created, open the Device Explorer, select the device in the bottom-left and and copy the "Connection String - primary key" to your separate notepad. As with previous identifiers, this information will be valuable later. 

  • Set up your device

Now that you have finished the device management setup in Microsoft's Azure portal, you're now able to setup real or simulated device to verify D2C (device-to-cloud) communication.

1.- To setup your device, install the Azure IoT device SDKs needed for your chosen IoT device, go to the Microsoft documentation to download the appropriate SDKs.

NOTE: If you don't have an IoT device, don't worry as this tutorial will guide you through a device simulation using node.js. If you wish you use a simulated device, please follow the steps in the link provided below and return to this tutorial once complete. 

Now that you've enabled the simulated device app to send device-to-cloud messages with your IoT Hub, you can now scale your IoT solution.

  • Create a Service Bus namespace and add a queue

The second resource that we have to add is the Service Bus. When two or more parties want to exchange information they need a communication facilitator. Service Bus is a brokered or third-party communication mechanism. Following Microsoft's documentation, the Service Bus is a multi-tenant cloud service. Each user, such as an application developer, creates a namespace, then defines the communication mechanisms needed within that namespace.

1.- Add a new resource into your resource group previously created called "ubidots." This will point you to a search location where you will search for the resource "Service Bus."

2.- Search "Service Bus" within the search bar, then select the Service Bus option to open the parameters prompt in the next step. 

3.- Once the Service Bus builder is open, assign a name and set the prices, subscription, resource group, and location of your IoT Hub. Make sure that the resource group selected is the one previously created.

  • Name: We decided called "ubidots-queue" but custom nomenclature is appropriate when you are comfortable working in Azure's development.

  • Pricing tier: Click Basic > Select. The Basic tier is sufficient for this tutorial.

  • Resource group: Use the same resource group as your IoT Hub. 

  • Location: Use the same location as your IoT Hub.

Once you've assigned the information required, "Create" your Namespace.

4.- Next, back within the Service Bus press "+ Queue" to establish data communication between the IoT Hub and a "Logic App" that will be added later this tutorial.

Assign a name for the queue and then click "create" using the appropriate parameters of your queue.


5.- From the Resource tab, open the service bus queue, and then click Shared access policies > + Add.

6.- Next, enter a name for the policy, check Manage, and then click "Create".

  • Add an endpoint and a routing rule

The endpoints and routes option contains many features, but with this tutorial we will be using it to integrate with other services, like the logic app. When IoT Hub receives a message it first checks to see if there is a custom route (endpoint with a routing rule) available that matches a given rule. If there is a match it will send the message to that endpoint and not the Default Event Hub.

1.- Open your IoT Hub, click Endpoint, then press "+ Add"

2.- In the endpoint builder assign the parameters below:

  • Name: The name of the endpoint.

  • Endpoint type: Select Service Bus Queue

  • Service Bus namespace: Select the previously created namespace.

  • Service Bus queue: Select the queue you've previously created.

To finish - click "OK":

3.- Now we're going to add a routing rule. In your IoT Hub, click Routes, then press "+ Add".

4.- In the routes builder, assign the parameters below:

  • Name: The name of the routing rule.

  • Data source: Select DeviceMessages.

  • Endpoint: Select the endpoint you've previously created.

  • Query string: Enter "true" 

To finish, click "Save"

  • Create and configure a logic app

The third resource that we have to create is the Logic App; logic apps provide a way to simplify integrations and workflows to/in the cloud.

1.- Add a new resource into your resource group previously created called "ubidots"

2.- Search "Logic App" within the search bar, then select the Logic App option to open and create the option.

3.- Once the Logic App builder is open, you have to assign a name and set the prices, subscription, resource group, and location. Make sure that the resource group selected is the one previously created and the location is the same of the IoT Hub.

  • Name: The name of the logic app.

  • Resource group: Use the same resource group that your IoT hub uses.

  • Location: Use the same location that your IoT hub uses.

To finish, just click "create".

4.- From your Resources tab, select the logic app to open the Logic Apps Designer.

 In the Logic Apps Designer, scroll down and click Blank Logic App:

5.- Search and select "Service Bus":

Then, trigger it to the specific trigger titled: "Service Bus - When one or more messages arrive in a queue (auto-complete)"

6.- Next, enter a connection name and select the bus service previously created:

Then "Create" your action.

Once you've created the service bus connection, select the queue that was recently created and enter 175 for Maximum message count. We opted for 175 for this tutorial, but if a greater or lesser message count is needed in your application, please adjust appropriately.

Then, click "Save" button to save these changes.

  • Create an HTTP request for data transmission

1.- Create an HTTP request to send data to Ubidots. Working from the Logic App Designer from the previous step, create all HTTP request by selecting "+ New Step".

In the Click New step > Add an action, search "HTTP" within the search bar as you can see below, then select the HTTP option:

Select the first option "HTTP - HTTP":

Now you have to assign the parameters needed to make the HTTP Request to Ubidots. 

  • Method: POST 

  • Url: https://things.ubidots.com/api/v1.6/devices/azure 

  • Headers:

  1. x-auth-token  | Ubidots token 

  2.  content-type  | application/json 

  • Body: Content

NOTE: If you don't how get your Ubidots TOKEN, please reference this article How to get your Ubidots TOKEN. For more documentation about the Ubidots API reference click here

Once your parameters are entered and accurate, "Save" and "Run" your Logic App. 

Testing your setup with Node.js

As previously stated, we decide to use a simulated device using node.js. We recommend starting with the guide below and code below which serves as practice before your real device setup:

1.- Once you've finished the device simulation setup above and are able to handle data with your IoT Hub, modify the code called "SimulatedDevice.js" with the code below. Remember to assign the device's connection string saved from your notepad into the code below. 

Note: If you are working with a live device, please utilize the SDKs of your chosen device with this link

'use strict';

var clientFromConnectionString = require('azure-iot-device-mqtt').clientFromConnectionString;
var Message = require('azure-iot-device').Message;

var connectionString = '{Your device string connection}';

var client = clientFromConnectionString(connectionString);

function printResultFor(op) {
  return function printResult(err, res) {
    if (err) console.log(op + ' error: ' + err.toString());
    if (res) console.log(op + ' status: ' + res.constructor.name);
  };
}


var connectCallback = function (err) {
  if (err) {
    console.log('Could not connect: ' + err);
  } else {
    console.log('Client connected');

    // Create a message and send it to the IoT Hub every second
    setInterval(function(){
        var temperature = 20 + (Math.random() * 15);
        var humidity = 60 + (Math.random() * 20);
var data = JSON.stringify({ temperature: temperature, humidity: humidity });
        var message = new Message(data);
        console.log("Sending message: " + message.getData());
        client.sendEvent(message, printResultFor('send'));
    }, 5000);
  }
};

client.open(connectCallback);

Once you've modified the code with the new one, run you new code in your computers terminal using the following command: node SimulatedDevice.js .You should see the response below in your terminal, if successful. 

Now you're sending temperature and humidity values to azure, please go to the Logic app to visualize the messages received and the status of the request HTTP to Ubidots.

2.- In the logic app overview you will be able to monitor and check your logic app's run and trigger history. To learn more about your logic app's status or run and trigger history, see troubleshoot your logic app.

Selecting a message from your run history you can verify the INPUT and OUTPUT of the HTTP request to Ubidots:

As you can see above the status code of the output is 200 which means Ok – Successful request.  

3.- To visualize the data received in Ubidots, go to your Ubidots account and select the device section. Here you will see a new device called "azure".

Result

This guide introduces us to the Azure IoT world which provides many resources and features for your Internet of Things solutions. 

In this tutorial we learned how to send data from Azure to Ubidots. To achieve bi-directional communication, please refer to our next article to see how send data from Ubidots to Azure IoT, where you will also learn how to store the data received from Ubidots into an SQL data storage along the way. 

Happy Hacking :) 

Did this answer your question?