线程与 python 问题 (dht22) [英] Threading with python trouble (dht22)

查看:59
本文介绍了线程与 python 问题 (dht22)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

处理智能花园设置.我正在使用三种不同的功能进行线程处理,以便在触发时我可以在预定的时间内运行灯/泵/风扇.用灯和泵穿线时没有问题.但是当尝试使用 Dht22 线程时,程序会工作一段时间,然后抛出参数必须是 int,或者是 file.no() 格式"的错误我认为问题是由于数组格式,但我不知道如何仅从 dht22 读取温度或使线程与数组一起工作.感谢您的帮助

Dealing with a smart garden setup. I am doing threading with three different functions so that if triggered I can run the lamp/pump/fan for a predetermined time. While threading with lamp and pump there is no problem. But when attempting to thread with Dht22 the program will work for a while and then throw an error of "argument must be an int, or be format of file.no()" I think the problem is due to the array format, but I don't know how to read just the temperature from the dht22 or to make the thread work with the array. Thanks for the help

这是我的代码:

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 操作参数必须是一个整数,或者有一个 fileno() 方法'''

Error:
''' I/O operation on a closed file argument must be an int, or have a fileno() method '''

推荐答案

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

Sounds a bit like the hardware interface is not liking the threading. This could definitely be debugged and fixed, but this application does not scream multi-threading to me. A simple task queue should do the trick:

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))

这篇关于线程与 python 问题 (dht22)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆