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/2-farm/lessons/1-predict-plant-growth/single-board-computer-temp-...

71 lines
3.2 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": "4efc74299e19f5d08f2f3f34451a11ba",
"translation_date": "2025-08-26T22:16:22+00:00",
"source_file": "2-farm/lessons/1-predict-plant-growth/single-board-computer-temp-publish.md",
"language_code": "mo"
}
-->
# 發佈溫度 - 虛擬物聯網硬體與樹莓派
在本課程中,您將透過 MQTT 發佈由樹莓派或虛擬物聯網設備檢測到的溫度值,以便稍後用於計算 GDD。
## 發佈溫度
一旦讀取到溫度值,就可以透過 MQTT 發佈到某些「伺服器」代碼,該代碼將讀取這些值並存儲它們,以便用於 GDD 計算。
### 任務 - 發佈溫度
編程設備以發佈溫度數據。
1. 如果尚未打開,請打開 `temperature-sensor` 應用程式專案。
1. 重複您在第 4 課中所做的步驟以連接到 MQTT 並發送遙測數據,您將使用相同的公共 Mosquitto broker。
這些步驟包括:
- 添加 MQTT pip 套件
- 添加連接到 MQTT broker 的代碼
- 添加發佈遙測數據的代碼
> ⚠️ 如有需要,請參考 [連接到 MQTT 的指導](../../../1-getting-started/lessons/4-connect-internet/single-board-computer-mqtt.md) 和 [發送遙測數據的指導](../../../1-getting-started/lessons/4-connect-internet/single-board-computer-telemetry.md)(第 4 課)。
1. 確保 `client_name` 反映此專案的名稱:
```python
client_name = id + 'temperature_sensor_client'
```
1. 對於遙測數據,與其發送光線值,不如發送從 DHT 感測器讀取的溫度值,並將其作為 JSON 文件中的 `temperature` 屬性:
```python
_, temp = sensor.read()
telemetry = json.dumps({'temperature' : temp})
```
1. 溫度值不需要頻繁讀取——在短時間內不會有太大變化,因此將 `time.sleep` 設置為 10 分鐘:
```cpp
time.sleep(10 * 60);
```
> 💁 `sleep` 函數以秒為單位因此為了更容易閱讀該值是通過計算得出的。1 分鐘有 60 秒,因此 10 x (1 分鐘的 60 秒) 得出 10 分鐘的延遲。
1. 以與之前作業部分相同的方式運行代碼。如果您使用的是虛擬物聯網設備,請確保 CounterFit 應用程式正在運行,並且濕度和溫度感測器已在正確的引腳上創建。
```output
pi@raspberrypi:~/temperature-sensor $ python3 app.py
MQTT connected!
Sending telemetry {"temperature": 25}
Sending telemetry {"temperature": 25}
```
> 💁 您可以在 [code-publish-temperature/virtual-device](../../../../../2-farm/lessons/1-predict-plant-growth/code-publish-temperature/virtual-device) 資料夾或 [code-publish-temperature/pi](../../../../../2-farm/lessons/1-predict-plant-growth/code-publish-temperature/pi) 資料夾中找到此代碼。
😀 您已成功將溫度作為遙測數據從您的設備發佈出去。
---
**免責聲明**
本文件已使用 AI 翻譯服務 [Co-op Translator](https://github.com/Azure/co-op-translator) 進行翻譯。儘管我們努力確保翻譯的準確性,但請注意,自動翻譯可能包含錯誤或不準確之處。原始文件的母語版本應被視為權威來源。對於關鍵信息,建議尋求專業人工翻譯。我們對因使用此翻譯而引起的任何誤解或錯誤解釋不承擔責任。