Announcement

Collapse
No announcement yet.

Sun rise / sunset

Collapse
X
  • Time
  • Show
Clear All
new posts

  • Sun rise / sunset

    Could some kind person provide an actual working example of how to get sunrise/sunset info for a watch face? TIA!

  • #2
    Hi

    I'm afraid you'l have to compute it by yourself...

    Here is the algorithm I was using on my Suuto :



    /* PARAMETERS */

    /* Localtime */

    UTC=2; /* Add 2 hours to natural time to get legal time */

    /* Kind of computation */

    /* CONST=0.01454; /* sun center */
    CONST=-0.105; /* civil */
    /* CONST=-0.208; /* nautic */
    /* CONST=-0.309; /* astronomic */
    /* CONST=-0,01065; /* same as Bureau des latitudes */

    /* What do we wanna compute ? */

    /* tocalc = -1; /* sunrise */
    /* tocalc = 0; /* midday */
    tocalc = 1; /* sunfall */

    /***************/
    /* Compute postfix dynamically */

    if( UTC == 0 ) { postfix = "UTC"; }
    else if( UTC == 1 ) { postfix = "UTC+1"; }
    else if( UTC == 2 ) { postfix = "UTC+2"; }
    else if( UTC == 3 ) { postfix = "UTC+3"; }
    else if( UTC == 4 ) { postfix = "UTC+4"; }
    else if( UTC == 5 ) { postfix = "UTC+5"; }
    else if( UTC == 6 ) { postfix = "UTC+6"; }
    else if( UTC == 7 ) { postfix = "UTC+7"; }
    else if( UTC == 8 ) { postfix = "UTC+8"; }
    else if( UTC == 9 ) { postfix = "UTC+9"; }
    else if( UTC == 10 ) { postfix = "UTC+10"; }
    else if( UTC == 11 ) { postfix = "UTC+11"; }
    else if( UTC == 12 ) { postfix = "UTC+12"; }
    else if( UTC == -1 ) { postfix = "UTC-1"; }
    else if( UTC == -2 ) { postfix = "UTC-2"; }
    else if( UTC == -3 ) { postfix = "UTC-3"; }
    else if( UTC == -4 ) { postfix = "UTC-4"; }
    else if( UTC == -5 ) { postfix = "UTC-5"; }
    else if( UTC == -6 ) { postfix = "UTC-6"; }
    else if( UTC == -7 ) { postfix = "UTC-7"; }
    else if( UTC == -8 ) { postfix = "UTC-8"; }
    else if( UTC == -9 ) { postfix = "UTC-9"; }
    else if( UTC == -10 ) { postfix = "UTC-10"; }
    else if( UTC == -11 ) { postfix = "UTC-11"; }
    else if( UTC == -12 ) { postfix = "UTC-12"; }

    j = 1+Suunto.mod(SUUNTO_DAYS_AFTER_1_1_2000*4, 1461)/4;
    decimal=Suunto.mod(j*10,10)/10;
    j = j - decimal;

    /* OK */

    M = 357 + 0.9856 * j;

    C = 1.914 * Suunto.sind(M) + 0.02 * Suunto.sind(2 * M);

    L = 280 + C + 0.9856 * j;

    R = 0.053 * Suunto.sind(4*L)-2.465 * Suunto.sind(2*L);

    ET=(C + R) * 4;

    RESULT=ET;

    sindec = 0.3978 * Suunto.sind(L);

    cosdec = Suunto.sqrt(1-sindec*sindec);

    /* KO */

    declinaison = Suunto.atand2(sindec,cosdec);

    cosh = ( CONST - sind( declinaison ) * sind( SUUNTO_GPS_LATITUDE ) ) /
    ( Suunto.cosd ( declinaison ) * Suunto.cosd ( SUUNTO_GPS_LATITUDE ));

    sinh = Suunto.sqrt(1-cosh*cosh);


    h = Suunto.atand2(sinh,cosh);

    h = h / 15;
    lon = -SUUNTO_GPS_LONGITUDE / 15;
    ET = ET / 60;

    TL = 12 + tocalc * h + ET + lon;

    TL = TL + UTC;

    /* RESULT=TL*3600; */



    Should be easy to convert to Monkey C

    Comment


    • #3
      Have a look at my open source widget:

      https://github.com/haraldh/SunCalc/b...rce/SunCalc.mc

      You would use it like this:
      Code:
      using Toybox.Position as Position;
      using Toybox.Time as Time;
      
      var sc = new SunCalc();
      var info = Position.getInfo();
      if (info != null && info.accuracy != Position.QUALITY_NOT_AVAILABLE) {
          var loc = info.position.toRadians();
          var sunrise_moment = sc.calculate(Time.now.value(), loc[0], loc[1], SUNRISE);
          var sunset_moment = sc.calculate(Time.now.value(), loc[0], loc[1], SUNSET);
      }
      Then convert the moment to a time string like in
      https://github.com/haraldh/SunCalc/b...lcView.mc#L122

      Comment


      • #4
        Thank you very much, that is useful!

        Code:
            var sunrise_moment = sc.calculate(Time.now.value(), loc[0], loc[1], SUNRISE);
            var sunset_moment = sc.calculate(Time.now.value(), loc[0], loc[1], SUNSET);
        }
        but should be like this!?


        Code:
        		    var sunrise_moment = sc.calculate(new Time.Moment(Time.now().value()), loc[0], loc[1], SUNRISE);
        		    var sunset_moment = sc.calculate(new Time.Moment(Time.now().value()), loc[0], loc[1], SUNSET);
        Regards

        Comment


        • #5
          In fact:

          Code:
              var now = Time.now();
              var sunrise_moment = sc.calculate(now, loc[0], loc[1], SUNRISE);
              var sunset_moment  = sc.calculate(now, loc[0], loc[1], SUNSET);
          }

          Comment


          • #6
            But a watchface cannot access position no?

            Comment


            • #7
              Originally posted by RUPERTHAWKES View Post
              But a watchface cannot access position no?
              Activity.info will have your last location on many watches (for a period of time - maybe an hour or two). Once you get it, you can save it off in the object store, so you have something to use if the data in Activity.info is stale.
              My Connect IQ Apps in the Store
              Facebook - Instagram -
              Twitter

              Comment


              • #8
                Habing trouble even getting currentLocation for some reason!

                Comment


                • #9
                  var loc = ActI.getActivityInfo().currentLocation;
                  var latlon = loc.toDegrees();

                  Comment


                  • #10
                    You want to check currentLocation for a null, as it could be that.

                    Also, based on the last time you used GPS, you man need to start something that uses GPS, let lit local and record a bit (you can then discard that activity) and check again.

                    How long currentLocation is saved probably varies by both the watch and the FW.
                    My Connect IQ Apps in the Store
                    Facebook - Instagram -
                    Twitter

                    Comment


                    • #11
                      Hi

                      I'm trying to use this excellent class for calculating sunrise / sunset, and it looks fairly easy thanks to this class.

                      But I have a small problem.

                      When I add the code below, I get an errormessage: Permission required.
                      var info = Position.getInfo();

                      But when I try to add permission for positioning in the manifest file, this item is greyed out and can't be selected.

                      App minimum SDK version is set to 1.3.X and build target is set to fenix 3 HR

                      What am I doing wrong here?

                      Thanks a lot in advance

                      Kenth

                      Comment


                      • #12
                        Originally posted by KenJen2 View Post
                        Hi

                        I'm trying to use this excellent class for calculating sunrise / sunset, and it looks fairly easy thanks to this class.

                        But I have a small problem.

                        When I add the code below, I get an errormessage: Permission required.
                        var info = Position.getInfo();

                        But when I try to add permission for positioning in the manifest file, this item is greyed out and can't be selected.

                        App minimum SDK version is set to 1.3.X and build target is set to fenix 3 HR

                        What am I doing wrong here?

                        Thanks a lot in advance

                        Kenth
                        You are probably trying to use this code in a watch face. Position is not available to watch faces. If you've read the earlier posts in this thread, you would have found your answer. See this post by Jim
                        My Connect IQ apps
                        I only run so I can wear a Garmin!

                        Comment


                        • #13
                          Great, now it works as expected, thanks for the support.

                          I have added my code below, to help other users.

                          var sc = new SunCalc();

                          var loc = Toybox.Activity.getActivityInfo().currentLocation;
                          if(loc != null)
                          {
                          var latlon = loc.toRadians();
                          var now = new Time.Moment(Time.now().value());

                          var sunrise_moment = sc.calculate(now, latlon[0], latlon[1], SUNRISE);
                          var sunset_moment = sc.calculate(now, latlon[0], latlon[1], SUNSET);

                          var timeInfoSunrise = Greg.info(sunrise_moment, Time.FORMAT_SHORT);
                          var timeInfoSunset = Greg.info(sunset_moment, Time.FORMAT_SHORT);

                          sunInfoString = timeInfoSunrise.hour.format("%01d") + ":" + timeInfoSunrise.min.format("%02d") + " - " + timeInfoSunset.hour.format("%01d") + ":" + timeInfoSunset.min.format("%02d");
                          }


                          But as far as I can understand, the Toybox.Activity.getActivityInfo().currentLocation will only store my last GPS location for a period of time, and if the position is available I should save it in the object store, so I can use it later when it's not available anymore.
                          But how do I do this?

                          Thanks

                          Comment


                          • #14
                            save with a variable, sample : var lastLoc

                            Comment


                            • #15
                              cuurentLocation in Watch Face !

                              How to use

                              var loc = Toybox.Activity.getActivityInfo().currentLocation;
                              in Watch face !
                              doesn't work !
                              loc = null !

                              Comment

                              Working...
                              X