Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DHT11 Sensor Programming
#1
Question 
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()
Reply
#2
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()
Reply
#3
Never mind. Missing colons. Still used to C++
Reply
#4
Thank You!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Wiring DHT11 to Raspberry pi hcccs 3 4,014 Feb-06-2021, 08:42 PM
Last Post: hcccs
  DHT11 output to website problem cjdock 0 1,443 Oct-01-2019, 08:29 PM
Last Post: cjdock
  Code Wireless Temperature sensor and send sensor readings to google sheet jenkins43 0 2,168 Nov-29-2018, 12:44 PM
Last Post: jenkins43
  RPi with IR Sensor pimlicosnail 1 2,172 Jun-24-2018, 08:53 AM
Last Post: Larz60+
  Writing data from a DHT11 to LCD expplane 2 3,312 Aug-07-2017, 03:38 PM
Last Post: expplane

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020