Threading with python trouble (dht22)

我们两清 提交于 2020-12-13 17:57:28

问题


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

Here is my code:

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

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


回答1:


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


来源:https://stackoverflow.com/questions/60461901/threading-with-python-trouble-dht22

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!