All Collections
Connect your Devices
Connecting Azure IoT Hub with Ubidots. Part I: Sending data from IoT Hub to Ubidots
Connecting Azure IoT Hub with Ubidots. Part I: Sending data from IoT Hub to Ubidots

This article explores how to re-route your device's data on Azure IoT Hub to Ubidots

Written by Sergio M
Updated over a week ago


Table of contents

1. Create an Azure IoT Hub account

  • Log into your Microsoft Azure account.

  • Click on IoT Hub. If it is not displayed, you can search for it on the search bar at the top of the screen.

Component 6(1).png
  • Click on “Create”.

Component 7.png
  • Fill in the IoT Hub project details in the following way:

    • Subscription: Select your Azure subscription.

    • Resource group: Click the “Create new” button. A small window will be displayed, enter a meaningful name and click the “OK” button. This is the resource group in which you’ll place all of the other resources, so be aware of it. We named it as “UbidotsIoTHubResources”.

    • IoT Hub name: set any name that you find appropriate. We named it "UbidotsIotHub".

    • Region: Choose any region that suits your needs. The only thing you have to keep mind is that you’ll have to create all of the subsequent resources in this same region. In this article, “Central US” was selected.

    • Click on “Review + create”.

    • Click on “Create”.

      Component 8(1).png
  • You’ll be taken to a new page, like the following one. Wait until the deployment finishes and click the “Go to resource” button, this will take you to your recently created IoT Hub.

    Component 9(1).png

2. Create a Device on Azure IoT Hub

  • Once on your recently created Azure IoT Hub, click on “Devices” at the left side of the screen.

  • Click on “+ Add device”.

    Component 10.png
  • Set a name for your device. We named it as “raspberry-simulator”. However, you can choose any name.

  • Click on the “Save” button.

    Component 11(1).png
  • A new page will load where you’ll be able to see all of your created devices. Click on the device that you just created.

  • Copy the “Primary Connection String” to a notepad or any easy-to-access place.

    Component 12.png

3. Create Azure Service Bus

  • Add the “Service Bus” resource by searching for it on the search bar at the top of the screen or by using the “+ Create a resource” button, just as in the first step when you created the IoT Hub.

  • Click on “+Create”.

    Component 13.png
  • Fill in the following fields as it follows:

    • Subscription: Choose your active Azure subscription.

    • Resource group: Choose the same resource group as you did for the IoT Hub resource. If you have been following the same nomenclature, then it should be “UbidotsIoTHubResources”.

    • Namespace name: set any name that you find appropriate. In this article, it was set to “UbiNameSpace”.

    • Location: Choose the same location in which you placed your first resource. In this case, it is “Central US”.

    • Pricing tier: Choose any plan that suits your needs.

  • Click on “Review + create”.

  • Click on “Create”.

    Component 14.png
  • Wait for the deployment to finish.

  • Click on “Go to resource”.

Component 15.png
  • Click on “+ Queue”.

  • Set an appropriate name for your Queue. In this article it was set to “ubiqueue”, however, you can choose any name that you want.

  • Set “Max delivery count” according to your traffic needs.

  • Leave all the other parameters as their default options.

  • Click the “create” button.

  • Wait for the Queue to be created.

    Component 16.png
  • Scroll down until you find your recently created Queue and click on it.

  • Click on "Shared access policies".

  • Click on “+ Add”.

  • Set a name for your policy.

  • Check the “Manage” option.

  • Click on “Create”.

    Component 18.png

4. Create an Endpoint and Routing Rule

  • Go to your Ubidots account and create an UbiFunction. Its method should be “POST” and the runtime “Python”. You can name it as you wish. Visit this link to know more about this.

  • Copy your “Function’s HTTP’s endpoint URL”. This information will be used in later steps.

  • Head back to your IoT Hub.

  • Search for “Message routing” at the menu on the left side of the screen.

  • Click on the “Custom endpoints” tab.

  • Click on “Add”.

  • Click on “Service Bus queue”.

    Component 19(2).png
  • Set a name for your endpoint according to your needs.

  • Choose the “Service Bus namespace” created in the previous steps.

  • Choose the “Service Bus queue” created in the previous steps.

  • Leave all the other settings as they are and click on “Create”.

Component 20.png
  • Head back again to Azure IoT Hub.

  • Click on “Message routing”.

  • Click on the “Routes” tab.

  • Click on “Add”.

    Component 21.png
  • Set a name for the route.

  • On the “Endpoint” section, choose the previously created endpoint.

  • As “Data source” choose “Device Telemetry Messages”.

  • Make sure that “Routing query” is set to “true”.

  • Click on the “Save” button.

    Component 22.png

5. Create Azure Logic App

  • Go to the search bar at the top of the screen and search for “Logic apps”.

  • Click on “Add”.

    Component 23.png

  • Fill in the following information according to:

    • Subscription: Select your previously used subscription.

    • Resource Group: Choose the same resources group as in the previous steps.

    • Logic App name: Set any name that you find appropriate.

    • Region: Select the same region you have been choosing for all of the previous resources.

    • Enable log analytics: Mark this option according to your needs.

    • Plan type: Select this option according to your needs.

  • Click “Review + create”.

  • Click “Create”.

    Component 24.png
  • Wait for the deployment of the resource to be completed.

  • Click on “Go to resource”.

    Component 25.png
  • Scroll down until you find the “Blank Logic App” and click on it.

    Component 26.png
  • Use the search bar to look for “Service Bus” and click on it.

    Component 27.png
  • Select “When one or more messages arrive in a queue” option.

    Component 28.png
  • For “Queue name”, select the queue created in the previous steps. If you have been following the nomenclature of this article, it should be “ubiqueue”.

  • For “Maximum message count”, select it according to your needs.

  • For “How often do you want to check for items”, select the frequency that matches your needs.

  • Click on “+ New step”.

    Component 29.png
  • Use the search bar to look for “HTTP" and click on it.

    Component 30.png
  • Click on the “HTTP” option.

    Component 31.png
  • Select the HTTP method to “POST”.

  • On the URL field, fill it with your UbiFunction’s “Function’s HTTP’s endpoint URL”, as obtained in the first step of this section.

  • Add both the “x-auth-token” header and your Ubidots token in the corresponding section.

  • Click on the “Body” section and drag the “Content” block to it.

  • Click on “Save”.

    Component 32(1).png

6. Configure Ubidots Decoder

  • Go to the UbiFunction created in step 4, specifically to its Decoder section.

  • Delete all the code there and paste the following:

import requests
import time
import base64
import json

device = "Raspberry-pi-4-Azure"
token = "BBFF-WEplLU79KGBNVrLUt3lGEFIuOviAOg"

def main(args):

base64_payload = args["body"]
decoded_payload = base64.b64decode(base64_payload)
decoded_payload = decoded_payload.decode('utf-8')
decoded_payload = json.loads(decoded_payload)
ubidots_payload = {}
ubidots_payload["temperature"] = decoded_payload["temperature"]
ubidots_payload["humidity"] = decoded_payload["humidity"]
timestamp = decoded_payload["timestamp"]

res = send_data(device, ubidots_payload, token, **{"timestamp":timestamp})
##return {"status": res.status_code, "text": res.text} return buildResponse(200,"successfull request")
return buildResponse(res.status_code, res.text)

def buildResponse(status_code, output_text):
return {
"status_code": status_code,
"headers": {"Content-Type": "plain/text"},
"body": f"{output_text}",

def send_data(device, payload, token, **params):
url = f"{BASE_URL}/api/v1.6/devices/{device}/"
headers = {"X-Auth-Token": token, "Content-Type": "application/json"}
res = make_request("post", url, headers=headers, params=params, body_json=payload, attempts=5)
return res

def make_request(
Function to make a request with timeout and retries

req_session = requests.Session()
req_adapter = requests.adapters.HTTPAdapter(max_retries=attempts)
req_session.mount("https://", req_adapter)

response = req_session.request(


return response
  • Click on the “Make it live” button.

7. Configure Raspberry Pi Simulator

  • Go to the following link.

  • Edit line 15 and, there, set your device’s Primary Connection String” as obtained in section 2.

  • Add the following under line 31: timestamp:

  • It should look like this:

    Component 34.png
  • Click “Run” in order to start sending data to Azure and, ultimately, to Ubidots.

8. Check Data ingestion Both on Azure and Ubidots

  • Go to Azure.

  • Go to the Logic App created in step 5.

  • Click the “Runs history” tab.

  • Check that the app has been triggered.

    Component 35.png
  • Head back to Ubidots → Devices and you’ll be able to see a device named “raspberry-pi-4-azure” (if you followed the nomenclature).

    Component 36.png

9. Feedback, Suggestion and Related Articles

Feel free to post questions or suggestions in our community portal, or contact us via

Other users also found helpful...

Did this answer your question?