1

I am working with a Pi3, a bread board, a temp sensor and a 4-channel relay. Right now, I have two buttons on my bread board, which, when pushed are supposed to display the temperature and turn on a relay and turn of another. I am working towards building a smart thermostat so think of the buttons representing air-conditioning and heating, they cannot be on at the same time.

My issue is that, when displaying the temperature, it references the object (read_temp()), which displays the temperature correctly for one button and not the other. It will only display the very last part of equation which converts Celsius to Fahrenheit. For example, I will hit both buttons, one will display 75 degrees and the other will always display 32.

I have tried declaring the buttons differently as GPIO.inputs, I have moved to different GPIO pins thinking it is a hardware issue, I have switched the position of the buttons and I have tried returning both temp_c and temp_f in (read_temp()) object and nothing seems to do the trick.

Currently, it is the AC button that is not working properly but as I mentioned, I have switched the buttons and the pins around and it still only displays properly for one.

Thanks!

import RPi.GPIO as GPIO
import time
import os
import glob
import time
import sys
from gpiozero import Button
import pdb



# init list with pin numbers
GPIO.setmode(GPIO.BCM)

pinList = [2, 3, 27, 17]
AC = Button(12)
Heat = Button(21)

# loop through pins and set mode and state to 'high'

for i in pinList: 
    GPIO.setup(i, GPIO.OUT) 
    GPIO.output(i, GPIO.HIGH)


# time to sleep between operations in the main loop

##SleepTimeL = 5

# Setting up temperarture sensor
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
        temp_f = temp_c * 9.0 / 5.0 + 32.0
        return temp_f 


# main loop
try:
    while True:
        if AC.is_pressed:
            GPIO.output(2, GPIO.LOW)
            GPIO.output(3, GPIO.HIGH)
            print(read_temp()), "degrees farenheit"

        if Heat.is_pressed:
            GPIO.output(3, GPIO.LOW)
            GPIO.output(2, GPIO.HIGH)
            print(read_temp()), "degrees farenheit"

except KeyboardInterrupt:
    print "  Quit"

GPIO.cleanup()
Evan Lalo
  • 1,209
  • 1
  • 14
  • 34
  • I cant see the reason why pressing one button would return a different temp from the other. Have you tried debugging the value of "lines" or "temp_string" ? Basically look for reasons why temp_c would give you a zero value. – cvipul May 29 '17 at 12:45
  • Yeah, I have tried debugging that and have not had much luck. To be honest though, I am relatively new to the python debugger so I'l try and give it another shot. Thanks! – Evan Lalo May 29 '17 at 15:05

0 Answers0