DrRandom

sunTimes.h

Aug 23rd, 2022
1,049
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.17 KB | Source Code | 0 0
  1. #include <Arduino.h>
  2. #include <ArduinoJson.h>
  3. #include <utilities/globals.h>
  4. #include <utilities/networkSystem.h>
  5. #include <utilities/fileSystem.h>
  6. #include <utilities/timeSystem.h>
  7. #include <HTTPClient.h>
  8.  
  9. #include <stdio.h>
  10. #include <math.h>
  11.  
  12. #define SUN_TIMES_DEBUG true
  13. #define GEO_RESP_SIZE 1500
  14.  
  15. /* A macro to compute the number of days elapsed since 2000 Jan 0.0 */
  16. /* (which is equal to 1999 Dec 31, 0h UT)                           */
  17.  
  18. #define days_since_2000_Jan_0(y,m,d) \
  19.     (367L*(y)-((7*((y)+(((m)+9)/12)))/4)+((275*(m))/9)+(d)-730530L)
  20.  
  21. /* Some conversion factors between radians and degrees */
  22.  
  23. #ifndef PI
  24.  #define PI        3.1415926535897932384
  25. #endif
  26.  
  27. #define RADEG     ( 180.0 / PI )
  28. #define DEGRAD    ( PI / 180.0 )
  29.  
  30. /* The trigonometric functions in degrees */
  31.  
  32. #define sind(x)  sin((x)*DEGRAD)
  33. #define cosd(x)  cos((x)*DEGRAD)
  34. #define tand(x)  tan((x)*DEGRAD)
  35.  
  36. #define atand(x)    (RADEG*atan(x))
  37. #define asind(x)    (RADEG*asin(x))
  38. #define acosd(x)    (RADEG*acos(x))
  39. #define atan2d(y,x) (RADEG*atan2(y,x))
  40.  
  41.  
  42. /* Following are some macros around the "workhorse" function __daylen__ */
  43. /* They mainly fill in the desired values for the reference altitude    */
  44. /* below the horizon, and also selects whether this altitude should     */
  45. /* refer to the Sun's center or its upper limb.                         */
  46.  
  47.  
  48. /* This macro computes the length of the day, from sunrise to sunset. */
  49. /* Sunrise/set is considered to occur when the Sun's upper limb is    */
  50. /* 35 arc minutes below the horizon (this accounts for the refraction */
  51. /* of the Earth's atmosphere).                                        */
  52. #define day_length(year,month,day,lon,lat)  \
  53.         __daylen__( year, month, day, lon, lat, -35.0/60.0, 1 )
  54.  
  55. /* This macro computes the length of the day, including civil twilight. */
  56. /* Civil twilight starts/ends when the Sun's center is 6 degrees below  */
  57. /* the horizon.                                                         */
  58. #define day_civil_twilight_length(year,month,day,lon,lat)  \
  59.         __daylen__( year, month, day, lon, lat, -6.0, 0 )
  60.  
  61. /* This macro computes the length of the day, incl. nautical twilight.  */
  62. /* Nautical twilight starts/ends when the Sun's center is 12 degrees    */
  63. /* below the horizon.                                                   */
  64. #define day_nautical_twilight_length(year,month,day,lon,lat)  \
  65.         __daylen__( year, month, day, lon, lat, -12.0, 0 )
  66.  
  67. /* This macro computes the length of the day, incl. astronomical twilight. */
  68. /* Astronomical twilight starts/ends when the Sun's center is 18 degrees   */
  69. /* below the horizon.                                                      */
  70. #define day_astronomical_twilight_length(year,month,day,lon,lat)  \
  71.         __daylen__( year, month, day, lon, lat, -18.0, 0 )
  72.  
  73.  
  74. /* This macro computes times for sunrise/sunset.                      */
  75. /* Sunrise/set is considered to occur when the Sun's upper limb is    */
  76. /* 35 arc minutes below the horizon (this accounts for the refraction */
  77. /* of the Earth's atmosphere).                                        */
  78. #define sun_rise_set(year,month,day,lon,lat,rise,set)  \
  79.         __sunriset__( year, month, day, lon, lat, -35.0/60.0, 1, rise, set )
  80.  
  81. /* This macro computes the start and end times of civil twilight.       */
  82. /* Civil twilight starts/ends when the Sun's center is 6 degrees below  */
  83. /* the horizon.                                                         */
  84. #define civil_twilight(year,month,day,lon,lat,start,end)  \
  85.         __sunriset__( year, month, day, lon, lat, -6.0, 0, start, end )
  86.  
  87. /* This macro computes the start and end times of nautical twilight.    */
  88. /* Nautical twilight starts/ends when the Sun's center is 12 degrees    */
  89. /* below the horizon.                                                   */
  90. #define nautical_twilight(year,month,day,lon,lat,start,end)  \
  91.         __sunriset__( year, month, day, lon, lat, -12.0, 0, start, end )
  92.  
  93. /* This macro computes the start and end times of astronomical twilight.   */
  94. /* Astronomical twilight starts/ends when the Sun's center is 18 degrees   */
  95. /* below the horizon.                                                      */
  96. #define astronomical_twilight(year,month,day,lon,lat,start,end)  \
  97.         __sunriset__( year, month, day, lon, lat, -18.0, 0, start, end )
  98.  
  99. class sunTimes {
  100.     private:
  101.         const char* ipStackURL = "http://api.ipstack.com/check?access_key=0f4ce7d93a2ed67e7435d4227cc0e931&fields=main";
  102.  
  103.         boolean gotGeoData = false;
  104.  
  105.         double degToRad(double angleDeg);
  106.         double radToDeg(double angleRad);
  107.         double calcMeanObliquityOfEcliptic(double t);
  108.         double calcGeomMeanLongSun(double t);
  109.         double calcObliquityCorrection(double t);
  110.         double calcEccentricityEarthOrbit(double t);
  111.         double calcGeomMeanAnomalySun(double t);
  112.         double calcEquationOfTime(double t);
  113.         double calcTimeJulianCent(double jd);
  114.         double calcSunTrueLong(double t);
  115.         double calcSunApparentLong(double t);
  116.         double calcSunDeclination(double t);
  117.         double calcHourAngleSunrise(double lat, double solarDec);
  118.         double calcHourAngleSunset(double lat, double solarDec);
  119.         double calcJD(int year, int month, int day);
  120.         double calcJDFromJulianCent(double t);
  121.         double calcSunEqOfCenter(double t);
  122.         double calcSunriseUTC(double JD, double latitude, double longitude);
  123.         double calcSunsetUTC(double JD, double latitude, double longitude);
  124.  
  125.         /* New Function prototypes */
  126.         double __daylen__( int year, int month, int day, double lon, double lat,
  127.                         double altit, int upper_limb );
  128.         int __sunriset__( int year, int month, int day, double lon, double lat,
  129.                         double altit, int upper_limb, double *rise, double *set );
  130.         void sunpos( double d, double *lon, double *r );
  131.         void sun_RA_dec( double d, double *RA, double *dec, double *r );
  132.         double revolution( double x );
  133.         double rev180( double x );
  134.         double GMST0( double d );
  135.  
  136.     public:
  137.         void getGeoData();
  138.         void calcSunTimes();
  139.         void calcNewSunTimes();
  140. };
Advertisement
Add Comment
Please, Sign In to add comment