'TypeError: 'int' object is not callable' when comparing readings 5 seconds apart

'TypeError: 'int' object is not callable' when comparing readings 5 seconds apart

使用 dht22 传感器设置 raspberry Pi 以自动打开淋浴灯。我似乎无法让脚本读取传感器,等待 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 追溯(最近一次通话): 文件 "humitest.py",第 67 行,位于 time.sleep(2) 类型错误:'int' 对象不可调用

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) 时,这当然是无效的。