Search code examples
python-2.7raspbianadafruit

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


Setting up a raspberry Pi with a dht22 sensor to automatically turn on a shower light. I can't seem to get the script to read the sensor, wait 5 seconds and read it again and then do stuff if the reading has changed. I am not a programmer and have mucked this together with help from friends and lots of google searches, but ultimately I have no clue what I am doing.

I have a script that turns the light on based on the humidity reading above X however as relative humidity changes the script is not as accurate as i would like.

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)

Expected s it will keep printing the state based on changes

what I actually get is this:

sudo python humitest.py not showering 45.3 45.3 0 Traceback (most recent call last): File "humitest.py", line 67, in time.sleep(2) TypeError: 'int' object is not callable


Solution

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

    The line time.sleep=5 is your problem. Why are you setting the attribute "sleep", which is normally a function, to the int 5? Did you mean time.sleep(5)? You get the error because you reassign the attribute "sleep" to 5, and then you try to invoke 5, as in 5(), later on when you do time.sleep(2) which of course it not valid.