Posts: 4
Threads: 2
Joined: Nov 2017
Nov-28-2017, 05:50 PM
(This post was last modified: Nov-28-2017, 05:50 PM by hotwalk.)
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...
Posts: 12,031
Threads: 485
Joined: Sep 2016
Nov-28-2017, 06:03 PM
(This post was last modified: Nov-28-2017, 06:03 PM by Larz60+.)
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!
Posts: 12,031
Threads: 485
Joined: Sep 2016
Nov-28-2017, 06:10 PM
(This post was last modified: Nov-28-2017, 06:10 PM by Larz60+.)
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)
Posts: 4
Threads: 2
Joined: Nov 2017
Nov-29-2017, 03:10 PM
(This post was last modified: Nov-29-2017, 03:49 PM by hotwalk.)
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)
Posts: 103
Threads: 15
Joined: Nov 2017
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?
Posts: 2,125
Threads: 11
Joined: May 2017
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'))
Posts: 4
Threads: 2
Joined: Nov 2017
(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
|