树莓派4b OpenWrt 安装RPI.GPIO控制PWM风扇(附完整代码)

1、安装python3

# opkg update
# opkg install python3-base
# opkg install python3
# opkg install python3-pip
# opkg install python3-dev

2、安装RPI.GPIO

# pip install rpi.gpio

3、GPIO 控制PWM风扇

### 交互模式演示代码
# python

import RPi.GPIO as GPIO

# GPIO 设置
GPIO.setmode(GPIO.BCM)
GPIO.setup(14,GPIO.OUT)

# 高电平,开风扇
GPIO.output(14,GPIO.HIGH)

# 低电平,关风扇
GPIO.output(14,GPIO.LOW)

# 退出并清理
GPIO.cleanup()

4、系统实时温度查询

# cat /sys/class/thermal/thermal_zone0/temp

5、demo fan_control.py  

 1 import RPi.GPIO as GPIO
 2 import time
 3 import sys
 4 import logging
 5 
 6 # 日志配置
 7 logging.basicConfig(filename='/root/some_code/fan/temp.log', level=logging.INFO, filemode='a', format='%(message)s')
 8 log = logging.getLogger(__name__)
 9 
10 # 风扇GPIO控制通道
11 FAN_GPIO = 14
12 
13 # 转速调整间隔,单位为秒
14 SAMPLING = 10
15 
16 # 设定目标温度,取第一个参数
17 SP_TEMP = int(sys.argv[1:][0])
18 
19 log.info('-------- fan py started,temp is['+ str(SP_TEMP) +'] --------')
20 
21 # PID参数值只采用P控制
22 PID_Kp = 0.01
23 
24 # PID输出控制量为0~100的占空比对应0%到100%的转速
25 PID_OUTPUT = 0
26 
27 # 读取CPU的温度值
28 def get_cpu_temp():
29     with open('/sys/class/thermal/thermal_zone0/temp') as f:
30         cpu_temp = int(f.read())
31     return cpu_temp
32 
33 def main():
34     
35     # 设定GPIO的基本参数
36     GPIO.setwarnings(False)
37     GPIO.setmode(GPIO.BCM)
38     GPIO.setup(FAN_GPIO,GPIO.OUT)
39     GPIO.output(FAN_GPIO,GPIO.LOW)
40     
41     # 100Hz PWM
42     pwm = GPIO.PWM(FAN_GPIO,100)
43     
44     # 默认启动转速为10%
45     pwm.start(10)
46 
47     try:
48         while True:
49             temp = get_cpu_temp()
50 
51             log.info(' ')
52             log.info('date['+time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))+'],CPU Temperature:['+ str(temp) +']')
53             
54             # 比例控制算法
55             PID_OUTPUT = (temp - SP_TEMP * 1000) * PID_Kp
56             log.info('PID_OUTPUT:['+ str(PID_OUTPUT) +']')
57 
58             # 控制器输出限幅
59             if PID_OUTPUT > 100:
60                 PID_OUTPUT = 100
61             if PID_OUTPUT < 0:
62                 PID_OUTPUT = 0
63             
64         # 通过控制PWM的占空比来控制转速
65             pwm.ChangeDutyCycle(PID_OUTPUT)
66             
67             time.sleep(SAMPLING)
68     except KeyboardInterrupt:
69         pass
70     GPIO.cleanup()
71 
72 if __name__=='__main__':
73     main()
展开代码

6、调用及用shell相关代码

    python fan_control.py  40

    shell 传入温度并切换到后台运行 

    给执行权限

    # chmod +x /root/some_code/fan/run_fan_control.sh

   调用

    # ./root/some_code/fan/run_fan_control.sh 40

#!/bin/bash

temp=$1

log_file="/root/some_code/fan/temp.log"

nohup python /root/some_code/fan/fan_control.py $temp >> $log_file 2>&1 &

echo $! > /root/some_code/fan/fc.pid

    关闭后台运行代码

pid_file="/root/some_code/fan/fc.pid"
pid=`cat $pid_file`
kill -9 $pid
rm -rf $pid_file

 7、添加到开机运行

fan_init.sh

#!/bin/sh /etc/rc.common

START=99

start(){
    sleep 5
    cd /root/some_code/fan/ && ./run_fan_control.sh 40
}

stop(){
    sleep 5
    pid_file="/root/some_code/fan/fc.pid"
    pid=`cat $pid_file`
    kill -9 $pid
    rm -rf $pid_file
}

给执行权限

# chmod +x /root/some_code/fan/fan_init.sh

# 复制到脚本到系统初始化脚本目录

# cp /root/some_code/fan/fan_init.sh /etc/init.d/

# 添加开机启动

# /etc/init.d/fan_init.sh enable

8、查看是否添加成功(如图,添加成功会在/etc/rc.d 目录下生成一个指向启动脚本的软连接)

# ls -lrt /etc/rc.d/ | grep fan_init

 

9、pip 安装GPIO报错解决:

pip直接安装报错,复制图中的包地址到浏览器手动下载,然后传到服务器上解压手动安装

手动安装报错,如下图,再安装gcc

 再次到解压后的包里面执行  python setup.py install  最后提示安装成功,可以使用 pip list查看

已经安装成功!

 

pwm风扇代码参考资料:https://blog.csdn.net/u011822609/article/details/128128975

posted @ 2024-05-11 21:37  往事隨風~  阅读(483)  评论(0编辑  收藏  举报