0

Well, I had written a code for Automatic Cooling with DHT 11, but I did not get any successful result in it, I want the Relay turn on when the humidity rises up, and I did not have any errors in my code, Please find me a solution!!

This is my code -

import RPi.GPIO as GPIO
import dht11
import lcddriver
import time

#Light = 26
Fan = 26

display = lcddriver.lcd()
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()

#GPIO.setup(Light, GPIO.OUT)
GPIO.setup(Fan, GPIO.OUT)

instance = dht11.DHT11(pin=17)

GPIO.output(Fan, False)

def fan_on():
    GPIO.output(Fan, True)
def fan_off():
    GPIO.output(Fan, False)

try:
    while True:
        result = instance.read()


#        if result.is_valid():
#       display.lcd_display_string("Temp : "+str(result.temperature)+" C",1)
#       display.lcd_display_string("Humid : "+str(result.humidity)+"%",2)
#           time.sleep(2)

    if (str(result.humidity)) >= 65:
            fan_on()
            #time.sleep(1)

        elif (str(result.humidity)) < 65:
            fan_off()
            #time.sleep(3)


except KeyboardInterrupt:
    print("Cleaning up!")
    display.lcd_clear()
    GPIO.output(Fan, False)
    GPIO.cleanup()
  • 3
    i see improperly indented code – jsotola Jun 27 at 17:05
  • 1
    This is a how do I write Python question. Not specific to the Pi. How can you get past the while true loop? – joan Jun 27 at 17:50
  • one thing you may want to research is hysteresis – Jaromanda X Jun 27 at 23:14
  • I did not have any errors in my code - if that were so, your code would do what you expect – Jaromanda X Jun 27 at 23:15
  • @joan - to be fair, I think there's just one line (the if statement) poorly indented in the question - if the code were exactly as in the question, the relay would never turn on/off "suddenly" (as per question title) or at all – Jaromanda X Jun 27 at 23:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.