tommygee123
tommygee123

Reputation: 19

Raspberry-pi - DHT11 + Relay trigger

I am a complete noob when it comes to Python and the Raspberry Pi unit but I am figuring it out.

I am working on a script to monitor the current temperature of my greenhouse that I am building. When the temp gets to 28C, I would like for it to activate my relay which will turn on the fan. At 26C the relay should turn off.

Build info: Raspberry Pi 3 dht11 tempurature - GPIO pin 20 single relay board - GPIO pin 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)

The current errors I am getting:

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

Upvotes: -1

Views: 1816

Answers (1)

abhi
abhi

Reputation: 1756

For the current error you're facing, keep in mind that Python relies heavily on indentation. It's not like other languages such as C++ and Java that use curly braces to arrange statements.

To fix the indentation in your code, please see below:

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)

In any if, else, elif, for, or while statement, the code that you want to execute must be indented within the statement in order for it to run, or else you will get the error that you are currently seeing.

There are a few more errors in your code but I'll let you figure out the rest! Welcome to programming in Python and using Raspberry Pi.

Upvotes: 1

Related Questions