Page 1 of 1

Need a little help and input on controller

Posted: Tue Sep 11, 2018 3:22 am
by JoshD086
Hello all, i am pretty new to raspberry pi and python language, i studied C and C++ but that was years ago before i decided on a different profession, and i am super rusty. I'm not 100% sure that this is posted in the correct section, if not, please point me in the right direction.

I am trying to build a controller that will turn a system off in part or whole depending on a couple temp inputs. It will be for a walk in produce storage area/fridge. I think I have got the basics figured out, but need to fine tune some things. The while loop is where most of the trouble is, I think. I would like to be able to keep the temp readings printing while the script is running but need the relays to continue to operate in the states programmed per those readings. If there is a better/cleaner way to achieve this, i am all for it. I would also like to figure out how to make the script run for maybe 15-30 minutes so long as the temp readings are correct, and then run every hour, or maybe its better to just have it run and just sleep for a given time. Any help or insight is greatly appreciated.
Here is the code:

Code: Select all

#!/usr/bin/env python3

import os
import glob
import time
from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
#GPIO Pins: 22 fan, 24 compressor
pinList = [22,24]
for i in pinList:
    GPIO.setup(i, GPIO.OUT)
    GPIO.output(i, GPIO.HIGH)

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

base_dir='/sys/bus/w1/devices/'
device_folder_1=glob.glob(base_dir + '28-041720f964ff')[0]
device_folder_2=glob.glob(base_dir + '28-041720f612ff')[0]
device_folder_3=glob.glob(base_dir + '28-0417211dadff')[0]
device_file_1=device_folder_1 + '/w1_slave'
device_file_2=device_folder_2 + '/w1_slave'
device_file_3=device_folder_3 + '/w1_slave'
set_temp = 50.0
ice_temp = 32.0

def read_temp_raw_1():
    f = open(device_file_1, 'r')
    lines = f.readlines()
    f.close()
    return lines

def read_temp_1():
    lines = read_temp_raw_1()
    while lines[0].strip() [-3:] != 'YES':
        time.sleep(10.0)
        lines = read_temp_raw_1()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_f_1= float(temp_string) / 1000 *9.0 / 5.0 + 32.0
        return temp_f_1

def read_temp_raw_2():
    f = open(device_file_2, 'r')
    lines = f.readlines()
    f.close()
    return lines

def read_temp_2():
    lines = read_temp_raw_2()
    while lines[0].strip() [-3:] != 'YES':
        time.sleep(10.0)
        lines = read_temp_raw_2()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_f_2= float(temp_string) / 1000 *9.0 / 5.0 + 32.0
        return temp_f_2

def read_temp_raw_3():
    f = open(device_file_3, 'r')
    lines = f.readlines()
    f.close()
    return lines

def read_temp_3():
    lines = read_temp_raw_3()
    while lines[0].strip() [-3:] != 'YES':
        time.sleep(10.0)
        lines = read_temp_raw_3()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_f_3= float(temp_string) / 1000 *9.0 / 5.0 + 32.0
        return temp_f_3
    
    
while True:
    print ("Interior Temp:", round(read_temp_1(), 1), "coil:", round(read_temp_2(), 1), "compressor:", round(read_temp_3(),1))
    if read_temp_2()>= ice_temp:
        GPIO.output(22, GPIO.HIGH)
        time.sleep(120)
       
    else:
       GPIO.output(22, GPIO.LOW)   
           
    if read_temp_1()<= set_temp:
       GPIO.output(24, GPIO.LOW)
       
    else:
        GPIO.output(24, GPIO.HIGH)
        time.sleep(300)