TypeError: объект int не вызывается при сравнении показаний с интервалом в 5 секунд - PullRequest
0 голосов
/ 12 мая 2019

Настройка Raspberry Pi с датчиком dht22 для автоматического включения душа. Кажется, я не могу заставить скрипт прочитать датчик, подождать 5 секунд и прочитать его снова, а затем выполнить какие-то действия, если показания изменились. Я не программист, и мне это удалось вместе с помощью друзей и поисков в Google, но в итоге я понятия не имею, что я делаю.

У меня есть сценарий, который включает свет в зависимости от показаний влажности выше X, однако при изменении относительной влажности сценарий не так точен, как хотелось бы.

import RPi.GPIO as GPIO
import Adafruit_DHT
sensor = Adafruit_DHT.DHT22
pin = 17
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.IN)                    #Read output from PIR motion sensor
time.sleep(2)                            #Waiting 2 seconds for the sensor to initiate

state=None
i=None
t2=None
t=None
d=5.0
while True:
#read humidity and store, wait 5 seocnds and read/store again
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) #takes reading from the sensor
    t='{1:0.1f}'.format(temperature, humidity) #takes the humidity reading and commits to 'h'
    time.sleep=5
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    t2='{1:0.1f}'.format(temperature, humidity)
#if the second reading is 5 or more that the first set the state to 1
    if t2 >= str(5.0+(float(t))):
        state=1                         #sets 'state' to 1 if the humidity is high
    else:
#If the second reading is 3 less that the first set the state to 0 
        t2 <= str((float(t)-3.0))
        state=0                         #sets 'state' to 0 if humidity is not high
#storing state as 'i' and trying to do a check loop
    if i == state:                        #state didn't change
        print "holding", t, i
        time.sleep(2)
#what to do if 'i' chnages
    else:
        i = state                          #if the states doesn't  match  set 'i'  to equal 'state'
        if i == 1:                         #what to do if 'state' is '1'
            print "Showering",t,t2,i
            time.sleep(2)
                                                                                            #end of curl
        else:                             #what to do if 'state' is '0'
            print "not showering",t,t2,i
            time.sleep(2)
time.sleep(2)

Ожидается, что он будет продолжать печатать состояние на основе изменений

что я на самом деле получаю, это:

sudo python humitest.py не душа 45,3 45,3 0 Traceback (последний вызов был последним): Файл "humitest.py", строка 67, в time.sleep (2) Ошибка типа: объект 'int' не может быть вызван

1 Ответ

0 голосов
/ 12 мая 2019
import RPi.GPIO as GPIO
import Adafruit_DHT
sensor = Adafruit_DHT.DHT22
pin = 17
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.IN)                    #Read output from PIR motion sensor
time.sleep(2)                            #Waiting 2 seconds for the sensor to initiate

state=None
i=None
t2=None
t=None
d=5.0
while True:
#read humidity and store, wait 5 seocnds and read/store again
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) #takes reading from the sensor
    t='{1:0.1f}'.format(temperature, humidity) #takes the humidity reading and commits to 'h'

    #time.sleep=5 # This line is your problem. What are you trying to do?

    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    t2='{1:0.1f}'.format(temperature, humidity)
#if the second reading is 5 or more that the first set the state to 1
    if t2 >= str(5.0+(float(t))):
        state=1                         #sets 'state' to 1 if the humidity is high
    else:
#If the second reading is 3 less that the first set the state to 0 
        t2 <= str((float(t)-3.0))
        state=0                         #sets 'state' to 0 if humidity is not high
#storing state as 'i' and trying to do a check loop
    if i == state:                        #state didn't change
        print "holding", t, i
        time.sleep(2)
#what to do if 'i' chnages
    else:
        i = state                          #if the states doesn't  match  set 'i'  to equal 'state'
        if i == 1:                         #what to do if 'state' is '1'
            print "Showering",t,t2,i
            time.sleep(2)
                                                                                            #end of curl
        else:                             #what to do if 'state' is '0'
            print "not showering",t,t2,i
            time.sleep(2)
time.sleep(2)

Линия time.sleep=5 - это ваша проблема.Почему вы устанавливаете атрибут "sleep", который обычно является функцией, в int 5?Вы имели в виду time.sleep(5)?Вы получаете ошибку, потому что вы переназначаете атрибут «sleep» на 5, а затем пытаетесь вызвать 5, как в 5 (), позже, когда вы делаете time.sleep(2), что, конечно, недопустимо.

...