2016-07-16 41 views
-1

当谈到Python和Raspberry Pi单元时,我是一个完整的noob,但我正在计算它。Raspberry-pi-DHT11 +中继触发器

我正在编写一个脚本来监视我正在建造的温室的当前温度。当温度达到28C时,我想让它激活我的继电器,它将打开风扇。在26℃时,继电器应关闭。

生成信息: 树莓派3 DHT11 tempurature - GPIO引脚20 单继电器板 - GPIO引脚21

import RPi.GPIO as GPIO 
import dht11 
import time 
import datetime 
from time import sleep 

# initialize GPIO 
GPIO.setwarnings(False) 
GPIO.setmode(GPIO.BCM) 
GPIO.cleanup() 

# Set relay pins as output 
GPIO.setup(21, GPIO.OUT) 

# read data using pin 20 
instance = dht11.DHT11(pin=20) 

while True: 
result = instance.read() 
tempHI = 28 
tempLOW = 26 
if result >= tempHI 
     GPIO.output(21, GPIO.HIGH) #turn GPIO pin 21 on 
ifels result < tempLOW 
     GPIO.output(21, GPIO.LOW) #Turn GPIO pin 21 off 
time.sleep(1) 

当前的错误我得到:

python ghouse.py 
File "ghouse.py", line 19 
result = instance.read() 
^ 
IndentationError: expected an indented block 
+1

Python使用缩进来分组块。阅读https://docs.python.org/release/3.4.3/tutorial/introduction.html#first-steps-towards-programming并使用Python感知编辑器。然后在行'while True:'下面添加四个空格。 – Dietrich

回答

1

对于当前请注意,Python严重依赖在缩进。它不像其他语言,如C++和Java,它们使用大括号来排列语句。

要修复在你的代码的缩进,请参阅以下内容:

import RPi.GPIO as GPIO 
import dht11 
import time 
import datetime 
from time import sleep 

# initialize GPIO 
GPIO.setwarnings(False) 
GPIO.setmode(GPIO.BCM) 
GPIO.cleanup() 

# Set relay pins as output 
GPIO.setup(21, GPIO.OUT) 

# read data using pin 20 
instance = dht11.DHT11(pin=20) 

while True: 
    result = instance.read() 
    tempHI = 28 
    tempLOW = 26 
    if result >= tempHI: 
     GPIO.output(21, GPIO.HIGH) #turn GPIO pin 21 on 
    ifels result < tempLOW: 
     GPIO.output(21, GPIO.LOW) #Turn GPIO pin 21 off 
time.sleep(1) 

在任何ifelseeliffor,或while声明,要执行必须在语句中缩进代码为了它运行,否则你会得到你目前看到的错误。

你的代码还有一些错误,但我会让你找出其余的!欢迎使用Python进行编程并使用Raspberry Pi。

+0

谢谢,学徒! – tommygee123

+0

真正把我抛弃的事实是,我的脚本将通过if语句的第一行运行,但被else语句卡住,抱怨语法错误: pi @ raspberrypi:〜/ dht11_python/DHT11_Python -master $ python ghouse.py 文件“ghouse.py”,第26行 else result tommygee123

+0

Progress: 'code' – tommygee123