Tryinghard
Tryinghard

Reputation: 1

Raspberry GPIO, time trigger

I am trying to make a GPIO trigger by time.

The first part is just a test, the second part is a WHILE is to trigger the time, I used a IF statement to match the time I wanted. That worked very fine. But inside the While python gives me an error stating

AttributeError: 'str' object has no attribute 'sleep'

I try to evoke:

from time import sleep

But still gives me error.

Here is the code:

import RPi.GPIO as GPIO
import datetime 
import time
from time import sleep

GPIO.setmode(GPIO.BCM)

GPIO.setwarnings(False)

GPIO.setup(26, GPIO.OUT)

GPIO.output(26, GPIO.LOW) #Turns On
time.sleep(3)
GPIO.output(26, GPIO.HIGH) #Turns off

print(datetime.datetime.now().strftime("%H:%M"))

while True :
    time = datetime.datetime.now().strftime("%H:%M")
    print(time)
    if time == "06:16":
        GPIO.output(26, GPIO.LOW)
        time.sleep(2)  # <===== This gives me error
        GPIO.output(26, GPIO.HIGH)
        time.sleep(1)
        print("YEah baby")
        break
        GPIO.cleanup()

Upvotes: 0

Views: 413

Answers (2)

Tryinghard
Tryinghard

Reputation: 1

I am very sorry for my noob question! I finally figure it out the problem.

    import RPi.GPIO as GPIO
import datetime
import time
from time import sleep

#SETUP THE GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(26, GPIO.OUT)

#BLINK TEST
GPIO.output(26, GPIO.LOW) #Turns On
time.sleep(3)
GPIO.output(26, GPIO.HIGH) #Turns off

#TIME PRINT FOR TEST
print(datetime.datetime.now().strftime("%H:%M"))

#TRACK TIME AND TURN ON THE GPIO
while True :
    time = datetime.datetime.now().strftime("%H:%M")
    print(time)
    if time == "11:41" :
        import RPi.GPIO as GPIO
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(26, GPIO.OUT)
        GPIO.output(26, GPIO.LOW)
        sleep(2)
        GPIO.output(26, GPIO.HIGH)
        sleep(1)
        print("YEah baby")

    GPIO.cleanup()

Upvotes: 0

rid
rid

Reputation: 63442

import time
from time import sleep

# ...

time.sleep(3)

Here, you're calling the sleep function from the time module, because when you wrote import time, you created a variable named time containing the time module.

# ...

while True:
    time = datetime.datetime.now().strftime("%H:%M")

Here, you're overriding the variable named time from above (this is called shadowing). The variable time now contains a string, so its type is now str.

    print(time)
    if time == "06:16":
        GPIO.output(26, GPIO.LOW)
        time.sleep(2)  # <===== This gives me error

Here, you're trying to call sleep on the time variable above, which is still a str. Since values of type str don't have a function sleep that you can call on them, you get the error "AttributeError: 'str' object has no attribute 'sleep'".

Since you imported sleep above with from time import sleep, you have the function sleep in scope, so you could use sleep(2) here instead of time.sleep(2).

Another way to resolve your issue is to prevent the time variable from being shadowed, by using a different name. If you called your variable now for example, instead of time, then time would still be bound to the time module, and you'd still be able to call sleep on it.

Upvotes: 1

Related Questions