Sunrise/sunset times


#1

I’m working on a SNAP-PAC project involving some lighting controls and I’ve run into something new (to me) that I hope someone else out there might have experience in to guide me through.

Some of the lights being controlled are exterior lights and we want them to switch on/off according to the sunrise/sunset times that day (which are obviously changing every day). I realize an easy solution may be to use a photocell sensor but I’m hoping someone’s achieved this through either a mathematical equation or pulling info off a time server somewhere.


#2

Arlin,

Great question, one that I asked about 8 years ago of one of the engineers at Opto when I was still back at the hospital.
The answer back then was that the controller does not have the ‘grunt’ to do the math. Bear in mind you would need to enter your lat/long to calculate the rise and set times.
The other problem is that the controller uses a different epoch time. This is really important when working with celestial bodies.

The question has come up again about a year ago, and at that time we looked at doing your second suggestion, that is pulling a web page in and stripping it off that… At that time, we had not gained the new HTTP commands and it was pretty tricky to do.
Since then, Mary has written the weather station subroutine (see summer code contest forum), I wonder if we can get rise/set times from weather underground and add it to that subroutine?
[Edit a few days latter. Mary and I looked, the rise/set times are not included. Drats.]

Long story short, love your idea. Lets have a crack at it.

Got any URLS that have rise and set times on it? (with as little other cruft as possible)???

(To all the lurkers on this forum, lets all get stuck into this one and see what we as a community can come up with).

Ben

P.S What we do at Opto for our parking lot lights is use an LDR (Light Dependent Resistor) connected to a SNAP-IDC5-SW module.
We use two of them. Not sure of the software, I was only involved with the hardware set up for them.


#3

Quick thought… now that we have softpac, I wonder if we can use the power of the PC to do the real calculations…
What would be fantastic is if we could use two line elements… this way we could calculate the rise set times of any body, the sun, moon, planets and satellites (I have a future project for my Opto system that would really benefit from this).


#4

Whoa, check this out…

http://williams.best.vwh.net/sunrise_sunset_algorithm.htm

And this one here has some sample code (big job to convert this to OptoScript I suspect).

Im thinking a screen scrape might be a LOT simpler…

Either that or just a table.

Ben


#5

Thanks for the tips Ben. I tried converting the algorithm into OptoScript but wound up with severely incorrect values (and not enough time and patience to solve where I went wrong!). Quite honestly I don’t need anything super precise for this application so I’m do a simple GetMonth query and do a rough adjustment on a monthly basis as to when my dusk and dawn times are.

I considered doing a table but it would require periodic updating. I’ve found sites where I can download a month’s worth of sunrise/sunset data into a CSV format and it wouldn’t take much to format that into a recipe file that you could retrieve via PAC Display. But it would still require me manually downloading the data on a monthly basis… not automatic enough for my liking.

If anyone has been successful with running actual algorithms or pulling data from a timeserver somewhere, I would definitely be interested in trying it out though.


#6

ive created a php script on my server that will dish up the sunrise/sunset times for the nominated lat, long and GMT offset. there is an option to get civillian (type=1), nautical (type=2) and astronomical (type=3) times as well (script plucked straight from php manual and manipulated a bit).
so if you hit:
http://www.22solutions.com.au/sunrise/sunrise.php?lat=-37.55&long=143.85&offset=11&type=0
you’ll get the sunrise/sunset times for ballarat. change the type to get the different times.
enter your own lat, long and offset and it will return the data. lat is positive for north and long is positive for east. i tried with Los Angeles data and it was all good.
http://www.22solutions.com.au/sunrise/sunrise.php?lat=34.052&long=-118.2428&offset=-8&type=0

i have started on building some opto code to get this into some variables, but for some reason softpac keeps returning -443 on the status…hmmm…

heres the php script in case anyone else wants it:


<?php
$lat = isset($_GET['lat'])?$_GET['lat']:'';    //either get nav or clear the string
$long = isset($_GET['long'])?$_GET['long']:'';    //either get nav or clear the string
$offset = isset($_GET['offset'])?$_GET['offset']:'';    //either get nav or clear the string
$type = isset($_GET['type'])?$_GET['type']:'0';    //either get nav or clear the string

if ($lat == '' or $long == '' or $offset == ''){echo "error";}
else{

switch ($type)
{
    case "1":
        $zenith=96;
        echo "<br><p>Civilian Twilight start- ".date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
        echo "<br>Civilian Twilight end- ".date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
        break;
    case "2":
        $zenith=102;
        echo "<br><p>Nautical Twilight start- ".date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
        echo "<br>Nautical Twilight end- ".date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
        break;
    case "3":
        $zenith=108;
        echo "<br><p>Astronomical Twilight start- ".date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
        echo "<br>Astronomical Twilight end- ".date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
        break;
    default:
        $zenith=90+50/60;
        echo "<br><p>Sunrise- ".date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
        echo "<br>Sunset- ".date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
        break;        
}
}
?> 

might be a start for someone out there who is keen to get this going as well.

Nick


#7

arlin
i implemented the lookup table a few years ago and i agree, its not a total automatic solution. i found a site that would give the sr/ss times for the whole year for melbourne so i just load these into a file, store on the controller and read once early morning to get them.

what i am trying to do with the php code i posted above is to have a web server return sr/ss times by calling this php script and passing the lat, long and gmt offset of the place the controller is in. using the pac control httpget command, the idea is to have these times returned and inserted into some variables in the controller.

i hope this will help with solving the problem of sr/ss times. its probably not the most desirable way (which would be on the controller itself), but at least its more automatic than updating some files/tables manually.

i will try to knock up some pac control code to get this happening

Nick


#8

I would be interested in seeing the PAC coding you come up with Nick. Implementing a table once a year wouldn’t be the worst thing. We’re not talking about critical systems here so even if I were to forget and the system recycled the previous year’s data, I don’t the the rise/set times would be off by so much that it would matter.


#9

Must admit, I have been toying with doing this. Couldn’t find my preference of a php script (so thank you Nick), but managed to get a yearly table from here I thought I would revisit this when I had got some more important stuff done (including looking at using Python).

My thoughts were to use a script on a Linux system using a CRON job to generate the times and then send the times to the Scratchpad and get the Opto to read them from there.

I had figured that the power of the Opto itself probably was not up to the number crunching required, but then if it only needs to be once a day/week perhaps it wouldn’t be such a problem, especially if there are ‘quiet times’.


#10

after a bit of coding i have a working combination that will extract the sr/ss times from my website. the php on my site is the php posted above. this chart will connect to the website, perform a GET to extract the data and then put into numeric variables.

I tried to use the built in httpget command but for some reason kept getting -404 back. i have left that block in the chart and if anyone can enlighten me as to what i’ve done wrong i’d be much appreciative. i connected to the web server ‘old school’ way to get the data.

to use, just supply the lat, long and gmt offset that you want the times for. also, the comment about ‘type’ in my previous post also applies but i didn’t append this to my GET request.

check it out and i hope it helps with the original question/post.

Let me know if you have any problems.

sunrise.zip (2.94 KB)

compiled in pac control basic 9.3

Nick


#11

Hi Nick,

Wow! Thanks! You are a php wizard. That SoftPAC -443 error you mentioned earlier has be reproduced and ticketed. Hopefully a fix will be coming soon.

As for this -404 you mention, I can’t get that. It works perfectly for me on my SNAP-PAC-R1 with a couple different firmware versions I’ve tried. What hardware/firmware are you using that gives you the -404 back from HttpGet?

Here’s the lovely reply I get:

Thanks for sharing!

-Mary


#12

Hi Mary,
Thanks, not sure about being a php whizz tho…
Glad the softpac issue was reproduced.
I am using 9.2a in an R2.

I think I might get my R2 a nice firmware upgrade for Christmas.

Did you try it with Temecula data to see if the times are correct?


#13

Ah! You ran into this issue KB82023 which has a work-around – minor adjustment needed to your header (I added this [0] element):

 
stGetHeader[0] = "host:" + sHostname;
stGetHeader[1] = "Accept: text/html"; //set content type

I plugged in 33.4936 & 117.1475 (Temecula) but that comes back with:
<br><p>Sunrise- 10:04<br>Sunset- 20:05

Hmm. Maybe user error on my part?


#14

Ah thanks Mary. I will apply the fix. Should have checked the kb’s before coding the old school way… Never mind

Did you specify a gmt offset as well? I think I hard coded it to 11. I’m not sure what Temeculas offset is. Also remember the lat is east referenced and long is north referenced (I see you know that but for any one else giving it a shot…). So if you are 147.89W it would mean -147.89.

Thanks again mary


#15

Ah! That was it. I threw the minus sign in there and fixed my offset and got:

<br><p>Sunrise- 07:04<br>Sunset- 17:05

Weather.com reported this for yesterday:
Sunrise: 6:42 am
Sunset: 4:41 pm

Maybe they’re talking about the “crack” of dawn and yours is for the completion? :wink:

Very nice! Thanks again!


#16

Ok great Mary. If you add a type to the get request (type=1, 2 or 3. Check the php script to see what type is what) you may get back corresponding times. As you say, they might have firrst light and last light as opposed to official sr/ss

Great glad it worked, kinda…


#17

Interesting topic in a thread regarding ‘php sun functions’

I have looked further into calculating the values of sunrise and set using a Taylor Series
sin x = x - x3/3! + x5/5! - x7/7! + …,

and to the use of the simpler COrdinate Rotation DIgital Computer (CORDIC) method to see if it would give any advantages.

Both require some number crunching of sorts - the former multiplication and division, the latter only using addition, subtraction and bitshifts. This leads us to a question that has been asked before - that of the number CPU/Controller Cycles to carry out commands to guesstimate how long calculating a number would take and then to see which method would be quicker.

In our case, we normally have a computer close by and so I think for us the php script method is ideal as we already use php to talk to the controller. (Although running Python also looks interesting.)

Thanks for all of your input guys.

Hate to lose a challenge so I think I will be back!


#18

Oh! This sounds like a good place for SoftPAC! I suspect that the addition/subtraction/bitshift difference would pale in comparison to which hardware you run it on. Some of the [URL=“http://www.opto22.com/lp/pc_based_controller.aspx”]SoftPAC tests we did were several orders of magnitude faster running the same code on a PC (of course, this varies widely depending on your PC)…


#19

I totally agree with you Mary. But what I would say is that if you are running remotely you want something small light and reliable and so for me a PC is not such a good option as a PAC Controller. If you have a PC locally then yes, SoftPAC is ideal.

I remember a (few?) years ago when we were using assembler to code systems with 8080A processors we would pinch and scrape every single CPU sycle we could to improve execution times. You certainly learnt some neat unorthodox cheats/methods to get you want you wanted quickly!!


#20

Can I just check that the 2 code samples here run with PAC Sim 9.3a? The reason I ask is that I am having problems getting them to run.

The PAC Sim ReadMe suggests that at R9.1b Http Get doesn’t work, but at 9.3a Http Get has had some bugs fixed, does it now work?