连接你的IoT设备到云端-虚拟IoT硬件和RaspberryPi


文档摘要

连接你的IoT设备到云端 - 虚拟IoT硬件和Raspberry Pi 在本课程的这一部分中,你将连接你的虚拟IoT设备或Raspberry Pi到IoT Hub,以便发送遥测数据并接收命令。 连接设备到IoT Hub 下一步是连接你的设备到IoT Hub。 任务 - 连接到IoT Hub 在VS Code中打开 文件夹。如果你使用的是虚拟IoT设备,请确保虚拟环境正在终端中运行。 安装一些额外的Pip包: 在 is a library to communicate with your IoT Hub. Add the following imports to the top of the 文件中,位于现有导入语句下方: 此代码导入与IoT Hub通信所需的SDK。

连接你的IoT设备到云端 - 虚拟IoT硬件和Raspberry Pi

在本课程的这一部分中,你将连接你的虚拟IoT设备或Raspberry Pi到IoT Hub,以便发送遥测数据并接收命令。

连接设备到IoT Hub

下一步是连接你的设备到IoT Hub。

任务 - 连接到IoT Hub

  1. 在VS Code中打开soil-moisture-sensor文件夹。如果你使用的是虚拟IoT设备,请确保虚拟环境正在终端中运行。

  2. 安装一些额外的Pip包:

    pip3 install azure-iot-device

    azure-iot-device is a library to communicate with your IoT Hub.

  3. Add the following imports to the top of the app.py文件中,位于现有导入语句下方:

    from azure.iot.device import IoTHubDeviceClient, Message, MethodResponse

    此代码导入与IoT Hub通信所需的SDK。

  4. 删除这个循环中的import paho.mqtt.client as mqtt line as this library is no longer needed. Remove all the MQTT code including the topic names, all code that uses mqtt_client and the handle_command. Keep the while True: loop, just delete the mqtt_client.publish行。

  5. 在导入语句下方添加以下代码:

    connection_string = "<connection string>"

    替换<连接字符串> with the connection string you retrieved for the device earlier in this lesson.

    This is not best practice. Connection strings should never be stored in source code, as this can be checked into source code control and found by anyone. We are doing this here for the sake of simplicity. Ideally you should use something like an environment variable and a tool like `python-dotenv"。你将在接下来的一课中了解更多关于这一点的内容。

  6. 在此代码下方,添加以下内容以创建一个可以与IoT Hub通信的设备客户端对象,并将其连接起来:

    device_client = IoTHubDeviceClient.create_from_connection_string(connection_string) print('Connecting') device_client.connect() print('Connected')
  7. 运行此代码。你会看到设备连接。

    pi@raspberrypi:~/soil-moisture-sensor $ python3 app.py Connecting Connected Soil moisture: 379

发送遥测数据

现在你的设备已连接,你可以将遥测数据发送到IoT Hub而不是MQTT代理。

任务 - 发送遥测数据

  1. while True循环内,在休眠之前添加以下代码:

    message = Message(json.dumps({ 'soil_moisture': soil_moisture })) device_client.send_message(message)

    这段代码创建一个IoT Hub Message containing the soil moisture reading as a JSON string, then sends this to the IoT Hub as a device to cloud message.

Handle commands

Your device needs to handle a command from the server code to control the relay. This is sent as a direct method request.

Task - handle a direct method request

  1. Add the following code before the while True循环:

    def handle_method_request(request): print("Direct method received - ", request.name) if request.name == "relay_on": relay.on() elif request.name == "relay_off": relay.off()

    这定义了一个方法handle_method_request, that will be called when a direct method is called by the IoT Hub. Each direct method has a name, and this code expects a method called relay_on to turn the relay on, and relay_off to turn the relay off.

    This could also be implemented in a single direct method request, passing the desired state of the relay in a payload that can be passed with the method request and available from the request object.

  2. Direct methods require a response to tell the calling code that they have been handled. Add the following code at the end of the handle_method_request函数来创建对请求的响应:

    method_response = MethodResponse.create_from_method_request(request, 200) device_client.send_method_response(method_response)

    这段代码使用HTTP状态码200发送直接方法请求的响应,并将其发送回IoT Hub。

  3. 在该函数定义下方添加以下代码:

    device_client.on_method_request_received = handle_method_request

    这段代码告诉IoT Hub客户端在调用直接方法时使用handle_method_request函数。

你可以在code/picode/virtual-device文件夹中找到这段代码。

你的土壤湿度传感器程序已连接到IoT Hub!

声明:
本文件灏天文库团队进行了翻译。尽管我们力求准确,但请注意,翻译可能包含错误或不准确之处。原文档以其原始语言为准。我们不对因使用此翻译而产生的任何误解或误译负责。


作者与出处
原作者: microsoft
来源:microsoft
许可证:MIT
整理: 灏天文库整理
由灏天文库结构化整理,提供目录导航、全文检索与在线阅读,便于系统化学习
发布者: 作者: microsoft 转发
评论区 (0)
U