线程问题 python (dht22)

Threading with python trouble (dht22)

处理智能花园设置。我正在使用三个不同的函数进行线程处理,这样如果被触发,我可以在预定时间内 运行 lamp/pump/fan。使用 lamp 和泵进行穿线时没有问题。但是当尝试使用 Dht22 线程时,程序将工作一段时间然后抛出 "argument must be an int, or be format of file.no()" 错误我认为问题是由于数组格式,但我不知道如何只读取温度dht22 或使线程与数组一起工作。感谢帮助

这是我的代码:

import time
import datetime
import grovepi
import threading


# Pin-modes

dht_sensor = 4                  
light_sensor = 0           
moisture_sensor = 1         

pump = 3        ######
lamp = 7        ######       
fan = 8         ######

grovepi.pinMode(dht_sensor, "INPUT")
grovepi.pinMode(light_sensor, "INPUT")
grovepi.pinMode(moisture_sensor, "INPUT")

grovepi.pinMode(pump, "OUTPUT")
grovepi.pinMode(lamp, "OUTPUT")
grovepi.pinMode(fan, "OUTPUT")

# Threshold values

temp_crit_val = 90       
light_crit_val = 10
moisture_crit_val = 60

def lamp_auto():
    while True:
         readTime = datetime.datetime.now().strftime("%S")

        if(11 < int(readTime) < 19) or (31 < int(readTime) < 39) or (51 < int(readTime) < 59):
             actualTime = datetime.datetime.now().strftime("%H:%M")
             light = grovepi.analogRead(light_sensor)
             light = 100 * light / 1023
             print("Light = ",light)
             if ((actualTime > "07:03")and(actualTime < "18:34")): #sunrise and sunset
                 if light <= light_crit_val:
                     grovepi.digitalWrite(lamp, 1)
            else:
                grovepi.digitalWrite(lamp, 0)
        else:
            grovepi.digitalWrite(lamp,0)

        time.sleep(5)


def pump_auto():     
    while True:
         readTime = datetime.datetime.now().strftime("%S")

         if(11 < int(readTime) < 19) or (31 < int(readTime) < 39) or (51 < int(readTime) < 59):
             soil_moisture = grovepi.analogRead(moisture_sensor)
             soil_moisture = 100 - (100 * soil_moisture / 1023)
             print("Soil Moisture = ",soil_moisture)

             if soil_moisture <= moisture_crit_val:
            grovepi.digitalWrite(pump, 1)
        else:
            grovepi.digitalWrite(pump, 0)

    time.sleep(5)


def fan_auto():
     readTime = datetime.datetime.now().strftime("%S")

     if(11 < int(readTime) < 19) or (31 < int(readTime) < 39) or (51 < int(readTime) < 59):
         [temp,hum] = grovepi.dht(dht_sensor,1)
         temp = temp*9/5+32
         if all ([temp,hum]):
             print('temperature={} humidity={}'.format(temp,hum)
             if temp >= temp_crit_val:
                 grovepi.digitalWrite(fan, 1)
                time.sleep(50)
             else:
                 grovepi.digitalWrite(fan, 0)
      time.sleep(5)


x = threading.Thread(target=lamp_auto)
x.start()
time.sleep(0.5)
y =  threading.Thread(target=pump_auto)
y.start()

while True:
    fan_auto()

错误:
''' I/O 对已关闭文件的操作 参数必须是一个 int,或者有一个 fileno() 方法 '''

听起来有点像硬件接口不喜欢线程。这绝对可以调试和修复,但这个应用程序并没有向我尖叫多线程。一个简单的任务队列应该可以解决问题:

import time
import datetime
import grovepi


# Pin-modes

dht_sensor = 4                  
light_sensor = 0           
moisture_sensor = 1         

pump = 3        ######
lamp = 7        ######       
fan = 8         ######

grovepi.pinMode(dht_sensor, "INPUT")
grovepi.pinMode(light_sensor, "INPUT")
grovepi.pinMode(moisture_sensor, "INPUT")

grovepi.pinMode(pump, "OUTPUT")
grovepi.pinMode(lamp, "OUTPUT")
grovepi.pinMode(fan, "OUTPUT")

# Threshold values

temp_crit_val = 90       
light_crit_val = 10
moisture_crit_val = 60

def lamp_auto():
    readTime = datetime.datetime.now().strftime("%S")

    if(11 < int(readTime) < 19) or (31 < int(readTime) < 39) or (51 < int(readTime) < 59):
        actualTime = datetime.datetime.now().strftime("%H:%M")
        light = grovepi.analogRead(light_sensor)
        light = 100 * light / 1023
        print("Light = ",light)
        if ((actualTime > "07:03")and(actualTime < "18:34")): #sunrise and sunset
            if light <= light_crit_val:
                grovepi.digitalWrite(lamp, 1)
        else:
            grovepi.digitalWrite(lamp, 0)
    else:
        grovepi.digitalWrite(lamp,0)

    return time.time() + 5 #return next scheduled time to execute


def pump_auto():
     readTime = datetime.datetime.now().strftime("%S")

     if(11 < int(readTime) < 19) or (31 < int(readTime) < 39) or (51 < int(readTime) < 59):
         soil_moisture = grovepi.analogRead(moisture_sensor)
         soil_moisture = 100 - (100 * soil_moisture / 1023)
         print("Soil Moisture = ",soil_moisture)

         if soil_moisture <= moisture_crit_val:
             grovepi.digitalWrite(pump, 1)
     else:
         grovepi.digitalWrite(pump, 0)

     return time.time() + 5 #return next scheduled time to execute


def fan_auto():
     readTime = datetime.datetime.now().strftime("%S")

     if(11 < int(readTime) < 19) or (31 < int(readTime) < 39) or (51 < int(readTime) < 59):
         [temp,hum] = grovepi.dht(dht_sensor,1)
         temp = temp*9/5+32
         if all ([temp,hum]):
             print('temperature={} humidity={}'.format(temp,hum))
             if temp >= temp_crit_val:
                 grovepi.digitalWrite(fan, 1)
                 return time.time() + 50 #return next scheduled time to execute
             else:
                 grovepi.digitalWrite(fan, 0)
     return time.time() + 5 #return next scheduled time to execute



#using collections.deque might be faster or more efficient here, but it wouldn't be noticible.
task_queue = [(0, lamp_auto), #schedule the three services to start right away
              (0, pump_auto),
              (0, fan_auto)]

while len(task_queue) > 0: #while there are remaining tasks
    t, func = task_queue.pop(0) #get next task
    wait_time = t - time.time()
    if wait_time > 0:
        time.sleep(wait_time)
    t_next = func()
    #insert (t_next, func) into queue, such that the queue remains sorted
    for i in range(len(task_queue)):
        if t_next < task_queue[i][0]:
            task_queue.insert(i, (t_next, func))
            break
    else:
        task_queue.append((t_next, func))