入手了樹莓派之后,買了個樹莓派的亞克力外殼,送了一個風扇。
但是送的這個風扇,只有正負極,不能自主控制。
風扇如果一直轉,噪音讓人有點受不了,如果沒有風扇,樹莓派散熱效果又要受到影響。
然后在實驗室找了個三極管,準備用樹莓派的GPIO口來根據(jù)CPU的溫度來智能控制風扇是否轉動。
安裝三極管控制風扇教程
樹莓派溫控風扇教程:樹莓派 根據(jù) CPU 溫度控制風扇起停
溫控風扇的不足之處
我設置的溫度是溫度高于48度就轉動,低于42度就停止轉動。
在冬天用起來是非常舒服的,確實是樹莓派高負荷的時候才會轉動,但是到了第二年5月左右,就發(fā)現(xiàn)風扇轉動的頻率變高了,考慮到了是室溫的變化導致了這種情況。
到6月左右,風扇幾乎是大半天都在轉動了,于是手動去調(diào)節(jié)了代碼的設置,也在想解決方案:怎么才能讓樹莓派在冬天和夏天都只在高負載的時候運轉呢?
想到了網(wǎng)上有很多樹莓派教程,獲取室溫,如果可以獲取到室溫,設置的溫度根據(jù)室溫浮動在一個區(qū)間,這樣就可以保持風扇轉動的靈活性了,樹莓派也多了個檢測室溫的功能了。然后找到朋友弄了個DS18B20的溫度傳感器模塊。
安裝溫度傳感器教程
安裝使用DS18B20教程:樹莓派+溫度傳感器實現(xiàn)室內(nèi)溫度監(jiān)控
安裝遇到的問題
最后焊接了之后,接到樹莓派上,但是樹莓派一直都檢測不到溫度。單獨檢測又是沒問題的。晚上回到寢室,又弄了半天,發(fā)現(xiàn)這個模塊只有在3.3V的供電下才能使用,在5V的接口下沒辦法使用(不知道是不是我操作有問題)。
第二個問題就是執(zhí)行了修改文件,更新信息的操作,在devices文件夾下面沒有文件。搞了半天,最后發(fā)現(xiàn)重啟就可以了,sudo reboot直接重啟,然后再進去文件夾下面看就有了溫度傳感器的信息了。
上一個傳感器看錯了圖片,裝反了傳感器的針腳,開機傳感器就燒了,發(fā)出一股燒焦的塑料的味道。。。
樹莓派引腳圖片

溫控風扇代碼介紹
網(wǎng)上找的代碼,要么不全,要么不容易懂,所以我結合網(wǎng)上的代碼,自己也寫了個萬能代碼,主要特點:
- 兼容沒有溫度傳感器的情況
- 自動尋找溫度傳感器的位置,不需要手動設置
- 生成日志文件,方便查看記錄與可視化
- 一次設置,以后基本上都不用管了
GitHub地址及使用參數(shù)說明:https://github.com/ly1102/rpi_automatic_fan
代碼:
# -*- coding:utf-8 -*-
# date: 2018-10-08 23:54
"""
控制樹莓派風扇
"""
import os
import time
import commands
import RPi.GPIO as GPIO
from datetime import datetime
T_HIGH = 48 # 溫度>48度開始轉動,在溫度傳感器失效時使用
T_LOW = 42 # 溫度<42度停止轉動,在溫度傳感器失效時使用
T_DIFF_HIGH = 24 # 溫差>24度開始轉動
T_DIFF_LOW = 18 # 溫差<18度停止轉動
T_SENOR_DIFF = 0 # 溫度傳感器和真實環(huán)境溫度矯正值
fan_pin = 12 # 風扇IO針腳BOARD編號
NPN = True # 控制風扇用的是NPN三極管
IS_LOG_FILE = True # 是否輸出溫度信息到文件
IS_LOG_CONSOLE = True # 是否輸出溫度信息到控制臺
time_interval = 5 # 檢測溫度間隔時間單位秒
log_file_duration = 12 # 日志記錄保留時間長度,單位小時
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
log_file_path = os.path.join(base_dir, 'temperature_log')
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(fan_pin, GPIO.OUT)
temp_path = None
if NPN:
START = 1
STOP = 0
else:
START = 0
STOP = 1
# 自動獲取溫度傳感器溫度信息所在的文件位置
try:
for each_dir in os.listdir('/sys/bus/w1/devices/'):
if '28-' in each_dir:
temp_path = '/sys/bus/w1/devices/'+each_dir+'/w1_slave'
except Exception as e:
print('Warning!: 沒有檢測到溫度傳感器!Error: {}'.format(e))
def get_gpu_temp():
gpu_temp = commands.getoutput('/opt/vc/bin/vcgencmd measure_temp').replace('temp=', '').replace('\'C', '')
return float(gpu_temp)
def get_env_temp():
with open(temp_path, 'r') as temp_file:
# 讀取文件所有內(nèi)容
text = temp_file.read()
# a7 01 4b 46 7f ff 09 10 e0 : crc=e0 YES
# a7 01 4b 46 7f ff 09 10 e0 t=26437
temp_line = text.split("\n")[1]
# a7 01 4b 46 7f ff 09 10 e0 t=26437
temperature_data = temp_line.split(" ")[9]
# 't=26437'
env_temperature = float(temperature_data[2:])
# 26437
env_temperature = env_temperature / 1000
# 26.437
# print(temperature)
return env_temperature
def main():
count = 0
env_temp = None
temp_diff = None
count_per_min = 60/time_interval
while True:
gpu_temp_loop = get_gpu_temp()
if temp_path is not None:
env_temp = get_env_temp()
temp_diff = gpu_temp_loop - env_temp
if IS_LOG_FILE:
try:
if count <= count_per_min * 60 * log_file_duration:
fp = open(log_file_path, 'a')
if temp_diff is None:
fp.write('{} CPU temp: {}C\n'.format(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), gpu_temp_loop))
else:
fp.write('{} CPUtemp: {}C, ENVtemp: {}C, DIFF: {}C\n'.format(datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
gpu_temp_loop, env_temp, temp_diff))
count += 1
fp.close()
else:
fp = open(log_file_path, 'w')
fp.write('{} CPU temp: {}C\n'.format(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), gpu_temp_loop))
count = 0
fp.close()
except Exception as e:
print('Error: {}-{}'.format(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), e))
if IS_LOG_CONSOLE:
if temp_diff is None:
print('{} CPU temp: {}C\n'.format(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), gpu_temp_loop))
else:
print('{} CPUtemp: {}C, ENVtemp: {}C, DIFF: {}C\n'.format(datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
gpu_temp_loop, env_temp, temp_diff))
if temp_diff is None:
if gpu_temp_loop > T_HIGH:
GPIO.output(fan_pin, START)
elif gpu_temp_loop < T_LOW:
GPIO.output(fan_pin, STOP)
else:
if temp_diff > T_DIFF_HIGH:
GPIO.output(fan_pin, START)
elif temp_diff < T_DIFF_LOW:
GPIO.output(fan_pin, STOP)
time.sleep(time_interval)
if __name__ == '__main__':
main()