Mhp1972
Posts: 6
Joined: Sun Dec 10, 2017 9:03 pm

Control relay from temperature probe

Sun Dec 10, 2017 9:15 pm

Hi every one
Hope you can help a rookie in phyton and Raspberry PI. I'am trying to control a relay, from data coming from my temperature probe DS18B20. I have the code below and my relay will not turn on and off, i probaly u the statement for turning PIN 11 HIGH or LOW at the wrong place in the code or used wrong definition. Temperatur readings is working fine.

Hope you can help thank you Michael

import os
import glob
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
relay=11
GPIO.setup(11,GPIO.OUT)
#GPIO.output(11, GPIO.HIGH)


os.system("modprobe w1=gpio")
os.system("modprobe w1=therm")

base_dir = "/sys/bus/w1/devices/"
device_folder = glob.glob(base_dir + "28*")[0]
device_file = device_folder + "/w1_slave"

def read_temp_raw():
f = open(device_file, "r")
lines = f.readlines()
f.close()
return lines

def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != "YES":
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find("t=")
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
return temp_c,

if (read_temp <= 24):
GPIO.output(relay, GPIO.HIGH)
elif (read_temp > 24):
GPIO.output(relay, GPIO.LOW)

while True:

print(read_temp())
time.sleep(5)
GPIO.cleanup()
Last edited by Mhp1972 on Tue Dec 12, 2017 7:30 pm, edited 1 time in total.

Paul Hutch
Posts: 243
Joined: Fri Aug 25, 2017 2:58 pm
Location: Blackstone River Valley, MA, USA

Re: Control relay from temperature probe

Sun Dec 10, 2017 10:57 pm

Please post your code using the "code" format feature so we can check thew indention. In Python indentation is crucial and what I see has none.

As a wild guess based on assuming perfect indentation:
Since the temperature value is a floating point number odd things can happen in the comparisons.

Try changing this line

Code: Select all

elif (read_temp > 24):
to just

Code: Select all

else:

PingoBags
Posts: 44
Joined: Mon Nov 06, 2017 10:53 pm
Location: NS, Canada

Re: Control relay from temperature probe

Sun Dec 10, 2017 11:28 pm

You have GPIO.cleanup() in your loop, I'm sure that's just gonna reset your pins. Remove that part from you while true loop.

Try, instead of setting pin 11 high to turn the relay on, try setting it low to turn it on. Set it high to turn it off. Worth a shot, my relay turns on when set low.
Like

Code: Select all

if (read_temp <= 24):
  GPIO.output(relay, GPIO.LOW)
elif (read_temp > 24):
  GPIO.output(relay, GPIO.HIGH)
Or you can do a one liner. It'l do the same thing as the code above

Code: Select all

GPIO.output(relay, not (read_temp <= 24))

User avatar
DougieLawson
Posts: 33043
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Control relay from temperature probe

Mon Dec 11, 2017 12:58 am

Delete these two lines

Code: Select all

os.system("modprobe w1=gpio")
os.system("modprobe w1=therm")
Add a line to /boot/config.txt

Code: Select all

dtoverlay=w1-gpio
to replace that stuff.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

Mhp1972
Posts: 6
Joined: Sun Dec 10, 2017 9:03 pm

Re: Control relay from temperature probe

Mon Dec 11, 2017 8:37 pm

Thank you

For your reply, unfurtunatley i still having problems controlling the relay

pcmanbob
Posts: 3986
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Control relay from temperature probe

Mon Dec 11, 2017 9:13 pm

So I can see several problems with your code.

you have the if statement outside your while True loop and you have clean up within your while True loop.
also you are calling the temp read loop several times at different points when it might change between them.

So I have changed your code

Code: Select all

import os
import glob
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
relay=11
GPIO.setup(11,GPIO.OUT)
GPIO.output(11, GPIO.HIGH)
print "Relay should be off"




base_dir = "/sys/bus/w1/devices/"
device_folder = glob.glob(base_dir + "28*")[0]
device_file = device_folder + "/w1_slave"

def read_temp_raw():
    f = open(device_file, "r")
    lines = f.readlines()
    f.close()
    return lines

def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != "YES":
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find("t=")
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        return temp_c,


while True:

    result = (read_temp())
    
    if (result <= 24):
        GPIO.output(relay, GPIO.HIGH)
        Print "Relay should be off"
    else:
        GPIO.output(relay, GPIO.LOW)
        Print "Relay should be on"        
    time.sleep(5)
    
so now your if statement is inside you while True loop and when we get the temp we save the resulting temp in a variable called result.

now we can test this variable in the if statement and print it so we know what temp we are working with, also changed the elif to else and removed the test as if we don't want the relay off we must want it on.
also added some print statements so you can see what should be happening.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Mhp1972
Posts: 6
Joined: Sun Dec 10, 2017 9:03 pm

Re: Control relay from temperature probe

Tue Dec 12, 2017 6:14 am

Thank you pcmanbob i Will try the Codee When i get Home from works :)

PingoBags
Posts: 44
Joined: Mon Nov 06, 2017 10:53 pm
Location: NS, Canada

Re: Control relay from temperature probe

Tue Dec 12, 2017 11:35 am

Mhp1972 wrote:
Tue Dec 12, 2017 6:14 am
Thank you pcmanbob i Will try the Codee When i get Home from works :)
If pcmanbob code doesn't work, I suggest you post up a picture of your gpio pins.
You're using BOARD, pin 11, which is not the same as BCM pin 11. BCM pin 11 is actually BOARD pin 23. Not saying you have mixed them up, just saying it's worth a look at.

Also this part of his code

Code: Select all

GPIO.setup(11,GPIO.OUT)
GPIO.output(11, GPIO.HIGH)
print "Relay should be off"
I believe will toggle the the relay on/off when you first run the script. So if you have a light hooked to the relay, it'l flicker.
I think by default, GPIO.setup sets it to low which will turn the relay on.
Changing this

Code: Select all

GPIO.setup(11,GPIO.OUT)
GPIO.output(11, GPIO.HIGH)
to

Code: Select all

GPIO.setup(11, GPIO.OUT,initial=1)
should prevent that.

Mhp1972
Posts: 6
Joined: Sun Dec 10, 2017 9:03 pm

Re: Control relay from temperature probe

Tue Dec 12, 2017 7:24 pm

i will try :)
Last edited by Mhp1972 on Tue Dec 12, 2017 7:42 pm, edited 3 times in total.

DirkS
Posts: 8093
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Control relay from temperature probe

Tue Dec 12, 2017 7:36 pm

On line 39 change

Code: Select all

    result = (read_temp())
to

Code: Select all

    result = read_temp()

Return to “Python”

Who is online

Users browsing this forum: No registered users and 15 guests