最近一直在研究树莓派的 GPIO,传感器很简单,就是检测高电平和低电平,于是就写了下面这个传感器组合程序,选择传感器的 BORARD 通道号和输出通道号,在选择模式(土壤传感器,雨水传感器,光线传感器,人体传感器)程序就会运行。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
channel = input("请输入传感器BOARD通道号:")
light = input("请输入灯光BOARD通道号:")
chosemode = input('请选择模式: \n(1.土壤 2.雨水 3.光线 4.人体)')
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(channel, GPIO.IN)
GPIO.setup(light, GPIO.OUT)
if chosemode == 1:
mode = "土壤"
out1 = "潮湿"
out2 = "干燥"
if chosemode == 2:
mode = "雨水"
out1 = "有雨"
out2 = "无雨"
if chosemode == 3:
mode = "光线"
out1 = "有光"
out2 = "无光"
if chosemode == 4:
mode = "人体"
out1 = "有人"
out2 = "无人"
try:
while True:
if GPIO.input(channel) == GPIO.LOW:
nowtime = time.strftime('%m-%d %H:%M:%S',time.localtime(time.time()))
print(nowtime)
print(mode + "检测结果:" + out1)
GPIO.output(light, GPIO.LOW)
else:
nowtime = time.strftime('%m-%d %H:%M:%S',time.localtime(time.time()))
print(nowtime)
print(mode + "检测结果:" + out2)
GPIO.output(light, GPIO.HIGH)
time.sleep(5)
except KeyboardInterrupt:
GPIO.cleanup()
print("已取消")