User avatar
ckovel
Posts: 3
Joined: Sat Sep 06, 2014 3:27 pm

sunwait not waiting for sun

Sun Sep 07, 2014 11:32 pm

Disclosure: I am noob.
I have been working on a perl program to take pictures during daylight. I came across sunwait in this post : http://www.raspberrypi.org/forums/viewt ... it#p568935
and a few other places on the forum.
The issue is not with the python program. it runs and takes pictures. The problem is that sunwait is not working as expected in cron.
I've tried (with and without the 'root' & and path):
* 3 * * * root /usr/local/bin/sunwait sun up -0:00:01 40.8244N 74.2124W; python /home/pi/Documents/python/TLTrial-Dylght.py
* 4 * * * root /usr/local/bin/sunwait sun down +0:00:01 40.8244N 74.2124W; killall python
+> intention of first line start the sunwait job to start taking pictures when the sun comes up.
+> intention of second line shut down all python jobs when the sun goes down.
++> however, pictures get taken from the moment the cron job kicks off

i've tried:
3 * * * * sunwait sun up -0:00:01 40.8244N 74.2124W; python /home/pi/Documents/python/TLTrial-Dylght.py
15 * * * * sunwait sun down +0:00:01 40.8244N 74.2124W; killall python
++> same situation the jobs kick in at 3 am and stop at 3 pm.

a. is there any way to test the sunwait program ?
b. am i calling the jobs wrong?
any help is appreciated.

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK

Re: sunwait not waiting for sun

Mon Sep 08, 2014 3:32 pm

To test sunwait

Code: Select all

sunwait -p 53.9142N 2.1893W
Which should return

Code: Select all

Using location:             53.914200N, 2.189300W
Date:                        8 Sep 2014 
Local time:                 16:17 
Day length:                 13:12 hours
With civil twilight         14:21 hours
With nautical twilight      15:49 hours
With astronomical twilight  17:25 hours
Length of twilight:  civil   0:34 hours
                  nautical   1:18 hours
              astronomical   2:06 hours
Current specified time zone: BST (1 from UTC) 
Sun transits meridian 1306 BST
                   Sun rises 0631 BST, sets 1940 BST
       Civil twilight starts 0555 BST, ends 2016 BST
    Nautical twilight starts 0511 BST, ends 2100 BST
Astronomical twilight starts 0423 BST, ends 2149 BST
Obviously make allowances for when you run it.

Crontab can be difficult and confusing. There are effectively 2 types, system-wide and user, the system wide needs the user putting in, the user crontab doesn't. The way I do it is to directly edit the /etc/crontab file using nano or something (yout need to be root or use sudo) and forget all about the user crontabs scattered around.

Here you go, my /etc/crontab

Code: Select all

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user	command

#15 19 * * * root /sbin/shutdown -h now
17 *	* * *	root    cd / && run-parts --report /etc/cron.hourly
25 6	* * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6	* * 7	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6	1 * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
0 0 * * * root /etc/scripts/midnite >/dev/null 2>&1
* * * * * root /etc/scripts/minute >/dev/null 2>&1
*/10 * * * * root /etc/scripts/10min >/dev/null 2>&1
2 */8 * * * root /etc/scripts/8hour >/dev/null 2>&1
#2 1,9,17 * * * root /etc/scripts/8hour >/dev/null 2>&1
#50 2 * * 0 root reboot # 
7 */8 * * * root /etc/scripts/ipcheck >/dev/null 2>&1
3 3 * * * root sunwait sun up +0:30:00 53.9142N 2.1893W ; echo 'light' > /root/lightordark 
4 3 * * * root sunwait -p 53.9142N 2.1893W > /root/suntimes
3 14 * * * root sunwait sun down -0:20:00 53.9142N 2.1893W ; echo 'dark' > /root/lightordark
58 23 * * * root /etc/scripts/monthmeter >/dev/null 2>&1
#
I use sunwait to simply write 'light' or 'dark' into a file which is then interrogated by other scripts.

I had to compile sunwait for the Pi myself, which is a bit daunting for noobs, precompiled exceutables for generic linux are likely to be x86 processor (PC) which definitely won't work on a Pi, where did you get the executable from ?
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

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

Re: sunwait not waiting for sun

Mon Sep 08, 2014 7:29 pm

ckovel wrote: 3 * * * * sunwait sun up -0:00:01 40.8244N 74.2124W; python /home/pi/Documents/python/TLTrial-Dylght.py
You've not got enough wait time.

Code: Select all

3 3 * * * /usr/local/bin/sunwait civ up +0:01:00 51.2xx0197N 1.1xx3011W ; echo 'light' > /var/run/lightordark
3 14 * * * /usr/local/bin/sunwait civ down -0:01:00 51.2xx0197N 1.1xx3011W ; echo 'dark' > /var/run/lightordark
my system (where I've coded it for civil twighlight) has an hour leeway starts waiting at 03:03 local for sunrise and starts waiting at 14:03 for local for sunset.

The file I write from root's crontab, /var/run/lightordark has rw-r--r-- permissions so any program can read it.
Last edited by DougieLawson on Tue Jan 26, 2016 12:26 am, edited 1 time in total.
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK

Re: sunwait not waiting for sun

Mon Sep 08, 2014 7:39 pm

Dougies crontab wasn't really inspired by my own..... :mrgreen:

Where you're putting the lightordark file probably makes more sense than where I'm putting it.
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

User avatar
ckovel
Posts: 3
Joined: Sat Sep 06, 2014 3:27 pm

Re: sunwait not waiting for sun

Tue Sep 09, 2014 2:34 am

Thanks to both. My first discovery from the "sunwait -p" command was my permissions weren't there :oops: . It was not allowed to run as an executable so I ran "sudo chmod +x sunwait". I can now run the program from the cmd line. (sunwait -p is hugely useful.) I expect I will be ok tomorrow morning.

I'll also just run for civ twilight which is all i am looking for.

Nestboxtech
Posts: 73
Joined: Fri Apr 11, 2014 9:45 pm

Re: sunwait not waiting for sun

Sat Apr 23, 2016 1:40 pm

DougieLawson wrote:
ckovel wrote: 3 * * * * sunwait sun up -0:00:01 40.8244N 74.2124W; python /home/pi/Documents/python/TLTrial-Dylght.py
You've not got enough wait time.

Code: Select all

3 3 * * * /usr/local/bin/sunwait civ up +0:01:00 51.2xx0197N 1.1xx3011W ; echo 'light' > /var/run/lightordark
3 14 * * * /usr/local/bin/sunwait civ down -0:01:00 51.2xx0197N 1.1xx3011W ; echo 'dark' > /var/run/lightordark
my system (where I've coded it for civil twighlight) has an hour leeway starts waiting at 03:03 local for sunrise and starts waiting at 14:03 for local for sunset.

The file I write from root's crontab, /var/run/lightordark has rw-r--r-- permissions so any program can read it.
Could you post a quick example of how you read the file content into a python variable, then 'do something' based on that...
I've got as far as this, (the incrementing 'fileserial' is where I will put a call to picamera). I want it to check for "light" vs "dark" within the loop, not outside as it currently does...

Code: Select all

#file created by sunwait to indicate if its light or dark
lightordarkfile="/pathto/lightordark_test"

def lightordark(): # read the txt file created by sunwait to see if its light or dark
	f = open(lightordarkfile)
	lightordark_status=f.readline()

# Set the initial serial for saved images to 1
fileSerial = 1

while True:
	if 'light' in lightordark_status:
		# Set FileSerialNumber to 000X using four digits
		fileSerialNumber = "%04d" % (fileSerial)
		s_fileSerialNumber=str(fileSerialNumber)

		# Increment the fileSerial
		fileSerial += 1

		time.sleep(1)

	else:
		print " ....do nothing"
http://nestboxtech.blogspot.co.uk/

Nestboxtech
Posts: 73
Joined: Fri Apr 11, 2014 9:45 pm

Re: sunwait not waiting for sun

Sat Apr 23, 2016 9:10 pm

Figured it out:

Code: Select all

while True:
	time.sleep(2)
	
	#re populate lightordark_status
	f = open(lightordarkfile)
	lightordark_status=f.readline()
	
	if 'light' in lightordark_status:
		# Set FileSerialNumber to 000X using four digits
		fileSerialNumber = "%04d" % (fileSerial)
		s_fileSerialNumber=str(fileSerialNumber)
		print 'fileSerial = '+fileSerialNumber+' (light)'	

		# Increment the fileSerial
		fileSerial += 1
		
		takePic_Picamera()

	else:
		print 'lightordark_status = '+lightordark_status+ ' dont do anything...'
http://nestboxtech.blogspot.co.uk/

Return to “Beginners”