Azure Data Manager for Agriculture - Sensor Telemetry Data

71 views Asked by At

I'm able to follow each and every step of the following URL regarding sensor integration as both parner and customer.

https://learn.microsoft.com/en-us/azure/data-manager-for-agri/how-to-set-up-sensor-as-customer-and-partner

I've sent my sensor data using IoThub device connection string. Post this how can I check my telemetry data using REST API and how to route this sensor data to the endpoint may be storage account or any another. Please suggest.

1

There are 1 answers

3
Sampath On

The Rest API below is used to retrieve cloud-to-device messages for a specific device of Azure IoT Hub.

GET https://fully-qualified-iothubname.azure-devices.net/devices/{id}/messages/deviceBound?api-version=2020-03-13
Authorization: <shared_access_token>

Response: enter image description here

The code below is to create a Shared Access Signature (SAS) token for Azure IoT Hub devices using Node.js.



const crypto = require('crypto');

// Function to generate SAS token
const generateSasToken = function(resourceUri, signingKey, policyName, expiresInMins) {
    // URL encode the resource URI
    resourceUri = encodeURIComponent(resourceUri);

    // Set expiration in seconds
    const expires = Math.ceil(Date.now() / 1000) + expiresInMins * 60;

    // Construct the string to sign
    const toSign = resourceUri + '\n' + expires;

    // Use crypto to create HMAC-SHA256 signature
    const hmac = crypto.createHmac('sha256', Buffer.from(signingKey, 'base64'));
    hmac.update(toSign);
    const base64UriEncoded = encodeURIComponent(hmac.digest('base64'));

    // Construct the SAS token
    let token = `SharedAccessSignature sr=${resourceUri}&sig=${base64UriEncoded}&se=${expires}`;
    if (policyName) token += `&skn=${policyName}`;

    return token;
};

// Example usage
const endpoint = "myhub.azure-devices.net/devices/device1";
const policyName = 'device'; // Replace with your policy name
const policyKey = "..."; // Replace with your policy key (or)Shared access policies device key give all persion for the device key 
const token = generateSasToken(endpoint, policyKey, policyName, 60);

console.log("Generated SAS Token:", token);

enter image description here

You can follow the steps below to configure message routing from your IoT devices to a storage account within your IoT Hub:

  • Select your IoT Hub and click on the "Message Routing" option in the navigation pane.

  • Click on the "+ Add" button to create a new route for message routing and next to the "Endpoint" field, click on the "+ Add" button to specify the endpoint for your route.

  • Select "Blob storage" as the endpoint type from the available options and enter a name for your endpoint, e.g., "IoTStorageEndpoint".

enter image description here

  • Click on "Pick a container" to choose the storage container where you want to store the routed messages. If the desired container doesn't exist, you can create a new one by clicking on the "+ Containers" button and specifying the name and access level.

  • Choose whether to use the default naming convention for blobs or customize it according to your preferences. Click "Create" to create the storage endpoint once you've configured all necessary details. Enter a name for the route, select "Device Telemetry Messages" as the data source, enable the route, and specify the route query, e.g., "level='storage'"/true.

enter image description here

  • Click "Save" to save the route configuration.

Send telemetry data to Azure IoT Hub : enter image description here

Azure Storage: enter image description here