Звучит так, как будто аппаратному интерфейсу не нравится многопоточность. Это, безусловно, может быть отлажено и исправлено, но это приложение не требует многопоточности. Простая очередь задач должна сделать свое дело:
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))