gpt4 book ai didi

python - python 线程问题 (dht22)

转载 作者:太空宇宙 更新时间:2023-11-03 19:40:28 24 4
gpt4 key购买 nike

处理智能花园设置。我正在使用三个不同的函数进行线程处理,这样如果触发,我就可以运行灯/泵/风扇预定的时间。与灯和泵线程时没有问题。但是当尝试使用 Dht22 进行线程时,程序将工作一段时间,然后抛出“参数必须是 int,或者是 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() 方法 '''

最佳答案

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

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 - python 线程问题 (dht22),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60461901/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com