You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
IoT-For-Beginners/translations/mo/1-getting-started/lessons/4-connect-internet/single-board-computer-mqtt.md

93 lines
3.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<!--
CO_OP_TRANSLATOR_METADATA:
{
"original_hash": "90fb93446e03c38f3c0e4009c2471906",
"translation_date": "2025-08-26T23:19:12+00:00",
"source_file": "1-getting-started/lessons/4-connect-internet/single-board-computer-mqtt.md",
"language_code": "mo"
}
-->
# 通過網絡控制你的夜燈 - 虛擬物聯網硬件與樹莓派
物聯網設備需要編寫代碼,通過 MQTT 與 *test.mosquitto.org* 通信,發送光傳感器讀取的遙測值,並接收控制 LED 的指令。
在本課程的這部分,你將把樹莓派或虛擬物聯網設備連接到 MQTT broker。
## 安裝 MQTT 客戶端套件
為了與 MQTT broker 通信,你需要在樹莓派或虛擬設備的虛擬環境中安裝 MQTT 的 pip 套件。
### 任務
安裝 pip 套件
1. 在 VS Code 中打開夜燈項目。
1. 如果你使用的是虛擬物聯網設備,請確保終端正在運行虛擬環境。如果你使用的是樹莓派,則不需要使用虛擬環境。
1. 運行以下命令來安裝 MQTT 的 pip 套件:
```sh
pip3 install paho-mqtt
```
## 編寫設備代碼
設備已準備好編寫代碼。
### 任務
編寫設備代碼。
1.`app.py` 文件的頂部添加以下導入:
```python
import paho.mqtt.client as mqtt
```
`paho.mqtt.client` 庫允許你的應用通過 MQTT 進行通信。
1. 在光傳感器和 LED 的定義之後添加以下代碼:
```python
id = '<ID>'
client_name = id + 'nightlight_client'
```
`<ID>` 替換為一個唯一的 ID該 ID 將用作此設備客戶端的名稱,並在稍後用於此設備發布和訂閱的主題名稱。*test.mosquitto.org* broker 是公共的,許多人都在使用,包括其他正在完成此作業的學生。擁有唯一的 MQTT 客戶端名稱和主題名稱可以確保你的代碼不會與其他人的代碼發生衝突。稍後在創建服務器代碼時,你也需要使用此 ID。
> 💁 你可以使用像 [GUIDGen](https://www.guidgen.com) 這樣的網站來生成唯一的 ID。
`client_name` 是此 MQTT 客戶端在 broker 上的唯一名稱。
1. 在這段新代碼的下方添加以下代碼,以創建 MQTT 客戶端對象並連接到 MQTT broker
```python
mqtt_client = mqtt.Client(client_name)
mqtt_client.connect('test.mosquitto.org')
mqtt_client.loop_start()
print("MQTT connected!")
```
此代碼創建客戶端對象,連接到公共 MQTT broker並啟動一個處理循環該循環在後台線程中運行監聽任何已訂閱主題的消息。
1. 以與作業前一部分相同的方式運行代碼。如果你使用的是虛擬物聯網設備,請確保 CounterFit 應用正在運行,並且光傳感器和 LED 已在正確的引腳上創建。
```output
(.venv) ➜ nightlight python app.py
MQTT connected!
Light level: 0
Light level: 0
```
> 💁 你可以在 [code-mqtt/virtual-device](../../../../../1-getting-started/lessons/4-connect-internet/code-mqtt/virtual-device) 文件夾或 [code-mqtt/pi](../../../../../1-getting-started/lessons/4-connect-internet/code-mqtt/pi) 文件夾中找到此代碼。
😀 你已成功將設備連接到 MQTT broker。
---
**免責聲明**
本文件已使用 AI 翻譯服務 [Co-op Translator](https://github.com/Azure/co-op-translator) 進行翻譯。雖然我們致力於提供準確的翻譯,但請注意,自動翻譯可能包含錯誤或不準確之處。原始文件的母語版本應被視為權威來源。對於關鍵資訊,建議使用專業人工翻譯。我們對因使用此翻譯而引起的任何誤解或錯誤解釋不承擔責任。