在智能风扇之前,主要功能是通过温度控制风扇的自动开关和软件控制风扇开关,但可以实现python
软件没有完成,所以它就消失了。app
我用的覆盆子派,下图附引脚注释:spa
注:树莓派的版本不同,引脚的含义会有一些差异,请注意!code
个人温度传感器是DHT附实物图:11温度模块:blog
树莓派链接温度传感器
VCC接5V或者3.3Vinput
DATA接GPIO嘴(我接的是BCM17号针脚,物理针脚11号)it
GND接GNDclass
根据上述引脚分布图,很容易链接成功。import
获取温度和湿度(python)
import RPi.GPIO as GPIO
import time
channel = 17 ///引脚引号
data = []
j = 0
GPIO.setmode(GPIO.BCM)
time.sleep(1)
GPIO.setup(channel, GPIO.OUT)
GPIO.output(channel, GPIO.LOW)
time.sleep(0.02)
GPIO.output(channel, GPIO.HIGH)
GPIO.setup(channel, GPIO.IN)
while GPIO.input(channel) == GPIO.LOW:
continue
while GPIO.input(channel) == GPIO.HIGH:
continue
while j < 40:
k = 0
while GPIO.input(channel) == GPIO.LOW:
continue
while GPIO.input(channel) == GPIO.HIGH:
k = 1
if k > 100:
break
if k < 8:
data.append(0)
else:
data.append(1)
j = 1
print "sensor is working."
print data
humidity_bit = data[0:8]
humidity_point_bit = data[8:16]
temperature_bit = data[16:24]
temperature_point_bit = data[24:32]
check_bit = data[32:40]
humidity = 0
humidity_point = 0
temperature = 0
temperature_point = 0
check = 0
for i in range(8):
humidity = humidity_bit[i] * 2 ** (7 - i)
humidity_point = humidity_point_bit[i] * 2** (7 - i)
temperature = temperature_bit[i] * 2 ** (7 -i)
temperature_point = temperature_point_bit[i]* 2 ** (7 - i)
check = check_bit[i] * 2 ** (7 - i)
tmp = humidity humidity_point temperature temperature_point
if check == tmp:
print "temperature : ", temperature, ", humidity : " , humidity///输出获得的温度和湿度
else:
print "wrong"
print "temperature : ", temperature, ", humidity : " , humidity, " check : ", check, " tmp : ", tmp
GPIO.cleanup()
注:代码的位置有点混乱,必须自己调整,但代码本身没有问题。