2015-10-15 39 views
1

我正在製作一個自動溫室,通過測量溫度,有些事情要像打開一個窗口或控制風扇一樣根據值的變化在溫度,我使用tkinter在python上創建了這個程序,所以當條件成立時,elif,如果它們是在函數內部def它們發送打印,但是當我運行我的代碼時,即使溫度值發生變化,我也會將此溫度值發送給其他def函數他們使用,所以他們似乎總是使用第一,如果忽略其餘條件。我嘗試了調試,但是因爲我正在使用Beaglebone中的Geany,所以似乎無法找到選項。通過線程向def函數發送變化的變量

#!/usr/bin/python2.7 

import thread 
import Tkinter as tk 
from time import sleep 

import Adafruit_BBIO.PWM as PWM 
import Adafruit_BBIO.GPIO as GPIO 
import Adafruit_BBIO.ADC as ADC 

#PWM.start(channel,duty,freq=1000,polarity=0) 
#values for duty are from 0.0 to 100.0 (%) 

PWM.start("P8_13",0,50) 
GPIO.setup("P8_15",GPIO.OUT) 
GPIO.output("P8_15",GPIO.LOW) 
GPIO.setup("P8_16",GPIO.OUT) 
GPIO.output("P8_16",GPIO.LOW) 


PWM.start("P9_14",0,50) 



ADC.setup() 

def med_temp(close): 
     adclose=close 
     while adclose < 1: 
         raw=ADC.read('P9_33')*1800 
         t=raw/10 
         temp1=round(t,0) 
         tempvar.set(temp1) 
         sleep(1) 

def window_thread(close, value): 
     gpioclose1=close 
     temp=value 

     while gpioclose1<1: 
       if temp>=30.0 : 
         #print temp 
         duty=90 
         PWM.set_duty_cycle("P9_14",duty) 
         sleep(.1) 
         windvar.set('OPEN',) 
         label11["fg"]='green' 

       else: 
         duty=0 
         PWM.set_duty_cycle("P9_14",duty) 
         sleep(.1) 
         windvar.set('CLOSE') 
         label11["fg"]='red' 



def fan1_thread(close,value): 
     gpioclose=close 
     temp=value 
     while gpioclose<1: 
       if temp<=23.0: 
         #out 
         GPIO.output("P8_15", GPIO.LOW)#OUT 
         GPIO.output("P8_16", GPIO.HIGH) 
         sleep(.1) 
         ventvar.set('OUT ') 
         label6["fg"]='#6c132b' 


       elif temp>=40.0: 
       #in 
         GPIO.output("P8_15", GPIO.HIGH)#IN 
         GPIO.output("P8_16", GPIO.LOW) 
         sleep(.1) 
         ventvar.set('IN ') 
         label6["fg"]='#5cbdea' 

       else: 
         #off 
         GPIO.output("P8_15", GPIO.LOW)#OFF 
         GPIO.output("P8_16", GPIO.LOW) 
         sleep(.1) 
         ventvar.set('OFF ') 
         label6["fg"]='red' 






def fan2_thread(close,value): 
       pwmclose=close 
       temp=value 
       while pwmclose<1: 
         if temp<23.0: 
           duty=100 
           PWM.set_duty_cycle("P8_13",duty) 
           sleep(.1) 
           dutyvar.set(duty) 

         elif temp<25.0 and temp>=23.0: 
           duty=50 
           PWM.set_duty_cycle("P8_13",duty) 
           sleep(.1) 
           dutyvar.set(duty) 

         elif temp>=40.0: 
           duty=100 
           PWM.set_duty_cycle("P8_13",duty) 
           sleep(.1) 
           dutyvar.set(duty) 
         else: 
           duty=0 
           PWM.set_duty_cycle("P8_13",duty) 
           sleep(.1) 
           dutyvar.set(duty) 


def salir(): 
     pwmclose = 1 
     adclose =1 
     gpioclose =1 
     gpioclose1=1 
     PWM.stop("P8_13") 
     PWM.cleanup() 
     GPIO.cleanup() 
     exit() 




mainw = tk.Tk() 
mainw.title("Greenhouse control") 

#contenido 


tempvar = tk.DoubleVar() 
ventvar = tk.StringVar() 
dutyvar = tk.DoubleVar() 
windvar = tk.StringVar() 

label1 = tk.Label(mainw,text='  Temperature: ', width=len('  Temperature: '),bg='red') 
label2 = tk.Label(mainw, textvariable=tempvar, bg='white') 
label3 = tk.Label(mainw, text='C', width=len('C'), bg='white') 
label4 = tk.Label(mainw, text='  FAN ', width=len('     FAN '),bg='purple') 
label5 = tk.Label(mainw, text='Estado: ',width=len('Estate: ')) 
label6 = tk.Label(mainw, textvariable=ventvar,bg='white') 
slider = tk.Scale(mainw, variable = dutyvar,from_=100, to=0)# 
label9 = tk.Label(mainw, text=' WINDOW  ', width=len('     WINDOW  '), bg='purple') 
label10 = tk.Label(mainw, text='Estate: ',width=len('State: ')) 
label11 = tk.Label(mainw, textvariable=windvar, bg='white') 

thread.start_new_thread(window_thread,(0,tempvar,)) 
thread.start_new_thread(med_temp, (0,)) 
thread.start_new_thread(fan1_thread, (0,tempvar,)) 
thread.start_new_thread(fan2_thread, (0,tempvar,)) 

salirButton = tk.Button(mainw, text='EXIT', command=salir,activeforeground='red') 

#set layout 
label2.grid(row=0, column=1) 
label1.grid(row=0, column=0) 
label3.grid(row=0, column=2) 
label4.grid(row=1,column=0, columnspan=4) 
label5.grid(row=2, column=0) 
label6.grid(row=2, column=1) 
slider.grid(row=2, column=2, columnspan=2) 
label9.grid(row=3, column=0, columnspan=4) 
label10.grid(row=4, column=0) 
label11.grid(row=4, column=1) 
salirButton.grid(row=5, column=0, columnspan=4) 
mainw.mainloop() 

所以我去發送打印溫度裏面,如果我得到PY_VARO。在這一行 thread.start_new_thread(window_thread,(0,tempvar,)) 我發送到def函數0關閉函數,我發送tempvar是一個雙精度。如果我寫 thread.start_new_thread(window_thread,(0,tempvar.get))
我得到在打印臨時的,如果裏面: 綁定的方法DoubleVar.get Tkinter.DoubleVar

,所以我想,如果條件不會被忽略,但是當我啓動線程時,我將錯誤的值發送給def了?什麼正確的格式發送給一個函數?

+0

我認爲你有錯別字temp = valor(value)。此外,函數永不退出,即只在初始啓動時執行,因爲while變量不會改變,即「while adclose <1」,addclose永遠不會改變,所以while()循環永遠不會退出。在測試過程中簡化程序並單獨測試每個功能而無需線程的時間。 –

+0

我得到PY_VARO - Tkinter的StringVar()等都使用get和set方法來訪問這些值。 Effbot的頁面說明了set()方法http://effbot.org/tkinterbook/variable.htm –

+0

Tkinter和線程不會混合,應該避免。相反,看看使用'後'方法:http://effbot.org/tkinterbook/widget.htm – ebarr

回答

0

Welp,我是通過創建許多線程做錯了。我終於讓我的溫室做我想做的事。這是我的最終代碼:

#!/usr/bin/python2.7 

import thread 
import Tkinter as tk 
from time import sleep 

import Adafruit_BBIO.PWM as PWM 
import Adafruit_BBIO.GPIO as GPIO 
import Adafruit_BBIO.ADC as ADC 

#PWM.start(channel,duty,freq=1000,polarity=0) 
#values for duty are from 0.0 to 100.0 (%) 

#VENTILADOR 
PWM.start("P8_13",0,4000) 
GPIO.setup("P8_15",GPIO.OUT) 
GPIO.output("P8_15",GPIO.LOW) 
GPIO.setup("P8_16",GPIO.OUT) 
GPIO.output("P8_16",GPIO.LOW) 

#VENTANA 
PWM.start("P9_14",1,50)#start closed servo 


#temperatura 
ADC.setup() 

def med_temp(close): 
     adclose=close 
     while adclose < 1: 
         raw=ADC.read('P9_33')*1800 
         t=raw/10 
         temp=round(t,0) 
         tempvar.set(temp) 
         if temp <23: 
           GPIO.output("P8_15", GPIO.LOW)#OUT 
           GPIO.output("P8_16", GPIO.HIGH) 
           PWM.set_duty_cycle("P8_13",100) 
           PWM.set_duty_cycle("P9_14",1)#closed 
           sleep(.1) 
           windvar.set('CLOSE') 
           label11["fg"]='red' 
           ventvar.set('OUT ') 
           label6["fg"]='#6c132b' 

           dutyvar.set(100) 

         elif temp >= 23 and temp < 25: 
           PWM.set_duty_cycle("P8_13",50) 
           GPIO.output("P8_15", GPIO.LOW)#OUT 
           GPIO.output("P8_16", GPIO.HIGH) 
           PWM.set_duty_cycle("P9_14",1)#closed 
           sleep(.1) 

           windvar.set('CLOSE') 
           label11["fg"]='red' 

           ventvar.set('OUT ') 
           label6["fg"]='#6c132b' 

           dutyvar.set(50) 

         elif temp>=25 and temp<30: 
           GPIO.output("P8_15", GPIO.LOW)#OFF 
           GPIO.output("P8_16", GPIO.LOW) 
           PWM.set_duty_cycle("P8_13",0) 
           PWM.set_duty_cycle("P9_14",1)#closed 
           sleep(.1) 

           windvar.set("CLOSE") 
           label11["fg"]='red' 

           ventvar.set('OFF ') 
           label6["fg"]='red' 

           dutyvar.set(0) 
           #print temp 
           #print'OFF 0% CLOSE' 

         elif temp>=30 and temp < 35: 
           GPIO.output("P8_15", GPIO.LOW)#OFF 
           GPIO.output("P8_16", GPIO.LOW) 
           PWM.set_duty_cycle("P8_13",0) 
           PWM.set_duty_cycle("P9_14",5)#opened 
           sleep(.1) 

           windvar.set('OPEN') 
           label11["fg"]='green' 

           ventvar.set('OFF ') 
           label6["fg"]='red' 

           dutyvar.set(0) 
           print temp 
           #print'OFF 0% OPEN' 

         elif temp>=35 and temp < 40: 
           GPIO.output("P8_15", GPIO.HIGH)#IN 
           GPIO.output("P8_16", GPIO.LOW) 
           PWM.set_duty_cycle("P8_13",50) 
           PWM.set_duty_cycle("P9_14",5)#opened 
           sleep(.1) 

           windvar.set('OPEN') 
           label11["fg"]='green' 

           ventvar.set('IN ') 
           label6["fg"]='#5cbdea' 

           dutyvar.set(50) 
           print temp 
           #print'IN 50% OPEN' 
         else: 
           GPIO.output("P8_15", GPIO.HIGH)#IN 
           GPIO.output("P8_16", GPIO.LOW) 
           PWM.set_duty_cycle("P8_13",100) 
           PWM.set_duty_cycle("P9_14",5)#opened 
           sleep(.1) 

           windvar.set('OPEN') 
           label11["fg"]='green' 

           ventvar.set('IN ') 
           label6["fg"]='#5cbdea' 

           dutyvar.set(100) 
           print temp 
           #print'IN 100% OPEN' 
         sleep(1) 


def salir(): 
     adclose =1 
     #gpioclose =1 
     #gpioclose1=1 
     PWM.stop("P8_13") 
     PWM.stop("P9_14") 
     PWM.cleanup() 
     GPIO.cleanup() 
     exit() 




mainw = tk.Tk() 
mainw.title("Control de Invernadero") 

#contenido 


tempvar = tk.DoubleVar() 
ventvar = tk.StringVar() 
dutyvar = tk.DoubleVar() 
windvar = tk.StringVar() 

label1 = tk.Label(mainw,text='  Temperatura: ', width=len('  Temperatura: '),bg='red') 
label2 = tk.Label(mainw, textvariable=tempvar, bg='white') 
label3 = tk.Label(mainw, text='C', width=len('C'), bg='white') 
label4 = tk.Label(mainw, text='  VENTILADOR ', width=len('     VENTILADOR '),bg='purple') 
label5 = tk.Label(mainw, text='Estado: ',width=len('Estado: ')) 
label6 = tk.Label(mainw, textvariable=ventvar,bg='white') 
slider = tk.Scale(mainw, variable = dutyvar,from_=0, to=100, orient = tk.HORIZONTAL)# 
label9 = tk.Label(mainw, text=' VENTANA  ', width=len('     VENTANA  '), bg='purple') 
label10 = tk.Label(mainw, text='Estado: ',width=len('Estado: ')) 
label11 = tk.Label(mainw, textvariable=windvar, bg='white') 

thread.start_new_thread(med_temp, (0,)) 


salirButton = tk.Button(mainw, text='SALIR', command=salir,activeforeground='red') 

#set layout 
label2.grid(row=0, column=1) 
label1.grid(row=0, column=0) 
label3.grid(row=0, column=2) 
label4.grid(row=1,column=0, columnspan=4) 
label5.grid(row=2, column=0) 
label6.grid(row=2, column=1) 
slider.grid(row=2, column=2, columnspan=2) 
label9.grid(row=3, column=0, columnspan=4) 
label10.grid(row=4, column=0) 
label11.grid(row=4, column=1) 
salirButton.grid(row=5, column=0, columnspan=4) 
mainw.mainloop()