目标:
在Azure IoT Edge上查看MPU6050数据
我想将模块部署到IoT Edge设备。因此,为了将MPU6050传感器部署为模块,我会遇到以下疑问。如果我是Azure的新手,如果有人可以给我他/她的见解,那将非常有帮助。
当前位置:
已在Azure门户上创建Edge实例,仅保留“设置模块”部分。我已将Raspberry Pi配置为充当边缘设备,并且可以查看Azure Edge中存在的列表。在Azure门户上已创建新注册表。仅保留将我的MPU6050读取图像文件推送到注册表中。
疑问:
iot-hub-sdk配置为读取MPU6050数据->在docker上构建并运行它->将本地docker推送到我已经创建的Azure注册表中->该注册表链接被复制并粘贴到边缘设备部署中->该Edge实例已链接到我的物理Edge设备->因此,当运行Edge功能时,我可以在没有Internet的本地连接的Edge设备上看到整个传感器数据连接
对于我上面提到的任何查询的任何帮助或建议,我们将不胜感激。
感谢与欢呼!
答案 0 :(得分:0)
这里有一个有关如何为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容器注册表,还设置您的部署清单(哪个模块在哪个Edge设备上运行)。
根据评论中的要求,我构建了一个快速完整的示例(尽管未对其进行测试)。当您使用VS代码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)