樹莓派的CPU溫度並將其顯示在LCD上
通常是因為CPU溫度變化相對較快,而您的程式每0.1秒更新一次顯示。為了減少顯示跳動,您可以考慮以下幾種方法:
延長更新間隔:增加時間間隔,降低顯示更新的頻率。這可以通過調整time.sleep()函數的參數來實現。
平滑溫度讀數:使用一個簡單的移動平均或其他平滑技術來平均最近幾次的溫度讀數,從而減少顯示的波動。
條件更新:只有當溫度變化超過某個閾值時才更新LCD上的溫度顯示。
以下是一個結合了這些改進的示例程式碼:
python
複製程式碼
import RPi.GPIO as GPIO
from RPLCD.i2c import CharLCD
from datetime import datetime
import time
import os
# 設定LCD的I2C地址
lcd = CharLCD('PCF8574', 0x27, cols=16, rows=2)
# 設定GPIO引腳和模式
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
backlight_state = False
last_button_state = GPIO.input(17)
debounce_time = 0.2
last_press_time = time.time()
# 溫度讀數列表,用於計算移動平均
temp_readings = []
def get_cpu_temp():
res = os.popen('vcgencmd measure_temp').readline()
return float(res.replace("temp=", "").replace("'C\n", ""))
def average_temp(new_temp):
temp_readings.append(new_temp)
if len(temp_readings) > 5
temp_readings.pop(0) # 保持列表只有最新的五個數據
return sum(temp_readings) / len(temp_readings)
try:
while True:
now = datetime.now()
date_str = now.strftime("%Y-%m-%d")
time_str = now.strftime("%H:%M:%S")
current_temp = get_cpu_temp()
avg_temp = average_temp(current_temp)
current_button_state = GPIO.input(17)
if current_button_state == GPIO.LOW and last_button_state == GPIO.HIGH and (time.time() - last_press_time) > debounce_time:
backlight_state = not backlight_state
lcd.backlight_enabled = backlight_state
last_press_time = time.time()
last_button_state = current_button_state
lcd.cursor_pos = (0, 0)
lcd.write_string(date_str + ' ' * (16 - len(date_str)))
lcd.cursor_pos = (1, 0)
lcd.write_string(f"{time_str} T:{avg_temp:.1f}C")
time.sleep(1.0) # 將更新間隔設為1秒
except KeyboardInterrupt:
lcd.clear()
finally:
GPIO.cleanup()
在這個版本中,我們將更新間隔增加到了1秒,並且使用了一個簡單的移動平均來平滑溫度讀數。這樣可以顯著減少顯示的波動。
temp_readings.append(new_temp)
if len(temp_readings) > 5:
temp_readings.pop(0) # 保持列表只有最新的五個數據
return sum(temp_readings) / len(temp_readings)
ChatGPT
這段程式碼定義了一個函數 average_temp,其目的是計算並返回溫度讀數的移動平均值。這是通過以下步驟實現的:
新增溫度讀數: 函數接受一個參數 new_temp,這是最新的溫度讀數。這個新的溫度值會被添加到全局變數 temp_readings 的列表中。temp_readings 用於存儲最近的溫度讀數。
維持讀數列表大小: 函數檢查 temp_readings 列表的長度。如果長度超過5,則使用 pop(0) 方法移除列表中的第一個元素。這是因為我們只關心最近的5個讀數,超出部分的舊數據會被丟棄。這樣做可以確保列表中始終只有最近的五個數據。
計算移動平均值: 使用 sum(temp_readings) 得到列表中所有溫度讀數的總和,然後除以列表長度 len(temp_readings),計算出平均值。這個平均值將會是最近五次測量的平均溫度,有助於平滑短期內的溫度波動。
這個移動平均的方法有效地平滑了溫度讀數,降低了顯示在LCD上的數據波動,使得溫度讀數更穩定,更容易讓使用者讀取。
lcd.write_string(date_str + ' ' * (16 - len(date_str)))
lcd.cursor_pos = (1, 0)
lcd.write_string(f"{time_str} T:{avg_temp:.1f}C")
這部分程式碼是用於控制LCD顯示的具體內容和位置,逐行解釋如下:
設定游標位置:
python
複製程式碼
lcd.cursor_pos = (0, 0)
這行程式碼將LCD的游標位置設定在第一行的開始處。cursor_pos 的值是一個元組,第一個元素表示行(從0開始計數),第二個元素表示列(從0開始計數)。所以 (0, 0) 表示第一行第一列的位置。
寫入日期字符串:
python
複製程式碼
lcd.write_string(date_str + ' ' * (16 - len(date_str)))
這行程式碼在LCD的第一行寫入日期信息。date_str 是包含日期的字符串,格式通常是 "YYYY-MM-DD"。' ' * (16 - len(date_str)) 是在日期後面添加足夠的空格來填充整行,確保日期顯示在行的左側,並且整行的總字符數為16(假設LCD有16列)。這樣可以保持顯示的整潔性。
設定游標到第二行開始處:
python
複製程式碼
lcd.cursor_pos = (1, 0)
這裡,游標被移動到第二行的開始位置。(1, 0) 表示第二行的第一列。
寫入時間和溫度信息:
python
複製程式碼
lcd.write_string(f"{time_str} T:{avg_temp:.1f}C")
在第二行,時間和溫度信息將被寫入。time_str 包含當前的時間,格式是 "HH:MM
"。avg_temp 是平均溫度,格式化為一位小數(例如,23.4)。這裡的 f"{time_str} T:{avg_temp:.1f}C" 是一個格式化的字符串,它將時間和溫度合併到一個字符串中,並在溫度後添加攝氏度的符號 'C'。這樣做可以讓使用者一眼看到時間和相對應的溫度。
總結來說,這部分程式碼主要負責將日期、時間和溫度數據在LCD上進行美觀、有序的展示。通过这种方式,用户可以直观地查看到日期、当前时间及CPU温度。