2016-07-16 42 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