【问题标题】:View MPU6050 data on Azure IoT Edge在 Azure IoT Edge 上查看 MPU6050 数据
【发布时间】:2019-02-04 12:07:54
【问题描述】:

目标:

在 Azure IoT Edge 上查看 MPU6050 数据

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

当前位置:

Edge 实例已在 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 Registry --> 该注册表链接被复制并粘贴到边缘设备部署中 --> 该边缘实例已链接到我的物理边缘设备 --> 因此,当边缘功能运行时,我可以在没有互联网的本地连接边缘设备上看到整个传感器数据连接

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

感谢和干杯!

【问题讨论】:

    标签: azure docker azure-iot-hub azure-iot-sdk azure-iot-edge


    【解决方案1】:

    有一个关于如何为 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 容器注册表,并设置你的部署清单(哪些模块在哪个边缘设备上运行)。

    按照 cmets 的要求,我构建了一个快速完整的示例(虽然没有测试它)。该示例仅基于使用 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)
    

    【讨论】:

    • 感谢您的回复。我之前尝试过使用 VS Code。在 main.py 代码中,我应该在 main 函数下编写整个代码来读取 MPU6050 传感器数据吗?代码是否必须只读取数据并将其存储在某些变量下,或者是否有任何其他专门要做的方法(例如将其存储在注册表中,而不仅仅是将其分配给某个变量)?
    • 我添加了完整的示例模块代码。希望这有助于您入门。当然,您不需要将所有内容都放在一个文件中。您可以随意拆分。
    • 这能解决您的问题吗?如果是这样,请接受答案或提供更多信息
    • 我对延误表示歉意。最近我一直无法处理它。但是您上传的代码与我之前在 VS Code 上执行的代码几乎相同并推送到 Azure。但即使我在 VS Code 上安装了相同的库,它也会显示一些库导入错误。我不知道我错过了什么。
    • 如果您发布您收到的实际错误消息会有所帮助。另外,你能发布你的 dependencies.txt 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-03
    • 2023-01-04
    相关资源
    最近更新 更多