Python Forum

Full Version: DHT11 Sensor Programming
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Might be a better question for a Raspberry Pi forum, but I figured I'd go right to the source. I am building a temperature sensor and using python to program it. The sensor is a DHT11 and I've successfully wired it up and got it to work decently.

I normally program with C++ and I've just barely started using Python and I'm just trying to write a simple 'if' statement using the input from the sensor.

I should also note that before I added the if statements, it worked perfectly and was printing out the values of the temperature and humidity. But now I basically want to be able to store this value so that I can use it in other programs.

My code looks like:
import RPi.GPIO as GPIO
import dht11
import time
import datetime

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

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

try:
    for x in range(50):
        result = instance.read()
        if result.is_valid():
            print("Last valid input: " + str(datetime.datetime.now()))
            print("Temperature: %d C" % result.temperature)
            print("Temperature: %d F" % ((result.temperature * 9/5+32)))
            print("Humidity: %d %%" % result.humidity)
            farenheit = (result.temperature * 9/5 + 32)
            if result.temperature == 24
                print("TURN AC ON. TEMP AT 75")
            elif result.temperature > 24
                print("TURN AC ON")
            elif result.temperature < 24
                print("TURN AC OFF")
        time.sleep(1)

except KeyboardInterrupt:
    print"Keyboard Interrupt Noticed"
finally:
    GPIO.cleanup()
It's missing the : at the end of the if condition.
import RPi.GPIO as GPIO
import dht11
import time
import datetime
 
# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()
 
# read data using pin 17
instance = dht11.DHT11(pin=17)
 
try:
    for x in range(50):
        result = instance.read()
        if result.is_valid():
            print("Last valid input: " + str(datetime.datetime.now()))
            print("Temperature: %d C" % result.temperature)
            print("Temperature: %d F" % ((result.temperature * 9/5+32)))
            print("Humidity: %d %%" % result.humidity)
            farenheit = (result.temperature * 9/5 + 32)
            if result.temperature == 24:
                print("TURN AC ON. TEMP AT 75")
            elif result.temperature > 24:
                print("TURN AC ON")
            elif result.temperature < 24:
                print("TURN AC OFF")
        time.sleep(1)
 
except KeyboardInterrupt:
    print"Keyboard Interrupt Noticed"
finally:
    GPIO.cleanup()
Never mind. Missing colons. Still used to C++
Thank You!