Using GPS for telling sun rise and sun set?

Posted on
  • Hi
    I was thinking if bangle knows the GPS and the current time (as part of that) could it not work out if time till sunset etc?
    I was thinking of just adding this as information to a watch face I wanted to make but I think it could also be useful in automating some things with the brightness of some apps etc, I.e. higher after dark etc, or even change the colours as it gets "darker".

  • Not very up on my basic astrophysics so sorry if this is an easy equation

  • That's a neat idea - yes, it should be possible. Although most people won't have their GPS on most of the time so I don't know how useful it'd be unless it remembered the last known position.

    I'd imagine there's some JS out there that'll take lat/lon and time and give you sunrise/sunset... It can't be too painful

  • I think this would do it (­4911820ddcbbc) thou I think I can make it a bit faster for the bangle as we have a function for the date etc.

    Also would be a good example of how to get the lat/lon from bangle? I am thinking one of the moon apps would be a good place to start?

  • @DrBard One thing to watch out for is the acos part, which is one of the last calculations you have to do - there is an issue in the Bangle firmware with that function on negative values and you'll get strange results. There are several ways to estimate acos you can find online and I've used one of those for now myself and it's close enough for this kind of thing. It's a known issue but fairly new so fixed for Espruino but it's not in the latest standard Bangle firmware.

    function acos_estimate(x) {

    return (-0.69813170079773212 * x * x - 0.87266462599716477) * x + 1.5707963267948966;


  • Thanks @BenJabituya.
    Looking at your very cool clock, I assume that you are getting the GPS (when not already saved) from ...

    Bangle.on('GPS', function (g) {
        if (g.fix) {
            astral_settings.lon = g.lon;
            astral_settings.astral_default = false;
            config_file = require("Storage").open("astral.config.t­xt", "w");

    I have never worked with bangles GPS so trying to workout the best way to call the lat/lon without scrwing something up and calling the GPS all the time or something lol

  • @DrBard yep that's basiscally it. BTW there's actually another app that does specific sun and moon calcs which you might want to look at also:

    I'm not getting correct metrics from that app though but again I think it's just because of the acos issue; I've actually found a better function for that now than the one I gave before (Don't know what voodoo magic they do to work these out!):­/acos.html

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

Using GPS for telling sun rise and sun set?

Posted by Avatar for DrBard @DrBard