0
votes

Objective:

View MPU6050 data on Azure IoT Edge

I would like to deploy a module to my IoT Edge Device. So in order to deploy MPU6050 sensor as a module, I am stuck up with the following doubts. It would be really helpful if someone could give me his/her insights on this as I am a newbie to Azure.

Current position:

Edge instance has been created on Azure portal and only "set modules" part is remaining. I have configured my Raspberry Pi to function as an edge device and can view listings present in Azure Edge. New registry has been created on Azure portal. Only pushing of my MPU6050-reading-image file onto the registry is remaining.

Doubts:

  1. I have downloaded the SDK for python to customise it to read MPU6050 data. But I cannot understand the whole function on how it works. If there is any tutorial to create our own code to read any sensor data and build it would be very supportive. (I am unable to find any online)
  2. I am aware on how to run a python file on docker. But how can this whole SDK be deployed onto Azure Registry so that I can just give a single link on the module deployment of edge device?
  3. I am doubtful if I am going on the right track about the entire process. Correct me if I am wrong:

The iot-hub-sdk is configured to read MPU6050 data --> it is built and run on docker --> the local docker is pushed into Azure Registry that I have already created --> that registry link is copied and pasted into the edge device deployment --> That Edge instance is linked to my physical Edge device --> So when the Edge function is run I can see the entire sensor data on a locally connected Edge device that does not have internet connection

Any help or suggestion regarding any of my queries mentioned above would be really appreciated..

Thanks & Cheers!

1

1 Answers

0
votes

There is a tutorial on how to create Python based modules for IoT Edge: https://docs.microsoft.com/en-us/azure/iot-edge/tutorial-python-module

As the tutorial suggests, I recommend to use Visual Studio Code with the IoT Edge extension. Then you get the Python module template, the Dockerfile etc. You can directly from VS Code push your custom module into your private container registry, e.g. Azure Container Registry and also set your deployment manifest (which module(s) to run on which Edge device).

As requested in the comments, I build a quick complete sample (did not test it though). The sample is just based on the the template sample when you create a new Python module using the VS code IoT Edge extension

import random
import time
import sys
import iothub_client
# pylint: disable=E0611
from iothub_client import IoTHubModuleClient, IoTHubClientError, IoTHubTransportProvider
from iothub_client import IoTHubMessage, IoTHubMessageDispositionResult, IoTHubError

# messageTimeout - the maximum time in milliseconds until a message times out.
# The timeout period starts at IoTHubModuleClient.send_event_async.
# By default, messages do not expire.
MESSAGE_TIMEOUT = 10000

# global counters
RECEIVE_CALLBACKS = 0
SEND_CALLBACKS = 0

# Choose HTTP, AMQP or MQTT as transport protocol.  Currently only MQTT is supported.
PROTOCOL = IoTHubTransportProvider.MQTT

# Callback received when the message that we're forwarding is processed.
def send_confirmation_callback(message, result, user_context):
    global SEND_CALLBACKS
    print ( "Confirmation[%d] received for message with result = %s" % (user_context, result) )
    map_properties = message.properties()
    key_value_pair = map_properties.get_internals()
    print ( "    Properties: %s" % key_value_pair )
    SEND_CALLBACKS += 1
    print ( "    Total calls confirmed: %d" % SEND_CALLBACKS )


class HubManager(object):

    def __init__(
            self,
            protocol=IoTHubTransportProvider.MQTT):
        self.client_protocol = protocol
        self.client = IoTHubModuleClient()
        self.client.create_from_environment(protocol)

        # set the time until a message times out
        self.client.set_option("messageTimeout", MESSAGE_TIMEOUT)

    # Forwards the message received onto the next stage in the process.
    def forward_event_to_output(self, outputQueueName, event, send_context):
        self.client.send_event_async(
            outputQueueName, event, send_confirmation_callback, send_context)

def main(protocol):
    try:
        print ( "\nPython %s\n" % sys.version )
        print ( "IoT Hub Client for Python" )

        hub_manager = HubManager(protocol)

        print ( "Starting the IoT Hub Python sample using protocol %s..." % hub_manager.client_protocol )
        print ( "The sample is now waiting for messages and will indefinitely.  Press Ctrl-C to exit. ")

        while True:
            # Build the message with simulated telemetry values.
            # Put your real sensor reading logic here instead
            temperature = TEMPERATURE + (random.random() * 15)
            humidity = HUMIDITY + (random.random() * 20)
            msg_txt_formatted = MSG_TXT % (temperature, humidity)
            message = IoTHubMessage(msg_txt_formatted)
            hubManager.forward_event_to_output("output1", message, 0)
            time.sleep(10)

    except IoTHubError as iothub_error:
        print ( "Unexpected error %s from IoTHub" % iothub_error )
        return
    except KeyboardInterrupt:
        print ( "IoTHubModuleClient sample stopped" )

if __name__ == '__main__':
    main(PROTOCOL)