Steve_Townsend
Posts: 5
Joined: Mon Feb 29, 2016 10:20 am

Greater than signs not working

Mon Feb 29, 2016 11:03 am

Hello

I started using a raspberry pi 2 a few weeks ago with the intention of creating a system that can take a temperature reading from a thermostat sending it back to the pi and then closing or opening a relay to power an actuator that would heat an area.
So far this has been going well. I've spent some time reading through "programming your raspberry pi" by Simon Monk, and that has helped alot.

I have connected 2 heat sensors to the pi using a bread board which can talk to the pi perfectly and give me temperature readings. I have also connected a pi face to the board to give me the relays and outputs I need to open and close according to the temprature.

The problem is, if I get a temprature reading from the sensor saying 20°, for example, my code says if the temprature is 22 or less it should open a relay, above 22 it should close the relay. Currently it closes the relay no matter what the temprature is, or if I swap the greater than signs so it is opposite it opens them no matter what.

A second problem, I've set the code so if the relays open it controls outputs 3 and 4 but 3 blinks once and 4 remains on until told to close.

Any help is greatly appritiated
Thanks

This is the code,

Code: Select all

import os
import time

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

temp_sensor_1 = '/sys/bus/w1/devices/28-0000073ee0a2/w1_slave'

def temp_raw_1():
    f = open(temp_sensor_1, 'r')
    lines = f.readlines()
    f.close()
    return lines

def read_temp_1():
    lines_1 = temp_raw_1()
    while lines_1[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines_1 = temp_raw_1()
    temp_output_1 = lines_1[1].find('t=')
    if temp_output_1 != -1:
        temp_string_1 = lines_1[1].strip()[temp_output_1+2:]
        temp_c_1 = float(temp_string_1) / 1000.0
        return temp_c_1


temp_sensor_2 = '/sys/bus/w1/devices/28-0000043dfb4e/w1_slave'

def temp_raw():
    f = open(temp_sensor_2, 'r')
    lines = f.readlines()
    f.close()
    return lines

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

while True:
    print('outside;')
    print(read_temp_1())
    print ('inside;')
    print(read_temp())
    print ('---')

    if read_temp_1 <= 22:
        import pifacedigitalio as p
        p.init()
        p.digital_write(3,1)
        print ('outside Relay 3 Open')
        print ('-----')

    if read_temp_1 > 22:
        import pifacedigitalio as p
        p.init()
        p.digital_write (3,0)
        print ('outside Relay 3 Closed')
        print('-----')

    if read_temp <= 22:
        import pifacedigitalio as p
        p.init()
        p.digital_write(4,1)
        print ('inside Relay 4 Open')
        print ('----')

    if read_temp > 22:
        import pifacedigitalio as p
        p.init()
        p.digital_write (4,0)
        print ('inside Relay 4 Closed')
        print ('-----')
    
    time.sleep(25)

And it prints out.
Lounge 19.5
Bedroom 17

Relay closed
Relay closed

texy
Forum Moderator
Forum Moderator
Posts: 5157
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Greater than signs not working

Mon Feb 29, 2016 2:48 pm

Steve_Townsend wrote: if read_temp_1 > 22:
.
.
.
.
.
if read_temp <= 22:
[/code]
Hi and welcome to the forum.
Do you see a difference between the 2 lines?
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

Steve_Townsend
Posts: 5
Joined: Mon Feb 29, 2016 10:20 am

Re: Greater than signs not working

Mon Feb 29, 2016 2:58 pm

Hi Texy

I belive that the top line looks at the value of 'read_temp_1'(one sensor) and if it's above or below 22 it should open or close the relay

The second line is the same but 'read_temp' is what I have named the other sensor
Thanks for replying :D

User avatar
RogerW
Posts: 285
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: Greater than signs not working

Mon Feb 29, 2016 3:13 pm

I think
if read_temp_1 <= 22:
should be
if read_temp_1() <= 22:

read_temp_1 is a function and should be called.

Incidentally it is inefficient to put the imports in the loop. Convention is to put them at the top of the file. That may go for
p.init() as well.

User avatar
jojopi
Posts: 3084
Joined: Tue Oct 11, 2011 8:38 pm

Re: Greater than signs not working

Mon Feb 29, 2016 3:16 pm

You should use Python3, where it is actually an error to attempt to compare the name of a function against a number.

Steve_Townsend
Posts: 5
Joined: Mon Feb 29, 2016 10:20 am

Re: Greater than signs not working

Mon Feb 29, 2016 3:19 pm

I've just changed the code to say 'read_temp_1 ()' and the same for 'read_temp' too and its worked!

But the other problem still remains that when the I want outputs 3 and 4 to have the LED light up only output 4 lights up the 3rd output blinks once and stays off.

But thanks for the help Roger!!


As for using Python 3 I don't think the sensors work with it but I may be wrong. If we can get the LED outputs working I'll still to python 2 for now but thanks for the input. :)

texy
Forum Moderator
Forum Moderator
Posts: 5157
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Greater than signs not working

Mon Feb 29, 2016 3:37 pm

Try this :

Code: Select all

while True:
    print('outside;')
    temp_c_1=read_temp_1()
    print(temp_c_1)
    print ('inside;')
    temp_c=read_temp()
    print(temp_c)
    print ('---')

    if temp_c_1 <= 22.0:
        print ('outside Relay 3 Open')
        print ('-----')

    if temp_c_1 > 22.0:
        print ('outside Relay 3 Closed')
        print('-----')

    if temp_c <= 22.0:
        print ('inside Relay 4 Open')
        print ('----')

    if temp_c > 22.0:
        print ('inside Relay 4 Closed')
        print ('-----')

Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

Steve_Townsend
Posts: 5
Joined: Mon Feb 29, 2016 10:20 am

Re: Greater than signs not working

Mon Feb 29, 2016 3:47 pm

Thanks Texy

I've added the code and this also works.
But if I remove
'Import digitalfaceio as p'
'p.init ()'
'P.digital.write (x,y)'
Won't it stop talking to the pi face digital board all together?
Or will it still open the outputs?

User avatar
RogerW
Posts: 285
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: Greater than signs not working

Mon Feb 29, 2016 3:53 pm

Import digitalfaceio as p
Only needs to appear once so put it at the beginning of your file
p.init ()
probably only needs to execute once so again I would put that at the beginning of your program
P.digital.write (x,y)
is doing the work so leave it where it is

texy
Forum Moderator
Forum Moderator
Posts: 5157
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Greater than signs not working

Mon Feb 29, 2016 4:02 pm

Sorry, I removed those lines so that I could test the code, but as RogerW has pointed out, you probably don't want/need to do it the way you were anyway....

Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

Steve_Townsend
Posts: 5
Joined: Mon Feb 29, 2016 10:20 am

Re: Greater than signs not working

Mon Feb 29, 2016 4:04 pm

That's brilliant it's all working perfectly!

Fyi the reason the 3rd output didn't work was because of the import and p.init commands as you guys suggested.
Thanks again for all your input it's really helped!

Return to “Beginners”