davidanddiesel
Posts: 15
Joined: Sun Jan 18, 2015 4:03 am

Alarm Clock Errors

Sun Jan 18, 2015 4:08 am

Code: Select all

import os
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18,GPIO.OUT)


try:
	while True:
		
		time.sleep(1)
		timeraw = str(os.popen("sudo date").read())
		timeraw2 = timeraw.split("n")[1]
		time1 = timeraw2.split("E")[0]
		print(time1)
		if time1 == ('17 22:59:02'):
			
			print("TIME")
			for i in range(5000):
				GPIO.output(18,True)
				time.sleep(0.0001)
				GPIO.output(18,False)
				time.sleep(0.0001)
			
		else:
			print("NOT TIME")	


except KeyboardInterrupt:
	GPIO.cleanup()
The code prints the correct time every second, but always prints "NOT TIME", even when the time is equal to the time specified. Here are the results from the code:

Code: Select all

NOT TIME
 17 22:58:38 
NOT TIME
 17 22:58:39 
NOT TIME
 17 22:58:40 
NOT TIME
 17 22:58:41 
NOT TIME
 17 22:58:42 
NOT TIME
 17 22:58:44 
NOT TIME
 17 22:58:45 
NOT TIME
 17 22:58:46 
NOT TIME
 17 22:58:47 
NOT TIME
 17 22:58:48 
NOT TIME
 17 22:58:49 
NOT TIME
 17 22:58:50 
NOT TIME
 17 22:58:51 
NOT TIME
 17 22:58:52 
NOT TIME
 17 22:58:53 
NOT TIME
 17 22:58:55 
NOT TIME
 17 22:58:56 
NOT TIME
 17 22:58:57 
NOT TIME
 17 22:58:58 
NOT TIME
 17 22:58:59 
NOT TIME
 17 22:59:00 
NOT TIME
 17 22:59:01 
NOT TIME
 17 22:59:02 
NOT TIME
 17 22:59:03 
NOT TIME
 17 22:59:04 
NOT TIME
 17 22:59:06 
NOT TIME
PLEASE HELP

User avatar
rpdom
Posts: 17172
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Alarm Clock Errors

Sun Jan 18, 2015 12:50 pm

It looks to me like time1 has a space on the front. Try adding a space to the front of your test.

Also, why sudo date? :shock:

sudo is not needed for a simple "date" command. Also you should look into using some of the Python Date functions, rather than using the system date command as those will be more flexible and controllable.

beta-tester
Posts: 1377
Joined: Fri Jan 04, 2013 1:57 pm
Location: de_DE

Re: Alarm Clock Errors

Sun Jan 18, 2015 1:09 pm

instead of

Code: Select all

if time1 == "...":
you can use in case of leading blanks

Code: Select all

if time1.endswith("..."):
or

Code: Select all

if time1.find("...") != -1:
so it doesn't matter where the matching string is found, if it is found.

and yes, as rpdom told, getting the time in python is possible without initiate a bash command line call.
date.today()... take a look to the python online documentation.
with using the time/date you don't need to compate the times by strings, you can do it more precisely.
{ I only give negative feedback }
RPi B (256MB), B (512MB), B+, ZeroW; 2B; 3B, 3B+; 4B (4GB)

User avatar
DougieLawson
Posts: 39120
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Alarm Clock Errors

Sun Jan 18, 2015 1:53 pm

rpdom wrote:Also you should look into using some of the Python Date functions, rather than using the system date command as those will be more flexible and controllable.

Code: Select all

import time
print time.strftime('%d %H:%M:%S')
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

davidanddiesel
Posts: 15
Joined: Sun Jan 18, 2015 4:03 am

Re: Alarm Clock Errors

Sun Jan 18, 2015 3:48 pm

Thank you! I tried both ways, and the code seems to work! Now time to set it up overnight :D !

Return to “Troubleshooting”