首页 > 解决方案 > 我的功能使它无法做任何事情,除非我重新启动程序

问题描述

from tkinter import *
import RPi.GPIO as GPIO
import time
import sys




DHTPIN = 14

GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.OUT)
GPIO.output(21, GPIO.LOW)
MAX_UNCHANGE_COUNT = 100

STATE_INIT_PULL_DOWN = 1
STATE_INIT_PULL_UP = 2
STATE_DATA_FIRST_PULL_DOWN = 3
STATE_DATA_PULL_UP = 4
STATE_DATA_PULL_DOWN = 5

GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.OUT)
GPIO.output(21, GPIO.LOW)

root = Tkinter.Tk()
root.geometry("700x700")
root.title("House Control")

cur_temp = 70
counter = Tkinter.IntVar()
def toggle29():
  if GPIO.input(21):
    GPIO.output(21, GPIO.LOW)
    toggleButton29["text"] = "Turn BLUE 29 On"
  else:
    GPIO.output(21, GPIO.HIGH)
    toggleButton29["text"] = "Turn BLUE 29 Off"

def system_state():
        if on_btn.config('text')[-1] == 'On':
            on_btn.config(text='Off')

        else:
            on_btn.config(text='On')

def system_ac_state():    
    if ac_btn.config('text')[-1] == 'Cooling': 
        ac_btn.config(text='Heating')
        ac_heating()
        main()
    else:
    ac_btn.config(text='Cooling')
    ac_cooling()

def ac_heating():
    print("heating")
    pass
def ac_cooling():
    print("cooling")
    pass

def cool_temp():
    print ("cooling")

def heat_temp():
    print ("heating")

def onClick1(event=None):
    counter.set(counter.get() - 1)

def onClick(event=None):
    counter.set(counter.get() + 1)

def toggle0():
  if GPIO.input(17):
    GPIO.output(17, GPIO.LOW)
    toggleButton0["text"] = "Turn RED 0 On"
  else:
    GPIO.output(17, GPIO.HIGH)
     toggleButton0["text"] = "Turn RED 0 Off"

 def read_dht11_dat():
     GPIO.setup(DHTPIN, GPIO.OUT)
     GPIO.output(DHTPIN, GPIO.HIGH)
     time.sleep(0.05)
     GPIO.output(DHTPIN, GPIO.LOW)
     time.sleep(0.02)
     GPIO.setup(DHTPIN, GPIO.IN, GPIO.PUD_UP)

     unchanged_count = 0
     last = -1
     data = []
     while True:
         current = GPIO.input(DHTPIN)
         data.append(current)
         if last != current:
             unchanged_count = 0
             last = current
         else:
             unchanged_count += 1
             if unchanged_count > MAX_UNCHANGE_COUNT:
                 break

     state = STATE_INIT_PULL_DOWN

     lengths = []
     current_length = 0

     for current in data:
         current_length += 1

         if state == STATE_INIT_PULL_DOWN:
         if current == GPIO.LOW:
             state = STATE_INIT_PULL_UP
         else:
             continue
     if state == STATE_INIT_PULL_UP:
         if current == GPIO.HIGH:
             state = STATE_DATA_FIRST_PULL_DOWN
         else:
             continue
     if state == STATE_DATA_FIRST_PULL_DOWN:
         if current == GPIO.LOW:
             state = STATE_DATA_PULL_UP
         else:
             continue
     if state == STATE_DATA_PULL_UP:
         if current == GPIO.HIGH:
             current_length = 0
             state = STATE_DATA_PULL_DOWN
         else:
             continue
     if state == STATE_DATA_PULL_DOWN:
         if current == GPIO.LOW:
             lengths.append(current_length)
             state = STATE_DATA_PULL_UP
         else:
             continue
 if len(lengths) != 40:
     print ("Data not good, skip")
     return False

 shortest_pull_up = min(lengths)
 longest_pull_up = max(lengths)
 halfway = (longest_pull_up + shortest_pull_up) / 2
 bits = []
 the_bytes = []
 byte = 0

 for length in lengths:
     bit = 0
     if length > halfway:
         bit = 1
     bits.append(bit)
 print ("bits: %s, length: %d") % (bits, len(bits))
 for i in range(0, len(bits)):
     byte = byte << 1
     if (bits[i]):
         byte = byte | 1
     else:
         byte = byte | 0
     if ((i + 1) % 8 == 0):
         the_bytes.append(byte)
         byte = 0
 print (the_bytes)
 checksum = (the_bytes[0] + the_bytes[1] + the_bytes[2] + the_bytes[3]) & 
0xFF
 if the_bytes[4] != checksum:
     print ("Data not good, skip")
     return False

 return the_bytes[0], the_bytes[2]

当这个 def main 运行时,它会锁定我的整个程序。我需要能够从中获取信息,以便我可以将这些信息用于 if 和 when 语句。那么我如何运行这个脚本而不是锁定我的整个脚本呢?

def main():
    print ("Raspberry Pi wiringPi DHT11 Temperature test program\n")
    while True:
        result = read_dht11_dat()
        if result:
            humidity, temperature= result
            temp = (temperature * 9/5)+32     
            test = 73
            low = temp >= test
            print ("humidity: %s %%,  Temperature: %s C (F %s)") % 
(humidity, temperature, temp)
            if low:
              while True:  
                GPIO.output(21,GPIO.HIGH)
                print ("humidity: %s  r %s c %s") % (humidity, temperature, 
temp)
                continue
            else:
                GPIO.output(21, GPIO.LOW)
            print ("1humidity: %s ") % (low)
        time.sleep(1)
        continue

def destroy():
    GPIO.cleanup()    

toggleButton0 = Tkinter.Button(root, text="0 Carport Lights", 
command=toggle0, foreground="white", background="red")
toggleButton0.grid(row=0,column=0)    
Tkinter.Label(root, text="Current Temperature").grid(row=0, column=1)    
Tkinter.Label(root, text=cur_temp).grid(row=1, column=1)
Tkinter.Label(root, text="Desired Temperature").grid(row=0, column=2)     
Tkinter.Label(root, textvariable=counter).grid(row=1, column=2)
Tkinter.Button(root, text="Increase", command=onClick, fg="dark green", bg = 
"white").grid(row=1, column=3)
Tkinter.Button(root, text="Decrease", command=onClick1, fg="dark green", bg 
= "white").grid(row=1, column=0)
Tkinter.Label(root, text="You are using :").grid()
ac_btn = Tkinter.Button(root, text="Heating", command=system_ac_state, 
fg="dark green", bg = "white")
ac_btn.grid()
Tkinter.Button(root, text="Main", command=main, fg="dark green", bg = 
"white").grid(row=2, column=1)
Tkinter.Label(root, text="Turn System:").grid()

运行时,应用程序将检查系统的状态,如果它打开,它将检查 system_ac_state 并使用主脚本中的变量来控制 gpio。

on_btn = Tkinter.Button(root, text="On", command=system_state, fg="dark 
green", bg = "white")
on_btn.grid()

root.mainloop()

标签: pythontkintergpio

解决方案


推荐阅读