SunSet_2a01 (community library)
Summary
Name | Value |
---|---|
Name | SunSet_2a01 |
Version | 1.0.2 |
Installs | |
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.- 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.
- 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.
- 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.
- If you do not set the date, it defaults to midnight, January 1st of year 0
- 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.
- 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
You can find the original at http://souptonuts.sourceforge.net/code/sunrise.c.html
Browse Library Files