SunSet_2a01 (community library)

Summary

Name Value
Name SunSet_2a01
Version 1.0.2
Installs 291
License MIT
Author Peter Buelow (goballstate@gmail.com)
Download .tar.gz
All Versions 1.0.2, 1.0.1

Deprecated, use sunset 1.0.6 or later

Example Build Testing

Device OS Version:

This table is generated from an automated build. Success only indicates that the code compiled successfully.

Library Read Me

This content is provided by the library maintainer and has not been validated or approved.

Calculate Sunrise and Sunset based on time and latitude and longitude

This is a modification of the sunrise.c posted by Mike Chirico back in 2004. See the link below to find it. I needed an algorithm that could tell me when it was dark out for all intents and purposes. I found Mike’s code, and modified it a bit to be a library that can be used again and again.

License

This is governed by the MIT license. Use it as you want, make changes as you want.

Details

To use SunPosition, you need to a few bits of local information.

  1. Accurate time. If you’re running this with something that can get GPS time or use NTP, then results will always be very accurate. If you do not have a good clock source, then the results are going to be very accurate relative to your not so accurate clock. For the Photon, I use SparkTime and an NTP source. With my Teensy, I connect up a GPS source and read the time from that.
  2. You need an accurate position, both latitude and longitude, so the library can figure out where you are. The library will only take longitude as a positive value, as it doesn’t matter. Putting in a negative value because the US is -80 something, means you will get odd results.
  3. Prior to calculating sunrise or sunset, you must update the current date for the library, including DST if applicable. The library doesn’t track the date, so calling it every day without changing the date means you will always get the calculation for the last accurate date you gave it.
  4. If you do not set the date, it defaults to midnight, January 1st of year 0
  5. The library always calculates for UTC, but does apply a timezone offset. You can calculate both the offset and UTC with related function calls if you would like.
  6. The library returns a double that indicates how many seconds past midnight relative to the set date that sunrise or sunset will happen. If the sun will rise at 6am local to the set location and date, then you will get a return value of 360.0. Decimal points indicate fractions of a minute. It’s up to you to figure out how to use the data.

SunPosition is a C++ class, and no C implementation is provided.

Example

This is relative to an Arduino type environment. Create a global object, and initialize it and use it in loop().

#include “SunRise.h”
#define TIMEZONE    -5
#define LATITUDE    40.0000
#define LONGITUDE    89.0000

SunRise sun;

void setup()
{
// Set your clock here to get accurate time and date
// Next, tell SunRise where we are
sun.setPosition(LATITUDE, LONGITUDE, TIMEZONE);
}

void loop()
{
int dstupdate = calculateDST();    // A pseudo function to calculate the current timezone and any DST offset that might apply.

sun.setCurrentDate(year(), month(), day());
sun.setTZOffset(dstupdate);
double sunrise = sun.calcSunrise();
double sunset = sun.calcSunset();
double sunriseUTC = sun.calcSunriseUTC();
double sunsetUTC = sun.calcSunriseUTC();
}

Notes

  • This is a general purpose calculator, so you could calculate when Sunrise was on the day Shakespeare died. Hence some of the design decisions
  • Again, don’t use negative values for longitude, it doesn’t matter
  • Date values are absolute, are not zero based, and should not be abbreviated (e.g. don’t use 15 for 2015 or 0 for January)
  • This library may run well enough on a 16KHz Arduino, but it’s fairly math intensive and uses quite a bit of memory, so it won’t run fast. It works very well on the ARM M core chips like the Teensy and Photon though.
  • For time, I like the SparkTime for Photon which does a good job of keeping accurate time. GPS also works, but you will need to implement a way to calculate if your are DST or not. Other platforms will need to implement a good solution for keeping an accurate date.
  • I can be used as a general purpose library on any Linux machine as well. You just need to compile it into your RPI or Beagle project.
  • You can find the original at http://souptonuts.sourceforge.net/code/sunrise.c.html

    Browse Library Files