Turning tings ON or OFF at Sunset or Sunrise using an IOT is very simple to accomplish. I am using the Particle Photon to do that.
Timelord, and Dusk2dawn both libraries work, but I decided to use this library maintained by Peter Buelow listed here:
This library is not available in the Particle Web IDE, so you will need to install it. Just click the plus (+) sign top-right corner of the IDE, two files will be added, just change their name to sunset, paste their respective code from the library listed above, that’s it.
Now convert hours to minute, so 24 hours is equal to 1440 minutes. This library provides sunrise and sunset in minutes, let’s do the work using minutes.
int eMinutes = Time.hour() * 60 + Time.minute(); // Current hours plus minutes to minutes
This variable, eMinutes, will track the elapsed time since midnight in minutes. Two if(s) like so, and you are done:
if (eMinutes >= Sunrise && eMinutes <= Sunset) // Turn things ON at sunrise else if (eMinutes >= Sunset || eMinutes <= Sunrise) // Turn things OFF at sunset
If you are interested to find out the meridian or mid-point, just add Sunrise and Sunset and divided by 2.
int meridian = Sunrise + Sunset / 2;
Just in case you want to convert eMinutes back to hours and minutes, do the following:
int cHours = eMinutes/60; int cMinutes = eMinutes%60;