Old question needs new answer! how to determine sunset time

I have done a fair amount of research in the forum on determining sunset time. One good way was to use a webhook to Weatherunderground. Unfortunately, Weather underground will no longer provide a free API key (they are now owned by IBM) so this service is no longer available…

Does any one have any suggestions on another approach to determining sunset times on a daily basis… At the moment I am using a photo transistor to tell my Electron when it is “dusk” and then waiting for a fixed period to ensure it is dark (night) to do what I need done (close a gate) The problem with this approach is thunder storms and very cloudy days…

Apparently this is a pretty hard question, because there are a lot of aspects to the definition of “sunset”. Is it when the sun touches the horizon, or when it sinks below it? Do you want to account for atmospheric refraction? But for your case, it doesn’t sound like you are concerned with being super accurate.

Googling around, I was able to find several openly available web services. But doing that might just land you in the same boat you are in now, trying to use a service that eventually goes dark on you.

There are formulas you can use to calculate it yourself, which you can find on Wikipedia and the NOAA. There might be some ready-to-run code examples in C/C++ out there, but I didn’t have time to dig that deeply right now.

I have been using the Dark Sky app on my phone for quite a while to get wx info. It so happens they have an API that can deliver a bunch of stuff, including sunrise/sunset times. You can call the API up to 1K times/day without charge, which I think is sufficient for you :wink:. Look for the "daily" section of this: How Dark Sky users can use the Apple Weather app - Apple Support.

I don't see mention of what they define "sunset" as, but I'd wager it's what Wikipedia says:

The time of sunset is defined in astronomy as the moment when the upper limb of the Sun disappears below the horizon.

1 Like

I have some Weather Underground Sunset Times being pulled from a webhook that as of right now is still working. Looks like it’s new API keys that will not be generated.

I can confirm the old API keys for Weather Underground are still working for now.

I’ll need to switch so I’ll probably move over to Dark Sky for that into the future.

I use https://openweathermap.org . Free up to 60 calls per minute.

4 Likes

After a deal of research, and following one of dougal’s suggestions, I have settled on using a Particle.io library "TimeLord 0.0.1; a library that calculates sunset , sunrise, etc. for your particular latitude and longitude. TimeLord is simple to implement (I use the sunrisesunset.ino included example) and it works great. It does the sunset time calculation on board my Electron so I am not dependent on an outside source.

Thanks for everyone’s input!!

David G.

2 Likes

I’m getting an error about the date in this part of the SunriseSunset example:
byte today[] = { 0, 0, 12, 27, 10, 2012 }; // store today’s date (at noon) in an array for TimeLord to use

The error is:
sunrisesunset.ino:20:47: narrowing conversion of ‘2012’ from ‘int’ to ‘byte {aka unsigned char}’ inside { } [-Wnarrowing]

I read that a byte can only go up to 255. How did you get around this?

A quick look into the library sources will show that it uses an offset of 2000 for the year :wink:

What I ended up doing was creating an unsigned char variable "year" above the setup

unsigned char year = 2019;

  • then putting "year" in place of 2019 down in the setup code. That worked!

byte today = { 0, 0, 12, 12, 12, year}; // store today's date (at noon) in an array for TimeLord to use

This will work for most years but not for all due to the way how the Gregorian calendar calculates leap years (i.e. years divisible by 100 but not 400 will not line up after that “illegal” conversion).

I know this topic is an oldie(over a year), but just in case I found out that this Arduino sunrise/sunset code works very well in the Particle environment:
https://www.arduino.cc/reference/en/libraries/sunset/

1 Like

Hi Alex

Thanks for the tip. I ended up using the Time Lord library and it works just fine.

Thanks again for the help and stay safe!

Best regards

David G.

1 Like