准备
设计 & 踩过的坑
需要的材料
- 树莓派
- 三极管(型号 S8050 某宝上很容易买到)
- 杜邦线
- 风扇
电路设计
使用三极管做开关,电路连接如下图:
程序设计
使用Python导入RPi.GPIO的包,直接调用包的方法控制GPIO引脚输出电平的高低。
在”/home/pi”目录下新建文本 GPIO.py 并写入以下Python程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
import RPi.GPIO as GPIO import time import logging
logger = logging.getLogger("mylog")
logger.setLevel(level=logging.DEBUG)
handler = logging.FileHandler("/home/pi/TempGPIOController.log") handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter)
console = logging.StreamHandler() console.setLevel(logging.WARNING)
logger.addHandler(handler) logger.addHandler(console) FAN=23
GPIO.setmode(GPIO.BCM) GPIO.setup(FAN,GPIO.OUT)
def set_fan(value): logger.info('设置风扇(BCM23)电平状态:'+str(value)) GPIO.output(FAN,value) pass
while True: file = open("/sys/class/thermal/thermal_zone0/temp") temp = float(file.read()) / 1000 file.close() logger.info('温度: '+str(temp)) if temp>=57: set_fan(1) elif temp<=41: set_fan(0) time.sleep(60)
|
设置程序开机自启动:
在”/etc/systemd/system”目录下新建文本 TempGPIOController.service 并写入以下自启动配置内容:
(使用vi命令即可完成编写,如不熟悉vi请查询学习,这里不详细说)
1 2 3 4 5 6 7 8 9 10 11 12 13
| [Unit] Description=TempGPIOController After=multi-user.target
[Service] TimeoutStartSec=30 Restart=always RestartSec=30 ExecStart=/usr/bin/python3 /home/pi/GPIO.py ExecStop=/bin/kill $MAINPID
[Install] WantedBy=multi-user.target
|
执行以下命令即可设置该程序自启动:
1
| sudo systemctl enable TempGPIOController
|
测试程序是否正常:
执行以下命令即可运行此前写好的Python程序
1
| sudo systemctl start TempGPIOController
|
然后查看运行状态:
1
| sudo systemctl status TempGPIOController
|
若输出以下内容则运行正常:Active: active (running)
1 2 3 4 5 6 7 8 9 10
| pi@raspberrypi:/etc/systemd/system $ sudo systemctl status TempGPIOController ● TempGPIOController.service - TempGPIOController Loaded: loaded (/etc/systemd/system/TempGPIOController.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2021-08-09 09:29:31 CST; 37s ago Main PID: 3249 (python3) Tasks: 1 (limit: 4915) CGroup: /system.slice/TempGPIOController.service └─3249 /usr/bin/python3 /home/pi/GPIO.py
8月 09 09:30:41 raspberrypi systemd[1]: Started TempGPIOController.
|
过两分钟可以查看程序输出的日志,发现温度状况及风扇运转状况与我们预想想的一致:
1 2 3 4 5
| pi@raspberrypi:/etc/systemd/system $ tail -10 /home/pi/TempGPIOController.log 2021-08-09 09:29:31,610 - mylog - INFO - 温度: 56.965 2021-08-09 09:30:31,655 - mylog - INFO - 温度: 57.939 2021-08-09 09:30:31,655 - mylog - INFO - 设置风扇(BCM23)电平状态:1 2021-08-09 09:31:31,716 - mylog - INFO - 温度: 46.738
|
重启树莓派测试是否程序正常自启动
重启树莓派后查看运行状态:
1
| sudo systemctl status TempGPIOController
|
若输出以下内容则运行正常:Active: active (running)
1 2 3 4 5 6 7 8 9 10
| pi@raspberrypi:/etc/systemd/system $ sudo systemctl status TempGPIOController ● TempGPIOController.service - TempGPIOController Loaded: loaded (/etc/systemd/system/TempGPIOController.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2021-08-09 09:38:31 CST; 59s ago Main PID: 3211 (python3) Tasks: 1 (limit: 4915) CGroup: /system.slice/TempGPIOController.service 3211 /usr/bin/python3 /home/pi/GPIO.py
8月 09 09:40:23 raspberrypi systemd[1]: Started TempGPIOController.
|
查看日志,确认已输出在最近几分钟的日志:
1 2 3
| pi@raspberrypi:/etc/systemd/system $ tail -2 /home/pi/TempGPIOController.log 2021-08-09 09:38:32,101 - mylog - INFO - 温度: 44.79 2021-08-09 09:39:32,162 - mylog - INFO - 温度: 43.329
|
查看风扇运行状态,发现与日志所记载的一致。
成品图
树莓派(请忽略图上的红外灯管和摄像头,它们是模拟遥控器自动化控制空调用的,与本章的温控风扇无关)
程序日志:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| 2021-08-09 09:28:31,602 - mylog - INFO - 温度: 56.478 2021-08-09 09:29:31,610 - mylog - INFO - 温度: 56.965 2021-08-09 09:30:31,655 - mylog - INFO - 温度: 57.939 2021-08-09 09:30:31,655 - mylog - INFO - 设置风扇(BCM23)电平状态:1 2021-08-09 09:31:31,716 - mylog - INFO - 温度: 46.738 2021-08-09 09:32:31,778 - mylog - INFO - 温度: 42.842 2021-08-09 09:33:31,839 - mylog - INFO - 温度: 41.381 2021-08-09 09:34:31,900 - mylog - INFO - 温度: 40.407 2021-08-09 09:34:31,901 - mylog - INFO - 设置风扇(BCM23)电平状态:0 2021-08-09 09:35:31,958 - mylog - INFO - 温度: 45.764 2021-08-09 09:36:32,019 - mylog - INFO - 温度: 47.712 2021-08-09 09:37:32,081 - mylog - INFO - 温度: 49.66 2021-08-09 09:38:32,142 - mylog - INFO - 温度: 52.582 2021-08-09 09:39:32,203 - mylog - INFO - 温度: 52.095 2021-08-09 09:40:32,264 - mylog - INFO - 温度: 53.069 2021-08-09 09:41:32,326 - mylog - INFO - 温度: 54.043 2021-08-09 09:42:32,387 - mylog - INFO - 温度: 55.017 2021-08-09 09:43:32,448 - mylog - INFO - 温度: 54.043 2021-08-09 09:44:32,509 - mylog - INFO - 温度: 55.017 2021-08-09 09:45:32,571 - mylog - INFO - 温度: 55.504 2021-08-09 09:46:32,610 - mylog - INFO - 温度: 54.53 2021-08-09 09:47:32,672 - mylog - INFO - 温度: 55.504 2021-08-09 09:48:32,733 - mylog - INFO - 温度: 55.991 2021-08-09 09:49:32,794 - mylog - INFO - 温度: 55.991 2021-08-09 09:50:32,855 - mylog - INFO - 温度: 56.478 2021-08-09 09:51:32,917 - mylog - INFO - 温度: 56.965 2021-08-09 09:52:32,978 - mylog - INFO - 温度: 57.452 2021-08-09 09:52:32,978 - mylog - INFO - 设置风扇(BCM23)电平状态:1 2021-08-09 09:53:33,039 - mylog - INFO - 温度: 48.686 2021-08-09 09:54:33,101 - mylog - INFO - 温度: 44.79 2021-08-09 09:55:33,162 - mylog - INFO - 温度: 43.329
|
结语
如读者踩到了其他的坑可联系笔者共同探讨:
邮箱:flashvayne@gmail.com
如果读者担心三极管型号对不上而踩坑,笔者目前还剩下几十个三极管,免费送,你付邮费就可。