Requirements
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.
Click on “Create”.
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”.
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.
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”.
Set a name for your device. We named it as “raspberry-simulator”. However, you can choose any name.
Click on the “Save” button.
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.
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”.
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”.
Wait for the deployment to finish.
Click on “Go to resource”.
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.
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”.
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”.
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”.
Head back again to Azure IoT Hub.
Click on “Message routing”.
Click on the “Routes” tab.
Click on “Add”.
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.
5. Create Azure Logic App
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”.
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”.
Use the search bar to look for “HTTP" and click on it.
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”.
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
BASE_URL = "https://industrial.api.ubidots.com"
device = "Raspberry-pi-4-Azure"
token = "BBFF-WEplLU79KGBNVrLUt3lGEFIuOviAOg"
def main(args):
print(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(
method,
url,
params=None,
headers=None,
body=None,
body_json=None,
attempts=1,
timeout=10,
):
"""
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(
method=method.upper(),
url=url,
params=params,
headers=headers,
data=body,
json=body_json,
timeout=timeout,
)
req_session.close()
response.raise_for_status()
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:
timestamp: Date.now(),
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.
Head back to Ubidots → Devices and you’ll be able to see a device named “raspberry-pi-4-azure” (if you followed the nomenclature).
9. Feedback, Suggestion and Related Articles
Feel free to post questions or suggestions in our community portal, or contact us via support@ubidots.com.
Other users also found helpful...