Electrical_0ne_2
Posts: 2
Joined: Mon Sep 27, 2021 11:27 am

airconditioning project

Mon Sep 27, 2021 1:29 pm

i found the following code in threads and they seem to work for my setup, i just need to combine them in some way.
the file system is (home)pi- folder-'console'(blank _init_.py)- subfolder-data(clock.py, temp.py ad aircon.py).
i want the on button to import and start a running subloop of def functions and a off button to stop the subloop until the start button is pressed again. also the temperatures of the room, evaporator and condenser to be imported and displayed with clock tab in one window.
while the .py's work they have errors due to construction.
clock- main window.

Code: Select all

#clock.py
from tkinter import Tk, END
from tkinter.ttk import *
import datetime        
import os
type='Linux'

import smbus
DEVICE_BUS = 1
bus = smbus.SMBus(DEVICE_BUS)

window = Tk()
window.title("Sydtems Mode Rev 1.")
window.geometry('408x340') # ('408x310')
window.resizable(True, True)# (False, True) expands length ways    

def clock():
    date2_time = '  '
    date_time = datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S/%p")
    date,time1 = date_time.split()
    time2,time3 = time1.split('/')
    hour,minutes,seconds =  time2.split(':')
    space = ('')
    if int(hour) > 11 and int(hour) < 24:
        time = str(int(hour) - 12) + ':' + minutes + ':' + seconds + ' ' + time3
    else:
        time = time2 + ' ' + time3
    time_label.config(text = time)
    date_label.config(text= date)
    date2_label.config(text = date2_time)
    space_label.config(text = space)
    
    time_label.after(1000, clock)


def alarm():
    main_time = datetime.datetime.now().strftime("%H:%M %p")
    alarm_time = get_alarm_time_entry.get()
    alarm_time1,alarm_time2 = alarm_time.split(' ')
    alarm_hour, alarm_minutes = alarm_time1.split(':')
    main_time1,main_time2 = main_time.split(' ')
    main_hour1, main_minutes = main_time1.split(':')
    if main_time2 == 'PM':
        main_hour = str(int(main_hour1) - 12)
    else:
        main_hour = main_hour1
    if int(alarm_hour) == int(main_hour) and int(alarm_minutes) == int(main_minutes) and main_time2 == alarm_time2:
        os.system('beep -f 5000')# beep through Linux sys.
        alarm_status_label.config(text='Time Is Up')

        get_alarm_time_entry.config(state='enabled')
        set_alarm_button.config(state='enabled')
        get_alarm_time_entry.delete(0,END)
        alarm_status_label.config(text = '')
    else:
        alarm_status_label.config(text='Alarm Has Started')
        get_alarm_time_entry.config(state='disabled')
        set_alarm_button.config(state='disabled')
    alarm_status_label.after(1000, alarm)

### outside security lights ###
def security_lights():
    if True:
        light_button.config(state='enabled')
        bus.write_byte_data(0x11, 3, 0xFF)# security lights        
    if False:
        pass

def light_Stop():
    if True:
        Stop_button.config(state='enabled')
        bus.write_byte_data(0x11, 3, 0x00)# security lights        
    if False:
        pass

### airconditing ###
def Fan():
    fan_button.config(state='enabled')
    if True:
        bus.write_byte_data(0x12, 1, 0xFF)# turns on indoorfan first.
        # start aircon.py in a sub loop
    if False:
        pass

def acoff():
    off_button.config(state='enabled')
    if True:
        bus.write_byte_data(0x12, 1, 0x00)# turns on indoorfan
        bus.write_byte_data(0x12, 2, 0x00)
        bus.write_byte_data(0x12, 3, 0x00)
        bus.write_byte_data(0x11, 4, 0x00)
        # stop aircon.py sub loop
    if False:
        pass

### tabs and labels ###
tabs_control = Notebook(window)
clock_tab = Frame(tabs_control)
alarm_tab = Frame(tabs_control)
systems_tab = Frame(tabs_control)
ac_tab = Frame(tabs_control)

tabs_control.add(clock_tab, text='Clock.')
tabs_control.add(alarm_tab, text='Alarm.')
tabs_control.add(ac_tab, text='Aircon.')
tabs_control.pack(expand = 1, fill ="both")

### clock ###
space_label = Label(clock_tab, font = 'calibri 7 bold', foreground = 'black')
space_label.pack(anchor='n') #position
date2_label = Label(clock_tab, font = 'calibri 5 bold', foreground = 'black')
date2_label.pack(anchor='n') #position (top)
date_label = Label(clock_tab, font = 'calibri 15 bold', foreground = 'black')
date_label.pack(anchor='n') #position (top)
time_label = Label(clock_tab, font = 'calibri 27 bold', foreground = 'black') # added module here
time_label.pack(anchor='center')

### alarm ###
space_label = Label(alarm_tab, font = 'calibri 7 bold', foreground = 'black')
space_label.pack(anchor='n') #position
alarm_instructions_label = Label(alarm_tab, font = 'calibri 12 bold', text = "Enter Alarm. Eg -> 01:30 PM, 01 -> H, 30 -> Min")
alarm_instructions_label.pack(anchor='s')
get_alarm_time_entry = Entry(alarm_tab, font = 'calibri 15 bold')
get_alarm_time_entry.pack(anchor='center')
set_alarm_button = Button(alarm_tab, text = "Set Alarm", command=alarm)
set_alarm_button.pack(anchor='s')
alarm_status_label = Label(alarm_tab, font = 'calibri 15 bold')
alarm_status_label.pack(anchor='s')

space_label = Label(alarm_tab, font = 'calibri 7 bold', foreground = 'black')
space_label.pack(anchor='n') #position
light_button = Button(alarm_tab, text = "Security Lights", state='active', command=lambda:[security_lights()])
light_button.pack(anchor='s')
Stop_button = Button(alarm_tab, text = "Lights Off", state='active', command=lambda:[light_Stop()])
Stop_button.pack(anchor='s')

### aircon ###
space_label = Label(ac_tab, font = 'calibri 15 bold', foreground = 'black')
space_label.pack(anchor='n') #position
fan_button = Button(ac_tab, text = "A/C On", state='active', command=lambda:[Fan()])
fan_button.pack(anchor='s')
off_button = Button(ac_tab, text = "A/C Off", state='active', command=lambda:[acoff()])
off_button.pack(anchor='s')

clock()
window.mainloop()
main error issue is continuous use of a conditional statement with a constant value. a if statement/else with no while to make it constant, when i add it gives errors and if i use the while statement that it wants on buttons it locks the buttons and they don't respond,
temp.py to be displayed in main clock aircon tab- with the buttons function. temp.py is a sensor read/display code but as it updates each sensor consecutively it takes 3 seconds to complete each cycle making the clocks second counter to stall and skip at 3 second timing. so i want this to be a sub loop so it independently reads writes

Code: Select all

#temp.py
from tkinter import Tk
from tkinter.ttk import *

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

#sensor ID and addresses
from w1thermsensor import W1ThermSensor
sensorID_1 = "3c03e83293b3"
sensorID_2 = "3c01e28374f4"
sensorID_3 = "3c06e2856l5a"

window = Tk()
window.title("Systems Temp.")
window.geometry('408x340')
window.resizable(True, True)

def temp():    
    for mySensor in W1ThermSensor.get_available_sensors():
        curSensorID = mySensor.id
        curTemp = mySensor.get_temperature()
        curTemp = round(curTemp,2)
        
        if curSensorID == sensorID_1:
            myTempCabin = curTemp
            cabin = ("  Cabin=  " + str(myTempCabin) + "*C")
            cab_label.config(text = cabin)
            
        elif curSensorID == sensorID_2:
            myTempCondenser = curTemp
            condenser = ("  Condenser=  " + str(myTempCondenser) + "*C")
            con_label.config(text = condenser)
            
        elif curSensorID == sensorID_3:
            myTempEvaporator = curTemp
            evaporator = ("  Evaporator=  " + str(myTempEvaporator) + "*C")
            evap_label.config(text = evaporator)
            
    window.after(333, temp)

tabs_control = Notebook(window)
ac_tab = Frame(tabs_control)
tabs_control.add(ac_tab, text='aircon.')
tabs_control.pack(expand = 1, fill ="both")

cab_label = Label(ac_tab, font = 'calibri 15 bold', foreground = 'black')
cab_label.pack(anchor='n') #position
con_label = Label(ac_tab, font = 'calibri 15 bold', foreground = 'black')
con_label.pack(anchor='n') #position
evap_label = Label(ac_tab, font = 'calibri 15 bold', foreground = 'black')
evap_label.pack(anchor='n') #position

window.after_idle(temp)

window.mainloop()

the last code needed for any air-conditioner is a control code using temperature sensors to turn the compressor and condenser fan on/off by relays. aircon.py-

Code: Select all

#aircon.py

from tkinter import Tk
from tkinter.ttk import *

import smbus
DEVICE_BUS = 1
bus = smbus.SMBus(DEVICE_BUS)

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

from w1thermsensor import W1ThermSensor
sensorID_1 = "3c03e83293b3"
sensorID_2 = "3c01e28374f4"
sensorID_3 = "3c06e2856l5a"

window = Tk()
window.title("Systems Aircon")
window.geometry('408x340') # ('408x310')
window.resizable(True, True)# (False, True) expands length ways        

def aircon():
    for mySensor in W1ThermSensor.get_available_sensors():# error redefining name 'mySensor' from outer scope (line 15).
        curSensorID = mySensor.id
        curTemp = mySensor.get_temperature()
        curTemp = round(curTemp,2)
        
        if curSensorID == sensorID_1:
            value = curTemp
            if value >= 26: # turns audio alarm on over 30*C
                bus.write_byte_data(0x11, 4, 0xFF)
            elif value <= 25:# turns audio alarm off under 29.99*C
                bus.write_byte_data(0x11, 4, 0x00)
            
        elif curSensorID == sensorID_2:
            value = curTemp
            if value >= 65.00:# turns condenser fan on over 65*C
                bus.write_byte_data(0x12, 2, 0xFF)
            elif value <= 64.99:# turns condenser fan off under 65*C
                bus.write_byte_data(0x12, 2, 0x00)# turns off condensor fan 

        elif curSensorID == sensorID_3:
            value = curTemp
            if value >= 26.00:# turns on compressor over 26*C
                bus.write_byte_data(0x12, 3, 0xFF) 
            elif value <= 24.00:#turns off compressor at 24*C
                bus.write_byte_data(0x12, 3, 0x00)

    window.after(1000, aircon)

tabs_control = Notebook(window)
ac_tab = Frame(tabs_control)
tabs_control.add(ac_tab, text='aircon.')
tabs_control.pack(expand = 1, fill ="both")

window.after_idle(aircon)

window.mainloop()
the A/C button should start def aircon() function loop as well as the evaporator's fan which is a constant on outside the aircon() loop.
the A/C off button stops the def aircon() loop which stopps the compressor and all fans. it resets until the A/C on button is pressed again.

in conclusion
A. how do i fix windows to get clock as main window containing everything else?
B. how to create a temperature sub loop and import multiple readouts to main clock window?
C. how to activate and deactivate a def function loop by buttons in main clock window?
D. how to fix a conditional statement using a constant Value error where the constant is variable by the change in data...
cheers.

JennyWilliamson
Posts: 1
Joined: Fri Oct 01, 2021 8:17 pm

Re: airconditioning project

Fri Oct 01, 2021 8:18 pm

Following

Return to “Other projects”