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...
#1
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 switch():
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    GPIO.setup(12,GPIO.IN)
    if temperature > 30 and GPIO.input(12) == True:            #MAX TEMP
        GPIO.setup(12,GPIO.OUT)
        GPIO.output(12, GPIO.LOW)
        print('ACF ON')
    elif temperature < 29 and GPIO.input(12) == False:          #MIN TEMP
        GPIO.setup(12,GPIO.OUT)
        GPIO.output(12, GPIO.HIGH)
        print('ACF OFF')
    


while True:
    switch()
    sleep(0.1)
when i run this code, my Relay switches Off (because it is colder than 30°C) but when the script runs another cycle, it rill turn off for a short duration and turns off immediatly after...
my aim is to have a switch that turns the relay on, when the temperature is over 30°C and off when its colder than 29°C...
Reply
#2
does your:
GPIO.setup(12,GPIO.IN)
if run by itself activate the relay?
some io pins can be memory mapped to activate upon read.
also, with the else clause, you are always calling
either:
GPIO.setup(12,GPIO.OUT)
GPIO.output(12, GPIO.LOW)
or:
GPIO.setup(12,GPIO.OUT)
GPIO.output(12, GPIO.HIGH)
which is definately sending a signal to your relay.

you can keep a simple text file which holds nothing more than high or low,
which shows the last setting, if already there, don't mess with the settings!
Reply
#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
#4
where do i have to put the textfile? in the folder where the script is located?

by the way, ive tried to accomplish the same by:

def switch():
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    if temperature > 30 and GPIO.input(12) == True:    #MAX TEMP
        GPIO.output(12, GPIO.LOW)
        print('ACF ON')
    elif temperature < 29 and GPIO.input(12) == False:
        GPIO.output(12, GPIO.HIGH)
        print('ACF OFF')
    else:
        sleep(10)
Reply
#5
And another thought, cant you not read the current status of the PIN. For instance, check if the PIN is set HIGH or LOW by just reading right bit from the memory. Because most GPIO pins must memory mapped and reading relevant memory should give you the state? This way you could avoid creating a file to store the state?

Useless reading PIN values is not an option?
Reply
#6
What you want to use is something like a Schmitt trigger. You can do this as a class because you need the last state. Using global variables is not good.

Here an example:

import time


class SchmittTrigger:

    def __init__(self, low_val, high_val):
        self.state = False
        self.low_val = low_val
        self.high_val = high_val

    def update(self, value):
        if value < self.low_val:
            self.state = True
        elif value > self.high_val:
            self.state = False
        # when the value is between low and high
        # the last state is unchanged
        return self.state


temperatures = [15, 20, 26, 28, 29, 30, 31, 32, 33, 30, 30, 29, 28, 27, 26, 25]
schmitt_trigger = SchmittTrigger(28, 31)

text = 'Temperature at {} °C heater is {}.'

for temp in temperatures:
    time.sleep(1)
    print(text.format(temp, 'on' if schmitt_trigger.update(temp) else 'off'))
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#7
(Nov-29-2017, 05:40 PM)DeaD_EyE Wrote: What you want to use is something like a Schmitt trigger. You can do this as a class because you need the last state. Using global variables is not good.

Here an example:

import time


class SchmittTrigger:

    def __init__(self, low_val, high_val):
        self.state = False
        self.low_val = low_val
        self.high_val = high_val

    def update(self, value):
        if value < self.low_val:
            self.state = True
        elif value > self.high_val:
            self.state = False
        # when the value is between low and high
        # the last state is unchanged
        return self.state


temperatures = [15, 20, 26, 28, 29, 30, 31, 32, 33, 30, 30, 29, 28, 27, 26, 25]
schmitt_trigger = SchmittTrigger(28, 31)

text = 'Temperature at {} °C heater is {}.'

for temp in temperatures:
    time.sleep(1)
    print(text.format(temp, 'on' if schmitt_trigger.update(temp) else 'off'))

sounds good, sadly im a pretty newb in python...
could you give me a few more details on how i could form that into a fitting code for my needings :D
Reply


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,722 Dec-08-2020, 04:31 AM
Last Post: duckredbeard
  Problem connecting to 2 switches, HELP! Netcode 0 1,195 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