首页 > 解决方案 > 在 Azure IoT Edge 上查看 MPU6050 数据

问题描述

客观的:

在 Azure IoT Edge 上查看 MPU6050 数据

我想将一个模块部署到我的 IoT Edge 设备。因此,为了将 MPU6050 传感器部署为模块,我遇到了以下疑问。如果有人能给我他/她对此的见解,那将非常有帮助,因为我是 Azure 的新手。

当前位置:

边缘实例已在 Azure 门户上创建,只剩下“设置模块”部分。我已将 Raspberry Pi 配置为边缘设备,并且可以查看 Azure Edge 中的列表。已在 Azure 门户上创建了新注册表。只剩下将我的 MPU6050-reading-image 文件推送到注册表中了。

疑点:

  1. 我已经下载了适用于 python 的 SDK 来定制它来读取 MPU6050 数据。但我无法理解它是如何工作的整个功能。如果有任何教程可以创建我们自己的代码来读取任何传感器数据并构建它,那将是非常支持的。(我在网上找不到任何东西)
  2. 我知道如何在 docker 上运行 python 文件。但是,如何将整个 SDK 部署到 Azure Registry 上,以便我可以在边缘设备的模块部署上提供一个链接?
  3. 我怀疑我是否在整个过程中走在正确的轨道上。如果我错了,请纠正我:

iot-hub-sdk 配置为读取 MPU6050 数据 --> 它在 docker 上构建和运行 --> 本地 docker 被推送到我已经创建的 Azure 注册表中 --> 该注册表链接被复制并粘贴到边缘设备部署 --> 该边缘实例已链接到我的物理边缘设备 --> 因此,当边缘功能运行时,我可以在没有互联网连接的本地连接边缘设备上看到整个传感器数据

对于我上面提到的任何查询的任何帮助或建议将不胜感激..

谢谢和干杯!

标签: azuredockerazure-iot-hubazure-iot-sdkazure-iot-edge

解决方案


有一个关于如何为 IoT Edge 创建基于 Python 的模块的教程:https ://docs.microsoft.com/en-us/azure/iot-edge/tutorial-python-module

正如教程所建议的,我建议将 Visual Studio Code 与 IoT Edge 扩展一起使用。然后你会得到 Python 模块模板、Dockerfile 等。你可以直接从 VS Code 将你的自定义模块推送到你的私有容器注册表中,例如 Azure 容器注册表,还可以设置你的部署清单(哪个模块在哪个边缘设备上运行)。

按照评论中的要求,我构建了一个快速完整的示例(虽然没有测试它)。该示例仅基于使用 VS code IoT Edge 扩展创建新 Python 模块时的模板示例

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)

推荐阅读