Python Forum
Relay switches On and Off every cylcle...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Relay switches On and Off every cylcle...
#3
You will have to create the file with an initial value with a text editor

This should do the trick:
import RPi.GPIO as GPIO
import Adafruit_DHT
from time import sleep
 
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12,GPIO.OUT)
GPIO.setup(16,GPIO.OUT)
 
###LOW = ON     HIGH = OFF###
 
sensor = Adafruit_DHT.AM2302
pin = 2
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
 
def get_setting():
    with open('last_setting.txt') as f:
        last_setting = f.read

def save_setting(value):
    with open('last_setting.txt', 'w') as f:
        f.write(value) + '\n'

def switch():
    get_setting()
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    GPIO.setup(12,GPIO.IN)
    if temperature > 30 and GPIO.input(12) == True:            #MAX TEMP
        if last_setting != 'LOW':
            GPIO.setup(12,GPIO.OUT)
            GPIO.output(12, GPIO.LOW)
            save_setting('LOW')
        print('ACF ON')
    elif temperature < 29 and GPIO.input(12) == False:          #MIN TEMP
        if last_setting != 'HIGH':
            GPIO.setup(12,GPIO.OUT)
            GPIO.output(12, GPIO.HIGH)
            save_setting('HIGH')
        print('ACF OFF')
      
while True:
    switch()
    sleep(0.1)
Reply


Messages In This Thread
RE: Relay switches On and Off every cylcle... - by Larz60+ - Nov-28-2017, 06:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert looping home security to a one time scan of switches and sensors duckredbeard 0 1,757 Dec-08-2020, 04:31 AM
Last Post: duckredbeard
  Problem connecting to 2 switches, HELP! Netcode 0 1,231 Nov-15-2019, 10:30 PM
Last Post: Netcode

Forum Jump:

User Panel Messages

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