Sunrise/sunset times


#21

I’m still seeing that -443 error coming back from the HttpGet on both R9.3a SoftPAC and PAC Sim, unfortunately. I’ll spare you the gory details, and hope to post more/better news soon.


#22

Just to be clear, though, Nick’s work-around of using the “old school” tcp comm handle works fine. And should work even on older firmware! (If you import his sample chart, you’ll see block 11 “get url and convert to numeric” connected which works fine, it’s his “http get” hanging out on the side that was getting the -443).
Thanks again, Nick!


#23

Sorry, I meant the HttpGet. Spent a lot of time trying to get it to work yesterday using, among other things, the workaround and generally confusing myself!

I don’t know if I can’t see the wood for the trees but is there anywhere that explains some of the variables/terms and how they are used i.e. the Http Get commands like the Get Header/GET_SrcTblBody. (1700 and 1701 seem very scant in detail on some of the newer commands!)
nSendStatus = HttpGet(DstStrTblBody, DstStrTblHdr, GET_SrcStrTblBody, 0, StrCmd, nHttpStatus, nPort, StrHostname);


#24

Hi All,

Here’s a little subroutine/example to grab a web page from a remote server and store it as a local file. A second subroutine parses the local file. (Similar to a previous weather example I posted, but a bit more versatile since I broke it into 2 parts, and used a tcp comm handle instead of HttpGet.)

This sample strategy grabs weather data, Nick’s sunrise/set, the Careers page of opto22.com, and the IP address of the device as seen from the web.

Also, it shows how you might parse a file by searching for strings in it.

Any/all feedback appreciated!

Thanks,
-OptoMary

p.s. I wrote this in PAC Control Basic 8.0 so it can be opened by anything since that.


#25

Thanks Mary - nice complete example!


#26

I know this thread is a few months old now but I finally got around to implementing it. I made a few customizations to Mary’s strategy to specifically suit my needs and it worked beautifully! Thanks to Nick Stephens for graciously hosting the sunrise/sunset data on his server!


#27

Just came across this, and thought I would share some code I modified to run on a PAC many years ago that I’ve been using for astronomical clock use. The math doesn’t seem to be too much for the PAC. I don’t remember how accurate it is, but I use it for turning off and on lights and it works great for that and it’s more reliable than photo cells.

I have this running in a subroutine with the following parameters:

fLatitude - The Latitude where you want the sunrise/sunset time.
fLongitude
nDayOfYear - The number of days since 12/31 last year (So Jan 1 = 1 and Feb 1 = 32, etc.)
fUTCOffset - Adjust the result into the local time zone
nSunset - This is the output in seconds since midnight for sunset
nSunrise - This is the output in seconds since midnight for sunrise

The rest are all local subroutine variables.

The code:


//Calculate Sunset given the latitude, longitude and day of year
//Adapted from http://williams.best.vwh.net/sunrise_sunset_algorithm.htm
//which came from the Almanac for Computers, 1990 USNO
//Define Pi and Degree and Radian conversion factors.
fPi = 3.14159265;
fDegToRad = .0174532925;
fRadToDeg = 57.2957795;
//Define Zenith (official sunset zenith)
fZenith = 90.83;




//Get the Longitude Hour
fLngHour = fLongitude / 15.0;


//Calculate an approximate sunset time for the longitude
fApproxTime = nDayOfYear + ((18.0 - fLngHour)/24.0);


//Sun Mean Anomaly
fSunMeanAnomaly = (.9856 * fApproxTime) - 3.289;


//Sun True Longitude
fSunTrueLongitude = fSunMeanAnomaly + (1.916 * Sine(fSunMeanAnomaly * fDegToRad)) + (.02 * Sine(2 * fSunMeanAnomaly * fDegToRad)) + 282.634;


//Make sure fSunTrueLongitude is between 0 and 360, adjust by 360.
if (fSunTrueLongitude>360.0) then
  fSunTrueLongitude = fSunTrueLongitude - 360.0;
elseif (fSunTrueLongitude < 0.0) then
  fSunTrueLongitude = fSunTrueLongitude + 360.0;
endif


//Sun Right Ascension (RA)
fSunRightAscension = Arctangent(0.91764 * Tangent(fSunTrueLongitude * fDegToRad)) * fRadToDeg + 360;
//Make sure fSunRightAscension is between 0 and 360, adjust by 360
if (fSunRightAscension>360.0) then
  fSunRightAscension = fSunRightAscension - 360.0;
elseif (fSunRightAscension < 0.0) then
  fSunRightAscension = fSunRightAscension + 360.0;
endif


//Put RA and L in the same quadrant
fSunTrueLongitudeQuadrant = Truncate(fSunTrueLongitude / 90.0) * 90.0;
fSunRAQuadrant = Truncate(fSunRightAscension / 90.0) * 90.0;


fSunRightAscension = fSunRightAscension + fSunTrueLongitudeQuadrant - fSunRAQuadrant;


//RA converted to Hours
fSunRAHours = fSunRightAscension / 15.0;


//Calc suns Declination
fSunSinDeclination = 0.39782 * Sine(fSunTrueLongitude * fDegToRad);
fSunCosDeclination = Cosine(Arcsine(fSunSinDeclination));




//Calc suns local hour angle
fSunLocalHourAngle = (Cosine(fZenith * fDegToRad) - (fSunSinDeclination * Sine(fLatitude * fDegToRad))) / (fSunCosDeclination * Cosine(fLatitude * fDegToRad));


if (fSunLocalHourAngle < -1.0) then //Check if the sun never sets on this date
  nSunset=-1; //No sunset
else
  fSunH = Arccosine(fSunLocalHourAngle) * fRadToDeg / 15.0;
  fLocalMeanTime = fSunH + fSunRAHours - (0.06571 * fApproxTime) - 6.622;
  fUTCTime = fLocalMeanTime - fLngHour;
  fSunsetTime = fUTCTime + fUTCOffset;
  //Adjust to be within 0 and 24
  if (fSunsetTime > 24.0) then
    fSunsetTime = fSunsetTime - 24.0;
  elseif (fSunsetTime < 0.0) then
    fSunsetTime = fSunsetTime + 24.0;
  endif
  nSunset = fSunsetTime * 3600;  //Put the result in seconds since midnight;
endif






/////////////Sunrise//////////////






//Calculate an approximate sunrise time for the longitude
fApproxTime = nDayOfYear + ((6.0 - fLngHour)/24.0);


//Sun Mean Anomaly
fSunMeanAnomaly = (.9856 * fApproxTime) - 3.289;


//Sun True Longitude
fSunTrueLongitude = fSunMeanAnomaly + (1.916 * Sine(fSunMeanAnomaly * fDegToRad)) + (.02 * Sine(2 * fSunMeanAnomaly * fDegToRad)) + 282.634;


//Make sure fSunTrueLongitude is between 0 and 360, adjust by 360.
if (fSunTrueLongitude>360.0) then
  fSunTrueLongitude = fSunTrueLongitude - 360.0;
elseif (fSunTrueLongitude < 0.0) then
  fSunTrueLongitude = fSunTrueLongitude + 360.0;
endif


//Sun Right Ascension (RA)
fSunRightAscension = Arctangent(0.91764 * Tangent(fSunTrueLongitude * fDegToRad)) * fRadToDeg + 360;
//Make sure fSunRightAscension is between 0 and 360, adjust by 360
if (fSunRightAscension>360.0) then
  fSunRightAscension = fSunRightAscension - 360.0;
elseif (fSunRightAscension < 0.0) then
  fSunRightAscension = fSunRightAscension + 360.0;
endif


//Put RA and L in the same quadrant
fSunTrueLongitudeQuadrant = Truncate(fSunTrueLongitude / 90.0) * 90.0;
fSunRAQuadrant = Truncate(fSunRightAscension / 90.0) * 90.0;


fSunRightAscension = fSunRightAscension + fSunTrueLongitudeQuadrant - fSunRAQuadrant;


//RA converted to Hours
fSunRAHours = fSunRightAscension / 15.0;


//Calc suns Declination
fSunSinDeclination = 0.39782 * Sine(fSunTrueLongitude * fDegToRad);
fSunCosDeclination = Cosine(Arcsine(fSunSinDeclination));




//Calc suns local hour angle
fSunLocalHourAngle = (Cosine(fZenith * fDegToRad) - (fSunSinDeclination * Sine(fLatitude * fDegToRad))) / (fSunCosDeclination * Cosine(fLatitude * fDegToRad));


if (fSunLocalHourAngle > 1.0) then //Check if the sun never rises on this date
  nSunrise=-1; //No sunrise
else
  fSunH = (360 - Arccosine(fSunLocalHourAngle) * fRadToDeg) / 15.0;
  fLocalMeanTime = fSunH + fSunRAHours - (0.06571 * fApproxTime) - 6.622;
  fUTCTime = fLocalMeanTime - fLngHour;
  fSunriseTime = fUTCTime + fUTCOffset;
  //Adjust to be within 0 and 24
  if (fSunriseTime > 24.0) then
    fSunriseTime = fSunriseTime - 24.0;
  elseif (fSunriseTime < 0.0) then
    fSunriseTime = fSunriseTime + 24.0;
  endif
  nSunrise = fSunriseTime * 3600;  //Put the result in seconds since midnight;
endif


#28

Attached the subroutine. It is in 9.0 Basic.


#29

Ha! Proving once again that there is actually life outside of Internet. Instead of struggling with tcp comms handles and 443 errors, just use the good old United States Naval Observatory Basic program from 1990. Nice One Philip and thanks! :cool:


#30

Yep, very nice Phillip… Just one question, how do you calculate the nDayOfYear?


#31

Built-in function: GetJulianDay();